PDA

View Full Version : Creating a cube?



interwound
01-10-2012, 06:47 PM
Hey all, I know this probably a pretty simple question but I must be missing something because I can't get it to display or use the proper coordinates. Heres the code I have so far:



//Init Reasources
GLfloat cube_vertices[] = {
// front
-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
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,

//etc
};
glGenBuffers(1, &vbo_cube_vertices);
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);

//Shader and attribute creation:
if ((vs = create_shader("triangle.v.glsl", GL_VERTEX_SHADER)) == 0) return 0;
if ((fs = create_shader("triangle.f.glsl", GL_FRAGMENT_SHADER)) == 0) return 0;

const char* attribute_name;
attribute_name = "coord3d";
attribute_coord3d = glGetAttribLocation(program, attribute_name);

attribute_name = "v_color";
attribute_v_color = glGetAttribLocation(program, attribute_name);

const char* uniform_name;
uniform_name = "mvp";
uniform_mvp = glGetUniformLocation(program, uniform_name);




//On display
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glUseProgram(program);
glEnableVertexAttribArray(attribute_coord3d);
// Describe our vertices array to OpenGL (it can't guess its format automatically)
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
glVertexAttribPointer(
attribute_coord3d, // attribute
3, // number of elements per vertex, here (x,y,z)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
glDrawArrays(GL_ARRAY_BUFFER, 0, 3);
glDisableVertexAttribArray(attribute_coord3d);
glDisableVertexAttribArray(attribute_v_color);
glutSwapBuffers();




//Shaders:

//Vertex:

attribute vec3 coord3d;
attribute vec3 v_color;
uniform mat4 mvp;
varying vec3 f_color;
void main(void) {

gl_Position = mvp * vec4(coord3d, 1.0);

f_color = v_color;

}


//Fragment:
varying vec3 f_color;

void main(void) {
gl_FragColor = vec4(f_color.x, f_color.y, f_color.z, 1.0);

}


I think I posted all relevant code, if not let me know! And Thanks! Oh and heres the link of the tutorial I was learning from: OpenGl Programming (http://en.wikibooks.org/wiki/OpenGL_Programming)

Rosario Leonardi
01-10-2012, 07:19 PM
glDrawArrays(GL_ARRAY_BUFFER, 0, 3);
???

try

glDrawArrays(GL_QUADS, 0, 4*6);

interwound
01-10-2012, 07:36 PM
Ah, fantastic! I would never have caught that by my self. A few questions though, what does the 3rd parameter mean? And how did you get 4*6? Also, how come if I try to render with GL_Triangles the triangles are sticking every which way and not forming squares(which is what I had been shooting for)?

Rosario Leonardi
01-11-2012, 05:23 PM
glDrawArray say:
I want to draw quads (first argument), take the datas from the bound buffer object, the vertex start from 0 (second argument) and I have 24 vertex (last argument).
4 * 6 is 4 vertex for 6 faces, it's a cube.
If you want to use triangles then you have to pass the data in the right order.

ex: if you want to draw a quad you send vertexes 0 1 2 3, then
if you want to draw the same quad with two triangle you need to send 6 vertexes 0 1 2 for the first one, and 1 2 3 for the second one. OpenGl don't know how your data are organized, so don't know if your bunch of float represent triangles, quad, tristrip or flowers.

Bye