OpenGL - Simple 2D Texture Not Being Displayed











up vote
1
down vote

favorite












I have written a very simple OpenGL program using glfw and glew. It compiles fine and runs, but the simple texture I am passing to the fragment shader is not being displayed.



The only geometry is a single quad.



I am only trying to display a single solid color, the first element of the texture. I know that the fragment shader works at all because I can display a solid color without the texture:



#version 320 es
mediump out vec4 color;
precision mediump float;
precision mediump sampler2D;

in vec2 texCoord;
uniform sampler2D tex;

void main() {
color = vec4(0.5, 0.75, 0.5, 1.0);
}


Simple fragment shader, solid color works



However, when I use the texture sampler I get nothing, like the sampler is sampling only 0s (changing only main()):



void main() {
color = vec4(texture(tex, vec2(0, 0)).rgb, 1);
}


enter image description here



I am populating and generating the texture in what I believe is the correct manner (TEX_SIZE is defined as #define TEX_SIZE (32*32)):



//Texture Data
GLfloat texData[TEX_SIZE * 3] = {1.0};

//Create texture
GLuint tex;
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGB, GL_FLOAT, texData);
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_WRAP_R, GL_REPEAT);


And here is the entire draw loop:



//Render loop
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
&& !glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(programID);

//Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(programID, "tex"), 0);

//Draw the quad
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, gIndices);
glDisableClientState(GL_VERTEX_ARRAY);

if (glGetError()) {
puts((char *)glewGetErrorString(glGetError()));
}

glfwSwapBuffers(window);
glfwPollEvents();
}


Why is this happening? I'm totally stuck.



EDIT: Changed the array initialization, as per suggestion:



//Texture Data
GLfloat texData[TEX_SIZE * 3];
for (int i = 0; i < TEX_SIZE * 3; i++) {
texData[i] = 1.0;
}


There was no change. Output is still all black.










share|improve this question




















  • 1




    Do you bind the texture-unit 0 to tex in your shader?
    – tkausl
    16 hours ago










  • @tkausl My whole frag shader is pasted above, so I guess not? How do I do that?
    – bpmw
    16 hours ago






  • 1




    Your shader loading code is not, however. You do it the same way you'd set other uniforms, just set a int (0 in this case) to the uniform tex. Your error-handling is broken by the way, it ignores the first error it sees.
    – tkausl
    16 hours ago












  • @tkausl here is my loading code. What function do I need to use to bind my uniform to 0?
    – bpmw
    16 hours ago






  • 1




    @tkausl But am I not already doing that in the render loop with glUniform1i(glGetUniformLocation(programID, "tex"), 0);?
    – bpmw
    16 hours ago















up vote
1
down vote

favorite












I have written a very simple OpenGL program using glfw and glew. It compiles fine and runs, but the simple texture I am passing to the fragment shader is not being displayed.



The only geometry is a single quad.



I am only trying to display a single solid color, the first element of the texture. I know that the fragment shader works at all because I can display a solid color without the texture:



#version 320 es
mediump out vec4 color;
precision mediump float;
precision mediump sampler2D;

in vec2 texCoord;
uniform sampler2D tex;

void main() {
color = vec4(0.5, 0.75, 0.5, 1.0);
}


Simple fragment shader, solid color works



However, when I use the texture sampler I get nothing, like the sampler is sampling only 0s (changing only main()):



void main() {
color = vec4(texture(tex, vec2(0, 0)).rgb, 1);
}


enter image description here



I am populating and generating the texture in what I believe is the correct manner (TEX_SIZE is defined as #define TEX_SIZE (32*32)):



//Texture Data
GLfloat texData[TEX_SIZE * 3] = {1.0};

//Create texture
GLuint tex;
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGB, GL_FLOAT, texData);
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_WRAP_R, GL_REPEAT);


And here is the entire draw loop:



//Render loop
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
&& !glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(programID);

//Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(programID, "tex"), 0);

//Draw the quad
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, gIndices);
glDisableClientState(GL_VERTEX_ARRAY);

if (glGetError()) {
puts((char *)glewGetErrorString(glGetError()));
}

glfwSwapBuffers(window);
glfwPollEvents();
}


Why is this happening? I'm totally stuck.



EDIT: Changed the array initialization, as per suggestion:



//Texture Data
GLfloat texData[TEX_SIZE * 3];
for (int i = 0; i < TEX_SIZE * 3; i++) {
texData[i] = 1.0;
}


There was no change. Output is still all black.










share|improve this question




















  • 1




    Do you bind the texture-unit 0 to tex in your shader?
    – tkausl
    16 hours ago










  • @tkausl My whole frag shader is pasted above, so I guess not? How do I do that?
    – bpmw
    16 hours ago






  • 1




    Your shader loading code is not, however. You do it the same way you'd set other uniforms, just set a int (0 in this case) to the uniform tex. Your error-handling is broken by the way, it ignores the first error it sees.
    – tkausl
    16 hours ago












  • @tkausl here is my loading code. What function do I need to use to bind my uniform to 0?
    – bpmw
    16 hours ago






  • 1




    @tkausl But am I not already doing that in the render loop with glUniform1i(glGetUniformLocation(programID, "tex"), 0);?
    – bpmw
    16 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have written a very simple OpenGL program using glfw and glew. It compiles fine and runs, but the simple texture I am passing to the fragment shader is not being displayed.



