So, I'm trying to study vbo and just do some examples, but opengl is killing me. I've been four hours trying to do this code work, but I dont have a clue why this dont work.
I was trying to draw 4 squares with different colors.
But if I try to draw using GL_POINTS i only see 4 points in the screen, and is supposed to be 9 points. If I try using GL_QUADS it simply don't draw nothing.
The interesting is that when I change this line:
glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
to this:
glDrawElements(GL_POINTS, 64, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
Opengl draw the 9 dots, but only that, GL_QUADS dont work.
Please, someone give me a light on this problem, it's killing me!!
The complete code is here:
#define GL_GLEXT_PROTOTYPES
#include<GL/glut.h>
#include<iostream>
using namespace std;
#define BUFFER_OFFSET(offset) ((GLfloat*) NULL + offset)
#define VERTICES 0
#define INDICES 1
#define NUM_BUFFERS 2
void init_2(){
GLuint buffers[NUM_BUFFERS];
GLfloat vertices[][3]= {
{0.0, 0.0, 0.0},
{0.0, 40.0, 0.0},
{0.0, 80.0, 0.0},
{40.0, 0.0, 0.0},
{40.0, 40.0, 0.0},
{40.0, 80.0, 0.0},
{80.0, 0.0, 0.0},
{80.0, 40.0, 0.0},
{80.0, 80.0, 0.0}
};
GLuint indices[][4] = {
{0, 1, 4, 3},
{1, 2, 5, 4},
{3, 4, 7, 6},
{4, 5, 8, 7}
};
glGenBuffers(NUM_BUFFERS, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
void display(void){
glPushMatrix();
glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glPopMatrix();
glutSwapBuffers();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
gluOrtho2D((GLdouble) -1.0, (GLdouble) 90.0, (GLdouble) -1.0, (GLdouble) 90.0);
init2();
}
int main(int argv, char** argc) {
glutInit(&argv, argc);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("myCode.cpp");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Here's the problem:
GLuint indices[][4] = {
...
glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
You declare GLuint indices, but then tell glDrawElements that you're giving it bytes.
GLuint is a 32-bit type, so there are 4 bytes for each. As your index data is very small, each 32-bit value has only data in the bottom byte, leaving the rest as zero.
So in the first 16 bytes, which are the first 4 GLuints, there are only 4 discrete values, so you see 4 points. If you try 64 (which is 16 * 4), you will see all 9 points. However these points are mixed in with a lot of zeros, so when you try to render as GL_QUADS, nothing will be drawn.
Related
I am trying to simulate a cube bouncing between walls. For this purpose, I have constructed 2 vertex array objects, one of them representing the cube and the other representing walls. Firstly, I created walls. I am drawing walls with glDrawElements call since I have used index (element) buffers to create it. I have also created a cube model, but I am not using index buffer for it, so I will use glDrawArrays call to draw it as far as I know. My walls are showing up nicely, but I cannot figure out how to draw the cube (at this step, I am not even trying to animate the cube, I just want to draw it) This is what my code looks like:
#include "Cube.h"
#include <cstring>
GLuint vao[2];
void init()
{
cube();
GLuint program = InitShader( "vshader.glsl", "fshader.glsl" );
glUseProgram( program );
GLuint vPosition = glGetAttribLocation(program, "vPosition");
glGenVertexArrays(2, vao);
glBindVertexArray(vao[0]);
GLuint cube_buffer;
glGenBuffers(1, &cube_buffer);
glBindBuffer(GL_ARRAY_BUFFER, cube_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubePoints), cubePoints, GL_STATIC_DRAW);
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
const float mid_value = 0.0f;
const float far_value = 0.35f;
const float near_value = 1.0f;
const float positions[60] = {
/// positions /// colors
mid_value, -near_value, 1.0, 0.0, 0.0, /// 0
near_value, -near_value, 1.0, 0.0, 0.0, /// 1
near_value, mid_value, 0.0, 1.0, 0.0, /// 2
near_value, near_value, 0.0, 1.0, 0.0, /// 3
mid_value, near_value, 1.0, 1.0, 0.0, /// 4
-near_value, near_value, 1.0, 1.0, 0.0, /// 5
-near_value, mid_value, 0.0, 0.0, 1.0, /// 6
-near_value, -near_value, 0.0, 0.0, 1.0, /// 7
far_value, -far_value, 1.0, 0.0, 0.0, /// 8
far_value, far_value, 0.0, 1.0, 0.0, /// 9
-far_value, far_value, 1.0, 1.0, 0.0, /// 10
-far_value, -far_value, 0.0, 0.0, 1.0 /// 11
};
unsigned int indices[36] = {
7,11,0,
0,8,1,
1,8,2,
2,9,3,
3,9,4,
4,10,5,
5,10,6,
6,11,7,
11,0,8,
8,2,9,
9,4,10,
10,6,11,
};
glBindVertexArray(vao[1]);
GLuint room_buffer;
glGenBuffers(1, &room_buffer);
glBindBuffer(GL_ARRAY_BUFFER, room_buffer);
glBufferData(GL_ARRAY_BUFFER, 60 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)0); /// positions
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(2* sizeof(float))); /// colors
GLuint index_buffer;
glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
// glDrawArrays(...); --> how to use this, or should I really use this here?
glutSwapBuffers();
}
void idle() {
glutPostRedisplay();
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize( 600, 600 );
glutCreateWindow( "Bouncing Cube" );
glewExperimental = GL_TRUE;
glewInit();
init();
glutDisplayFunc(display);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutMainLoop();
}
In this code, cubePoints is filled by cube() method in another translation unit. I am not exactly sure how to draw cube on the screen, and how to use glDrawArrays here? Should I bind or unbind anything to draw it? I feel like I am not using vertex array objects properly. Below is what my walls look like:
I simply want cube to appear on the far side, where the white rectangle is located at. Is it possible to call glDrawArrays with glDrawElements?
You must bind the Vertex Array Object before the "draw" call. The drawing instruction uses the data stored in the currently bound Vertex Array Object:
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vao[0]);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(vao[1]);
glDrawArrays(...);
glutSwapBuffers();
}
I am trying to draw two quads using vertex buffer objects in OpenGL. They should be draw with different colors. Like you see, the first quad has a red, green, blue and yellow vertices. The second quad has different colors, but the problem is that they second quad gets drawn completely yellow. This is my code:
GLfloat vertices[24] = {10.0, 10.0, 0.0, 10.0, -10.0, 0.0, -10.0, -10.0, 0.0, -10.0, 10.0, 0.0,
20.0, 20.0, 0.0, 20.0, 10.0, 0.0, 10.0, 10.0, 0.0, 10.0, 20.0, 0.0 };
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLfloat colors[24] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.7, 0.2, 1.0, 0.2, 1.0, 0.7, 0.8, 0.0, 0.45 };
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLfloat) * 24, colors, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, colorBuffer);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, 8);
This is how the quads are drawn:
The second quad is the one on top right. It should be multicolored, but it's drawn using the 4th element of the color buffer. What should I do to fix it?
See OpenGL 2.1 Reference Pages; glColorPointer:
glColorPointer specifies the location and data format of an array of color components to use when rendering. ...
If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target while a color array is specified, pointer is treated as a byte offset into the buffer object's data store.
In your code buffer is bound to the target GL_ARRAY_BUFFER;
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
Then colorBuffer is bound to the target GL_ELEMENT_ARRAY_BUFFER and the color array is defined:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, colorBuffer);
glColorPointer(3, GL_FLOAT, 0, 0);
Since buffer is still bound to the target GL_ARRAY_BUFFER at this point, glColorPointer uses buffer. This causes that the vertex coordinates are treated as colors and that`s what you can see in the rendering.
To solve the issue you have to bint colorBuffer to the target GL_ARRAY_BUFFER:
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
Like the picture showed in the link below. I want to color my robot hand with white color(RGB: 0.9 0.9 0.9). When I use glColor3f, the white color display correctly. But when I store RGB data in GLfloat array and call glColorPointer, the color becomes so weird.
Here is code of right arm
static const GLfloat playerBodyColor[] = {0.9, 0.9, 0.9};
glEnableClientState(GL_COLOR_ARRAY);
//glColor3f(0.9, 0.9, 0.9);
glColorPointer(3, GL_FLOAT, 0, playerBodyColor);
glTranslatef(0.0, player_body_height/6, player_body_width/2+LENGTH_UNIT/2);
glRotatef(340.0, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(2*LENGTH_UNIT, 0.0, 0.0);
glScalef (4.0, 1.0, 1.0);
copyCodeSolidCube (LENGTH_UNIT);
glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
If I change code like this:
static const GLfloat playerBodyColor[] = {0.9, 0.9, 0.9};
//glEnableClientState(GL_COLOR_ARRAY);
glColor3f(0.9, 0.9, 0.9);
//glColorPointer(3, GL_FLOAT, 0, playerBodyColor);
glTranslatef(0.0, player_body_height/6, player_body_width/2+LENGTH_UNIT/2);
glRotatef(340.0, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(2*LENGTH_UNIT, 0.0, 0.0);
glScalef (4.0, 1.0, 1.0);
copyCodeSolidCube (LENGTH_UNIT);
//glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
The color shows correct.
The picture of correct-color code:
http://imgur.com/S6zt5wf
The picture of wrong-color code:
http://imgur.com/8m5Ag1O
In the code, I also called a function copyCodeSolidCube.
Here is part of copyCodeSolidCube code:
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_NORMAL_ARRAY);
glVertexPointer (3, GL_FLOAT, 0, vert);
glNormalPointer (GL_FLOAT, 0, norm);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, one);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, two);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, three);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, four);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, five);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, six);
glDisableClientState (GL_VERTEX_ARRAY);
glDisableClientState (GL_NORMAL_ARRAY);
Where is the problem?
With glColorPointer(), you specify an array of colors, one for each vertex. For example, if your cube has 8 vertices, you need an array of 8 colors. Since you have 3 components per color, the total size of the array you need is 24 floats.
Or, since you currently have 4 vertices per draw call, you need 4 colors in the color array, for 4 * 3 = 12 float values.
The big advantage of using this is of course that you can color each vertex individually. If you really want the same color for each vertex, glColor3f() was actually the right call to use. Or you can use a slight variation:
glColor3fv(playerBodyColor);
This is equivalent to the glColor3f() call you had, except that it's more convenient if you already have the color in an array of 3 values.
I have been playing around with OpenGL for a while now and wanted to get familiar with Vertex Array Objects(VAO's). I am using the cube example here to test with. First of all I verified that I could draw a cube by manually binding the two Vertex Buffers and Element Array Buffer and Vertex Attribs for each call of the render function Here is my code
void Project::initTest(void)
{
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,
};
glGenBuffers(1, &vbo[0]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
GLfloat cube_colors[] = {
// front colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
// back colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
};
glGenBuffers(1, &vbo[1]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_colors), cube_colors, GL_STATIC_DRAW);
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,
};
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
glBindBuffer(GL_ARRAY_BUFFER,0);
}
And here is the draw function
void Project::drawTest()
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glVertexAttribPointer(0, 3,GL_FLOAT,GL_FALSE,0,0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 1);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
}
The result is a cube as expected.
Using Vertex Array Object
This is my implementation of the buffers being packed into a VAO. The initTest() with the following code:
void Project::initTest()
{
//Upload Vertex Data into VBO's
//Upload Index Data into Element Array Buffer
//...
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);//Position Buffer
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);//Colour Buffer
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
glBindVertexArray(0);
//Unbind ARRAY and ELEMENT_ARRAY Buffers like before
}
Then in my draw function I just call the VAO like this
void Project::drawDebug()
{
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES,36, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);
}
However the result is this:
Ultimately, I would like to know where I am going wrong with this. I also would like to know if you bind an ELEMENT_ARRAY_BUFFER within the currently bound VAO, do you need to explicity rebind that ELEMENT_ARRAY_BUFFER before calling glDraw* or does the VAO detect that it has an index buffer and renders using indices.
It is going wrong here:
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);//Position Buffer
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);//Colour Buffer
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
This has nothing to do with VAOs at all, and would fail without VAOs the same way.
When switching to VAOs, you also seem to have switched from using two separate VBOs for your attributes. You can of course do that, and you could have done it that way without VAOs. But you have to do it correctly: glVertexAttribPointer() will make the currently bound GL_ARRAY_BUFFER part of the attrib pointer, which is vbo[1] in both cases. So you are using the color data as color and position.
However, your original code was also wrong: It did only use the postion data as color and position (which together with the clamping of the color values to the range [0,1] resulted in that asymmetrical coloring you get).
As I side note: whenever you see two GL binding operations for the same binding target right after another, the first one is always useless.
So the correct code would be (if you are acutally using two different VBOs now, which is unclear):
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);//Position Buffer
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);//Colour Buffer
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I wrote the code below in C++ using Opengl library, it gives me a simple triangle on the screen (2d)
how can i change the code in the render function to write a 3d box or primitive object?
the code is below:
#include <iostream>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
void render(void);
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);
glutCreateWindow("Welcome");
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(0.0,1.0,0.0);
glVertex2f(-0.5, -0.5);
glColor3f(1.0,0.0,0.0);
glVertex2f(0.5,0.0);
glColor3f(0.0,0.0,1.0);
glVertex2f(0.0,0.5);
glEnd();
glutSwapBuffers();
}
As a follow-up to Denis's answer...
If you are willing to learn a bit more OpenGL (perhaps a tall order if you just want to draw a cube...) here is the code that I reuse when doing small demos. It uses vertex array+buffer objects and creates a textured rectangular prism centered at (0, 0, 0). You'll need to provide your own texture.
The code to generate the rectangular prism is:
void GLmakeRect(GLuint vao, GLuint vbo[2], float w, float h, float d, GLuint storageHint) {
/*
8|---|9
0|BAC|1
10|---|---|---|12
|LEF|TOP|RIG|
11|---|---|---|13
3|FRO|2
4|---|5
|BOT|
6|---|7
*/
float verts[] = {
-0.5*w, 0.5*h, -0.5*d, 1.0/3.0, 0.25,
0.5*w, 0.5*h, -0.5*d, 2.0/3.0, 0.25,
0.5*w, 0.5*h, 0.5*d, 2.0/3.0, 0.5,
-0.5*w, 0.5*h, 0.5*d, 1.0/3.0, 0.5,
-0.5*w, -0.5*h, 0.5*d, 1.0/3.0, 0.75,
0.5*w, -0.5*h, 0.5*d, 2.0/3.0, 0.75,
-0.5*w, -0.5*h, -0.5*d, 1.0/3.0, 1.0,
0.5*w, -0.5*h, -0.5*d, 2.0/3.0, 1.0,
-0.5*w, -0.5*h, -0.5*d, 1.0/3.0, 0.0,
0.5*w, -0.5*h, -0.5*d, 2.0/3.0, 0.0,
-0.5*w, -0.5*h, -0.5*d, 0.0, 0.25,
-0.5*w, -0.5*h, 0.5*d, 0.0, 0.5,
0.5*w, -0.5*h, -0.5*d, 1.0, 0.25,
0.5*w, -0.5*h, 0.5*d, 1.0, 0.5
};
int polys[] = {
0, 3, 1, 1, 3, 2, // TOP
3, 4, 2, 2, 4, 5, // FRONT
4, 6, 5, 5, 6, 7, // BOTTOM
8, 0, 9, 9, 0, 1, // BACK
10, 11, 0, 0, 11, 3, // LEFT
1, 2, 12, 12, 2, 13 // RIGHT
};
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts[0], storageHint);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(polys), &polys[0], storageHint);
}
You can call the function like this during initialization:
GLuint vao, vbo[2]
glGenVertexArrays(1, &vao);
glGenBuffers(2, &vbo[0]);
GLmakeRect(vao, vbo, 1, 1, 1, GL_STATIC_DRAW);
To draw the cube in your render loop, do:
glBindTexture(GL_TEXTURE_2D, yourTextureHandleHere);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
If you just like to display a simple cube, take a look at "glutSolidCube" (and, maybe, the other "glutSolidABC" functions): glutSolidCube
But, when displaying 3d objects, you have to setup a projection matrix, a world matrix (transformation of the "viewer" or "camera") and, if you like to transform your objects, a model matrix for each object.
Some good kickstart lessons can be found at lighthouse3d.com in the "tutorials -> glut" section.
If you like to create your own meshes (a cube, sphere, etc) you have to write a mesh or surface class, which contains vertices and indices. When a rendercall appears, this class has to draw all of its vertices and triangles - your cube / sphere / etc.