NeHe OpenGL Lesson47 - CG Vertex Shader

lesson47_screenshot

This samples us how to apply Cg vertex shader in OpenGL. Shader is a kind of short c-like code that could run on the GPU. One the one hand, we could get much flexibility while rendering our-own visual effect. This will keep us away from hard understanding OpenGL render states settings. On the other hand, we could move some work form the CPU to GPU, this could be better for us balance the whole workload between CPU and GPU.

 

The Usage of CG Vertex Shader

The workflow to use cg vertex shader almost the same as GLSL or HLSL. Usually, the general steps as following:

1) Setup the shader context: check the supported version number, check whether video card support shader or not, create some container to hold all shaders and so on;

2) Compile the shader from source code;

3) Get the shader program from the compiled result;

4) Retrieve the shader parameters from shader program;

5) While rendering, bind or set shader program, and set the shader parameters;

6) Release shader program resource while shut down;

 

Set up Cg & Get the latest Vertex Profile

cgContext = cgCreateContext();
...
cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);

 

Compile the Cg shader Source code

cgGLSetOptimalOptions(cgVertexProfile);
cgVertexProgram = cgCreateProgramFromFile(cgContext, 
    CG_SOURCE, "CG/Wave.cg", cgVertexProfile, "main", 0);

 

Load the Shader Program & Retrieve the Shader Parameter

cgGLLoadProgram(cgVertexProgram);
position = cgGetNamedParameter(cgVertexProgram, "IN.position");

 

Draw with Shader Program

复制代码
cgGLEnableProfile(cgVertexProfile);    
// Bind Our Vertex Program To The Current State
cgGLBindProgram(cgVertexProgram);
cgGLSetParameter4f(color, 0.5f, 1.0f, 0.5f, 1.0f);
// Submit verterx data here
...
cgGLDisableProfile(cgVertexProfile);
复制代码

 

Release Cg Shader Program

cgDestroyProgram(cgVertexProgram);
cgDestroyContext(cgContext);

 

The full source code could be found here.

posted @   opencoder  阅读(431)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示