简体   繁体   中英

OpenGL Normals not appearing in triangles

I'm using a normal array to render a normal dataset as follows:

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer( GL_FLOAT, 0, nArray );

My vertices are rendered as follows:

glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, 0, vArray );
glDrawElements( GL_TRIANGLES, iSize, GL_UNSIGNED_INT, iArray );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );

For the sake of this example, I'm filling my nArray as follows (the error still occurs when normals are calculated correctly):

for(int i = 0; i < nSize * 3; ++i){
    nArray[i] = randomNumber();
}

Where randomNumber() is returning a random float number between 0 and 1.

I expect that nArray should be filled with random floats between 0 and 1, and these should be displayed completely randomly on the surface I am generating.

When I render the output, the "normals" are displayed as follows. I do not care that it doesn't calculate a correct normal, but that it seems the normals for two side by side triangles are the same.

不是三角形的法线图片

Below is a wireframe of the surface to show the separate triangles: 上面图象的Wireframe图象

The shade method I am using is: glShadeModel(GL_FLAT);

I expected that each individual triangle would have completely different greyscale values, but that is not the case. Any help on where I am going wrong is appreciated.

The shade method I am using is: glShadeModel(GL_FLAT);

With you so far.

When I render the output, the "normals" are displayed as follows. I do not care that it doesn't calculate a correct normal, but that it seems the normals for two side by side triangles are the same

Yup, you requested that via GL_FLAT :

GL primitives can have either flat or smooth shading. Smooth shading, the default, causes the computed colors of vertices to be interpolated as the primitive is rasterized, typically assigning different colors to each resulting pixel fragment. Flat shading selects the computed color of just one vertex and assigns it to all the pixel fragments generated by rasterizing a single primitive.

If you want to interpolate between the normals of all three vertices you need to use GL_SMOOTH .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM