Hi,
I've just started to learn Cg and I am using the book The Cg Tutorial. In this book, the last example of Chapter 3 shows a Fragment program in which a texture is sampled on a simple Triangle object. I am trying to write an OpenGL application and bind this Fragment program to it. The problem is that my texture is not properly sampled on the Triangle object: I only see one color of my texture on the Triangle object instead of a part of the texture. The pictures below show what I mean. Left is the texture I want to sample and right is the result.
There must be something wrong with retrieving the texture coordinates I suppose.
Below are some snippets of my code. I hope someone can tell me what I am doing wrong.
Thanks in advance!
(Fragment program)struct fragment_Output {
float4 color : COLOR;
};
fragment_Output fTexture(float2 texCoord : TEXCOORD0,
uniform sampler2D decal)
{
fragment_Output OUT;
OUT.color = tex2D(decal, texCoord);
return OUT;
}
(Setting the viewport)
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,3.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
(Setting up Cg and loading texture)
context = cgCreateContext();
fProgram = cgCreateProgramFromFile(context, CG_SOURCE, "texture.cg",
CG_PROFILE_ARBFP1, "fTexture", NULL);
//cgGLLoadProgram(vProgram);
cgGLLoadProgram(fProgram);
// Create The Texture
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
// Set texture
textureParameter = cgGetNamedParameter(fProgram, "decal");
cgGLSetTextureParameter(textureParameter, texture[0]);
(Drawing Triangle Object)
glClear(GL_COLOR_BUFFER_BIT);
if (fProgram) {
cgGLEnableProfile(CG_PROFILE_ARBFP1);
cgGLBindProgram(fProgram);
cgGLEnableTextureParameter(textureParameter);
}
glBegin(GL_TRIANGLES);
glVertex2f(-0.8, 0.8);
glVertex2f(0.8, 0.8);
glVertex2f(0.0, -0.8);
glEnd();
glFlush();
cgGLDisableTextureParameter(textureParameter);
cgGLDisableProfile(CG_PROFILE_ARBVP1);
cgGLDisableProfile(CG_PROFILE_ARBFP1);