GLSL:未呈现的制服变化

时间:2011-03-27 00:47:51

标签: c++ opengl glsl shader

我正在调整OpenGL Superbible 2010中的着色器示例,以了解着色器的使用情况。

我想要将作为制服传递的定向光的红色分量更改为漫反射着色器。向量

GLfloat vDiffuseColor [] = {redDirectionalLight, greenDirectionalLight, blueDirectionalLight, 1.0f};

是全球定义的。

如果我在编译之前指定变量但是在代码运行时没有指定变量,我可以调整浅色。我正在使用的过度键盘功能会在运行时增加颜色值,但不会重新加载修改后的统一。在SetupRC函数中,我找到diffuseColor制服

的位置
locColor = glGetUniformLocation(DirectionalLightSimpleDiffuseShader, "diffuseColor");

我在这里缺少什么?

vertex program:
#version 130

// Incoming per vertex... position and normal
in vec4 vVertex;
in vec3 vNormal;

// Set per batch
uniform vec4    diffuseColor;   
uniform vec3    vLightPosition;
uniform mat4    mvpMatrix;
uniform mat4    mvMatrix;
uniform mat3    normalMatrix;

// Color to fragment program
smooth out vec4 vVaryingColor;

void main(void) 
    { 
    // Get surface normal in eye coordinates
    vec3 vEyeNormal = normalMatrix * vNormal;

    // Get vertex position in eye coordinates
    vec4 vPosition4 = mvMatrix * vVertex;
    vec3 vPosition3 = vPosition4.xyz / vPosition4.w;

    // Get vector to light source
    vec3 vLightDir = normalize(vLightPosition - vPosition3);

    // Dot product gives us diffuse intensity
    float diff = max(0.0, dot(vEyeNormal, vLightDir));

    // Multiply intensity by diffuse color
    vVaryingColor.rgb = diff * diffuseColor.rgb;
    vVaryingColor.a = diffuseColor.a;

    // Let's not forget to transform the geometry
    gl_Position = mvpMatrix * vVertex;
    }

片段程序:

#version 130

out vec4 vFragColor;
smooth in vec4 vVaryingColor;

void main(void)
   { 
   vFragColor = vVaryingColor;
   }

这是RenderScene代码,通过glutDisplayFunc调用:

void RenderScene (void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



    modelViewMatrix.PushMatrix(); 

    M3DMatrix44f mCamera; // CAMERA MATRIX

       cameraFrame.GetCameraMatrix(mCamera);
    modelViewMatrix.PushMatrix(mCamera);  


    M3DMatrix44f vLightPos= {0.0f, 10.0f, 5.0f, 1.0f};
    M3DVector4f  vLightEyePos;
    m3dTransformVector4 (vLightEyePos, vLightPos, mCamera);


    shaderManager.UseStockShader (GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),
                                  vFloorColor);


    floorBatch.Draw();

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

         modelViewMatrix.PushMatrix(); 
         modelViewMatrix.MultMatrix(spheres[i]); 


            glUseProgram (DirectionalLightSimpleDiffuseShader);
            glUniform4fv (locColor, 1, vDiffuseColor); 
            glUniform3fv (locLight, 1, vEyeLight);
            glUniformMatrix4fv (locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix());



            glUniformMatrix4fv (locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix());
            glUniformMatrix3fv (locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix());


            sphereBatch.Draw();
        //  glUseProgram(0);
            modelViewMatrix.PopMatrix(); 
    }

1 个答案:

答案 0 :(得分:2)

请告诉我你正在改变vDiffuseColor而不是redDirectionalLightgreenDirectionalLightblueDirectionalLight

GLfloat vDiffuseColor [] = {redDirectionalLight, greenDirectionalLight, blueDirectionalLight, 1.0f};

通过复制初始化程序创建一个数组,没有与原始三个变量的连接。

如果你想按名称解决这三个组件,而是使用(假设是C ++):

GLfloat vDiffuseColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat&   redDirectionalLight = vDiffuseColor[0];
GLfloat& greenDirectionalLight = vDiffuseColor[1];
GLfloat&  blueDirectionalLight = vDiffuseColor[2];