c++ - Modern OpenGL issue texturing plane -
i have been having difficulty texturing plane made. first quad on plane textured properly, rest of plane seems use first pixel of texture ends solid color. seems work if make 1 giant plane , texture that, when try break plane sections keep getting issue. i’m assuming missing far coordinates go, understanding thought supposed between 0 , 1? appreciated.
[![enter image description here][1]][1]
texture coordinates
glfloat grasstexcoords[] { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f };
setting vao
gluint makeplane() { float size = 5; (int = -5; < 5; ++i) { (int j = -5; j < 5; ++j) { verts.push_back({ glm::vec3((i * size), -11.f, (j * size)) }); verts.push_back({ glm::vec3((i * size), -11.f, (j * size) + size) }); verts.push_back({ glm::vec3((i * size) + size, -11.f, (j * size)) }); verts.push_back({ glm::vec3((i * size) + size, -11.f, (j * size)) }); verts.push_back({ glm::vec3((i * size), -11.f, (j * size) + size) }); verts.push_back({ glm::vec3((i * size) + size, -11.f, (j * size) + size) }); } } gluint vbo; glgenbuffers(1, &vbo); glbindbuffer(gl_array_buffer, vbo); glbufferdata(gl_array_buffer, verts.size() * sizeof(vertexpos), verts.data(), gl_static_draw); gluint vbotex; glgenbuffers(1, &vbotex); glbindbuffer(gl_array_buffer, vbotex); glbufferdata(gl_array_buffer, 2 * 6 * sizeof(glfloat), &grasstexcoords, gl_static_draw); gluint vao; glgenvertexarrays(1, &vao); glbindvertexarray(vao); glenablevertexattribarray(0); glbindbuffer(gl_array_buffer, vbo); glvertexattribpointer(0, 3, gl_float, gl_false, 0, null); glenablevertexattribarray(1); glbindbuffer(gl_array_buffer, vbotex); glvertexattribpointer(1, 2, gl_float, gl_false, 0, null); return vao; }
rendering
void render() { glviewport(0, 0, window.getsize().x, window.getsize().y); glclearcolor(.4f, .4f, .4f, 1.f); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); //1st program gluseprogram(sphereprogram); glenable(gl_cull_face); glenable(gl_depth_test); glbindvertexarray(vao); gldrawarrays(gl_triangles, 0, objpointcount); //2nd program glfrontface(gl_ccw); gldepthmask(gl_false); gluseprogram(cubeprogram); glbindtexture(gl_texture_cube_map, textureid); glbindvertexarray(cubevao); gldrawarrays(gl_triangles, 0, 36); gldepthmask(gl_true); //3rd program glfrontface(gl_ccw); gldisable(gl_cull_face); glenable(gl_texture_2d); sf::texture::bind(&grasstex); gluseprogram(planeprogram); glbindvertexarray(planevao); gldrawarrays(gl_triangles, 0, verts.size()); //----------------------- window.display(); //window.setframeratelimit(fps); window.setverticalsyncenabled(true); }
vertex shader
#version 410 layout (location = 0) in vec3 vertexpos; layout (location = 1) in vec2 texcoords; uniform mat4 view, proj; out vec3 poseye; out vec2 coords; void main() { coords = texcoords; //repeat texture on plane gl_position = proj * view * vec4(vertexpos, 1.0); poseye = (view * vec4(vertexpos, 1.0)).xyz; }
fragment shader
#version 410 in vec3 poseye; in vec2 coords; out vec4 fragcolor; uniform sampler2d tex; //fog const vec3 fogcolor = vec3(0.2, 0.2, 0.2); const float minfograd = 300; const float maxfograd = 900; void main() { vec4 texture = texture2d(tex, coords); fragcolor = texture; float distance = length(-poseye); float fogfactor = (distance - minfograd) / (maxfograd - minfograd); fogfactor = clamp(fogfactor, 0.0, 1.0); fragcolor.rgb = mix(fragcolor.rgb, fogcolor, fogfactor); }
the problem here is, texture coordinates supplied first quad (for first 6 vertices). other vertices seem [0,0], causes them read top-left texel. solution here provide enough texture coordinates of vertices.
texture coordinates in general not between 0 , 1. 1 can specify how values outside of [0,1] should treated setting gl_texture_wrap_[rst]
.
note, not supplying enough data in vbo can lead crashes (depends on driver) when opengl tries read outside buffer.
Comments
Post a Comment