Ok, i'm just starting out trying to learn Cg. Here is the shader i wrote:
struct vShader1Out {
float4 position : POSITION;
float4 color : COLOR;
};
vShader1Out vShader1(float2 position : POSITION)
{
vShader1Out OUT;
OUT.position = float4(position, 0, 1);
OUT.color = float4(1, 0, 0, 1);
return OUT;
}
This is basically the same as the first shader from the Cg tutorial book. I draw a square in openGL, and then pass the position paramater to the shader. I expected only the color of the square to be changed to red, but when i run it the square is a different size and in a different place. does anyone know whats causing this?
Here is the openGL code i wrote:
#include <GL/glut.h>
#include <GL/gl.h>
#include <Cg/cg.h>
#include <Cg/cgGL.h>
CGcontext cgContext;
CGprogram vShader;
CGparameter positionParam;
CGprofile vProfile = CG_PROFILE_VP20;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex2f(0.25, 0.25);
glVertex2f(0.75, 0.25);
glVertex2f(0.75, 0.75);
glVertex2f(0.25, 0.75);
glEnd();
glFlush();
}
void init(void)
{
// initialise GL
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
// initialise Cg
cgContext = cgCreateContext();
vShader = cgCreateProgramFromFile(cgContext, CG_SOURCE, "vShader1.cg", vProfile, "vShader1", NULL);
cgGLLoadProgram(vShader); // load vertex shader
positionParam = cgGetNamedParameter(vShader, "position");
cgGLEnableProfile(vProfile);
cgGLBindProgram(vShader);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("My Cg Shader");
init();
glutDisplayFunc(display);
glutMainLoop();
// Release shaders once main drawing loop has stopped running
cgDestroyProgram(vShader);
cgDestroyContext(cgContext);
return 0;
}