• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Mesh rendering

## Recommended Posts

zander76    122
Hello Everybody I posted this message in the beginner forum and relalised that it didn''t really fit. I am using vertex and index lists in gl and i was wondering about the best way to handle a problem. Imagine a box 8 vertex 24 Indicies 24 Normals So here is the question. Are the normals related to the vertex or to the indicies. If its related to the vertecies then i could do a average at each point to define a normal that would work for the 3 faces that are attached to it. But what i would like is to assign the normals to the indicies. So for each vertex there would be 3 normals, one for each attached face. So is this possible in gl or do i have to have 24 vertices to match the normals. Thanks for your help. Ben

##### Share on other sites
exa_einstein    122
24 normals for a cube? isn''t it a bit more than necessary?

ok, you have two main methods for applying normals in all the 3d (only THEORY and NOT implemented by DX/OGL, you must do it yourself):
first, _flat_ is about that every face has only one normal, so it has constant color. better for objs with sharp edges->better for your cube.

_smooth_ needs some type of smoooth_shading and every triangle has THREE, in most cases different normals, but every vertex has only one(and is used by all triangles which use your vertex.)

result: normals aren''t related to anything. They only "change color" of vertex.

result 2: it is possible. everything i possible in OpenGL...

##### Share on other sites
theZapper    150
quote:
Original post by zander76
So here is the question. Are the normals related to the vertex or to the indicies. If its related to the vertecies then i could do a average at each point to define a normal that would work for the 3 faces that are attached to it.

But what i would like is to assign the normals to the indicies. So for each vertex there would be 3 normals, one for each attached face.

Normals are related to the Vertices. Each vertex is a single point in space and has only one normal. The normal simply defines how light will reflect of the suface at that vertex position. So you should have 8 normals, not 24.

I think you might also be a bit confused about indices and how you use them, it might help to read the section on Vertex Lists in ''The OpenGL programming guide'', that should clear up most of your problems.

Hope that helps.

---
"When I''m in command, every mission''s a suicide mission" - Zapp Branigan

##### Share on other sites
khirsah    122
Yah, unfortunately right now I think you''d have to have 24 vertices. I don''t think there''s a way to specify multiple normals for one vertex.

##### Share on other sites
zander76    122
Thanks guys..

I appreciate the help. I didn''t think it was possible without duplicating the verticies. That really sucks. What about texture coordinates. Is it the samething.

Ben

##### Share on other sites
theZapper    150
A vertex will also usually have only 1 texture co-ordinate, but you can have more if you use multitexturing.

---
When I''m in command, every mission''s a suicide mission!

##### Share on other sites
zander76    122
Hello

I just took a look at nehes lesson 6, the rotating box.

// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

As you can probobly see is that the texture coordinates go from first of the image to the last and do that again for the next face.
The problem with this, if texture coordinates are not using the indicies the you can't do this. Each vertex requires 3 different u,v coordanites, for the sides a -1,1 and 1,1 and what ever the top is.

Later, Ben

[edited by - zander76 on August 13, 2003 11:38:56 AM]

##### Share on other sites
theZapper    150
That section of code has nothing to do with indices, they are only used in vertex arrays.

I don''t think you quite understand what''s going on here, do you know what the two functions ''glTexCoord2f()'' and ''glVertex3f()'' are actually doing?

---
When I''m in command, every mission''s a suicide mission!

##### Share on other sites
azimuth    122
A cube has eight vertexes and six surfaces. Not sure where you''re getting the "indices" from.

If you specify the normals of a cube, you''ve got a few options. One is to specify a normal for each surface. glNormal*() sets the normal for every vertex after it, until the next glNormal*() call. So to specify the normal for a surface, you would do something like:

glNormal()
glVertex3f()
glVertex3f()
glVertex3f()
glVertex3f()
glNormal()
...

You can also specify the normal for each vertex. This is used in Gourand shading, where the normal for a vertex is the average of the normals of its associated surfaces:
glNormal()
glVertex3f()
glNormal()
glVertex3f()
glNormal()
glVertex3f()
glNormal()
glVertex3f()

Hope this helps.

##### Share on other sites
zander76    122
Sorry.. i guess i should have been more clear. That was just an example of a problem i would like to solve using an index list instead of just straight calls. The example was ment to show the problem of using an index list with texture coordinates.

Here is a better example. (if there is a mistake sorry, i am just trying to do it in my head)

Vertex data.
<-1, 1, 1> <1,1,1>
<-1, 1, -1> <1,1,-1>
<-1, -1, 1> <1,-1,1>
<-1, -1, -1> <1,-1,-1>

Texcoords
<-1,1> <1,1>
<-1,-1> <1,-1>
<-1,1> <1,1>
<-1,-1> <1,-1>

Index list

// Front Face
1,4,3
1,2,4

// Side Face
2,7,4
2,6,7

Now here is the problem. I have 8 texture coordinates and 8 verticies. This may seem correct but if you take a look at point 2 it needs 2 different texture coordinates. On the front face it should have texture coordinates of <1,1> but on the side face connected to it it needs to have texture coordinates of <-1,1>

Does that explain the problem a little better. One the front face point 2 will be top/right of the texture image but on the side its top/left.

Thanks, Ben

##### Share on other sites
zander76    122
Oh one more thing. I have started writing an extention that will allow everything to be indexed color, texture coords and normals.

Thanks, Ben

##### Share on other sites
GamerSg    378
How i wish Vertex arrays supported texture indices. Currently my models can get bloated by about 3 times the original vertices count because for every different texture coordinate for each vertex, the vertex has to be split. This is a huge waste of memory IMO.

Ofcourse i can use textured indices, but id have to use immediate mode and im afraid ill be losing more speed in exchange for efficient use of memory.

##### Share on other sites
zander76    122
Hey. I really need to figure out how to recast the parameter of a pointer to a function. If you can tell me that i should have a version of my extention ready.

Here is what i have:

typedef struct{
GLint size;
GLenum type;
GLsizei stride;
const GLvoid *pointer;
void (__stdcall *func)(const GLvoid *);
GLuint *index;
} GLIndexArray;

GLIndexArray vertexArray;

// Here is the problem the declaration uses const GLvoid * but
// glVertex3fv takes const GLfloat *
vertexArray.func = glVertex3fv;

//This is really need so i can also do this. (use the int version)
vertexArray.func = glVertex3iv;

Any ideas.
Ben

##### Share on other sites
silvermace    634
you would have to create an indexing scheme
eg.

struct face_t{  int normal[3]; //index to the normals that make this face  int verts[3];  //index to verts that make this face}face_t faces[numFaces];float vert_pool[numVerts];float norm_pool[numNormals];

you could even create a normal lookup table to save memory.

A GOOD friend will come bail you out of jail...
but, a TRUE friend will be sitting next to you saying, "Damn, we fucked up."
Ingite 3D Game Engine Home -- Just click it

[edited by - silvermace on August 14, 2003 6:48:03 AM]