Results 1 to 4 of 4

Thread: Texture mapping with GLSL

  1. #1
    Senior Member Regular Contributor
    Join Date
    Jul 2003
    Location
    Bangalore, Karnataka, India
    Posts
    123

    Texture mapping with GLSL

    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.

    Code :
    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

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Oct 2001
    Posts
    595

    Re: Texture mapping with GLSL

    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

  3. #3
    Senior Member Regular Contributor
    Join Date
    Jul 2003
    Location
    Bangalore, Karnataka, India
    Posts
    123

    Re: Texture mapping with GLSL

    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

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Oct 2001
    Posts
    595

    Re: Texture mapping with GLSL

    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.

Similar Threads

  1. opengl bump mapping (normal mapping) problem with VBO and GLSL
    By emrekaya413 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-29-2018, 03:52 AM
  2. trying to do texture mapping in GLSL 150
    By noobscratcher in forum OpenGL: GLSL
    Replies: 4
    Last Post: 03-25-2012, 03:33 PM
  3. GLSL UVW mapping
    By Itun.itu in forum OpenGL: GLSL
    Replies: 2
    Last Post: 05-03-2011, 01:07 PM
  4. Shadow mapping in GLSL
    By dogdemir in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 02-20-2009, 09:13 AM
  5. GLSL shadow mapping & depth buffer copy into a texture
    By dletozeun in forum OpenGL: GLSL
    Replies: 3
    Last Post: 01-18-2007, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean