Jump to content
  • Advertisement
Sign in to follow this  
Damocles

OpenGL a really basic cg vertex shader - lil help?

This topic is 4080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm trying to learn vertex shaders, and have hit a weird problem at the very first step... I'm doing the basic "turn everything green" vertex shader, and it looks like this:
void main(float3 pos : POSITION,
	
	out float3 oPos : POSITION,
	out float3 color : COLOR)
{
	oPos = pos;
	color = float3(0,1,0);
}

and the openGL code to use this shader:
CGcontext context;
CGprofile vertexProfile;
CGprogram program;

void checkForCgError(const char *situation)
{
	CGerror error;
	const char *string = cgGetLastErrorString(&error);

	if (error != CG_NO_ERROR) 
	{
		Log<<"***********ERROR:  "<<situation<<" "<<string<<endl;
		if (error == CG_COMPILER_ERROR) 
		{
			Log<<cgGetLastListing(context)<<endl;
		}
		DebugBreak();
	}
}

void initCG()
{
	context = cgCreateContext();
	checkForCgError("creating cg context -- ");
	vertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
	checkForCgError("getting latest cg profile -- ");
	cgGLSetOptimalOptions(vertexProfile);
	checkForCgError("Setting optimal options -- ");
	program = cgCreateProgramFromFile(
		context,				  /* Cg runtime context */
		CG_SOURCE,                /* Program in human-readable form */
		"test.cg",			  /* Name of file containing program */
		vertexProfile,        /* Profile: OpenGL ARB vertex program */
		"main",					  /* Entry function name */
		NULL);
	checkForCgError("Creating program from file -- ");
	cgGLLoadProgram(program);
	checkForCgError("Loading program keyframer.c -- ");

	cgGLBindProgram(program);
	checkForCgError("binding vertex program -- ");
	cgGLEnableProfile(vertexProfile);
	checkForCgError("enabling vertex profile -- ");
}

and the problem? It turns the entire scene into one green rectangle that occupies the top-right 1/4 of the screen. Anybody know what is going wrong?

Share this post


Link to post
Share on other sites
Advertisement
What happens when you aren't using the Cg shader?

In the vertex shader you also need to multiple pos by the Modelview and Projection matrices. Thus the power of vertex shaders [smile], you have to do it all:


void main(float3 pos : POSITION,
out float3 oPos : POSITION,
out float3 color : COLOR,
uniform float4x4 ModelViewProj )
{
oPos = mul( ModelViewProj, pos );
color = float3(0,1,0);
}






void initCG()
{
context = cgCreateContext();
checkForCgError("creating cg context -- ");
vertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
checkForCgError("getting latest cg profile -- ");
cgGLSetOptimalOptions(vertexProfile);
checkForCgError("Setting optimal options -- ");
program = cgCreateProgramFromFile(
context, /* Cg runtime context */
CG_SOURCE, /* Program in human-readable form */
"test.cg", /* Name of file containing program */
vertexProfile, /* Profile: OpenGL ARB vertex program */
"main", /* Entry function name */
NULL);
checkForCgError("Creating program from file -- ");
cgGLLoadProgram(program);
checkForCgError("Loading program keyframer.c -- ");

cgGLBindProgram(program);
checkForCgError("binding vertex program -- ");
cgGLEnableProfile(vertexProfile);
checkForCgError("enabling vertex profile -- ");

// Cache this variable globally, or as a member..
CGparameter ModelviewProj = cgGetNamedParameter( CgProgram_, "ModelViewProj" );

// Call this after you've set up the projection and modelview matrices, aka, before you render
cgGLSetStateMatrixParameter( ModelviewProj , CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY );


}



Hopefully that should solve your problem.

Share this post


Link to post
Share on other sites
I think your problem is that your vertex shader doesn´t multiply the vertices' positions with anything, so the vertex positions you supply in your application should be pretransformed already. But I don't know what the scene is you're talking about, so that's just a wild guess. If all vertices in your scene have positive x and y coordinates they would all end up in the top-right quarter of the screen. If you want to render a 'normal' 3D scene with your vertex shader you would have to multiply the vertices with the world, view and projection matrix.

Hope that helped

[edit]
bah, too slow
[/edit]

Share this post


Link to post
Share on other sites
Thanks for the help guys, that was indeed the problem. Looks like this cg malarkey is going to be a lot of work. Oh well, back to it...

Next problem in 5...4...3...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!