Trying to get cgGLSetTextureParameter to work

Started by
4 comments, last by V-man 12 years, 8 months ago
Hi everyone, I'm trying to set a texture sampler parameter in Cg, but no combination of functions are working. Initialising Cg like so:

bool SYM_SHADERMANAGER::Init()
	{
		//Create Cg context
		CgContext = cgCreateContext();

		//Check for errors
		if (!CheckCgErrors())
		{
		    	//Engine->Logger.AddEntry("Error: Could not create Cg context");
			//Engine->Console.PrintLine("> Error: Could not create Cg context");
			return false;
		}

		//Set deferred parameters
		cgSetParameterSettingMode(CgContext, CG_DEFERRED_PARAMETER_SETTING);

		//Set register states
		cgGLRegisterStates(CgContext);

		//Manage texture parameters
		cgGLSetManageTextureParameters(CgContext, CG_TRUE);

		return true;
	}

Setting texture parameter like this:

bool SYM_SHADER::SetTexture(string Param, SYM_TEXTURE *Texture)
	{
		//Get a handle to the texture parameter
		CGparameter CgParam = cgGetNamedEffectParameter(CgEffect, Param.c_str());
		if (CgParam == 0) return false;

		//Set the texture and sampler
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, Texture->Texture);
		
		cgGLSetTextureParameter(CgParam, Texture->Texture);
		if (!ShaderManager.CheckCgErrors()) return false;
		
		cgGLSetupSampler(CgParam, Texture->Texture);
		if (!ShaderManager.CheckCgErrors()) return false;
		
		cgSetSamplerState(CgParam);
		if (!ShaderManager.CheckCgErrors()) return false;
		
		return true;
	}

And rendering like this:

//If a shader has been bound
	if(Material->Shader)
	{
		//Get the first render pass
		CGpass Pass = Material->Shader->GetFirstPass();

		//Loop until all passes have finished
		while(Pass)
		{
			//Set current pass
			Material->Shader->SetPass(Pass);

			//Draw geometry
			switch(GeomType)
			{
				case 0:
					drawPlane();
					break;
		
				case 1:
					drawCube();
					break;
			
				case 2:
					drawSphere();
					break;
			
					default: break;
			}

			//Reset current pass
			Material->Shader->ResetPass(Pass);

			//Get the next pass
			Pass = Material->Shader->GetNextPass();
		}
	}

I've tried all sorts of texture/sampler functions from several Google sources, including cgGLSetupSampler, cgSetSamplerState, using glBindTexture or not, using the Cg texture name or the Sampler name. The several programs I'm testing with all work fine in FX Composer, and anything that doesn't involve textures works fine. Any help appreciated.

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Advertisement
After a lot of reading, I've come to the conclusion I only need the lines:

cgGLSetManageTextureParameters(CGContext, CG_TRUE);andcgGLSetupSampler(CGParam, GLuint);


However, it's still not working. CG gives no errors retrieving the sampler parameter (and returns an error if I try and use the Texture parameter, so I assume it IS the Sampler name I need).

Any ideas?

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

I'm really struggling with this one :(

I've tried every resource I can dig out. The NVIDIA Cg documentation is pretty useless (and in PDF format...?), and tutorials are thin.

A few questions, if someone has the knowledge:

- I'm using OpenGL immediate mode, will this give me any problems? (UV Coords and the likes)
- Do I have to do anything else to pass in UV Coords to the program, other than glTexCoord2f?
- Do I need to call cgGLSetupSampler() once, or every frame?
- Am I requesting the correct parameter? All examples I've found use the Sampler name.
- Amidoinitrite?

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Ok now I'm absolutely tearing my hair out. If anyone can give me even the smallest piece of advice on this I'll be forever in your debt, this bug is REALLY holding back my project :(

I've abandonned my main project for now and branched off a 'test project' for getting this thing to work.

The full load:

Shader wrapper:
#include <iostream>#include "SYM_Shader.h"using namespace std;namespace SYM{	SYM_SHADERMANAGER ShaderManager;		bool SYM_SHADERMANAGER::Init()	{		//Create Cg context		CgContext = cgCreateContext();		//Check for errors		if (!CheckCgErrors()) return false;		//Set deferred parameters		cgSetParameterSettingMode(CgContext, CG_DEFERRED_PARAMETER_SETTING);		//Set register states		cgGLRegisterStates(CgContext);		//Manage texture parameters		cgGLSetManageTextureParameters(CgContext, CG_TRUE);		return true;	}		void SYM_SHADERMANAGER::Shutdown()	{		cgDestroyContext(CgContext);	}	bool SYM_SHADERMANAGER::CheckCgErrors()	{		CGerror CgError;		const char *ErrorString;		ErrorString = cgGetLastErrorString(&CgError);				if (CgError != CG_NO_ERROR)		{			if (CgError == CG_COMPILER_ERROR)			{			   	cout << ErrorString << endl;			    	cout << cgGetLastListing(CgContext) << endl;			}			else cout << ErrorString << endl;						return false;		}				return true;	}	CGcontext SYM_SHADERMANAGER::GetContext()	{		return CgContext;	}	bool SYM_SHADER::Load(string Filename)	{		//Compile Cg program from file		CgEffect = cgCreateEffectFromFile(ShaderManager.GetContext(), Filename.c_str(), 0);		//Check for errors		if (!ShaderManager.CheckCgErrors()) return false;		//Get first technique		CgTechnique = cgGetFirstTechnique(CgEffect);		if (!ShaderManager.CheckCgErrors()) return false;		//Get matrix handles		ParamWorldMatrix = cgGetEffectParameterBySemantic(CgEffect, "WORLD");		ParamWorldViewProjMatrix = cgGetEffectParameterBySemantic(CgEffect, "WORLDVIEWPROJECTION");		ParamWorldInvTransMatrix = cgGetEffectParameterBySemantic(CgEffect, "WORLDINVERSETRANSPOSE");		ParamViewMatrix = cgGetEffectParameterBySemantic(CgEffect, "VIEW");		ParamViewInverseMatrix = cgGetEffectParameterBySemantic(CgEffect, "VIEWINVERSE");		ParamWorldViewMatrix = cgGetEffectParameterBySemantic(CgEffect, "WORLDVIEW");				//Get other params		ParamEyePos = cgGetEffectParameterBySemantic(CgEffect, "CAMERAPOSITION");		ParamTime = cgGetEffectParameterBySemantic(CgEffect, "Time");		return true;	}	CGpass SYM_SHADER::GetFirstPass()	{		//World		if(ParamWorldMatrix != 0) cgGLSetStateMatrixParameter(ParamWorldMatrix, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY);				//World view projection		if(ParamWorldViewProjMatrix != 0) cgGLSetStateMatrixParameter(ParamWorldViewProjMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);				//World inverse transpose		if(ParamWorldInvTransMatrix != 0) cgGLSetStateMatrixParameter(ParamWorldInvTransMatrix, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);				//View		if(ParamViewMatrix != 0) cgGLSetStateMatrixParameter(ParamViewMatrix, CG_GL_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);				//View inverse		if(ParamViewInverseMatrix != 0) cgGLSetStateMatrixParameter(ParamViewInverseMatrix, CG_GL_PROJECTION_MATRIX, CG_GL_MATRIX_INVERSE);				//Worldview		if(ParamWorldViewMatrix != 0) cgGLSetStateMatrixParameter(ParamWorldViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);				//CamPos		float EyePos[3];		EyePos[0] = 0.0f;		EyePos[1] = 0.0f;		EyePos[2] = -3.0f;		if(ParamEyePos != 0) cgSetParameter3fv(ParamEyePos, EyePos);		//Set time		//const float CgTime = (float)Engine->GetElapsedTime() / 1000;		//cgSetParameter1fv(ParamTime, &CgTime);				//Loop through float parameters		for(map<CGparameter, float> ::iterator Floats = FloatParams.begin(); Floats != FloatParams.end(); Floats++)		{			cgSetParameter1fv((*Floats).first, &(*Floats).second);		}				//Loop through float3 parameters		for(map<CGparameter, float*> ::iterator Floats = Float3Params.begin(); Floats != Float3Params.end(); Floats++)		{			cgSetParameter3fv((*Floats).first, (*Floats).second);		}							//Loop through texture parameters		for(map<CGparameter, SYM_TEXTURE*> ::iterator Textures = TextureParams.begin(); Textures != TextureParams.end(); Textures++)		{			cgGLSetTextureParameter((*Textures).first, (*Textures).second->Texture);			cgSetSamplerState((*Textures).first);		}		return cgGetFirstPass(CgTechnique);;	}	CGpass SYM_SHADER::GetNextPass(CGpass CgPass)	{		return cgGetNextPass(CgPass);	}	void SYM_SHADER::SetPass(CGpass CgPass)	{		cgSetPassState(CgPass);	}	void SYM_SHADER::ResetPass(CGpass CgPass)	{		cgResetPassState(CgPass);	}	void SYM_SHADER::Destroy()	{		cgDestroyEffect(CgEffect);	}		bool SYM_SHADER::AddFloatParam(string Semantic, float Value)	{		//Get parameter		CGparameter CgParam = cgGetNamedEffectParameter(CgEffect, Semantic.c_str());		if (CgParam == 0) return false;				//Add float to map		FloatParams[CgParam] = Value;	}		bool SYM_SHADER::AddFloat3Param(string Semantic, float F1, float F2, float F3)	{		//Get parameter		CGparameter CgParam = cgGetNamedEffectParameter(CgEffect, Semantic.c_str());		if (CgParam == 0) return false;				//Add floats to map		float Floats[3];		Floats[0] = F1;		Floats[1] = F2;		Floats[2] = F3;				Float3Params[CgParam] = Floats;	}		bool SYM_SHADER::AddTextureParam(string Semantic, string Filename)	{		//Get parameter		CGparameter CgParam = cgGetNamedEffectParameter(CgEffect, Semantic.c_str());		if (CgParam == 0)		{			cout << "No parameter: " << Semantic << endl;			return false;		}				//New texture		SYM_TEXTURE *Texture = new SYM_TEXTURE;				//Load texture		if(!Texture->Load(Filename))		{			cout << "Error loading texture " << Filename << endl;			delete Texture;			return false;		}				//Add parameter and texture to map		TextureParams[CgParam] = Texture;				return true;	}} //Namespace


Init and rendering:
#include <iostream>#include <GL/gl.h>#include <GL/glu.h>#include "Qt_OpenGL.h"using namespace std;Qt_OGLScene::Qt_OGLScene (QWidget* parent) : QGraphicsScene (parent){	if(!SYM::ShaderManager.Init()) QMessageBox::warning(NULL, "Error", "Could not initialise shader manager");		Shader = new SYM::SYM_SHADER;		Shader->Load("texture.cgfx");	Shader->AddTextureParam("DiffuseSampler", "rockwall.png");}Qt_OGLScene::~Qt_OGLScene (){	Shader->Destroy();	SYM::ShaderManager.Shutdown();}void Qt_OGLScene::drawForeground( QPainter* painter, const QRectF & rect ){	glEnable(GL_DEPTH_TEST);		glViewport(0, 0, 800, 800);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0f, 800 / 800, 0.1f, 1000.0f);		glMatrixMode(GL_MODELVIEW);		glShadeModel(GL_SMOOTH);	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();		glTranslatef(0.0, 0.2, -4.2);	glRotatef(35.0f, 1, 0, 0);	glRotatef(45.0f, 0, 1, 0);		glEnable(GL_TEXTURE_2D);		//If a shader has been bound	if(Shader)	{		//Get the first render pass		CGpass Pass = Shader->GetFirstPass();		//Loop until all passes have finished		while(Pass)		{			//Set current pass			Shader->SetPass(Pass);			//Draw geometry			drawCube();			//Reset current pass			Shader->ResetPass(Pass);			//Get the next pass			Pass = Shader->GetNextPass(Pass);		}	}	else	{		cout << "Error: Rendering with no shader loaded\n";	}}void Qt_OGLScene::drawCube(){	glBegin(GL_QUADS);		//Front		glTexCoord2f(0.0f, 0.0f); glNormal3f( 0.0f, 0.0f, -1.0f); glVertex3f(-1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glNormal3f( 0.0f, 0.0f, -1.0f); glVertex3f( 1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glNormal3f( 0.0f, 0.0f, -1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 1.0f); glNormal3f( 0.0f, 0.0f, -1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);				//Top		glTexCoord2f(0.0f, 1.0f); glNormal3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 0.0f); glNormal3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glNormal3f( 0.0f, 1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glNormal3f( 0.0f, 1.0f, 0.0f); glVertex3f( 1.0f,  1.0f, -1.0f);				//Left		glTexCoord2f(0.0f, 0.0f); glNormal3f( -1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(1.0f, 0.0f); glNormal3f( -1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glNormal3f( -1.0f, 0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 1.0f); glNormal3f( -1.0f, 0.0f, 0.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	glEnd();}


Anything at ALL appreciated, thanks in advance.


"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Did you actually tried to check, OpenGL errors?
Maybe the problem is not in CG but in OpenGL...

GLenum errCode;
const GLubyte *errString;
if ((errCode = glGetError()) != GL_NO_ERROR)
{
errString = gluErrorString(errCode);
fprintf (stderr, "OpenGL Error: %s\n", errString);
}

Did you actually tried to check, OpenGL errors?
Maybe the problem is not in CG but in OpenGL...

GLenum errCode;
const GLubyte *errString;
if ((errCode = glGetError()) != GL_NO_ERROR)
{
errString = gluErrorString(errCode);
fprintf (stderr, "OpenGL Error: %s\n", errString);
}



Why are you responding to a post made in 2008?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement