Click here to Skip to main content
14,331,896 members
Rate this:
Please Sign up or sign in to vote.
Stack over flow question:
Title: Why cant I see my texture load
Explanation: I have been trying to texture a cube I have created and I am not able to see the textures. I can just see a blank cube rendering. I have looked at the code to see if there is anything wrong with it however I don't see any problems but I think it is because I am new to OpenGL so maybe someone else can see what is wrong with the code.

This is my texture code within vertex_array constructor:

<pre lang="c++">

    vertex_array::vertex_array(float* vertex_buffer, int num_of_floats, const std::string& texture_file)
    {
    	glGenVertexArrays(1, &va_ID);
    	glBindVertexArray(va_ID);
    
    	glGenBuffers(1, &vb_ID);
    	glBindBuffer(GL_ARRAY_BUFFER, vb_ID);
    	glBufferData(GL_ARRAY_BUFFER, num_of_floats * sizeof(float), vertex_buffer, GL_STATIC_DRAW);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(0 * sizeof(float)));
    	glEnableVertexAttribArray(0);
    	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    	glEnableVertexAttribArray(1);
    
    	glGenTextures(1, &texture);
    	glBindTexture(GL_TEXTURE_2D, 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);
    	int width, height, nrChanells;
    	stbi_set_flip_vertically_on_load(true);
    	unsigned char* data = stbi_load(texture_file.c_str(), &width, &height, &nrChanells, 0);
    	if (data)
    	{
    		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    		glGenerateMipmap(GL_TEXTURE_2D);
    	}
    	else {std::cout << "failed to load texture" << std::endl;}
    	stbi_image_free(data);
    
    	glGenBuffers(1, &ib_ID);
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib_ID);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index_buffer), index_buffer, GL_STATIC_DRAW);
    	glBindVertexArray(0);
    }


This is my shader:

#version 330 core

layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texCoord;

out vec2 v_TexCoord;

uniform mat4 view;
uniform mat4 projection;

void main()
{
v_TexCoord = texCoord;
gl_Position = projection * view * position;
};

#version 330 core

layout(location = 0) out vec4 color;

in vec2 v_TexCoord;

uniform sampler2D u_Texture;

void main()
{
vec4 texColor = texture(u_Texture, v_TexCoord);
color = texColor;
//color = vec4(0.0, 0.7, 0.4, 1.0);
};


and is my main application code:

vertex_array va_1(cube1, 40, "resources/blocks.png");
shader shader_1("src/shader1.shader");
va_1.bind();
shader_1.bind();


What I have tried:

I have tried checking the code_____________________________
Posted
Comments
KarstenK 12-Nov-18 13:12pm
   
Use a solid color for your rendering code to see, if it renders. Check that the texture gets loaded.

I am not sure, but is void main() really correct?
BerthaDusStuf 12-Nov-18 15:30pm
   
I tried using a solid color and it still showed nothing. Also void main() is correct but thanks.
KarstenK 15-Nov-18 11:45am
   
try to restart the project with some working sample app.
BerthaDusStuf 18-Nov-18 9:50am
   
what do you mean by some working sample app?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100