Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Confused about vertices, indices and tex coords when using VBO/GLSL

This topic is 2301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there,

I have some problems with texturing. Let me set up the scenario: I have a custom-written .FBX importer which reads vertices, indices and UV coordinates from a file. If I have a cube, it'll have 8 vertices, 36 indices and 36 UV coordinates, as each vertex is used for three faces.

Now, the geometry is represented nice and dandy, and if I trace the UV coordinates, they are supposed to be correct, but when I try to draw the cube with the texture, the texture is mapped in a nonsensical fashion. I cannot find the cause of this problem, and after a week of frustration, I decided to ask you guys.

I use a struct for the vertex data, which has three properties (position : float x 3, normal : float x 3, uv : float x 2). I end up having eight of these, which is correct. Then I have an integer list which keeps track of the indices, going like 0, 1, 2, 1, 2, 3 etc., you know the drill. Now, the hardest thing for me to understand is, how does OpenGL know which UV coordinate should be used, when there's only eight vertices but 36 uv coordinates? I have set up a VBO which points to all the vertex properties, and finally I draw the cube using a DrawElements call. I use a GLSL shader to texture the cube, and I pass the texture and the texture coordinates to the shader correctly (at least I assume so, because the texture shows up on the cube).

Where could the problem be? This is how it should look like:

... and this is what it actually looks like:

As you can see, some coordinates (the top) are correct, while the sides are somewhat "skewed". I have done no changes to my importer, and the coordinates worked just fine before I changed my rendering code to use shaders. The shader code is like this:

[source lang="plain"]VERTEX SHADER:

#version 330

uniform mat4 projection;
uniform mat4 view;
uniform mat4 world;

layout(location = 0) in vec3 in_position;
layout(location = 1) in vec3 in_normal;
layout(location = 2) in vec2 in_texcoord;

out vec4 worldpos;
out vec3 normal;
out vec2 texcoord;

void main(void)
texcoord = in_texcoord;
normal = in_normal;
worldpos = projection * view * world * vec4(in_position, 1.0);


#version 330

in vec4 worldpos;
in vec3 normal;
in vec2 texcoord;

out vec3 out_diffuse;

uniform sampler2D colorMap;

void main(void)
out_diffuse = texture(colorMap, texcoord).xyz;

Share this post

Link to post
Share on other sites
A vertex for OpenGL describes all attributes, not just the positions. Your cube has 8 unique positions but when taking texture coordinates and normals into account you need 4 unique vertices for each face, for a total of 24 unique vertices to render the cube directly.

It would be possible (though whether it is practical is uncertain) to just use 8 positions as input and calculate both normals and texture coordinates on the fly using a geometry shader though.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!