1
\$\begingroup\$

her is my vertex array and texture coordinate..

float POS[]=
{
    -0.5,-0.5,-1.0,
    0.5,-0.5,-1.0,
    0.5,0.5,-1.0,
    -0.5,0.5,-1.0
};

float texCoords[] = {
0.0,0.0,
1.0,0.0,
1.0,1.0,
0.0,1.0
};

here is my texture creation

stbi_set_flip_vertically_on_load(1);
    openglTexBuff=stbi_load("lena.jpg",&w,&h,&c,0);


    glGenTextures(1,&TexID);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,TexID);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,w,h,GL_FALSE,GL_RGB,GL_UNSIGNED_BYTE,openglTexBuff);
    glGenerateMipmap(GL_TEXTURE_2D);
    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_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);


    glBindTexture(GL_TEXTURE_2D,0);
    stbi_image_free(openglTexBuff);'

and these are my shaders

#shader vertex
#version 330 core 

layout(location = 0) in vec4 positions;
layout(location = 1) in vec2 texCoords;
uniform mat4 translation_matrix; //inactive shader variables..
uniform mat4 rotation_matrix;

mat4 model_matrix; //
mat4 projection_matrix;
out vec4 pos;
out vec2 frag_TexCoord;

void main()
{

rotation_matrix;

gl_Position=rotation_matrix*translation_matrix*positions; //translation matrix operated with POS vector..
pos=positions;
frag_TexCoord=texCoords;
translation_matrix;

}



#shader fragment  
#version 330 core

layout(location=0) out vec4 color;
in vec4 pos;
in vec2 frag_TexCoord;rag_TexCoord;
uniform float col;
uniform sampler2D  a_texture;
void main()
{
col;
a_texture;

color=texture(a_texture,frag_TexCoord);

}

when i use (solid)color instead of texture in shader..everything works fine but when i use shader it gives solid color instead of texture..It seems like my whole tex is super-zoomed to output single color

here is draw call

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    box.shader.setUniformat4x4("translation_matrix",translation);
    //box.shader.setUniformat4x4("rotation_matrix",rotation_arr);
    box.shader.setUniform1f("col",1.0);
    glBindTexture(GL_TEXTURE_2D,TexID);
    box.shader.setUniform1i("a_texture",0);
    box.render();

box.render

void render()
    {
        vertices.bind();
        indices.bind();
        shader.bind();
        glDrawElements(render_As,vertices.getSize(),GL_UNSIGNED_INT,NULL);
        vertices.unbind();
        indices.unbind();

    }
\$\endgroup\$
7
  • \$\begingroup\$ Your title looks like you are trying to render a single colour instead of a texture, would be better to rephrase it and make it an actual question, so you can reach more people to help you. \$\endgroup\$
    – TomTsagk
    Aug 29, 2018 at 10:23
  • \$\begingroup\$ Could you suggest one?? \$\endgroup\$ Aug 29, 2018 at 10:24
  • \$\begingroup\$ Your new title seems better, remember to keep it simple, something like "Why does my opengl texture render as a single colour?" or "Why is my opengl texture not being rendered?" or something like that. \$\endgroup\$
    – TomTsagk
    Aug 29, 2018 at 10:26
  • \$\begingroup\$ Start by checking for errors (first after uploading the texture). glGetError() should return GL_NO_ERROR if everything is fine, otherwise it returns the error code. Also check if openglTexBuff is null after loading the jpeg. Basically check everything that could fail. \$\endgroup\$ Aug 29, 2018 at 10:47
  • \$\begingroup\$ @MaikSemder everything is fine, image is uploaded to GPU well in texture slot 0..any possible mess with texture coordinate of internal format?? \$\endgroup\$ Aug 29, 2018 at 10:58

1 Answer 1

1
\$\begingroup\$
layout(location = 1) in vec2 texCoords;

This is happening because your texture coordinate attribute is not at index 1 in your C/C++ code.

Typically one of three things will cause this.

  1. You're using the legacy glTexCoordPointer to set it up, which does not alias to index 1.
  2. You're using a glVertexAttribPointer call with an index other than 1.
  3. You're not actually setting a pointer for texture coordinates at all.

The default value for an unspecified attribute is {0, 0, 0, 1}, which - when that attribute is used as a texture coordinate - will sample from the bottom-left of the texture every time.

\$\endgroup\$
2
  • \$\begingroup\$ Yeah!!! my texcoord index was not at 1....This really pissed off my mind,By the way what does layout location describe about?? \$\endgroup\$ Aug 30, 2018 at 2:31
  • \$\begingroup\$ @ShivanshuRaj - see khronos.org/opengl/wiki/… and khronos.org/registry/OpenGL/extensions/ARB/… - but SE sites are not for extended discussions nor for tutorials, so I recommend that you read the documentation and if you have trouble with anything specific, ask another question. \$\endgroup\$ Aug 30, 2018 at 10:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .