开发者

Shader and opengl transformations

开发者 https://www.devze.com 2023-01-23 23:30 出处:网络
When i add shaders (in cg) to my opengl program all the local transformations (glRotatef, glTranslatef and glScalef between glPushMatrix and glPopMatrix) stop working. Transforms outside push/pop stil

When i add shaders (in cg) to my opengl program all the local transformations (glRotatef, glTranslatef and glScalef between glPushMatrix and glPopMatrix) stop working. Transforms outside push/pop still working though. So what might be the problem here?

update: I have a rotating cube in the center of the scene:

glPushMatrix();
glRotatef(angle, 1, 0, 0);
drawBox();
glPopMatrix();

and after that i send worldview and worldviewprojection matrices to shader:

cgGLSetStateMatrixParameter(
    myCgVertexParam_modelViewProj,
    CG_GL_MODELVIEW_PROJECTION_MATRIX,
    CG_GL_MATRIX_IDENTITY
);

cgGLSetStateMatrixParameter(
    myCgVertexParam_modelView,
    CG_GL_MODELVIEW_MATRIX,
    CG_GL_MATRIX_IDENTITY
);

Vertex shader code:

void C9E2v_fog(float4 position    : POSITION,
               float4 color       : COLOR,

            out float4 oPosition    : POSITION,
            out float4 oColor       : COLOR,
            out float  fogExponent  : TEXCOORD1,

        uniform float    fogDensity,  // Based on log2
        uniform float4x4 modelViewProj : MODELVIEW_PROJECTION_MATRIX,
        uniform float4x4 modelView : MODELVIEW_MATRIX)
{   
  // Assume nonprojective modelview matrix
  float3 eyePosition = mul(modelView, position).xyz;
  float fogDistance  = length(eyePosition);
  fogExponent  = fogDistance * fogDensity;
  oPosition    = mul(modelV开发者_运维知识库iewProj, position);

  //oDecalCoords = decalCoords;
  oColor       = color;
}

So in the end cube doesnt rotate, but if i do write (no push/pop)

glRotatef(angle, 1, 0, 0);
drawBox();

everything works fine. How do i fix that?


You can use either fixed function pipeline or programmable one. Since you switched to shaders, fixed function pipeline "stopped working". To switch back you need to glUseProgram(0). And you need to send those local transformations to the shader.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号