cgFX rendering issue

Started by
0 comments, last by iykxcy 16 years, 8 months ago
For the while now, I've been working with DirectX and its FX format, but lately I decided to visit my OpenGL roots and take a stab at cgFX. So far, it's rather easy to understand and implement. My problem is that while everything seems to show up, it's not displaying entirely correct. In my demo, I render 3 quads to the screen, each with its own texture. Each quad, I also have colored either red, green, or blue. The first quad, colored red, is in the middle. The second quad, colored green, is on the right. And finally, the third quad, colored blue is on the left. This render correctly if I never call cgSetPassState(), but if I do call it, the rendering of the quads changes. What happens is that the quads' color and textures are misplaced. The geometry and positions are correct, but the colors and textures are placed on the quads from left to right (left = red quad, middle = green quad, right = blue quad). Here is the source for my render call:

	CGpass	pass	=	cgGetFirstPass(m_pShader -> GetActiveTechnique());
	while(pass)
	{
		cgSetPassState(pass);


		glBindTexture(GL_TEXTURE_2D, 1);

		glPushMatrix();
		{
			glTranslatef(0, 0, -3);
			glRotatef(g_fRotation, 0, 0, 1);

			glBegin(GL_TRIANGLE_STRIP);
			{
				glColor4f(1, 0, 0, 1);	glTexCoord2f(0, 1);	glVertex3f(-1, 1, 0);
				glColor4f(1, 0, 0, 1);	glTexCoord2f(1, 1);	glVertex3f(1, 1, 0);
				glColor4f(1, 0, 0, 1);	glTexCoord2f(0, 0);	glVertex3f(-1, -1, 0);
				glColor4f(1, 0, 0, 1);	glTexCoord2f(1, 0);	glVertex3f(1, -1, 0);
			}
			glEnd();
		}
		cgGLSetStateMatrixParameter(cgGetEffectParameterBySemantic(m_cgEffect, "ModelViewProjection"), CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
		glPopMatrix();

		glPushMatrix();
		{
			glBindTexture(GL_TEXTURE_2D, 2);

			glTranslatef(2, 0, sin(g_fRotation / 180 * (float) M_PI) - 3);
			glRotatef(g_fRotation, 0, 0, 1);

			glBegin(GL_TRIANGLE_STRIP);
			{
				glColor4f(0, 1, 0, 1);	glTexCoord2f(0, 1);	glVertex3f(-1, 1, 0);
				glColor4f(0, 1, 0, 1);	glTexCoord2f(1, 1);	glVertex3f(1, 1, 0);
				glColor4f(0, 1, 0, 1);	glTexCoord2f(0, 0);	glVertex3f(-1, -1, 0);
				glColor4f(0, 1, 0, 1);	glTexCoord2f(1, 0);	glVertex3f(1, -1, 0);
			}
			glEnd();
		}
		cgGLSetStateMatrixParameter(cgGetEffectParameterBySemantic(m_cgEffect, "ModelViewProjection"), CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
		glPopMatrix();

		glPushMatrix();
		{
			glBindTexture(GL_TEXTURE_2D, 3);

			glTranslatef(-2, 0, cos(g_fRotation / 180 * (float) M_PI) - 3);
			glRotatef(g_fRotation, 0, 0, 1);

			glBegin(GL_TRIANGLE_STRIP);
			{
				glColor4f(0, 0, 1, 1);	glTexCoord2f(0, 1);	glVertex3f(-1, 1, 0);
				glColor4f(0, 0, 1, 1);	glTexCoord2f(1, 1);	glVertex3f(1, 1, 0);
				glColor4f(0, 0, 1, 1);	glTexCoord2f(0, 0);	glVertex3f(-1, -1, 0);
				glColor4f(0, 0, 1, 1);	glTexCoord2f(1, 0);	glVertex3f(1, -1, 0);
			}
			glEnd();
		}
		cgGLSetStateMatrixParameter(cgGetEffectParameterBySemantic(m_cgEffect, "ModelViewProjection"), CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
		glPopMatrix();

		
		cgResetPassState(pass);
		pass	=	cgGetNextPass(pass);
	}


And here is my cgFX file:

float4x4	ModelViewProj	:	ModelViewProjection;

sampler2D	TexMap	:	register(s0);


struct VS_Input
{
	float3	Position	:	POSITION;
	float4	Color		:	COLOR;
	float2	TexCoord	:	TEXCOORD;
};

struct VS_Output
{
	float4	Position	:	POSITION;
	float4	Color		:	COLOR;
	float2	TexCoord	:	TEXCOORD;
};


VS_Output vs_default(VS_Input In)
{
	VS_Output	Out;
	
	Out.Position	=	mul(ModelViewProj, float4(In.Position, 1.0));
	Out.Color	=	In.Color;
	Out.TexCoord	=	In.TexCoord;
	
	return Out;
}

float4 ps_default(VS_Output In) : COLOR
{
	return tex2D(TexMap, In.TexCoord) * In.Color;
}


technique technique_default
{
	pass
	{
		VertexProgram	=	compile arbvp1	vs_default();
		FragmentProgram	=	compile arbfp1	ps_default();
	}
}


Any incite would be appreciated. Also, if necessary, I can provide screenshots. [Edited by - iykxcy on August 2, 2007 12:57:54 PM]
Advertisement
Nevermind, found the cause by dumb luck. Turns out that you have pass the matrices to the effect BEFORE you draw the geometry, but AFTER you apply your transformations.

This topic is closed to new replies.

Advertisement