glDisableVertexAttribArray

glDisableVertexAttribArray

因此,我一直在用OpenGL 3.3(FreeGLUT + GLEW)完成一个旧的大学项目,但遇到了一些问题。

就在开始时,我运行程序,并且在编译BumpMap Fragment Shader时遇到错误:

#version 330 core
#define lightCount 10

in vec4 vertPos;
in vec4 eyeModel;

in vec3 normalInterp;

in vec4 ambient;
in vec4 color;
in vec4 spec;
in vec2 texuv;

in vec4 lightPosition[lightCount];

struct LightSource {

        vec4 Position;
        vec4 Direction;

        vec4 Color;

        float CutOff;

        float AmbientIntensity;
        float DiffuseIntensity;
        float SpecularIntensity;

        float ConstantAttenuation;
        float LinearAttenuation;
        float ExponentialAttenuation;

        int lightType;
};

layout(std140) uniform LightSources {

        LightSource lightSource[10];
};

uniform sampler2D diffuse_tex;
uniform sampler2D normal_tex;

out vec4 out_Color;

void main() {
        out_Color = vec4(0);

        for(int i=0; i<lightCount; i++) {

                if(lightSource[i].lightType == 0)
                        continue;

                vec3 NormalMap = texture2D(normal_tex, texuv).rgb;
                vec3 normal = normalize(NormalMap * 2.0 - 1.0); //normalize(normalInterp);

                vec4 LightDirection = vertPos - lightSource[i].Position;
                float Distance = length(LightDirection);
                LightDirection = normalize(LightDirection);

                vec4 ambientColor = ambient * lightSource[i].Color * lightSource[i].AmbientIntensity;
                vec4 diffuseColor  =  vec4(0, 0, 0, 0);
                vec4 dColor = texture2D(diffuse_tex, texuv);
                vec4 specularColor = vec4(0, 0, 0, 0);

                float DiffuseFactor = dot(normal, vec3(-LightDirection));

                if (DiffuseFactor > 0) {

                        diffuseColor = dColor * lightSource[i].Color * lightSource[i].DiffuseIntensity * DiffuseFactor;

                        vec3 VertexToEye = normalize(vec3(eyeModel - vertPos));
                        vec3 LightReflect = normalize(reflect(vec3(LightDirection), normal));

                        float SpecularFactor = dot(VertexToEye, LightReflect);

                        SpecularFactor = pow(SpecularFactor, 255);
                        if(SpecularFactor > 0.0){
                        //SpecularFactor = pow( max(SpecularFactor,0.0), 255);
                                specularColor = spec * lightSource[i].Color * lightSource[i].SpecularIntensity * SpecularFactor;
                        }
                }

                out_Color += ambientColor + diffuseColor + specularColor;
        }
}



因此,我查找了问题,尽管我觉得这很奇怪,但在一个我知道它处于正常工作状态的项目中遇到了这个问题,我还是将texture2D调用切换为texture调用,现在着色器可以编译,但是我得到了一个不同的结果错误,其中为场景中的第一个对象创建了缓冲区对象:
//Consts defined here for readability
#define VERTICES 0
#define COLORS 1
#define NORMALS 2
#define TEXUVS 3
#define AMBIENTS 4
#define TANGENTS 5
#define SPECULARS 6
#define SPECULARS_CONSTANTS 7
#define NOISE_SCALE 8

void BufferObject::createBufferObject() {

    glGenVertexArrays(1, &_vertexArrayObjectID);
    glBindVertexArray(_vertexArrayObjectID);

    glGenBuffers(1, &_vertexBufferObjectID);

    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*_vertexCount, _vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(VERTICES);
    glVertexAttribPointer(VERTICES, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);

    glEnableVertexAttribArray(COLORS);
    glVertexAttribPointer(COLORS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)sizeof(_vertices[0].XYZW));

    glEnableVertexAttribArray(NORMALS);
    glVertexAttribPointer(NORMALS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)));

    glEnableVertexAttribArray(TEXUVS);
    glVertexAttribPointer(TEXUVS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)));

    glEnableVertexAttribArray(AMBIENTS);
    glVertexAttribPointer(AMBIENTS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)+sizeof(_vertices[0].TEXUV)));

    glEnableVertexAttribArray(TANGENTS);
    glVertexAttribPointer(TANGENTS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)+sizeof(_vertices[0].TEXUV)+sizeof(_vertices[0].AMBIENT)));

    glEnableVertexAttribArray(SPECULARS);
    glVertexAttribPointer(SPECULARS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)+sizeof(_vertices[0].TEXUV)+sizeof(_vertices[0].AMBIENT)+sizeof(_vertices[0].TANGENT)));

    glEnableVertexAttribArray(SPECULARS_CONSTANTS);
    glVertexAttribPointer(SPECULARS_CONSTANTS, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)+sizeof(_vertices[0].TEXUV)+sizeof(_vertices[0].AMBIENT)+sizeof(_vertices[0].TANGENT)+sizeof(_vertices[0].SPECULAR)));

    glBindVertexArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);

    glDisableVertexAttribArray(VERTICES);
    glDisableVertexAttribArray(COLORS);
    glDisableVertexAttribArray(NORMALS);
    glDisableVertexAttribArray(TEXUVS);
    glDisableVertexAttribArray(AMBIENTS);
    glDisableVertexAttribArray(TANGENTS);
    glDisableVertexAttribArray(SPECULARS);
    glDisableVertexAttribArray(SPECULARS_CONSTANTS);

    Utility::checkOpenGLError("ERROR: Buffer Object creation failed.");
}



这就是我得到的所有信息。我已经移动了checkOpenGLError并发现glDisableVertexAttribArray(VERTICES)行给出了错误。
经过更多的挖掘之后,我发现您不应该设置glBindVertexArray(0)(至少在glDisableVertexAttribArray之前,根据我记得,我们将这些标志设置为0,这样我们就不会意外影响我们不需要的任何东西)

此时,错误移至我们绘制场景对象之一的位置。在这一点上,我碰壁了一点,不要去哪里了。我想我的问题是,在运行项目时是否存在需要设置的配置,或者仅在较新的图形卡上运行该配置是否可以解释不同的行为。最后一点,这是在Widnows中的Visual Studio 10(或15,还原所有更改且未重新定位解决方案时切换为10)的窗口上运行的,程序配置如下:
//GLUT Init
glutInit(&argc, argv);

glutInitContextVersion(3, 3);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);

glutInitWindowSize(windowWidth, windowHeight);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

windowHandle = glutCreateWindow(CAPTION);

//GLEW Init
glewExperimental = GL_TRUE;

GLenum result = glewInit();

//GLUT Init
std::cerr << "CONTEXT: OpenGL v" << glGetString(GL_VERSION) << std::endl;

glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glDepthRange(0.0,1.0);
glClearDepth(1.0);

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);

上面的上下文是:



让我知道是否需要任何附加信息,我不想添加任何不必要的困惑情况,并且该项目太大,无法将其全部粘贴到这里...

最佳答案

在您的着色器中,您使用的是glsl版本330内核,这意味着不建议使用texture2D(),而应改用texture()

至于您的INVALID OPERATION错误,问题在于您用glBindVertexArray(0);解除了对vao的绑定(bind),然后调用了glDisableVertexAttribArray(VERTICES);来对当前绑定(bind)的vao进行操作。您应该在这些调用下移动glBindVertexArray(0);

10-08 11:17