如何在GLSL中更新统一变量

paj*_*777 6 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);
Run Code Online (Sandbox Code Playgroud)

}

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

void GraphicsObject :: UpdateEyePosition(Vector3d&eyePosition){

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

DWORD bob = glGetError();
//bob equals 1281 after this call       
Run Code Online (Sandbox Code Playgroud)

}

我现在已经尝试了一些更新变量的方法,这是最新的化身,感谢观看,所有评论都欢迎.

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

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

        springs[i].Draw();

}
Run Code Online (Sandbox Code Playgroud)

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

Goz*_*Goz 4

这很可能是因为 EyePos 无效造成的。

如果将函数更改为以下内容会发生什么?

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

    DWORD bob = glGetError();
}
Run Code Online (Sandbox Code Playgroud)

编辑:为了响应您的更新,glBegin/glEnd 的文档表示,如果模式设置为不可接受的值,您将收到错误 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.
Run Code Online (Sandbox Code Playgroud)

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