Blitz3D Advanced: Introduction to the world of CG and OpenGL via GLDirect.
by Drago (Adam Templeton)

DISCLAMERS

  • I assume you have some knowledge of OpenGL, with the Tutorial, which is why it Is in the Advanced 3D section since it is a Complexe topic.
    I say again THIS IS NOT FOR THE FAINT HEARTED. I assume you know how openGL works.
  • Vertex and Pixel Shaders are not magic boxes that you can drop into your program to get cool looking water and what not,
    they require the program and them to be programmed together.
    since for cool looking water you might need to pass a sine wave etc.

Introduction

Welcome to my introduction to the World of Using CG with a GL compatable version of Blitz.
At the moment the only OpenGL Library in the public arena is Peter Scheutz's GLDirect
to use CG with this extension Download my CG expansion for Blitz.
which you just unpack into the Directory, you have GLdirect in, and copy over the userlibs.

So What is CG?

  • CG is Nvidia's Compiler to make creating Vertex and Pixel Shaders, alot easier.
  • CG Stands for C for Graphics, and it is a C like language for the Programming of Vertex and Pixel Shaders.
  • When you tell CG to load in a Shader, it Converts it to the ASM language that is used by Shaders.
  • CG makes sure that it uses the correct GL Extension for Your Video card, so it works on Both ATI, and Nvidia Cards.

Getting Started with CG and Blitz


Using CG in Blitz is almost 100% indentical as using it with OpenGL via C++, since I use the same names for calling the Dll as C/C++ does
This makes it useful if one day you start using C++ with openGL.
Anyway lets get started. The first thing you want to do is include the CG.bb file that is in the GL directory of GLDirect. all code assumes that you have all the GL includes in a subdirectory of you Program.


Also needed are some vars that will hold the information that you want to send and receive from the Shader, also requried is a Varable to hold a reference to the program itself


Here is some Background for those varables... A Model view Matrix holds the current world location of the model that is about to be drawn to the screen. The Projection matrix holds info about the screen, and its aspect ratio.

Loading A CG program to be used

I normally do all the initilisation for my GL programs in a function Called GLInit, so at the end of this function So here I will Explain all the Steps for loading a CG program to be able to be used in GL.


As stated in the code, this checks what your videocard will let you do with shaders, then chooses the best one that CG is able to use.


Next we get the parameters from the CG program and let blitz know how to reference them, this is so values can be passed between CG and Blitz.


Drawing a Scene

Now that the Vertex Shader is all loaded and ready to go, we can use it to take over the Vertex Transformations, yes, that is the purpose of a Vertex Shader, instead of the card handleing them normally you use the Vertex Shader, or as they are properly called in OpenGL Vertex Programs, since they Program Vertex's , to take control of them.


An Example of a CG Program

Here is the progam that is used with the Article It looks like C code becasue C for Graphics is C :)


I will Explain more about CG in another Article. here is that source as vertex Shader Assemberly. This will get all you old school programers excited... well maybe not, but hey it is Assemberly :)


Conclusion

There you go, this is my quick attempt to explain the example program that I used for CG I will be doing 2 more articles one for All CG commands in blitz, and One to explain the CG language itself... As well as probably makeing this article a bit better... ;)


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.