问题
I'm working on an OpenGL project on Windows, using GLEW to provide the functionality the provided Windows headers lack. For shader support, I'm using NVIDIA's Cg. All the documentation and code samples I have read indicate that the following is the correct method for loading an using shaders, and I've implemented things this way in my code:
- Create a Cg context with
cgCreateContext
. - Get the latest vertex and pixel shader profiles using
cgGLGetLatestProfile
withCG_GL_VERTEX
andCG_GL_FRAGMENT
, respectively. UsecgGLSetContextOptimalOptions
to create the optimal setup for both profiles. - Using these profiles and shaders you have written, create shader programs using
cgCreateProgramFromFile
. - Load the shader programs using
cgGLLoadProgram
.
Then, each frame, for an object that uses a given shader:
- Bind the desired shader(s) (vertex and/or pixel) using
cgGLBindProgram
. - Enable the profile(s) for the desired shader(s) using
cgGLEnableProfile
. - Retrieve and set any needed uniform shader parameters using
cgGetNamedParameter
and the various parameter setting functions. - Render your object normally
- Clean up the shader by calling
cgGLDisableProfile
However, things start getting strange. When using a single shader everything works just fine, but the act of loading a second shader with cgGLLoadProgram
seems to make objects using the first one cease to render. Switching the draw order seems to resolve the issue, but that's hardly a fix. This problem occurs on both my and my partner's laptops (fairly recent machines with Intel integrated chipsets).
I tested the same code on my desktop with a GeForce GTX 260, and everything worked fine. I would just write this off as my laptop GPU not getting along with Cg, but I've successfully built and run programs that use several Cg shaders simultaneously on my laptop using the OGRE graphics engine (unfortunately the assignment I'm currently working on is for a computer graphics class, so I can't just use OGRE).
In conclusion, I'm stumped. What is OGRE doing that my code is not? Am I using Cg improperly?
回答1:
You have to call cgGLEnableProfile
before you call cgGLBindProgram
. From your question it appears you do it the other way around.
From the Cg documentation for cgGLBindProgram:
cgGLBindProgram binds a program to the current state. The program must have been loaded with cgGLLoadProgram before it can be bound. Also, the profile of the program must be enabled for the binding to work. This may be done with the cgGLEnableProfile function.
来源:https://stackoverflow.com/questions/10401357/cg-problems-with-opengl