View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Manabreak  Members

Posted 28 August 2012 - 07:14 AM

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 330uniform 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);}FRAGMENT SHADER:#version 330in vec4 worldpos;in vec3 normal;in vec2 texcoord;out vec3 out_diffuse;uniform sampler2D colorMap;void main(void){ out_diffuse = texture(colorMap, texcoord).xyz;}[/source]

### #2Sponji  Members

Posted 28 August 2012 - 07:29 AM

You have to duplicate the vertices because they need different texture coordinates.
Derp

### #3BitMaster  Members

Posted 28 August 2012 - 07:32 AM

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.

### #4Manabreak  Members

Posted 28 August 2012 - 07:42 AM

Ah, I suspected something along those lines, but wasn't certain. Thanks!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.