1
\$\begingroup\$

I am trying to implement a simple PC program with OpenGL, using mandatorily Cg shaders (no Unity whatsoever).

I have found some tips on this page http://bobobobo.wordpress.com/2008/10/05/cg-1/ and kept the Cgprogram, Cgcontext and Cgprofile types in my own project.

Problem is: How can you access uniform variables in the C++ code? I have tried the glGetUniformLocation way, but that won't work because the first parameter must be the program's ID of GLuint type and I have CGprogram type.

I have already searched by myself (including the Nvidia Cg 'tutorial') but I can't seem to find both Cg and OpenGL discussed on a page without having to deal with Unity.

edit: I can now access the variables, but in my case I'm trying to change a texture, and it doesn't bind.

\$\endgroup\$
3
  • \$\begingroup\$ You do know that Cg is not simply a compatibility layer for GLSL right? It has its entire own runtime API because it can compile code to ARB VP/FP, dozens of NV assembly languages and of course GLSL. \$\endgroup\$ Commented Jun 21, 2014 at 3:16
  • \$\begingroup\$ Yes I do, but I didn't manage to understand how it works, there's so little documentation about it, so if someone can help me with this I'd be so grateful. \$\endgroup\$
    – Ety
    Commented Jun 21, 2014 at 23:13
  • \$\begingroup\$ Sorry, I have not used Cg in about 12 years. I think the original "The Cg Tutorial" is available for free from NVidia these days though. That explains all of the basics even though it is about 12 years old. See here for more details. Unity did not even exist when that book was written, so I have no idea what you are talking about to be honest though ;) \$\endgroup\$ Commented Jun 21, 2014 at 23:14

2 Answers 2

1
\$\begingroup\$

cgGetNamedParameter

Example code:

CGprogram cgprog;
cgprog = cgCreateProgram(...);
cgGLLoadProgram(cgprog);
CGparameter modelviewproj, lightpos;
modelviewproj = cgGetNamedParameter(cgprog, "modelviewproj");
lightpos = cgGetNamedParameter(cgprog, "lightpos");

Sample code paraphrased from the book Advanced Game Development with Programmable Graphics Hardware by Watt Policarpo.

\$\endgroup\$
4
  • \$\begingroup\$ Note: I don't really know how Cg works. I just happen to have a book that uses it. \$\endgroup\$ Commented Jun 23, 2014 at 7:17
  • \$\begingroup\$ Thank you very much but texture still won't change. I wish I had access to more examples from this book to sort this out. \$\endgroup\$
    – Ety
    Commented Jun 23, 2014 at 14:04
  • \$\begingroup\$ The book comes with a full C++/Cg rendering engine. Do you have an e-mail? Maybe I can send you some relevant pieces of code for handling textures. \$\endgroup\$ Commented Jun 23, 2014 at 22:36
  • \$\begingroup\$ Thank you very much. My e-mail is normally found on my profile. You can also find me on twitter @ety04. \$\endgroup\$
    – Ety
    Commented Jun 24, 2014 at 16:34
0
\$\begingroup\$

I have finally managed to find a way to access uniform texture variables.

First (after preparing the GL window) you have to initialize the Cg stuff:

myCgContext = cgCreateContext();
myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
cgGLSetOptimalOptions(myCgVertexProfile);
myCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgGLSetOptimalOptions(myCgFragmentProfile);

myCgVertexProgram =
    cgCreateProgramFromFile(myCgContext, CG_SOURCE,"../vertexshader.cg", myCgVertexProfile,"VS_Main",NULL);

myCgFragmentProgram =
    cgCreateProgramFromFile(myCgContext, CG_SOURCE,"../fragmentshader.cg", myCgFragmentProfile,"FS_Main",NULL);

cgGLLoadProgram(myCgVertexProgram);
cgGLLoadProgram(myCgFragmentProgram);

Then, in your display function, you have to do what you usually do, but put the following lines exactly BEFORE the actual draw (glDrawArrays in my case):

CGparameter loctex = cgGetNamedParameter(myCgFragmentProgram, "tex"); // in my fragment shader I have declared: "uniform sampler2D tex;"
cgGLSetTextureParameter(loctex, IDtex[1]); // IDtex is my array of texture IDs
cgGLEnableTextureParameter(loctex);

You can find more examples of accessing Cg variables in the NVIDIA OpenGL SDK projects (but that's difficult...); otherwise I have found great simple examples of using Cg with OpenGL here (scroll down & check right column): http://perso.limsi.fr/jacquemi/RGA-TD/index.html

\$\endgroup\$
1
  • \$\begingroup\$ That resource looks pretty good. Do you still need more samples? \$\endgroup\$ Commented Jun 24, 2014 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .