Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Cg Texture sampling problem

This topic is 4666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
(Setting up Cg and loading texture)
context = cgCreateContext();
fProgram = cgCreateProgramFromFile(context, CG_SOURCE, "texture.cg", 
                		CG_PROFILE_ARBFP1, "fTexture", NULL);

// 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);

// Set texture	
textureParameter = cgGetNamedParameter(fProgram, "decal");
cgGLSetTextureParameter(textureParameter, texture[0]);

(Drawing Triangle Object)

if (fProgram) {
  glVertex2f(-0.8, 0.8);
  glVertex2f(0.8, 0.8);
  glVertex2f(0.0, -0.8);


Share this post

Link to post
Share on other sites
It looks like you aren't supplying UV coordinates to map the texture to the triangle. You need to put some glTexCoord2f() calls before each glVertex2f().

Also, I've not used cgGLSetTextureParameter before but I don't think you need to. If you use the TEXUNIT0 semantic on decal, your textures bound in OpenGL will pass directly in to the shader.

Share this post

Link to post
Share on other sites
aah, UV coordinates were indeed the solution. I had a wrong understanding of what the Fragment program did. I thought the mapping took place inside the Fragment program but now that I look at it again I see that the coordinates of the texture are passed. At first I thought the pixel coordinates of the Triangle were passed. But now it's clear to me, thanks for the help!!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!