I just started to learn OpenGL version 4 and GLSL version 3.3. I want to create a simple quad using texture. However, my window just doesn’t show anything(blank screen).
My main.cpp
void LoadQuad(){
//Load the Texture
Texture = ReadBMP(“NeHe.bmp”);
//make and bind VAO
glGenBuffers(1, &gVAO);
glBindVertexArray(gVAO);
//create vertex data
GLfloat vertex_data[] = {
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
//Make and bind VBO
glGenBuffers(1, &gVBO);
glBindBuffer(GL_ARRAY_BUFFER, gVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
glEnableVertexAttribArray( vs.attrib("vert"));
glVertexAttribPointer( vs.attrib("vert"), 3, GL_FLOAT, GL_FALSE, 0, NULL);
GLfloat uv_data[] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 1.0f
};
//Make and bind UV buffer
glGenBuffers(1, &gUV);
glBindBuffer(GL_ARRAY_BUFFER, gUV);
glBufferData(GL_ARRAY_BUFFER, sizeof(uv_data), uv_data, GL_STATIC_DRAW);
glEnableVertexAttribArray(vs.attrib("inUV"));
glVertexAttribPointer(vs.attrib("inUV"), 2, GL_FLOAT, GL_FALSE, 0, NULL);
}
void Render(){
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Use the Shader program
glUseProgram(program);
//Set the MVP matrix
GLint MVPID = vs.attrib("MVP");
glUniformMatrix4fv( MVPID, 1, GL_FALSE, &MVP[0][0]);
//Set the texture
glActiveTexture(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture);
//Get a handle of "textureSampler" in fragment shader code
GLuint TextureID = fs.attrib("textureSampler");
glUniform1i(TextureID, 0);
//Draw the scene
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//Unuse the shader program
glUseProgram(0);
}
And then for the VS program:
#version 330 core
//vertex shader
//Input vertex data, changes frequently
in vec3 vert;
in vec2 inUV;
out vec2 outUV;
//uniform variables - stay constant for the whole mesh
uniform mat4 MVP;
void main(){
gl_Position = MVP * vec4(vert,1);
outUV = inUV;
}
FS program:
#version 330 core
//fragment shaders
in vec2 outUV;
out vec3 finalColor;
uniform sampler2D textureSampler;
void main(){
finalColor = texture2D(textureSampler, outUV).rgb;
}