Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Textures and glsl

  1. #1
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Textures and glsl

    Hello,
    I try to use a simple 2D texture with OpenGL 3.1 and by using a shader program. I tried to map on a sphere but it does not work. The Sphere is black.

    My steps were.

    1) To initialize the texture
    Code :
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     
    glTexImage2D(GL_TEXTURE_2D,
    	0,
    	GL_RGB,
    	128,
    	128,
    	0,
    	GL_RGB,
    	GL_UNSIGNED_BYTE,
    	pointer);

    2) Using a shader program with input attribute for the texels.
    Code :
    // vertex shader
    #version 140
     
    in vec3 MCVertex;
    in vec3 MCNormal;
    in vec2 TexCoord0;
    ...
     
    // fragment shader
    #version 140
     
    in float LightIntensity;
    in vec2 TexCoord;
     
    out vec4 FragColor;
     
    uniform sampler2D SampleTexture;
     
    void main()
    {
    vec3 lightColor = vec3(texture(SampleTexture, TexCoord));
    FragColor = vec4(lightColor * LightIntensity, 1.0);
    }

    3) before I start to render the geometry, I bound the texture
    Code :
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, _id);

    What I have forgotten? Or maybe the order of the commands are wrong?

    Thanks in advance!

  2. #2
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,718

    Re: Textures and glsl

    Have you tried texturing something simpler first, like a quad? That way, we can rule out whether it's the texture coordinate or the texture itself.

  3. #3
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: Textures and glsl

    Sorry no! But I could try. In OpenGL there are no Quads!

  4. #4
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,718

    Re: Textures and glsl

    In OpenGL there are no Quads!
    There are no spheres either, but you managed to render one of those, didn't you? Just make a mesh that's a quad instead of a sphere.

  5. #5
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: Textures and glsl

    So far I used simple boxes and generated texels.
    I used triangle strip to draw to draw each face of the box. (Quads are depricated.)

  6. #6
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: Textures and glsl

    Sorry... I did not say... the result is the same... it is black...

    The texel coordinates are 2D and in the interval [0,1]

  7. #7
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: Textures and glsl

    For example I used for the front face of the box the following vertices and texels:

    vertices: (-1,1,1),(-1,-1,1),(1,-1,1),(1,1,1)
    texels: (0,1), (0,0), (1,0), (1,1)

    The imagedata is rgb and the values are in the range [0,255].

  8. #8
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Textures and glsl

    There are two obvious reasons why the output may be black (I'm not saying this is the cause, however).

    // fragment shader
    #version 140

    in float LightIntensity;
    in vec2 TexCoord;

    out vec4 FragColor;

    uniform sampler2D SampleTexture;

    void main()
    {
    vec3 lightColor = vec3(texture(SampleTexture, TexCoord));
    FragColor = vec4(lightColor * LightIntensity, 1.0);
    First, remove the "LightIntensity" uniform from the shader and just output the texture colour.
    eg, FragColor = vec4(lightColor, 1.0);

    If that fails too, emit a Red colour eg vec4 (1.0);

    Secondly, what have you sent to GL as the uniform value to link the ActiveTexture Unit to the shader's uniform "SampleTexture"?
    In other words, have you set up the uniform parameter containing the value 0 and sent it to Opengl using glUniform1i (locationofUniform, 0)? How did you determine the location of the uniform "SampleTexture" - did you query for it, or set it explicitely?

  9. #9
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: Textures and glsl

    Hello,
    the LightIntensity is not the problem, for sure... I had not listed the complete code of the vertex shader, there is a LightIntensity as an output variable.

    So far... I assume there could be a configuration problem. To the question about "SampleTexture";
    I determined the location of the uniform variable "SampleTexture" and then I used "glUniform1iv(texLoc, 1, data)". The data is GLint and has one element.

    Ahh... How I have to handle the texels input into attribute "TexCoord0"? I did like with the vertices in a buffer object and with STATIC_DRAW.

  10. #10
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Textures and glsl

    LightIntensity is not the problem, for sure... I had not listed the complete code of the vertex shader, there is a LightIntensity as an output variable.
    I realize there is an output variable, but I wanted you to eliminate the possibility that it has a value other than 0.
    Perhaps emit vec4(LightIntensity) as FragData to test that part of the code.
    There is only two possible ways FragData is emitting black
    1. The lightIntensity value is 0
    2. The Texture is black, or not bound correctly to the shader.

    These are the reasons why you need to eliminate each possibility one at a time. If all fails emit a solid red colour to check you actually getting some valid output.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •