Basic CG Information

Hello,

Ive recently been asked to add shader functionality to a small project, and i must admit im a bit lost when it comes to shaders. Currently i have 2 functions for rendering:

renderWithShaders()
renderWithoutShaders()

the core render() call works out if shaders are being used and then uses the correct function…

This is where my problem comes in, ive got a basic cube that i use for testing everything but when i use CG shaders it doesn’t display, although im not sure if its down to the matrix, the position or me not writing it correctly…

Im just testing what i thought was a basic shader, and it wont work, where i send over position/color and want to just display it on the screen, however it wont…

Here is a quick code example of what im doing in both methods…

  • Without Shaders -

gl.glPushMatrix();
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glTranslatef(vPosition.fX, vPosition.fY, vPosition.fZ);

        gl.glBegin(gl.GL_QUADS);

            gl.glColor3f(0.0f,1.0f,0.0f);                                   
            gl.glVertex3f(vVerts[0].fX, vVerts[0].fY, vVerts[0].fZ);    // Top Right Of The Quad (Top)
            gl.glVertex3f(vVerts[1].fX, vVerts[1].fY, vVerts[1].fZ);    // Top Left Of The Quad (Top)
            gl.glVertex3f(vVerts[2].fX, vVerts[2].fY, vVerts[2].fZ);    // Bottom Left Of The Quad (Top)
            gl.glVertex3f(vVerts[3].fX, vVerts[3].fY, vVerts[3].fZ);    // Bottom Right Of The Quad (Top)

  gl.glEnd();
        gl.glDisable(GL.GL_DEPTH_TEST);
        gl.glPopMatrix();

  • With Shaders -

CgGL.cgGLEnableProfile(jlRenderer.getVertexShaderProfile());
        jlRenderer.CheckCgError();
        
        CgGL.cgGLBindProgram(cgpVertexProgram);
        jlRenderer.CheckCgError();
               
        if(ModelViewProjParam != null)
        {
            CgGL.cgGLSetStateMatrixParameter(ModelViewProjParam, 
            CgGL.CG_GL_MODELVIEW_PROJECTION_MATRIX, CgGL.CG_GL_MATRIX_IDENTITY);
            jlRenderer.CheckCgError();
        }

gl.glPushMatrix();

        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glTranslatef(vPosition.fX, vPosition.fY, vPosition.fZ);
        gl.glBegin(GL.GL_QUADS);
           
            CgGL.cgGLSetParameter3f(VertColor, 0.0f,1.0f,0.0f);                                   
            gl.glVertex3f(vVerts[0].fX, vVerts[0].fY, vVerts[0].fZ);    // Top Right Of The Quad (Top)
            gl.glVertex3f(vVerts[1].fX, vVerts[1].fY, vVerts[1].fZ);    // Top Left Of The Quad (Top)
            gl.glVertex3f(vVerts[2].fX, vVerts[2].fY, vVerts[2].fZ);    // Bottom Left Of The Quad (Top)
            gl.glVertex3f(vVerts[3].fX, vVerts[3].fY, vVerts[3].fZ);    // Bottom Right Of The Quad (Top)

gl.glEnd();
        gl.glDisable(GL.GL_DEPTH_TEST);
        gl.glPopMatrix();
        
        CgGL.cgGLDisableProfile(jlRenderer.getVertexShaderProfile());
        jlRenderer.CheckCgError();

  • Shader File -

// input vertex
struct VertIn {
    float4 position : POSITION;
    float4 color : COLOR0;
};

// output vertex
struct VertOut {
    float4 position   : POSITION;
    float4 color : COLOR0;
};

// vertex shader main entry
VertOut main(VertIn IN, uniform float4x4 modelViewProj) 
{
    VertOut OUT;
    OUT.position = mul(modelViewProj,IN.position);
    OUT.color    = IN.color;
    return OUT;
}

Any help would be great, im a tad confused as it if CG gets the position var from the glVertex3f() call or if i have to manually pass over the parameter like done with the colour. I just want a shader for now that emulates the normal renderWithoutShader(), so it basically renders the data via CG and not OGL, i dont want any fancy effects for now…

Thanks for any help,
Grofit

Do you have to use Cg or can you use GLSL?

Would rather use CG as its not limited to just being in OpenGL, whereas to my knowledge GLSL is only for OpenGL… I guess i could use HLSL although thats just a rebranded CG anyway…

Healthy /Bump as there are pretty much no other forums i can ask these questions on as they are all ad sites now :frowning:

If you’re using opengl for the rest of the project, why do is it matter if Cg can be used other places? Especially since you don’t seem to be getting the Cg to work. I don’t know much about Cg, but it was created by Nvidia, so does that make it less likely that an ati card would support it.

Cg is supported for multiple grafics card vendors. The CG runtime has different compile profiles (it can e.g. compile to ARB shader assembly) and chooses the best depending on the underlying hardware.

Download the official jogl demo sources (if you haven’t already) and look into the cg/runtime_ogl_vertex_fragment example included there to get working code to start from.

Thats EXACTLY what i did to start, but for some reason it didnt work, so i went round the net trying to find more information on it and just couldnt…

Isnt CG being the main Shader Language on the PS3? i would have expected ALOT more free documentation on it than there is… I will make sure my current demos are up to date and have another play around, although i cant see me doing anything wrong when im using the shader as its all been initilized correctly with the right profiles…

edit

seems the version i have is up to date, a couple of differences are that im not using lighting or normals in mine at the moment, however in your cgGL_vertex_example you guys are. Although if there were no normals or lighting i would expect the cube to just appear black rather than vanishing.

When working in 3d space with shaders if you use the vertex3f(…) do you need to make sure in the shader you pass the proj and modelview matrixes over and then do the mul(proj,…) mul(modelv,…) or do you just need the projection matrix? as shown in the example?