Opengl 加载glnormal指针并使用着色器

Opengl 加载glnormal指针并使用着色器,opengl,glsl,shader,vbo,Opengl,Glsl,Shader,Vbo,我正在尝试使用我的着色器与VAO一起绘制灯光。但是,每当我加载glNormalPointer时,我的着色器不会在对象上绘制,并且调试时遇到问题 以下是我的部分代码: glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); glNormalPointer(

我正在尝试使用我的着色器与VAO一起绘制灯光。但是,每当我加载glNormalPointer时,我的着色器不会在对象上绘制,并且调试时遇到问题

以下是我的部分代码:

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, vertices);
        glNormalPointer(GL_FLOAT, 0, normals);
        glDrawElements(GL_QUADS, 400, GL_UNSIGNED_INT, indices);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
它绘制正确,但照明不正确。如果我注释掉了用于正常工作的三行,那么程序将正确运行,这对我来说没有意义。法线只是曲面面的法线。它们应该是正确的,因为当我使用glNormal3f()和glVertex3f()加载它们时,对象呈现如下。着色器文件来自Lighthouse 3D网站,它们已正确渲染

这是在禁用glNormalPointer()的情况下进行的

这是在启用glNormalPointer的情况下进行的,如未注释掉中所示

我错过了什么吗

以下是着色器代码: 这是我从Lighthouse 3D获得的vertext着色器

varying vec4 diffuse,ambientGlobal,ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;

void main()
{   
vec4 ecPos;
vec3 aux;

/* first transform the normal into eye space and normalize the result */
normal = normalize(gl_NormalMatrix * gl_Normal);

/* now normalize the light's direction. Note that according to the
OpenGL specification, the light is stored in eye space. Also since 
we're talking about a directional light, the position field is actually 
direction */
ecPos = gl_ModelViewMatrix * gl_Vertex;
aux = vec3(gl_LightSource[2].position-ecPos);
lightDir = normalize(aux);

/* compute the distance to the light source to a varying variable*/
dist = length(aux);

/* Normalize the halfVector to pass it to the fragment shader */
halfVector = normalize(gl_LightSource[2].halfVector.xyz);

/* Compute the diffuse, ambient and globalAmbient terms */
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[2].diffuse;
ambient = gl_FrontMaterial.ambient * gl_LightSource[2].ambient;
ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient;

gl_Position = ftransform();
 } 
这是我从Lighthouse 3D获得的片段着色器:

varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;


void main()
{
vec3 n,halfV,viewV,ldir;
float NdotL,NdotHV;
vec4 color = ambientGlobal;
float att;

/* a fragment shader can't write a verying variable, hence we need
a new variable to store the normalized interpolated normal */
n = normalize(normal);

/* compute the dot product between normal and ldir */
NdotL = max(dot(n,normalize(lightDir)),0.0);

if (NdotL > 0.0) {

    att = 1.0 / (gl_LightSource[2].constantAttenuation +
            gl_LightSource[2].linearAttenuation * dist +
            gl_LightSource[2].quadraticAttenuation * dist * dist);
    color += att * (diffuse * NdotL + ambient);


    halfV = normalize(halfVector);
    NdotHV = max(dot(n,halfV),0.0);
    color += att * gl_FrontMaterial.specular * gl_LightSource[2].specular * pow(NdotHV,gl_FrontMaterial.shininess);
}

gl_FragColor = color;
}


我没有写任何东西,只是想让它们与我的代码一起工作。

结果应该与红色的一样,但这是通过使用glVertex3f()和循环实现的。我正试图通过使用gldrawerelements之类的工具来清理代码。这样更好一些,尽管我认为您可能应该向着色器显示
法线的值,以及源代码。您是否要求每个顶点的所有法线值?