Archived

This topic is now archived and is closed to further replies.

xyz

anyone using Tao.Cg...

Recommended Posts

Hi, I''m trying to make the simplest cg program possible... and I''m using Tao.Cg, but I don''t know how to load the program and get the parameters... this is what I''m doing now:
static IntPtr context;
static IntPtr program;

...

context = Cg.cgCreateContext();
program = Cg.cgCreateProgramFromFile(context, Cg.CG_SOURCE, "vertexShader.cg", Cg.CG_PROFILE_VP20, null, null);
Next, in c++, I would do:
cgGLLoadProgram(program);
param = cgGetNamedParameter(program, "ParamName");
Any help? Thanks

Share this post


Link to post
Share on other sites
Ok, heres a quick rundown by a no-expert (me). This is under C++, have no idea of what "Tao" is... none the less, hope it helps you out... cause i hate explaining things... Im also not going to use "source":

0) The variables I''ll be using:
/**///oo[ Cg ]
CGcontext Context = NULL; // One per App
CGprogram vProgram = NULL; // Vertex Program
CGprogram fProgram = NULL; // Fragment Program

// Profiles
CGprofile vProfile, fProfile; // Vertex & Fragment Profiles

// Parameters
CGparameter Kd = NULL;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1) Error Handling:
cgSetErrorCallback(GL_CgError);

This sets up a callback in case some error occurs, my error function is:
void GL_CgError(void)
{
const char * error;
error = cgGetErrorString(cgGetError());
MBox("Cg Error:", error);
};

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2) Get a Context:
Context = cgCreateContext();

if (Context == NULL) // Validate Our Context Generation Was Successful
{
MBox("Cg Error:", "Failed to Create a Cg Context...");
return;
};


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3) Choose your profile:
if(!GL_CgChooseProfile())
{
MBox("Cg", "No Profiles");
return;
};

This is my profile selection function:
bool GL_CgChooseProfile(void)
{
bool ret = true;

if (cgGLIsProfileSupported(CG_PROFILE_ARBVP1))
{
vProfile = CG_PROFILE_ARBVP1;
} else { ret = false; };

if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1))
{
fProfile = CG_PROFILE_ARBFP1;
} else { ret = false; };

return ret;
};

Returns false if it wasnt capable of finding the "standard" profiles. I dont use nVidia-specific profiles...


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4) Load the Shader Code:
GL_CgLoadShaders(); // Load Shaders

And the function:
void GL_CgLoadShaders()
{
vProgram = cgCreateProgramFromFile(Context, CG_SOURCE, "Shaders\\v01.cg", vProfile, NULL, NULL);

if (!cgIsProgramCompiled(vProgram)) { cgCompileProgram(vProgram); };

// Enable the appropriate vertex profile and load the vertex program.
cgGLEnableProfile(vProfile);
cgGLLoadProgram(vProgram);

fProgram = cgCreateProgramFromFile(Context, CG_SOURCE, "Shaders\\f01.cg", fProfile, NULL, NULL);
if (!cgIsProgramCompiled(fProgram)) { cgCompileProgram(fProgram); };

cgGLEnableProfile(fProfile);
cgGLLoadProgram(fProgram);
};

Ok, this function loads both the vertex program and the fragment program, and enables both.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5) Bind Programs after Loading:
cgGLBindProgram(vProgram); // Vertex
cgGLBindProgram(fProgram); // Fragment

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6) Set Parameters:
Kd = cgGetNamedParameter( vProgram, "Kd");
cgGLSetParameter4f(Kd, 1.0, 0.0, 0.0, 1.0);

The first lines gets a handle to the variable, the 2nd alters that variable.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7) Per frame stuff:
I''m not doing anything per-frame... that loaded and active shader code affects ewverything rendered... you could, as an example, alter the Kd variable per frame.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8) The Shader Code:

// Fragment Shader
struct C2E2f_Output
{
float4 color : COLOR;
};

C2E2f_Output main(float4 color : COLOR)
{
C2E2f_Output OUT;
OUT.color = color;
return OUT;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Vertex Shader
// Base Green Vertex Shader

struct C2E1v_Output
{
float4 position : POSITION;
float4 color : COLOR;
};

C2E1v_Output main(float2 position : POSITION,
uniform float4 Kd)
{
C2E1v_Output OUT;

OUT.position = float4(position, 0, 1);
OUT.color = Kd;

return OUT;
}

ok, the fragment shader does absolutely nothing, but serves as a placeholder for future changes.
The vertex shader processes the vertices, places them, and assigns a color, which is Kd.

Like I said... hope it helped...
If you have any other questions ask away, ill be watching this thread...

Salsa cooked it, your eyes eat it!
[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD][My DevDiary]
[Yann L.][Enginuity] [Penny Arcade] [MSDN][VS RoadMap][Humus][BSPs][UGP][NeHe]
                          Prozak - The GameDever formally known as pentium3id

Share this post


Link to post
Share on other sites
Tao is an opengl wrapper for .NET...

And I know how it is done in c++... my problem is doing it using Tao... cause I can''t find the methods allowing me to do what you just wrote...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A little late (I found it via google while searching for the same problem).

I think that Tao.Cg is still missing those functions. So you''ll have to write it on your own or go to the Axiom project and see if there''s what you are looking for.

-snake

Share this post


Link to post
Share on other sites