Jump to content
  • Advertisement
Sign in to follow this  
luckyyyyyy

VertexArray vs glVertex3fv...something wrong

This topic is 3785 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

hi guys... I have a problem with vertex array... see the result below... when i use simple " glVertex3fv " then its ok...nice result...like this... Free Image Hosting at allyoucanupload.com but when i use vertex array for speed up my software... then the model surface change like this... Free Image Hosting at allyoucanupload.com i am using material and light together... plz tell me solution...using vertex array the model should be look like glVertex3fv... thanks in advance...

Share this post


Link to post
Share on other sites
Advertisement
Hi, can you please post the rendering code (for both cases), it is difficult to understand what is going wrong without it ;-)

Share this post


Link to post
Share on other sites
Your geometry seems OK, but there's something wrong with the normals. Is your glNormalPointer pointing in the right place (it looks like it may not be)? Is GL_NORMAL_ARRAY enabled?

Share this post


Link to post
Share on other sites
thanks for reply guys...

ok...this is my rendering code...





if (VertexArray)
{
glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_DOUBLE, sizeof(GLpoint), FaceNormals);
glVertexPointer(3, GL_DOUBLE, sizeof(GLpoint), p_VERTICES);
glDrawElements(GL_TRIANGLES, 3*nb_Faces, GL_UNSIGNED_SHORT, p_Faces);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glPopClientAttrib();
}

if (!VertexArray)
{
glBegin(GL_TRIANGLES); // begin to draw triangles
GLFace * pFace = (GLFace *)&p_Faces;
for (int ix = 0; ix < nb_Faces; ix++, pFace++) // begin for loop to draw triangles meshes at ix is less than total number of faces of triangles
{
if (FaceNormals)
if (None) // if true then there is no any Normal Vector (No Shading)
if (FLAT) // if true then model is Flat
glNormal3dv((double *)&FaceNormals[ix]); // Applying the face normal (Flat Shading)
else // Else apply the vertice normal to each vertex
if (VertNormals && (!FLAT))
glNormal3dv((double *)&VertNormals[pFace->v1]); // Apply the Vertex Normal (Smooth Shading)

glVertex3dv((double *)&p_VERTICES[pFace->v1]); // Draw the first vertice of a triangle

if (VertNormals && (!FLAT))
glNormal3dv((double *)&VertNormals[pFace->v2]); // Apply the Vertex Normal (Smooth Shading)

glVertex3dv((double *)&p_VERTICES[pFace->v2]); // Draw the secong vertice of a triangle

if (VertNormals && (!FLAT))
glNormal3dv((double *)&VertNormals[pFace->v3]); // Apply the Vertex Normal (Smooth Shading)

glVertex3dv((double *)&p_VERTICES[pFace->v3]); // Draw the third(last) vertice of a triangle
}
glEnd(); // End to draw the triangles*/
}
}



i calculated normal vector myself from vertices...

my be as you say normal vector problem...but i could not understand what is the problem...

Share this post


Link to post
Share on other sites
Just one observation:


  • glNormalPointer(GL_DOUBLE, sizeof(GLpoint), FaceNormals);
    glVertexPointer(3, GL_DOUBLE, sizeof(GLpoint), p_VERTICES);

    --> From what I learned from your immediate rendering code, you just have one normal per face, so FaceNormals seems to provide too few normals. Try VertNormals instead, since for each vertex you'll need one normal, i.e. num_Normals = num_Vertices.

Share this post


Link to post
Share on other sites
sorry bro...

i could not understand about how to apply vertex normal in glNormalPointer...

because FaceNormals have only one normal per face but VertNormals have three normal per face...so how can i apply vertex normal in glNormalPointer...

i tried to apply vertex normal with for loop like

for( i to n )
glNormalPointer(GL_DOUBLE, sizeof(GLpoint), VertNormals);

nothing happened...

and plzzz one thing clear me...as u say FaceNormals has few normal...its means that using VertexArray we have to apply VertexNormal not FaceNormal...

i tried to apply Face and VertexNormal with different methods...but i couldn't succeed...

plzzz clear me..

Share this post


Link to post
Share on other sites
You can't just apply the normals iteratively, this isn't how the OpenGL vertex arrays work.

You need to make another array with the vertex normals stored in the same order as the vertices themselves.

Then pass this array into the glNormalPointer function instead of the FaceNormals.

Andy

Share this post


Link to post
Share on other sites
Quote:
Original post by luckyyyyyy
sorry bro...

i could not understand about how to apply vertex normal in glNormalPointer...

because FaceNormals have only one normal per face but VertNormals have three normal per face...so how can i apply vertex normal in glNormalPointer...

i tried to apply vertex normal with for loop like

for( i to n )
glNormalPointer(GL_DOUBLE, sizeof(GLpoint), VertNormals);

nothing happened...


What I think Lord_Evil meant was that instead of this:

    glNormalPointer(GL_DOUBLE, sizeof(GLpoint), FaceNormals);

you should do this instead:

    glNormalPointer(GL_DOUBLE, sizeof(GLpoint), VertNormals);

This is because you need to have the same number of normals as vertices and they need to be in the same order.

i.e. VertNormals[10] is the normal for the vertex p_VERTICES[10] etc.

Andy

Share this post


Link to post
Share on other sites
much much Thanks bro...

i have solved...by applying VertexNormals...it look like good...
glNormalPointer(GL_DOUBLE, sizeof(GLpoint), VertNormals); ==================> good result


again thanks...

but i am confused about FaceNormals...

if glNormal3dv gives a good result...then why something wrong with glNormalPointer with FaceNormals...

glNormalPointer(GL_DOUBLE, sizeof(GLpoint), FaceNormals); ==================> bad result ...why ????

Share this post


Link to post
Share on other sites
Quote:
Original post by luckyyyyyy
if glNormal3dv gives a good result...then why something wrong with glNormalPointer with FaceNormals...

glNormalPointer(GL_DOUBLE, sizeof(GLpoint), FaceNormals); ==================> bad result ...why ????


Because it works differently.

In the VertexArray version you're saying this:

    use this normal
    render this vertex

but in the !VertexArray version you're telling OpenGL:

    use this normal
    render this triangle using these 3 vertices

So in the VertexArray the normal is applied to each vertex individually, but in the !VertexArray version the normal applies to all of the vertices that you supply for the whole triangle.

The problem comes because you have 3 times as many normals defined in VertNormal as you do in FaceNormals. Thats because there are as many normals as there are vertices but there are a different number of faces. Typically there are only 1/3rd as many faces as triangles (please note that this is a simplifcation to make the example clearer) because it takes 3 vertices to make 1 triangle.

i.e. if you had 100 vertices then:
p_VERTICES[99];
VertNormals[99]; ====> as many normals as vertices
FaceNormals[33]; ====> you have 1/3rd as many face normals as vertex normals.

So OpenGL simply doesn't have enough data to work with. This can cause crashes and all sorts of horrible errors as well. I guess you were just lucky you didn't see any.

EDIT: actually thats not the only reason that it looks so screwed up with the lighting (sorry for all these edits i've only had 3 hours sleep!). When you supply the FaceNormals you mismatch the vertex to the normal. This means that Vertex[0] gets mapped to FaceNormals[0], now assuming that Vertices 0 to 2 form the first triangle/face then Vertex[1] needs to be mapped to FaceNormals[0] as well... instead though it get mapped as Vertex[1] -> FaceNormals[1], then it gets worse when Vertex[2] gets mapped too FaceNormals[2].
So by the time you've drawn the FIRST triangle you're already onto the 3rd faces normal! :EDIT

I hope that makes some sense :)

Andy

EDIT: edited for clarification.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!