4

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?

4

1 回答 1

2

你必须先打电话cgGLEnableProfile,然后再打电话cgGLBindProgram。从你的问题看来,你是反过来的。

来自 Cg 文档cgGLBindProgram

cgGLBindProgram 将程序绑定到当前状态。程序必须先用 cgGLLoadProgram 加载,然后才能绑定。此外,必须启用程序的配置文件才能使绑定起作用。这可以通过 cgGLEnableProfile 函数来完成。

于 2013-02-21T17:19:01.610 回答