Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: glsl Texture always shows a white colour triangle

  1. #1
    Newbie Newbie
    Join Date
    Jun 2017
    Posts
    1

    glsl Texture always shows a white colour triangle

    I'm trying to work on a texture mapping 2D to a triangle. But currently, I can only get a triangle that has a gradient colour without any texture on it. Which means, my texture function in glsl always return vec4(1,1,1,1) and my textCoords is working. How should I fix it? Any suggestions would be helpful to try!
    Here is also the same question I posted on stack overflow:https://stackoverflow.com/questions/...white-triangle

    By the way, have been working on this for 4 days.

    in texture class:
    constructor:
    (really new to here, might not have a good writing)
    Code :
    Texture::Texture(const std::string& fileName){
     
       int width, height, numComponents;
       //float* imageData = stbi_loadf(fileName.c_str(), &width, &height, &numComponents, 4);
       unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
       if(imageData == NULL){
          cerr << "Texture loading failed for "<<fileName<< endl;
       }
     
       glGenTextures(1, &m_texture);
       glBindTexture(GL_TEXTURE_2D, m_texture);
     
       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_MIN_FILTER, GL_LINEAR);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
       glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
     
     
       stbi_image_free(imageData);
     }

    and Binding function:
    Code :
    void Texture::Bind(){
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, m_texture);
    }



    in my main, it is used to create a shader, and connect shader to frag and vertex shader file. Then load all data to shader.
    Code :
    m_shader.generateProgramObject();
    m_shader.attachVertexShader( getAssetFilePath("VertexShader.vs").c_str() );
    m_shader.attachFragmentShader( getAssetFilePath("FragmentShader.fs").c_str()   );
    m_shader.link();
     
    // texture created here
    texture = Texture("Assets/bricks.jpg");
     
     
    // enable vertex attribute indices
    glGenVertexArrays(1, &m_vao_triangle);
    glBindVertexArray(m_vao_triangle);
     
    // Enable the attribute index location for "position" when rendering.
    GLint positionAttribLocation = m_shader.getAttribLocation( "position" );
    glEnableVertexAttribArray(positionAttribLocation);
     
    GLint textCoordLocation = m_shader.getAttribLocation( "atextCoord" );
    glEnableVertexAttribArray(textCoordLocation);
     
     
    // Restore defaults
    glBindVertexArray(0);
     
     
    CHECK_GL_ERRORS;
     
     
     
    ...
     
     
     
    //uploading vertex data to buffer
     
    vec3 triangleVertices[] = {
        // Construct equalaterial triangle
        vec3(0.0f, 0.0f, 0.0f),
        vec3(0.25f, 1.0f, 0.0),
        vec3(0.5f, 0.0f, 0.0f)
    };
     
    vec2 textCoords[] = {
             vec2(1.0f, 0.0f), 
             vec2(0.25f, 1.0f), 
             vec2(0.5f, 0.0f)};
     
     
     
    // Generate a vertex buffer object to hold the triangle's vertex data.
    glGenBuffers(1, &m_vbo_triangle);
     
    //-- Upload triangle vertex data to the vertex buffer:
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo_triangle);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices,
            GL_STATIC_DRAW);
     
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    CHECK_GL_ERRORS;
     
     
     
     
    //====generate buffer for holding texture coordinates====
    glGenBuffers(1, &m_uv_triangle);
    glBindBuffer(GL_ARRAY_BUFFER, m_uv_triangle);
    glBufferData(GL_ARRAY_BUFFER, sizeof(textCoords), textCoords,
                 GL_STATIC_DRAW);
     
    // Unbind the target GL_ARRAY_BUFFER, now that we are finished using it.
    glBindBuffer(GL_ARRAY_BUFFER, 0);
     
    CHECK_GL_ERRORS;
     
     
     
     
     
    ...
     
     
     
     
     
    //Map buffer data to shader
     
    glBindVertexArray(m_vao_triangle);
     
     glBindBuffer(GL_ARRAY_BUFFER, m_vbo_triangle);
     GLint positionAttribLocation = m_shader.getAttribLocation( "position" );
     glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
     
     
     
     glBindBuffer(GL_ARRAY_BUFFER, m_uv_triangle);
     GLint textCoordLocation = m_shader.getAttribLocation( "atextCoord" );
     glVertexAttribPointer(textCoordLocation,2, GL_FLOAT, GL_FALSE, 0, nullptr);
     
     
     //-- Unbind target, and restore default values:
     glBindBuffer(GL_ARRAY_BUFFER, 0);
     glBindVertexArray(0);
     
     CHECK_GL_ERRORS;
     
     
     
     
     
     
    ...
     
     
     
     
     
    //UPLOAD UNIFORM TO SHADER
       m_shader.enable();
     
        ...
     
        GLint uniformLocation_diffuse = m_shader.getUniformLocation("diffuse");
        glUniform1i(uniformLocation_diffuse, 0);
        CHECK_GL_ERRORS;
     
        m_shader.disable();
     
        CHECK_GL_ERRORS;
     
     
     
     
     
    ...
     
     
     
     
     
     
     
    //And draw function
     
    glBindVertexArray(m_vao_triangle);
    //   below I tried, but didn't work
    //   glClear(GL_STENCIL_BUFFER_BIT);
    //   glEnable(GL_BLEND);
    //   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //   glEnable(GL_DEPTH_TEST);
     
    texture.Bind();
    //   do these below: 
    //   glActiveTexture(GL_TEXTURE0);
    //   glBindTexture(GL_TEXTURE_2D, texture.m_texture);
     
    m_shader.enable();
     
    glDrawArrays(GL_TRIANGLES, 0, 3);
     
     
    m_shader.disable();
     
     
    // Restore defaults
    glBindVertexArray(0);
     
    CHECK_GL_ERRORS;


    Here I will also attach my shaders vertex shader:


    Code :
    #version 330
     
    in vec3 position;
    in vec2 atextCoord;
     
    uniform mat4 transform;
    out vec2 textCoord;
     
    void main() {
       gl_Position = transform * vec4(position, 1.0);
       textCoord = atextCoord;
    }


    And my fragment shader:

    Code :
    #version 330
     
    uniform sampler2D diffuse;
     
    out vec4 fragColor;
    in vec2 textCoord;
     
    void main() {
     
       fragColor = vec4(texture(diffuse, textCoord).rgb,1.0) *    vec4(textCoord,0.0,1.0); 
       // texture(...) shows vec4(1,1,1,1)
       // radiant colour can only prove my textCoord is working
       // fragColor = texture(diffuse, textCoord);   <- only shows a default texture
    }


    Here is my running result:
    Click image for larger version. 

Name:	Screen Shot 2017-06-30 at 3.22.15 PM.jpg 
Views:	23 
Size:	4.4 KB 
ID:	2404


    Here is my texture:
    Click image for larger version. 

Name:	bricks.jpg 
Views:	20 
Size:	70.1 KB 
ID:	2405
    Last edited by lalaphoon; 06-30-2017 at 02:02 PM. Reason: Adding an image to support

Posting Permissions

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