Cg problems with OpenGL

我怕爱的太早我们不能终老 提交于 2019-12-10 15:54:16

问题


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:

  1. Create a Cg context with cgCreateContext.
  2. Get the latest vertex and pixel shader profiles using cgGLGetLatestProfile with CG_GL_VERTEX and CG_GL_FRAGMENT, respectively. Use cgGLSetContextOptimalOptions to create the optimal setup for both profiles.
  3. Using these profiles and shaders you have written, create shader programs using cgCreateProgramFromFile.
  4. Load the shader programs using cgGLLoadProgram.

Then, each frame, for an object that uses a given shader:

  1. Bind the desired shader(s) (vertex and/or pixel) using cgGLBindProgram.
  2. Enable the profile(s) for the desired shader(s) using cgGLEnableProfile.
  3. Retrieve and set any needed uniform shader parameters using cgGetNamedParameter and the various parameter setting functions.
  4. Render your object normally
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!