0

When trying to render my opengl texture I just get a solid quad that is the base color of my texture.

Here is my code for loading textures and renderering / my shaders

vertexShader.vert

#version 400 core

in vec3 in_Position;
in vec3 in_Color;
in vec2 in_TextureCoord;

out vec3 pass_Color;
out vec2 pass_TextureCoord;

int main(void) {
  pass_Color = in_Color;
  pass_TextureCoord = in_TextureCoord;
  gl_Position = vec4(in_Position, 1.0);
}

fragmentShader.frag

#version 400 core

uniform sampler2D modelTexture;

in vec3 pass_Color;
in vec2 pass_TextureCoord;

out vec4 out_Color;

void main(void) {
  out_Color = texture(modelTexture, pass_TextureCoord);
}

TEXTURE LOADING

glEnable(GL_TEXTURE_2D);

glGenTextures(1, &textureID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);

int channels;
rawPixelData = SOIL_load_image(fileLocation, &width, &height, &channels,  SOIL_LOAD_RGB);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, rawPixelData);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(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_MIPMAP_LINEAR);

glBindTexture(GL_TEXTURE_2D, 0);

std::cout << "========================================" << std::endl;
std::cout << "LOADING TEXTURE: " << fileLocation << std::endl;
std::cout << "========================================" << std::endl;
std::cout << "| Width: " << width << ", Height: " << height << std::endl;
std::cout << "| Channels: " << channels << std::endl;
std::cout << "| Image ID: " << textureID << std::endl;
std::cout << "| Image Data: " << rawPixelData << std::endl;
std::cout << "| RESULT: " << SOIL_last_result() << std::endl;
std::cout << "========================================" << std::endl;
std::cout << "" << std::endl;

SOIL_free_image_data(rawPixelData);

TEXTURE DRAWING

glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model->getTexture()->getTextureID());
glUniform1i(glGetUniformLocation(program->getProgramID(), "modelTexture"), 0);

glBindVertexArray(model->getModel()->getVaoID());
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->getModel()->getIndiciesVBO());

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);

glDisable(GL_TEXTURE_2D);

VERTS / INDICES / COLOR ARRAYS

float vertss[12] = { 0.5f,  -0.5f, 0.0f,
                     0.5f,   0.5f, 0.0f,
                    -0.5f,   0.5f, 0.0f,
                    -0.5f,  -0.5f, 0.0f};

float color[12] = {1.0f, 0.0f, 0.0f,
                   0.0f, 1.0f, 0.0f,
                   0.0f, 0.0f, 1.0f,
                   1.0f, 1.0f, 0.0f};

//this is my indicies dont question the name =P
unsigned int intPtr[6] = {0, 1, 2,
                          2, 3, 0};
this->indicies = intPtr;


glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);

glGenBuffers(1, &vertsVBO);
glBindBuffer(GL_ARRAY_BUFFER, vertsVBO);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

unsigned int colorVBO = 0;
glGenBuffers(1, &colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), color, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindVertexArray(0);

glGenBuffers(1, &this->indiciesVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indiciesVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indicies, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

//in different class

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

glBindVertexArray(model->getVaoID());

unsigned int texturesVBO = 0;
glGenBuffers(1, &texturesVBO);
glBindBuffer(GL_ARRAY_BUFFER, texturesVBO);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), texCoords, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindVertexArray(0);

PART OF SHADER PROGRAM

programID = glCreateProgram();
glAttachShader(programID, vertexID);
glAttachShader(programID, fragmentID);

glBindAttribLocation(programID, 0, "in_Position");
glBindAttribLocation(programID, 1, "in_Color");
glBindAttribLocation(programID, 2, "in_TextureCoord");

glLinkProgram(programID);
glValidateProgram(programID);
  • How many "channels" does the image have? You are currently using RGB. Are you sure about that? Or is it RGBA? Also, why not use: `SOIL_load_OGL_texture(fileName, SOIL_LOAD_RGB, SOIL_CREATE_NEW_ID, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS);` – Brandon Feb 25 '16 at 01:49
  • using both SOIL_LOAD_RGB and SOIL_LOAD_RGBA results in the same visuals. I simply changed my texture code for texID to be SOIL_load_OGL_texture and no go – Cody Hafemeister Feb 25 '16 at 04:21
  • 2
    You could narrow this to a texture problem or a texture coord problem by modifying the fragment shader to `out_Color = vec4( pass_TextureCoord, 0.0, 1.0 );`. If you get a sane gradient, it is a texture binding problem; if you get a solid color, it's a texture coord problem. –  Feb 25 '16 at 06:27
  • @WilliamKappler thanks for that. I got a solid color so that narrows it down to a texture coord problem. What could be the issue do you think I was under the impression my texture coord layout was fine – Cody Hafemeister Feb 29 '16 at 19:22
  • @CodyHafemeister You say that the coordinates are set in another class - can you verify this is indeed happening correctly? If color and position works, but coords do not, that seems like the best suspect for a problem. –  Mar 01 '16 at 12:18
  • It turns out color doesn't actually work either if I set the shader out_Color to pass_Color which i programatically make 1.0f, 0.0f, 0.0f, 1.0f i dont actually get red I get the same textured color. – Cody Hafemeister Mar 04 '16 at 04:20

0 Answers0