Hi everyone,
I’m trying to learn OpenGL. I am able to render a triangle with fixed colors, but when I tried adding textures I only get black colors:
Main App:
//TEXTURE
glEnable(GL_TEXTURE_2D);
glGenBuffers(1, &tbo);
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(tex_coords), tex_coords, GL_STATIC_DRAW);
//Attributes
int tex_coords_index = glGetAttribLocation(program, "texCoords");
glEnableVertexAttribArray(tex_coords_index);
glVertexAttribPointer(tex_coords_index, 2, GL_FLOAT, GL_FALSE, 0, 0);
//Image
int width, height;
unsigned char *image = SOIL_load_image("c:/image.png", &width, &height, 0, GL_RGB);
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
//Uniform
int sampler2D = glGetUniformLocation(program, "tex");
glUniform1i(sampler2D, 0);
Shaders:
char *vertex_shader_source = {
"#version 330 core
"
"in vec3 vertices;"
"in vec2 texCoords;"
"out vec2 ftexCoords;"
"void main(){"
" gl_Position = vec4(vertices, 1.0f);"
" ftexCoords = texCoords;"
"}"
};
char *fragment_shader_source = {
"#version 330 core
"
"in vec2 ftexCoords;"
"out vec4 out_color;"
"uniform sampler2D tex;"
"void main(){"
" out_color = texture(tex, ftexCoords);"
//" out_color = vec4(0.0, 1.0, 0.0, 1.0);"
"}"
};
Any feedbacks and suggestions will be greatly appreciated.
Thanks.