PDA

View Full Version : A cube is not rendering



cifacia
01-19-2014, 11:03 AM
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.positionB uffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_positions ), cube_positions,GL_STATIC_DRAW);
cubeBuffers.vertexNumber=24;


glGenBuffers(1,&(cubeBuffers.normalBuffer));
glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.normalBuf fer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_normals), cube_normals,GL_STATIC_DRAW);

glGenBuffers(1,&(cubeBuffers.textCoordBuffer));
glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.textCoord Buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_texture_u vs), cube_texture_uvs,GL_STATIC_DRAW);

glGenBuffers(1,&(cubeBuffers.indexBuffer));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,cubeBuffers.i ndexBuffer);
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).textCoor dBuffer);
glVertexAttribPointer(
program.textureCoordAttribute,
2,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
}

glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).normalBu ffer);
glVertexAttribPointer(
program.vertexNormalAttribute,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);

glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).position Buffer);
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).inde xNumber, GL_UNSIGNED_SHORT,0);

}
glfwSwapBuffers();
glFlush();

}

...

void initShaders(){
programID = LoadShaders( "vs.vs", "fs.fs" );
glUseProgram(programID);
program.vertexPositionAttribute=glGetAttribLocatio n(programID, "aVertexPosition");
glEnableVertexAttribArray(program.vertexPositionAt tribute);
program.vertexNormalAttribute=glGetAttribLocation( programID, "aVertexNormal");
glEnableVertexAttribArray(program.vertexNormalAttr ibute);
program.vertexNormalAttribute=1;
glEnableVertexAttribArray(program.vertexNormalAttr ibute);

program.textureCoordAttribute=glGetAttribLocation( programID, "aTextureCoord");
glEnableVertexAttribArray(program.textureCoordAttr ibute);

program.nMatrixUniform=glGetUniformLocation(progra mID, "uNMatrix");
program.pMatrixUniform=glGetUniformLocation(progra mID, "uPMatrix");
program.mvMatrixUniform=glGetUniformLocation(progr amID, "uMVMatrix");

program.ambientColorUniform=glGetUniformLocation(p rogramID, "uAmbientColor");
program.samplerUniform=glGetUniformLocation(progra mID, "uSampler");
program.textureUniform=glGetUniformLocation(progra mID, "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

cifacia
01-20-2014, 02:22 PM
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.positionB uffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_positions ), cube_positions,GL_STATIC_DRAW);
cubeBuffers.vertexNumber=24;


glGenBuffers(1,&(cubeBuffers.normalBuffer));
glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.normalBuf fer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_normals), cube_normals,GL_STATIC_DRAW);

glGenBuffers(1,&(cubeBuffers.textCoordBuffer));
glBindBuffer(GL_ARRAY_BUFFER,cubeBuffers.textCoord Buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_texture_u vs), cube_texture_uvs,GL_STATIC_DRAW);

glGenBuffers(1,&(cubeBuffers.indexBuffer));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,cubeBuffers.i ndexBuffer);
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).textCoor dBuffer);
glVertexAttribPointer(
program.textureCoordAttribute,
2,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
}

glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).normalBu ffer);
glVertexAttribPointer(
program.vertexNormalAttribute,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);

glBindBuffer(GL_ARRAY_BUFFER,meshes.at(i).position Buffer);
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).inde xNumber, GL_UNSIGNED_SHORT,0);

}
glfwSwapBuffers();
glFlush();

}

...

void initShaders(){
programID = LoadShaders( "vs.vs", "fs.fs" );
glUseProgram(programID);
program.vertexPositionAttribute=glGetAttribLocatio n(programID, "aVertexPosition");
glEnableVertexAttribArray(program.vertexPositionAt tribute);
program.vertexNormalAttribute=glGetAttribLocation( programID, "aVertexNormal");
glEnableVertexAttribArray(program.vertexNormalAttr ibute);
program.vertexNormalAttribute=1;
glEnableVertexAttribArray(program.vertexNormalAttr ibute);

program.textureCoordAttribute=glGetAttribLocation( programID, "aTextureCoord");
glEnableVertexAttribArray(program.textureCoordAttr ibute);

program.nMatrixUniform=glGetUniformLocation(progra mID, "uNMatrix");
program.pMatrixUniform=glGetUniformLocation(progra mID, "uPMatrix");
program.mvMatrixUniform=glGetUniformLocation(progr amID, "uMVMatrix");

program.ambientColorUniform=glGetUniformLocation(p rogramID, "uAmbientColor");
program.samplerUniform=glGetUniformLocation(progra mID, "uSampler");
program.textureUniform=glGetUniformLocation(progra mID, "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



I'm not able to EDIT my post, but however I'm pretty sure now that the error is in the Cube/relative structures and not in the rendering part.

Can you give me a hint ? :(

Jesse
01-21-2014, 08:51 PM
I'm kind of clueless.


If you're clueless about your own code, what makes you think others have a better idea of what your code is supposed to do? People aren't here to debug your code. They may possibly answer specific questions about OpenGL, but it seems you don't have any specific questions to ask.

I haven't looked at your code, but it's obvious that this IBO is initialized incorrectly:






...

glGenBuffers(1,&(cubeBuffers.indexBuffer));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,cubeBuffers.i ndexBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube_indices), cube_indices,GL_STATIC_DRAW);
...

cifacia
01-22-2014, 02:38 AM
If you're clueless about your own code, what makes you think others have a better idea of what your code is supposed to do? People aren't here to debug your code. They may possibly answer specific questions about OpenGL, but it seems you don't have any specific questions to ask.

I haven't looked at your code, but it's obvious that this IBO is initialized incorrectly:

Well I'm clueless where the error is, not what the code does. I'm sorry for haven't made myself clear. My question was (according to the second post) what's wrong with my structures that lead to a black screen instead of the cube rendered ?
Anyway, I fixed that earlier and I'm sorry I haven't post it here, but even with that change the thing is still not working.