PDA

View Full Version : [OpenGL] Esercizio per disegnare un cubo mostra solo la faccia frontale


-Ivan-
13-12-2013, 12:17
Sto facendo un esercizio in cui cerco di disegnare un cubo con OpenGL.
Non riesco a capire perchè mi mostri solo la faccia frontale.
Per disegnarlo uso glDrawElements dopo aver creato un buffer di indici ed il buffer con i vertici del cubo.



// include...


enum Attrib_IDs { vPosition = 0 };

GLuint VERTEX_SHADER_ID;

GLuint VBOs[1]; // Vertex Buffer Objects
GLuint IBOs[1]; // Index Buffer Objects

GLuint SCREEN_WIDTH = 512, SCREEN_HEIGHT = 512;

glm::mat4 mvp; //model view projection matrix



#pragma region INITIALIZE DATA AND OPENGL

void ModelViewProjectionMatrix()
{
// Matrix to translate the cube deep far in the scene
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, 0.0, -4.0));

// The lookat function takes: eye (position of the camera), center (where the camera is pointed to), and up (top of the camera).
glm::mat4 view = glm::lookAt(glm::vec3(0.0, 2.0, 0.0), glm::vec3(0.0, 0.0, -4.0), glm::vec3(0.0, 1.0, 0.0));

// The perpective function takes: fovy (the lens angle), aspect (the aspect ratio), and zNear, zFar (clipping planes).
glm::mat4 projection = glm::perspective(45.0f, 1.0f * SCREEN_WIDTH/SCREEN_HEIGHT, 0.1f, 10.0f);


// We end up with a model view projection matrix giving us the result for our camera
mvp = projection * view * model;
}

void CreateVerticesData()
{
Vector3D cube_vertices[] = {
// front
Vector3D(-1.0, -1.0, 1.0),
Vector3D( 1.0, -1.0, 1.0),
Vector3D( 1.0, 1.0, 1.0),
Vector3D(-1.0, 1.0, 1.0),
// back
Vector3D(-1.0, -1.0, -1.0),
Vector3D( 1.0, -1.0, -1.0),
Vector3D( 1.0, 1.0, -1.0),
Vector3D(-1.0, 1.0, -1.0)
};

// Generate the Vertex Buffer Object storing the data for the 8 vertices of the cube.
// In total there are 24 floats representing the x, y, z components of each vertex.
glGenBuffers(1, VBOs);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
}

void CreateIndexData()
{
// Indices of the vertices of the cube.
// This is used to avoid duplicates. As the faces of the cube share vertices we can duplicate this vertices ending with a
// total of 36 (6 faces x 2 triangles per face x 3 vertices each triangle = 36) or we can store them just once and refer to them
// using these indices and drawing the cube with glDrawElements instead than with glDrawArray that just iterates through an array
// drawing every vertices it finds in it.
GLushort cube_elements[] = {
// front
0, 1, 2,
2, 3, 0,
// top
3, 2, 6,
6, 7, 3,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 5, 1,
1, 0, 4,
// left
4, 0, 3,
3, 7, 4,
// right
1, 5, 6,
6, 2, 1,
};

// Generate the Index Buffer Object to index the vertices inside the Vertex Buffer Object.
// The indexes are related to the 8 vertices making the cube.
glGenBuffers(1, IBOs);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOs[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
}

void Init()
{
glEnable(GL_DEPTH_TEST);
ModelViewProjectionMatrix();

CreateVerticesData();
CreateIndexData();

// Load and use the shaders
VERTEX_SHADER_ID = LoadShader("shader.vert", "shader.frag");
glUseProgram(VERTEX_SHADER_ID);

// Set up the value of the uniform value inside the vertex shader
GLuint mvp_Loc = glGetUniformLocation(VERTEX_SHADER_ID, "mvp");
glUniformMatrix4fv(mvp_Loc, 1, GL_FALSE, glm::value_ptr(mvp));

// Defines parameters to pass to the shaders
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition);
}

#pragma endregion

void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOs[0]);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

// marks the current window as needing to be redisplayed
glutPostRedisplay();
glFlush();
}

int _tmain(int argc, _TCHAR* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("Cube");

if( glewInit() )
{
std::cerr << "Error...exiting";
exit(EXIT_FAILURE);
}

Init();

glutDisplayFunc(Display);
glutMainLoop();

return 0;
}

-Ivan-
13-12-2013, 15:24
Ho trovato.
L'errore era in:

glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));


che invece deve essere:


glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));


Stavo ignorando l'asse Z. Purtroppo avevo copiato quella riga da un altro esercizio in cui disegnavo un triangolo e quindi avevo coordinate su due dimensioni.