View Full Version : OGL 3.1 core and CG runtime

11-29-2009, 05:49 PM
I'm trying to make OGL 3.2 core profile (actually, my card only supports 2.1, but I'm trying to only use non-deprecated functions) and CG runtime work together. I've seen OGL and (Pixar's) shaders before, but I'm new to both OGL 3.2 and Cg (~4 days old distribution).

The problem is I'm not sure how to bind the VBOs with the shader input (from what I understand, VAO are deprecated?).

Both shaders compile fine, setting the ModelViewProjection matrix works fine (I use CG runtime), the triangle I have in my VBO displays fine (and I'm not sure why!), but I cannot get the colors to display. I would say I'm missing a "semantic" bind somewhere, something that'd tell Cg "this VBO has the colors"; CG runtime call or opengl call, don't care.

Only solution I've found is the Cg toolkit generic_attrib example, which states ATTR0-5 as semantic parameters in shaders. It seems like this might be related to the value that's passed to glEnableVertexAttribArray, ie. 0/1 in my case, but that would limit the number of VBOs and also, my shaders won't compile with ATTR0 - compiler says ATTR (without the 0!) is unknown; I've also tried getting glGetProgramiv(cgGLGetProgramID(core.m_programs[1].second), GL_ACTIVE_ATTRIBUTES, &); which returned 0, glGetAttribLocation returns -1

I can interleave the data if needed, would prefer a multiple VBOs solution though.

I've been trying to make this work for 2 days now, but can't figure it out. Your help is very much appreciated!

Here's my OpenGL code

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 3 * 3 * sizeof(f32), vertices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glGenBuffers(1, &vbo_color);
glBindBuffer(GL_ARRAY_BUFFER, vbo_color);
glBufferData(GL_ARRAY_BUFFER, 3 * 3 * sizeof(f32), colors, GL_STATIC_DRAW);

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

vertex shader

struct vfconn
float4 position : POSITION;
float4 color : COLOR;

vfconn main(float4 position : POSITION, float4 color : COLOR, uniform float4x4 ModelViewProjection)
vfconn OUT;

OUT.position = mul(ModelViewProjection, position);
OUT.color = color;

return OUT;

fragment shader

struct Out
float4 color : COLOR;

Out main(float4 color : COLOR)
Out OUT;

OUT.color = float4(color.x, color.y, color.z,1);

return OUT;

PS: I hope you don't mind I chose Cg over GLSL; also, I know this might be more appropriate in a Cg forum, but most of the info I found was here, so I figured this is the friendlier place to be.

11-29-2009, 08:25 PM
Isn't it funny how you usually solve the problem just before or shortly after you ask?:) My thanks go to Mark Colbert from UCF for his slides; I really couldn't find the function mentioned anywhere and I kept overlooking it in the reference manual; I wish there were more examples!

The solution is (where vertices and colors are arrays of floats representing vertex positions / rgb:

cgGLEnableClientState(cgGetNamedParameter(programI d, "position"));
cgGLSetParameterPointer(cgGetNamedParameter(progra mId, "position"), 3, GL_FLOAT, 0, vertices);

cgGLEnableClientState(cgGetNamedParameter(programI d, "color"));
cgGLSetParameterPointer(cgGetNamedParameter(progra mId, "color"), 3, GL_FLOAT, 0, colors);

hope it helps!

EDIT: this replaces the whole OGL buffer creating thing, all is CG managed. Also, beware of cgGLxxxBuffer and cgxxxxBuffer family of functions, obviously concern some newer feature concerning uniform params (AFAIK)