PDA

View Full Version : Texture mapping with GLSL



iris_raj
05-21-2007, 04:22 AM
Dear Opengl Programmers,

I wanted to texture a quad using Pixel shaders. I have attempted to write the source code which is given below. My problem is Texture is getting mapped only when I disable the shader.



void init
//This is my texture initialization code
glClearColor(0.0f,0.0f,0.0f,0.0f);
//Checkered textured image from opengl redbook
makeCheckImages();
glGenTextures( 1, &tex1D);
glBindTexture(GL_TEXTURE_2D,tex1D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Sometimes setting these params
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // is REQUIRED for FBOs!!
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,checkImageHeight,0,GL_RGBA, GL_UNSIGNED_BYTE,checkImage);

glEnable(GL_TEXTURE_2D);

//This is my Render loop
void renderScene(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0,1.0,0.0);
glTranslatef(0.0f,0.0f,-7.0f);
glBindTexture(GL_TEXTURE_2D,tex1D);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(-3.0f,-3.0f);
glTexCoord2f(0.0, 1.0);
glVertex2f(3.0f,-3.0f);
glTexCoord2f(1.0, 1.0);
glVertex2f(3.0,3.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(-3.0,3.0);
glEnd();
//a+=0.5;
glFlush();
glutSwapBuffers();
}
//In the nain
void main()
{
//Other app and shader initialization
//Passing the texture unit to shader
glUniform1i(getUniLoc(p, "tex1"), tex1D);

}
//my Vertex Program

varying vec2 texture_coordinate;

void main()
{



// gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

texture_coordinate = vec2(gl_MultiTexCoord0);

//gl_Position = ftransform();
}

//my fragment programm
uniform sampler2D tex1;
varying vec2 texture_coordinate;

void main()
{
gl_FragColor = texture2D(tex1, texture_coordinate);

}

I am new to GLSL.
Please help
RAJESH.R

Mazy
05-21-2007, 04:29 AM
change glUniform1i(getUniLoc(p, "tex1"), tex1D);
to glUniform1i(getUniLoc(p, "tex1"), 0);

You are supposed to bind the texture unit number, not the texture number to the sampler

iris_raj
05-21-2007, 05:08 AM
Thanks a lot. It was a great opening for me.
Mazy, I have another doubt, why should we use
glActiveTexture(GLenum texture). In some of texture mapping examples,this being frequently used.

Thanks a lot
RAJESH.R

Mazy
05-21-2007, 06:47 AM
glActiveTexture(GLenum texture) is used to change the current active texture unit.

if you want to bind more than one texture in your GLSL program you need to put the textures on different units.

in the shader

uniform sampler2D tex1;
uniform sampler2D tex2;


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texturenum0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,texturenum1);

// and bind to the shader.

glUniform1i(getUniLoc(p, "tex1"), 0);
glUniform1i(getUniLoc(p, "tex2"), 1);

there we bind the sampler to the textureunit, and bind the textureObject to the unit as well.