I do not fully understand the concept of binding textures in OpenGL, I can't get this to render a jpg image for my project, any insight to what I may be doing wrong?
// Shader sources
const GLchar* vertexShaderSource = R"glsl(
#version 150 core
in vec2 position;
in vec3 color;
in vec2 texcoord;
out vec3 Color;
out vec2 Texcoord;
void main()
{
Color = color;
Texcoord = texcoord;
gl_Position = vec4(position, 0.0, 1.0);
}
)glsl";
const GLchar* fragmentShaderSource = R"glsl(
#version 150 core
in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D tex;
void main()
{
outColor = texture(tex, Texcoord) * vec4(Color, 1.0);
}
)glsl";
//creating the window
GLFWwindow *window= glfwCreateWindow(1200, 700, "NEX_Tests", NULL, NULL);
if(nullptr== window){
std::cout<<"ERROR HAS OCCURED WHILE LOADING THE WINDOW\n"<<"\n";
glfwTerminate();
return -1;
}
else{
int screenWidth,screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
glfwMakeContextCurrent(window);
glewExperimental=true;
if(GLEW_OK==!glewInit()){
std::cout<<"ERROR HAS OCCURED WHILE INITILIZING THE GLEW LIBRARY\n"<<"\n";
return EXIT_FAILURE;
}
//set the viewing port
glViewport(0, 0, screenWidth, screenHeight);
//creating the vetex array object;
GLuint vao,vbo;
glGenVertexArrays(1,&vao);
glGenBuffers(1,&vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
//creating the element buffer
GLuint ebo;
glGenBuffers(1,&ebo);
GLuint elements[]={
0,1,2,
2,3,0
};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(elements),elements,GL_STATIC_DRAW);
std::cout<<vbo<<"\n";
// glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,(3* sizeof(GLfloat)),(GLvoid*)0);
// glEnableVertexAttribArray(0);
//glBindBuffer(GL_VERTEX_ARRAY,0);
// glBindVertexArray(0);
//creating and linking the shaders
GLuint vertexShader= glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader,1,&vertexShaderSource,NULL );
glCompileShader(vertexShader);
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertexShader,GL_COMPILE_STATUS,&success);
if(!success){
glGetShaderInfoLog(vertexShader, 512,NULL,infoLog);
std::cout<<"ERROR HAS OCCURED WHILE COMPILING THE SHADERS\n"<<infoLog<<std::endl;
}
GLuint fragmentShader= glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader,GL_COMPILE_STATUS,&success);
if(!success){
glGetShaderInfoLog(fragmentShader,512,NULL,infoLog);
std::cout<<"ERROR HAS OCCURED WHILE COMPILING THE SHADERS\n"<<infoLog<<std::endl;
}
GLuint shaderProgram= glCreateProgram();
glAttachShader(shaderProgram,vertexShader);
glAttachShader(shaderProgram,fragmentShader);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
glGetProgramiv(shaderProgram,GL_LINK_STATUS,&success);
if(!success){
glGetProgramInfoLog(shaderProgram,512,NULL,infoLog);
std::cout<<"ERROR HAS OCCURED WHILE LINKING THE SHADERS\n"<<infoLog<<std::endl;
}
//delete the shaders
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
//layout of the vertex shader
GLint posAttrib = glGetAttribLocation(shaderProgram,"position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib,0,GL_FLOAT,GL_FALSE,7* sizeof(GLfloat),0);
GLint colAttrib = glGetAttribLocation(shaderProgram,"color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib,2,GL_FLOAT,GL_FALSE,7* sizeof(GLfloat),((void*)(2*sizeof(GLfloat))));
GLint TexAttrib= glGetAttribLocation(shaderProgram,"texcoord");
glEnableVertexAttribArray(TexAttrib);
glVertexAttribPointer(TexAttrib,2,GL_FLOAT,GL_FALSE,7*sizeof(GLfloat),((void*)(5*sizeof(GLfloat))));
//loading the textures
GLuint tex;
glGenTextures(2, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
std::cout<<tex<<"\n";
glActiveTexture(GL_TEXTURE0+ 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//loading the image
int me=200,me2=300;
int *width=&me,*height=&me2;
unsigned char* Image= SOIL_load_image("IceKarl.png", width, height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, *width, *height, 0, GL_RGB, GL_UNSIGNED_BYTE, Image);
SOIL_free_image_data(Image);
if(!Image){
std::cout<<"ERROR HAS OCCURED WHILE LOADING IN IMAGE\n";
}
//running loop
while(!glfwWindowShouldClose(window)){
glfwPollEvents();
glClearColor(0.0f, 0.00f, 0.00f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, 0);
//glDrawArrays(GL_TRIANGLES, 0, 6);
// glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
// glBindTexture(GL_TEXTURE_2D,0);
glfwSwapBuffers(window);
}
glDeleteTextures(1,&tex);
glDeleteProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteBuffers(1,&vbo);
glDeleteBuffers(1,&ebo);
glDeleteVertexArrays(1,&vao);
}
glfwTerminate();
return 0;
}
The 2nd argument of glVertexAttribPointer
is the tuple size of the attribute. In your case this is 2 vor the position (x, y), 3 for the color (r, g, b) and 2 for the texture coordinates (u, v):
GLint posAttrib = glGetAttribLocation(shaderProgram,"position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT,GL_FALSE,7* sizeof(GLfloat),0);
GLint colAttrib = glGetAttribLocation(shaderProgram,"color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT,GL_FALSE,7* sizeof(GLfloat),((void*)(2*sizeof(GLfloat))));
GLint TexAttrib= glGetAttribLocation(shaderProgram,"texcoord");
glEnableVertexAttribArray(TexAttrib);
glVertexAttribPointer(TexAttrib, 2, GL_FLOAT,GL_FALSE,7*sizeof(GLfloat),((void*)(5*sizeof(GLfloat))));
When you draw the mesh, then the texture and the vertex array object has to be bound.
The texture unit has to be set (glActiveTexture
) before binding the texture (glBindTexture
):
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, tex);
You have to assign the texture unit index to the texture sampler uniform (glUniform
):
glUniform1i(tex_loc, 1);
Get the location index of the texture sampler uniform (glGetUniformLocation
):
GLuint tex_loc = glGetUniformLocation(shaderProgram, "tex");
Bind the objects before the draw call:
int texture_unit = 1;
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(tex_loc, texture_unit);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
There is a further issue when you generate the texture names. Generate 1 texture name rather than 2:
glGenTextures(2, &tex);
GLuint tex;
glGenTextures(1, &tex);