Advertisement

Using indices with normals?

Started by February 01, 2016 12:24 PM
4 comments, last by WoopsASword 8 years, 10 months ago

Is it posible to use normals with indices? If so, how?

say for example i have


std::vector<GLfloat> vertices{
2.0f, 0.0f, -2.0f,  0.0f,  0.0f, -1.0f, 2.0f, 2.0f,
-2.0f, 0.0f, -2.0f, 0.0f,  0.0f, -1.0f, 0.0f, 2.0f,
-2.0f, 0.0f, 2.0f,  0.0f,  0.0f, -1.0f, 0.0f, 0.0f,
2.0f, 0.0f, 2.0f,  0.0f,  0.0f, -1.0f, 2.0f, 0.0f
};

std::vector<GLuint>indices{
0,1,3,
1,2,3
};

I just dont want to declare too many vertices. Using indices is my much preferred method when for example making a cube.

Its just a plane. I just want to visualize normal vectors with the use of geometry shader. Ive been trying to use the normal but still not showing on my scene.

As far as I know, you can use normals with indices, but keep in mind that the normal on your surface with be interpolated in the pixel shader, so the cube will look roundish. If you want to have hard edges, you have to declare a vertex for each corner of a face (so you'll have 4x6=24 vertices).

Advertisement
A cube is basically the pathological worst case for indexed geometry, because every edge is a hard edge. You need the full 24 vertices to have sufficient normals to render the hard edges, and you'll need all 24 anyway if you want to texture your box sanely.

Indexed geometry still saves you a ton of vertices, though, because each face is composed of two triangles, and those two triangles share two vertices (so you save 6*2 = 12 additional vertices).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Hello. Thanks. I manage to put a normal vector line on each edge of the plane. but I see another problem of using indices though. It is so hard to position the texture.

take for example this


std::vector<GLfloat> cuberVertices{


0.5f, 0.0f, -0.5f, 0.0f,1.0f,0.0f, 2.0f, 2.0f,
-0.5f, 0.0f, -0.5f, 0.0f,1.0f,0.0f, 0.0f, 2.0f,
-0.5f, 0.0f, 0.5f, 0.0f,1.0f,0.0f,   0.0f, 0.0f,
0.5f, 0.0f, 0.5f, 0.0f,1.0f,0.0f, 2.0f, 0.0f,


0.5f, 0.5f, 0.5f, 1.0f,0.0f,0.0f, 2.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f,0.0f,0.0f, 2.0f, 2.0f
};

The first 4 row was easy. but the next 2 row is hard. I mean. The image look stretched. The plane has a correct image position. the wall though looks stretched.

hehe.jpg
How can you map the coordinate of two vertex on a texture that requires 4 vertex?


The image look stretched. The plane has a correct image position. the wall though looks stretched. How can you map the coordinate of two vertex on a texture that requires 4 vertex?

That's where you get into the art of texture mapping and UV coordinates. It is one step of making beautiful 3D models, and part of the reason we use high quality modeling tools rather than trying to hard-code models in code.

Do you mean an index buffer for usage of normals only? as far as I know.. no.

Indicies in OpenGL are built to contain the index of a single vertex/UV/Normal.

So if you have x1,x2,x3,uv1,uv2,n1,n2,n3 (Representation of a single point)

All of that will conclude to a single index.

----------------

Texturing issue:

Try setting different wrapping or a different filtering method.

Although it looks like your coordinates should be tweaked correctly. (UV)

This topic is closed to new replies.

Advertisement