Hi all,
I’ve decided to pick some OpenGL that I haven’t touched for quite a lot.
I was just trying to draw a cube, but all I get is a black screen… I
know the question is extremely silly, but I can’t find where’s the issue here
I have some structs defined as:
struct Program{
GLuint programID;
GLuint vertexPositionAttribute;
GLuint textureCoordAttribute;
GLuint vertexNormalAttribute;
GLuint pMatrixUniform;
GLuint mvMatrixUniform;
GLuint nMatrixUniform;
GLuint samplerUniform;
GLuint textureUniform;
GLuint ambientColorUniform;
} typedef Program;
struct MeshBuffers{
GLuint positionBuffer;
GLuint indexBuffer;
GLuint normalBuffer;
GLuint textCoordBuffer;
GLuint textureID;
GLint vertexNumber;
GLint indexNumber;
} typedef MeshBuffers;
Then I build up a cube using those file:
Cube.h
#include "structs.h"
static GLfloat cube_positions[] = {
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
};
static GLfloat cube_normals[] = {
// Front face
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
// Back face
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
// Top face
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
// Bottom face
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
// Right face
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
// Left face
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0
};
static GLfloat cube_texture_uvs[] = {
// Front face
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Back face
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
// Top face
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
// Bottom face
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
// Right face
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
// Left face
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0
};
static GLfloat cube_indices[]={
0, 1, 2, 0, 2, 3, // Front face
4, 5, 6, 4, 6, 7, // Back face
8, 9, 10, 8, 10, 11, // Top face
12, 13, 14, 12, 14, 15, // Bottom face
16, 17, 18, 16, 18, 19, // Right face
20, 21, 22, 20, 22, 23 // Left face
};
MeshBuffers getCube();
Cube.cpp
...
struct MeshBuffers getCube(){
MeshBuffers cubeBuffers;
glGenBuffers(1,&(cubeBuffers.positionBuffer));
glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.positionBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_positions), cube_positions,GL_STATIC_DRAW);
cubeBuffers.vertexNumber=24;
glGenBuffers(1,&(cubeBuffers.normalBuffer));
glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.normalBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_normals), cube_normals,GL_STATIC_DRAW);
glGenBuffers(1,&(cubeBuffers.textCoordBuffer));
glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.textCoordBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_texture_uvs), cube_texture_uvs,GL_STATIC_DRAW);
glGenBuffers(1,&(cubeBuffers.indexBuffer));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,cubeBuffers.indexBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_indices), cube_indices,GL_STATIC_DRAW);
cubeBuffers.indexNumber=36;
return cubeBuffers;
}
...
And finally my main file:
int main( void ){
MeshBuffers cube = getCube();
while(1){
drawLoop();
}
}
void drawLoop(){
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
if(glfwGetKey('W')==GLFW_PRESS)
z-=0.05f;
if(glfwGetKey('S')==GLFW_PRESS)
z+=0.05f;
pMatrix=glm::perspective(45.0f, 1024.0f/768.0f, 0.1f, 10000.0f);
glUniform3f(program.ambientColorUniform, 0.8f,0.8f,0.8f);
mvMatrix = glm::mat4(1.0);
mvMatrix=glm::translate(mvMatrix,glm::vec3(0.0f,0.0f,z));
for(int i=0; i<meshes.size(); ++i){
if(useTexture){
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,meshes.at(i).textureID);
glUniform1i(program.samplerUniform,0);
glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).textCoordBuffer);
glVertexAttribPointer(
program.textureCoordAttribute,
2,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
}
glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).normalBuffer);
glVertexAttribPointer(
program.vertexNormalAttribute,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).positionBuffer);
glVertexAttribPointer(
program.vertexPositionAttribute,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,meshes.at(i).indexBuffer);
setMatrixUniform();
glDrawElements(GL_TRIANGLE_STRIP,meshes.at(i).indexNumber, GL_UNSIGNED_SHORT,0);
}
glfwSwapBuffers();
glFlush();
}
...
void initShaders(){
programID = LoadShaders( "vs.vs", "fs.fs" );
glUseProgram(programID);
program.vertexPositionAttribute=glGetAttribLocation(programID, "aVertexPosition");
glEnableVertexAttribArray(program.vertexPositionAttribute);
program.vertexNormalAttribute=glGetAttribLocation(programID, "aVertexNormal");
glEnableVertexAttribArray(program.vertexNormalAttribute);
program.vertexNormalAttribute=1;
glEnableVertexAttribArray(program.vertexNormalAttribute);
program.textureCoordAttribute=glGetAttribLocation(programID, "aTextureCoord");
glEnableVertexAttribArray(program.textureCoordAttribute);
program.nMatrixUniform=glGetUniformLocation(programID, "uNMatrix");
program.pMatrixUniform=glGetUniformLocation(programID, "uPMatrix");
program.mvMatrixUniform=glGetUniformLocation(programID, "uMVMatrix");
program.ambientColorUniform=glGetUniformLocation(programID, "uAmbientColor");
program.samplerUniform=glGetUniformLocation(programID, "uSampler");
program.textureUniform=glGetUniformLocation(programID, "uText");
}
.....
Where my shaders are:
vs.vs
#version 330 core
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
void main(void) {
vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = uPMatrix * vPosition;
vTextureCoord = aTextureCoord;
vTransformedNormal = uNMatrix * aVertexNormal;
}
fs.fs
#version 330 core
precision mediump float;
// These are unused for now.
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
uniform vec3 uAmbientColor;
uniform sampler2D uSampler;
void main(void) {
vec4 fragmentColor;
fragmentColor = vec4(0.9, 0.4, 0.4, 1.0);
gl_FragColor = vec4(fragmentColor.rgb* uAmbientColor, fragmentColor.a);
}
I’m sorry to ask such a vague and stupid question, but I’m kind of clueless.
I’m also sorry that the code here is quite a lot, but it should be fast readable as it is pretty standard I guess