The only geometry is a single quad.



I am only trying to display a single solid color, the first element of the texture. I know that the fragment shader works at all because I can display a solid color without the texture:



#version 320 es
mediump out vec4 color;
precision mediump float;
precision mediump sampler2D;

in vec2 texCoord;
uniform sampler2D tex;

void main() {
color = vec4(0.5, 0.75, 0.5, 1.0);
}


Simple fragment shader, solid color works



However, when I use the texture sampler I get nothing, like the sampler is sampling only 0s (changing only main()):



void main() {
color = vec4(texture(tex, vec2(0, 0)).rgb, 1);
}


enter image description here



I am populating and generating the texture in what I believe is the correct manner (TEX_SIZE is defined as #define TEX_SIZE (32*32)):



//Texture Data
GLfloat texData[TEX_SIZE * 3] = {1.0};

//Create texture
GLuint tex;
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGB, GL_FLOAT, texData);
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_WRAP_R, GL_REPEAT);


And here is the entire draw loop:



//Render loop
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
&& !glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(programID);

//Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(programID, "tex"), 0);

//Draw the quad
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, gIndices);
glDisableClientState(GL_VERTEX_ARRAY);

if (glGetError()) {
puts((char *)glewGetErrorString(glGetError()));
}

glfwSwapBuffers(window);
glfwPollEvents();
}


Why is this happening? I'm totally stuck.



EDIT: Changed the array initialization, as per suggestion:



//Texture Data
GLfloat texData[TEX_SIZE * 3];
for (int i = 0; i < TEX_SIZE * 3; i++) {
texData[i] = 1.0;
}


There was no change. Output is still all black.










share|improve this question















I have written a very simple OpenGL program using glfw and glew. It compiles fine and runs, but the simple texture I am passing to the fragment shader is not being displayed.



The only geometry is a single quad.



I am only trying to display a single solid color, the first element of the texture. I know that the fragment shader works at all because I can display a solid color without the texture:



#version 320 es
mediump out vec4 color;
precision mediump float;
precision mediump sampler2D;

in vec2 texCoord;
uniform sampler2D tex;

void main() {
color = vec4(0.5, 0.75, 0.5, 1.0);
}


Simple fragment shader, solid color works



However, when I use the texture sampler I get nothing, like the sampler is sampling only 0s (changing only main()):



void main() {
color = vec4(texture(tex, vec2(0, 0)).rgb, 1);
}


enter image description here



I am populating and generating the texture in what I believe is the correct manner (TEX_SIZE is defined as #define TEX_SIZE (32*32)):



//Texture Data
GLfloat texData[TEX_SIZE * 3] = {1.0};

//Create texture
GLuint tex;
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGB, GL_FLOAT, texData);
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_WRAP_R, GL_REPEAT);


And here is the entire draw loop:



//Render loop
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
&& !glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(programID);

//Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(programID, "tex"), 0);

//Draw the quad
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, gIndices);
glDisableClientState(GL_VERTEX_ARRAY);

if (glGetError()) {
puts((char *)glewGetErrorString(glGetError()));
}

glfwSwapBuffers(window);
glfwPollEvents();
}


Why is this happening? I'm totally stuck.



EDIT: Changed the array initialization, as per suggestion:



//Texture Data
GLfloat texData[TEX_SIZE * 3];
for (int i = 0; i < TEX_SIZE * 3; i++) {
texData[i] = 1.0;
}


There was no change. Output is still all black.







c++ opengl-es textures






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 16 hours ago

























asked 16 hours ago









bpmw

789




789








  • 1




    Do you bind the texture-unit 0 to tex in your shader?
    – tkausl
    16 hours ago










  • @tkausl My whole frag shader is pasted above, so I guess not? How do I do that?
    – bpmw
    16 hours ago






  • 1




    Your shader loading code is not, however. You do it the same way you'd set other uniforms, just set a int (0 in this case) to the uniform tex. Your error-handling is broken by the way, it ignores the first error it sees.
    – tkausl
    16 hours ago












  • @tkausl here is my loading code. What function do I need to use to bind my uniform to 0?
    – bpmw
    16 hours ago






  • 1




    @tkausl But am I not already doing that in the render loop with glUniform1i(glGetUniformLocation(programID, "tex"), 0);?
    – bpmw
    16 hours ago














  • 1




    Do you bind the texture-unit 0 to tex in your shader?
    – tkausl
    16 hours ago










  • @tkausl My whole frag shader is pasted above, so I guess not? How do I do that?
    – bpmw
    16 hours ago






  • 1




    Your shader loading code is not, however. You do it the same way you'd set other uniforms, just set a int (0 in this case) to the uniform tex. Your error-handling is broken by the way, it ignores the first error it sees.
    – tkausl
    16 hours ago












  • @tkausl here is my loading code. What function do I need to use to bind my uniform to 0?
    – bpmw
    16 hours ago






  • 1




    @tkausl But am I not already doing that in the render loop with glUniform1i(glGetUniformLocation(programID, "tex"), 0);?
    – bpmw
    16 hours ago








