如何在GLSL中更新统一变量

时间:2010-03-25 08:46:37

标签: c++ opengl glsl

我试图从我的应用程序更新我的着色器中的眼睛位置,但是当我尝试这个时,我一直收到错误1281。初始化后我没有问题,因为我随后尝试更新值。这是我的代码:

void GraphicsObject :: SendShadersDDS(char vertFile [],char fragFile [],                                     char filename []) {

            char *vs = NULL,*fs = NULL;

            vert = glCreateShader(GL_VERTEX_SHADER);
            frag = glCreateShader(GL_FRAGMENT_SHADER);

            vs = textFileRead(vertFile);
            fs = textFileRead(fragFile);
            const char * ff = fs;
            const char * vv = vs;

            glShaderSource(vert, 1, &vv, NULL);
            glShaderSource(frag, 1, &ff, NULL);

            free(vs); free(fs);

            glCompileShader(vert);
            glCompileShader(frag);

            program = glCreateProgram();
            glAttachShader(program, frag);
            glAttachShader(program, vert);

            glLinkProgram(program);
            glUseProgram(program);

        LoadCubeTexture(filename, compressedTexture);

        GLint location = glGetUniformLocation(program, "tex");
        glUniform1i(location, 0);
        glActiveTexture(GL_TEXTURE0);

        EyePos = glGetUniformLocation(program, "EyePosition");

        glUniform4f(EyePos, EyePosition.X(),EyePosition.Y(), 
                                    EyePosition.Z(), 1.0);          
        DWORD bob = glGetError();
        //All is fine here
        glEnable(GL_DEPTH_TEST);

}

这是我打电话来更新眼睛位置的功能:

void GraphicsObject :: UpdateEyePosition(Vector3d& eyePosition){

glUniform4f(EyePos, eyePosition.X(),eyePosition.Y(), 
                                    eyePosition.Z(), 1.0);

DWORD bob = glGetError();
//bob equals 1281 after this call       

}

我现在尝试了一些更新变量的方法,这是最新的化身,感谢您查看,欢迎所有评论。

更新:错误实际上并没有发生在这里,我假设它的错误,当我绘制一些弹簧时实际发生错误:

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

        springs[i].Draw();

}

当我绘制第一个时它很好但是在调用glEnd()响应glBegin(GL_LINE_STRIP)时调用第二个时出错。很抱歉给您带来不便,因为这不是我发布的错误,但至少如果有人想知道如何更新统一变量那么它就在这里。

1 个答案:

答案 0 :(得分:4)

这很可能是因为EyePos无效。

如果将功能更改为以下内容会怎样?

void GraphicsObject::UpdateEyePosition(Vector3d& eyePosition)
{
    EyePos = glGetUniformLocation(program, "EyePosition");
    glUniform4f(EyePos, eyePosition.X(),eyePosition.Y(), eyePosition.Z(), 1.0);

    DWORD bob = glGetError();
}

编辑:为响应您的更新,glBegin / glEnd的文档说如果mode设置为不可接受的值,您将收到错误1280(GL_INVALID_ENUM)。因此,您的问题是不支持GL_LINE_STRIP。

GL_INVALID_OPERATION is generated if glBegin is executed between a glBegin and the corresponding execution of glEnd.

GL_INVALID_OPERATION is generated if glEnd is executed without being preceded by a glBegin.

GL_INVALID_OPERATION is generated if a command other than glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, glEdgeFlag, glCallList, or glCallLists is executed between the execution of glBegin and the corresponding execution glEnd.

GL_INVALID_OPERATION返回错误1282和GL_INVALID_ENUM 1280 ......所以很大程度上取决于你得到的确切错误......