着色器和 opengl 转换
当我将着色器(在cg中)添加到我的opengl程序中时,所有本地转换(glRotatef、glTranslatef和glPushMatrix和glPopMatrix之间的glScalef)停止工作。转换外部推/弹出仍然有效。那么这里可能存在什么问题呢?
更新: 我在场景中心有一个旋转立方体:
glPushMatrix();
glRotatef(angle, 1, 0, 0);
drawBox();
glPopMatrix();
之后我将世界视图和世界视图投影矩阵发送到着色器:
cgGLSetStateMatrixParameter(
myCgVertexParam_modelViewProj,
CG_GL_MODELVIEW_PROJECTION_MATRIX,
CG_GL_MATRIX_IDENTITY
);
cgGLSetStateMatrixParameter(
myCgVertexParam_modelView,
CG_GL_MODELVIEW_MATRIX,
CG_GL_MATRIX_IDENTITY
);
顶点着色器代码:
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(modelViewProj, position);
//oDecalCoords = decalCoords;
oColor = color;
}
所以最终立方体不会旋转,但如果我写(没有推/弹出)
glRotatef(angle, 1, 0, 0);
drawBox();
一切正常。我该如何解决这个问题?
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(modelViewProj, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用固定功能管道或可编程管道。由于您切换到着色器,固定功能管道“停止工作”。要切换回来,您需要
glUseProgram(0)
。您需要将这些局部变换发送到着色器。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.