1




1




Do you bind the texture-unit 0 to tex in your shader?
– tkausl
16 hours ago




Do you bind the texture-unit 0 to tex in your shader?
– tkausl
16 hours ago












@tkausl My whole frag shader is pasted above, so I guess not? How do I do that?
– bpmw
16 hours ago




@tkausl My whole frag shader is pasted above, so I guess not? How do I do that?
– bpmw
16 hours ago




1




1




Your shader loading code is not, however. You do it the same way you'd set other uniforms, just set a int (0 in this case) to the uniform tex. Your error-handling is broken by the way, it ignores the first error it sees.
– tkausl
16 hours ago






Your shader loading code is not, however. You do it the same way you'd set other uniforms, just set a int (0 in this case) to the uniform tex. Your error-handling is broken by the way, it ignores the first error it sees.
– tkausl
16 hours ago














@tkausl here is my loading code. What function do I need to use to bind my uniform to 0?
– bpmw
16 hours ago




@tkausl here is my loading code. What function do I need to use to bind my uniform to 0?
– bpmw
16 hours ago




1




1




@tkausl But am I not already doing that in the render loop with glUniform1i(glGetUniformLocation(programID, "tex"), 0);?
– bpmw
16 hours ago




@tkausl But am I not already doing that in the render loop with glUniform1i(glGetUniformLocation(programID, "tex"), 0);?
– bpmw
16 hours ago












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The initial value of GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR. But you don't generate mipmaps. This causes that the texture is not complete.



OpenGL ES 3.2 Specification; 8.17 Texture Completeness; page 205




A texture is said to be complete if all the texture images and texture parameters
required to utilize the texture for texture application are consistently defined.



... a texture is complete unless any of the following
conditions hold true:




  • The minification filter requires a mipmap (is neither NEAREST nor LINEAR),
    and the texture is not mipmap complete.




Change the texture minification filter (glTexParameteri) to solve your issue:



e.g.



glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343472%2fopengl-simple-2d-texture-not-being-displayed%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    The initial value of GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR. But you don't generate mipmaps. This causes that the texture is not complete.



    OpenGL ES 3.2 Specification; 8.17 Texture Completeness; page 205




    A texture is said to be complete if all the texture images and texture parameters
    required to utilize the texture for texture application are consistently defined.



    ... a texture is complete unless any of the following
    conditions hold true:




    • The minification filter requires a mipmap (is neither NEAREST nor LINEAR),
      and the texture is not mipmap complete.




    Change the texture minification filter (glTexParameteri) to solve your issue:



    e.g.



    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      The initial value of GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR. But you don't generate mipmaps. This causes that the texture is not complete.



      OpenGL ES 3.2 Specification; 8.17 Texture Completeness; page 205




      A texture is said to be complete if all the texture images and texture parameters
      required to utilize the texture for texture application are consistently defined.



      ... a texture is complete unless any of the following
      conditions hold true:




      • The minification filter requires a mipmap (is neither NEAREST nor LINEAR),
        and the texture is not mipmap complete.




      Change the texture minification filter (glTexParameteri) to solve your issue:



      e.g.



      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        The initial value of GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR. But you don't generate mipmaps. This causes that the texture is not complete.



        OpenGL ES 3.2 Specification; 8.17 Texture Completeness; page 205




        A texture is said to be complete if all the texture images and texture parameters
        required to utilize the texture for texture application are consistently defined.



        ... a texture is complete unless any of the following
        conditions hold true:




        • The minification filter requires a mipmap (is neither NEAREST nor LINEAR),
          and the texture is not mipmap complete.




        Change the texture minification filter (glTexParameteri) to solve your issue:



        e.g.



        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);





        share|improve this answer












        The initial value of GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR. But you don't generate mipmaps. This causes that the texture is not complete.



        OpenGL ES 3.2 Specification; 8.17 Texture Completeness; page 205




        A texture is said to be complete if all the texture images and texture parameters
        required to utilize the texture for texture application are consistently defined.



        ... a texture is complete unless any of the following
        conditions hold true:




        • The minification filter requires a mipmap (is neither NEAREST nor LINEAR),
          and the texture is not mipmap complete.




        Change the texture minification filter (glTexParameteri) to solve your issue:



        e.g.



        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 hours ago









        Rabbid76

        29.8k112742




        29.8k112742






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343472%2fopengl-simple-2d-texture-not-being-displayed%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]