PDA

View Full Version : Drawing cube using vbo



Romulo
09-22-2014, 05:17 PM
Hi all
im´try to draw a cube using vertex buffer object and vertex array object

this is my code that contains the vbo class with draws a cube

the problem is that noting is draw on the screen!
Can you guys see what is wrong with my code?
Thanks!




#include "Vertex_buffer_object.h"


#include <cstring>
#include <iostream>

Vertex_buffer_object::Vertex_buffer_object()
{
initVB();
}


Vertex_buffer_object::~Vertex_buffer_object() {}

void Vertex_buffer_object::draw()
{
glPushMatrix();

glBindVertexArray(_vao);

glBindBuffer(GL_ARRAY_BUFFER, _positionVBO);
glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
glBindBuffer(GL_ARRAY_BUFFER, _colorVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);

//Enable vertex,normals,colors and indices state.
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, 0);

glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, NULL);

glEnableClientState(GL_ELEMENT_ARRAY_BUFFER);

glDrawElements(GL_QUADS, 6, GL_UNSIGNED_INT, 0);

glBindVertexArray(0);

//desabilitando modos de desenho.
glDisableClientState(GL_ELEMENT_ARRAY_BUFFER);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

//desabilitando vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

void Vertex_buffer_object::initVB()
{
if (!glGenBuffers || !glBindBuffer || !glBufferData)
{
std::cerr << "VBOs are not supported by your graphics card" << std::endl;
}


// Variables for the cube data
float _vertex[] = {((-1.0f, -1.0f, 1.0f),
( 1.0f, -1.0f, 1.0f),
( 1.0f, 1.0f, 1.0f),
(-1.0f, 1.0f, 1.0f),
(-1.0f, -1.0f, -1.0f),
(1.0f, -1.0f, -1.0f),
(1.0f, 1.0f, -1.0f),
(-1.0f, 1.0f, -1.0f))
};

float _normal[] = {((-1.0f, -1.0f, 1.0f),
( 1.0f, -1.0f, 1.0f),
( 1.0f, 1.0f, 1.0f),
(-1.0f, 1.0f, 1.0f),
(-1.0f, -1.0f, -1.0f),
(1.0f, -1.0f, -1.0f),
(1.0f, 1.0f, -1.0f),
(-1.0f, 1.0f, -1.0f))
};

static const GLsizeiptr ColorSize = 8 * 3 * sizeof(GLubyte);
static const GLubyte _color[] =
{
255, 0, 0,
255, 255, 0,
0, 255, 0,
0, 255, 0,
0, 0, 255,
255, 0, 0,
255, 255, 0,
0, 255, 0
};
// Element Indicies for the Cube
unsigned int _index[] =
{
0, 1, 2, 2, 3, 0,
3, 2, 6, 6, 7, 3,
7, 6, 5, 5, 4, 7,
4, 0, 3, 3, 7, 4,
0, 1, 5, 5, 4, 0,
1, 5, 6, 6, 2, 1
};

const unsigned _vertexSize = 24;

glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);

//gerando a vbo para as posições
glGenBuffers(1, &_positionVBO);
glBindBuffer(GL_ARRAY_BUFFER, _positionVBO);
glBufferData(GL_ARRAY_BUFFER, _vertexSize * sizeof(float), NULL, GL_STATIC_DRAW);
void* positionPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(positionPtr, (float*) _vertex, 24 * sizeof(float));
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para as normais
glGenBuffers(1, &_normalVBO);
glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float) , NULL, GL_STATIC_DRAW);
void* normalPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(normalPtr, (float*) _normal, 24 * sizeof(float));
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para as cores
glGenBuffers(1, &_colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, _colorVBO);
glBufferData(GL_ARRAY_BUFFER, ColorSize , NULL, GL_STATIC_DRAW);
void* colorPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(colorPtr, (GLubyte*) _color, ColorSize);
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para os indices
glGenBuffers(1, &_indexVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(unsigned int), NULL, GL_STATIC_DRAW);

//setando os dados para index
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
void* indexPtr = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(indexPtr,(unsigned int*) _index, 36 * sizeof(unsigned int));
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glBindVertexArray(0);
}

Romulo
09-23-2014, 07:25 AM
this is the class that actually calls the draw of the vbo class



#include "sg3d.h"


#include <SDL/SDL.h>
#include "gl_util.h"
#include "immediate.h"
#include "DisplayList.h"
#include "vertex_buffer_object.h"
#include "loader.h"
#include "Texture.h"
#include <iostream>

namespace topicos
{
SG3D::SG3D()
{
_end = false;
_rotation = 0;
start();
}

SG3D::~SG3D()
{
for (unsigned i = 0; i < _sceneObject.size(); ++i)
{
delete _sceneObject[i];
}
}

void SG3D::start()
{
unsigned w, h;
w = 800;
h = 600;
_sdl.videoMode(w, h);
GLUtil::initGL();
GLUtil::setView(w, h);
GLUtil::initGlew();
initObjects();
initLight();
// initShader();
mainLoop();


}
void SG3D::initShader()
{
Shader *sh = new Shader();
sh->makeFragmentShader("build\\shader\\noShader.frag");
sh->makeVertexShader("build\\shader\\noShader.vert");
sh->ativarShader();

}
void SG3D::initLight()
{
//posicao da luz. Um vetor apontando para baixo.
float light_position[] = { 1.f, 1.f, 1.f, 0.f};

//cor ambiente da luz zero
float light_ambient[] = {.0f, .0f, .0f, 1.f};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
//cor difusa da luz zero
float light_diffuse[] = { .9f, 1.f, .9f, 1.f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
//cor especular da luz zero
float light_specular[] = { 1.f, 1.f, 1.f, 1.f};
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
//posicao da luz. Se w = 0 -> luz direcional; se w = 1 -> luz pontual.
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

float mat_specular[] = {1.f, 1.f, 1.f, 1.f};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
float shininess[] = {50.f};

glMaterialfv(GL_FRONT, GL_SHININESS, shininess);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
}

/**
* Inicia aqui os diferentes objetos
**/
void SG3D::initObjects()
{

std::cout << "creating VBO mode" << std::endl;
Vertex_buffer_object* vbo = new Vertex_buffer_object();
_sceneObject.push_back(vbo);
std::cout << "\t\t\tend." << std::endl;
}

/**
* Para evitar que o sistema de eventos
* fique complicado ele nao vai ficar na classe MediaControl,
* mas deveria ficar lah. Assim evitaria o include de SDL
* nesta classe.
**/
void SG3D::mainLoop()
{
SDL_Event e;
while(!_end)
{
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
_end = true;
break;
case SDL_KEYDOWN:
switch(e.key.keysym.sym)
{
case SDLK_ESCAPE:
_end = true;
break;
} //END keydow check
break;
default:
break;
} //END type check
}//END while poll
update();
draw();
}//END while end
_sdl.quit();
}

/**
* Funcao responsavel por atualizacoes.
*
**/
void SG3D::update()
{
//Esse update eh para bonito. Nao seria legal ficar fazendo esse tipo de verificacao dentro do update.
if (_sceneObject.size() == 0)
{
std::cout << "Voce nao pode acessar o elemento zero. O tamanho da sua lista eh zero" << std::endl;
}
else
{
Vertex_buffer_object* obj = _sceneObject[0]; //Pegando o elemento zero (Erro caso não tenhamos objetos)
// obj->rotate((_rotation++), Vec3(1, 0, 0)); //Rodando o elemento zero.
}
}


/**
* Funcao responsavel por chamar a lista de objetos que serao desenhados.
*
**/
void SG3D::draw()
{
GLUtil::resetDraw();

for (std::vector<Vertex_buffer_object*>::iterator it = _sceneObject.begin(); it != _sceneObject.end(); ++it)
{
(*it)->draw();
}

/* ///drawing a simple cube with texture
Texture *text = new Texture();
unsigned int i = text->geraTextureDePNG("data/test.png");
*/
_sdl.swap();
}

}

carsten neumann
09-23-2014, 07:35 AM
It looks like you are using shaders, is there a specific reason why you are also using a lot of the non-programmable functionality (glLight*, glMaterial*, etc.)?

There is a problem with your buffer setup:


glBindBuffer(GL_ARRAY_BUFFER, _positionVBO);
glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
glBindBuffer(GL_ARRAY_BUFFER, _colorVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);

//Enable vertex,normals,colors and indices state.
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, 0);

glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, NULL);


There is only one GL_ARRAY_BUFFER bind point, that is there can at most one buffer be bound as your array buffer at any given time. The way to use it is to bind the buffer you want to work with, call functions that consider the currently bound buffer (glVertexPointer/glNormalPointer/... in your case), then bind the next buffer:



// bind buffer storing positions
glBindBuffer(GL_ARRAY_BUFFER, _positionVBO);
glEnableClientState(GL_VERTEX_ARRAY);
// take positions from buffer bound to GL_ARRAY_BUFFER - this command 'remembers' which buffer is bound to GL_ARRAY_BUFFER at the time of the call
glVertexPointer(3, GL_FLOAT, 0, 0);

// bind buffer storing normals - this has no effect on which buffer the positions are taken from
glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, 0);

// bind buffer storing colors
glBindBuffer(GL_ARRAY_BUFFER, _colorVBO);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, NULL);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);

Romulo
09-23-2014, 10:07 AM
Thank you so much for your answer.
The non-programmable functionality (glLight*, glMaterial*, etc.) that you mentioned im not using for now,this is for another project. I'm doing a college work and the first step is making a cube using a vbo.
I´m update the code but still not work,nothing is draw.



#include "Vertex_buffer_object.h"


#include <cstring>
#include <iostream>

Vertex_buffer_object::Vertex_buffer_object()
{
initVB();
}

Vertex_buffer_object::~Vertex_buffer_object() {}

void Vertex_buffer_object::draw()
{
glPushMatrix();

glBindVertexArray(_vao);

// bind buffer storing positions
glBindBuffer(GL_ARRAY_BUFFER, _positionVBO);
glEnableClientState(GL_VERTEX_ARRAY);

// take positions from buffer bound to GL_ARRAY_BUFFER
// this command 'remembers' which buffer is bound to
// GL_ARRAY_BUFFER at the time of the call
glVertexPointer(3, GL_FLOAT, 0, 0);

// bind buffer storing normals
// this has no effect on which buffer the positions are taken from
glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, 0);

// bind buffer storing colors
glBindBuffer(GL_ARRAY_BUFFER, _colorVBO);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, NULL);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);

glEnableClientState(GL_ELEMENT_ARRAY_BUFFER);

glDrawElements(GL_QUADS, 6, GL_UNSIGNED_INT, 0);

glBindVertexArray(0);

//desabilitando modos de desenho.
glDisableClientState(GL_ELEMENT_ARRAY_BUFFER);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

//desabilitando vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

void Vertex_buffer_object::initVB()
{
if (!glGenBuffers || !glBindBuffer || !glBufferData)
{
std::cerr << "VBOs are not supported by your graphics card" << std::endl;
}


// Variables for the cube data
float _vertex[] = {((-1.0f, -1.0f, 1.0f),
( 1.0f, -1.0f, 1.0f),
( 1.0f, 1.0f, 1.0f),
(-1.0f, 1.0f, 1.0f),
(-1.0f, -1.0f, -1.0f),
(1.0f, -1.0f, -1.0f),
(1.0f, 1.0f, -1.0f),
(-1.0f, 1.0f, -1.0f))
};

float _normal[] = {((-1.0f, -1.0f, 1.0f),
( 1.0f, -1.0f, 1.0f),
( 1.0f, 1.0f, 1.0f),
(-1.0f, 1.0f, 1.0f),
(-1.0f, -1.0f, -1.0f),
(1.0f, -1.0f, -1.0f),
(1.0f, 1.0f, -1.0f),
(-1.0f, 1.0f, -1.0f))
};

static const GLsizeiptr ColorSize = 8 * 3 * sizeof(GLubyte);
static const GLubyte _color[] =
{
255, 0, 0,
255, 255, 0,
0, 255, 0,
0, 255, 0,
0, 0, 255,
255, 0, 0,
255, 255, 0,
0, 255, 0
};
// Element Indicies for the Cube
unsigned int _index[] =
{
0, 1, 2, 2, 3, 0,
3, 2, 6, 6, 7, 3,
7, 6, 5, 5, 4, 7,
4, 0, 3, 3, 7, 4,
0, 1, 5, 5, 4, 0,
1, 5, 6, 6, 2, 1
};

const unsigned _vertexSize = 24;

glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);

//gerando a vbo para as posições
glGenBuffers(1, &_positionVBO);
glBindBuffer(GL_ARRAY_BUFFER, _positionVBO);
glBufferData(GL_ARRAY_BUFFER, _vertexSize * sizeof(float), NULL, GL_STATIC_DRAW);
void* positionPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(positionPtr, (float*) _vertex, 24 * sizeof(float));
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para as normais
glGenBuffers(1, &_normalVBO);
glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float) , NULL, GL_STATIC_DRAW);
void* normalPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(normalPtr, (float*) _normal, 24 * sizeof(float));
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para as cores
glGenBuffers(1, &_colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, _colorVBO);
glBufferData(GL_ARRAY_BUFFER, ColorSize , NULL, GL_STATIC_DRAW);
void* colorPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(colorPtr, (GLubyte*) _color, ColorSize);
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para os indices
glGenBuffers(1, &_indexVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(unsigned int), NULL, GL_STATIC_DRAW);

//setando os dados para index
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
void* indexPtr = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(indexPtr,(unsigned int*) _index, 36 * sizeof(unsigned int));
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glBindVertexArray(0);
}

carsten neumann
09-23-2014, 10:18 AM
Sorry, nothing else jumps out at me as being wrong. Are you checking for errors with glGetError? You could try to run this in a debug OpenGL context or under a GL debugger to see if that flags anything. Finally there is the always fun (;)) problem of drawing outside your camera's field of view, i.e. are you sure you are not drawing behind your camera?

Romulo
09-23-2014, 10:34 AM
well how could i know if i´m draw behind my camera?
Sorry i´m totally noob in open gl

before i was rendering some models in obj all appeared in front of the screen so i think this is not the problem


that is the class that init the context



#include "gl_util.h"

#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <iostream>

namespace topicos
{

GLUtil::GLUtil() {}
GLUtil::~GLUtil() {}

void GLUtil::initGlew()
{
GLenum err = glewInit();
if (GLEW_OK != err)
{
std::cout << "glew erro!" << std::endl;
}
else
{
std::cout << "glew working." << std::endl;
}
}


void GLUtil::initGL()
{
glClearColor(0.f, 0.f, 0.f, 0.f);
glClearDepth(1.f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void GLUtil::setView(unsigned w, unsigned h)
{
float ratio = ((float) w / (float) h);

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.f, ratio, .1f, 100.f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void GLUtil::resetDraw()
{
glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.f, 0.f, -30.f);
glColor3f( 1.f, 1.f, 1.f);
}

}//END namespace topicos

Romulo
09-23-2014, 11:28 AM
So i updated my sg3d class to show some errors, that you told me.
but nothing is shown on the screen :(



#include "sg3d.h"


#include <SDL/SDL.h>
#include "gl_util.h"
#include "immediate.h"
#include "DisplayList.h"
#include "vertex_buffer_object.h"
#include "loader.h"
#include "Texture.h"
#include <iostream>

namespace topicos
{
SG3D::SG3D()
{
_end = false;
_rotation = 0;
start();
}

SG3D::~SG3D()
{
for (unsigned i = 0; i < _sceneObject.size(); ++i)
{
delete _sceneObject[i];
}
}

void SG3D::start()
{
unsigned w, h;
w = 800;
h = 600;
_sdl.videoMode(w, h);
GLUtil::initGL();
GLUtil::setView(w, h);
GLUtil::initGlew();
initObjects();
//initLight();
// initShader();
mainLoop();


}
void SG3D::checkErrors()
{
freopen( "CON", "wt", stdout );
freopen( "CON", "wt", stderr );
GLenum err;
while ( ( err = glGetError() ) != GL_NO_ERROR)
{
std::cerr << err;
}
}

void SG3D::initShader()
{
Shader *sh = new Shader();
sh->makeFragmentShader("build\\shader\\noShader.frag");
sh->makeVertexShader("build\\shader\\noShader.vert");
sh->ativarShader();

}
void SG3D::initLight()
{
//posicao da luz. Um vetor apontando para baixo.
float light_position[] = { 1.f, 1.f, 1.f, 0.f};

//cor ambiente da luz zero
float light_ambient[] = {.0f, .0f, .0f, 1.f};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
//cor difusa da luz zero
float light_diffuse[] = { .9f, 1.f, .9f, 1.f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
//cor especular da luz zero
float light_specular[] = { 1.f, 1.f, 1.f, 1.f};
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
//posicao da luz. Se w = 0 -> luz direcional; se w = 1 -> luz pontual.
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

float mat_specular[] = {1.f, 1.f, 1.f, 1.f};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
float shininess[] = {50.f};

glMaterialfv(GL_FRONT, GL_SHININESS, shininess);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
}

/**
* Inicia aqui os diferentes objetos
**/
void SG3D::initObjects()
{

std::cout << "creating VBO mode" << std::endl;
Vertex_buffer_object* vbo = new Vertex_buffer_object();
_sceneObject.push_back(vbo);
std::cout << "\t\t\tend." << std::endl;
}

/**
* Para evitar que o sistema de eventos
* fique complicado ele nao vai ficar na classe MediaControl,
* mas deveria ficar lah. Assim evitaria o include de SDL
* nesta classe.
**/
void SG3D::mainLoop()
{
SDL_Event e;
while(!_end)
{
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
_end = true;
break;
case SDL_KEYDOWN:
switch(e.key.keysym.sym)
{
case SDLK_ESCAPE:
_end = true;
break;
} //END keydow check
break;
default:
break;
} //END type check
}//END while poll
update();
draw();
}//END while end
_sdl.quit();
}

/**
* Funcao responsavel por atualizacoes.
*
**/
void SG3D::update()
{
//Esse update eh para bonito. Nao seria legal ficar fazendo esse tipo de verificacao dentro do update.
if (_sceneObject.size() == 0)
{
std::cout << "Voce nao pode acessar o elemento zero. O tamanho da sua lista eh zero" << std::endl;
}
else
{
Vertex_buffer_object* obj = _sceneObject[0]; //Pegando o elemento zero (Erro caso não tenhamos objetos)
// obj->rotate((_rotation++), Vec3(1, 0, 0)); //Rodando o elemento zero.
}
}


/**
* Funcao responsavel por chamar a lista de objetos que serao desenhados.
*
**/
void SG3D::draw()
{
GLUtil::resetDraw();

for (std::vector<Vertex_buffer_object*>::iterator it = _sceneObject.begin(); it != _sceneObject.end(); ++it)
{
(*it)->draw();
}

/* ///drawing a simple cube with texture
Texture *text = new Texture();
unsigned int i = text->geraTextureDePNG("data/test.png");
*/
_sdl.swap();
}

}

Romulo
09-23-2014, 11:40 AM
looks like an unexpected thing occur when i hit start debug in codeblocks:
http://postimg.org/image/w4i4pllsd/
Is a triangle!
Why triangle if im try to render a cube?
Am I drawing wrong?


glDrawElements(GL_QUADS, 6, GL_UNSIGNED_INT, 0);

If its a cube then is 6 elements right?

carsten neumann
09-23-2014, 12:06 PM
No, the second parameter to glDrawElements is the number of indices to use. Each quad requires 4 indices to specify, so for the six sides of the cube you need a total of 24 vertices.

Romulo
09-23-2014, 12:23 PM
Ok so by your thinking it shows like this
24 indices

glDrawElements(GL_QUAD_STRIP, 24, GL_UNSIGNED_INT, 0); // not work
i have 24 vertices
but i not have 24 indices but 36 indices
unsigned int _index[] =
{
0, 1, 2, 2, 3, 0,
3, 2, 6, 6, 7, 3,
7, 6, 5, 5, 4, 7,
4, 0, 3, 3, 7, 4,
0, 1, 5, 5, 4, 0,
1, 5, 6, 6, 2, 1
};

take this indices in a tutorial

carsten neumann
09-23-2014, 01:03 PM
But you've also changed the primitive type to GL_QUAD_STRIP! Those work differently, the first 4 indices specify the first quad and every 2 additional indices specify the next one - also it's not possible to draw a cube with a single strip of quads (unless you introduce degenerate quads) you need at least 2 strips.
Those indices look to me like they might be supposed to be used with GL_TRIANGLES as primitive type.

May I gently suggest you re-read parts of that tutorial again so that you have a better understanding how primitives are specified - I think it will be very frustrating for you to continue just making (semi-) random changes like that. Of course for specific questions there's always this forum too.

Romulo
09-23-2014, 01:36 PM
so finally something is drawing on the screen, but only the front face. I'm tried to rotate but still show me only the front face



#include "Vertex_buffer_object.h"


#include <cstring>
#include <iostream>

Vertex_buffer_object::Vertex_buffer_object()
{
initVB();
}

Vertex_buffer_object::~Vertex_buffer_object() {}

void Vertex_buffer_object::draw()
{
glEnable(GL_DEPTH_TEST);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glPushMatrix();

glBindVertexArray(_vao);

// bind buffer storing positions
glBindBuffer(GL_ARRAY_BUFFER, _positionVBO);
glEnableClientState(GL_VERTEX_ARRAY);

// take positions from buffer bound to GL_ARRAY_BUFFER
// this command 'remembers' which buffer is bound to
// GL_ARRAY_BUFFER at the time of the call
glVertexPointer(3, GL_FLOAT, 0, 0);

// bind buffer storing normals
// this has no effect on which buffer the positions are taken from
glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, 0);

// bind buffer storing colors
glBindBuffer(GL_ARRAY_BUFFER, _colorVBO);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, NULL);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);

glEnableClientState(GL_ELEMENT_ARRAY_BUFFER);

glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);

glBindVertexArray(0);

//desabilitando modos de desenho.
glDisableClientState(GL_ELEMENT_ARRAY_BUFFER);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

//desabilitando vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

void Vertex_buffer_object::initVB()
{
if (!glGenBuffers || !glBindBuffer || !glBufferData)
{
std::cerr << "VBOs are not supported by your graphics card" << std::endl;
}


// Variables for the cube data
float _vertex[] =
{
// 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,
};

float _normal[] = {((-1.0f, -1.0f, 1.0f),
( 1.0f, -1.0f, 1.0f),
( 1.0f, 1.0f, 1.0f),
(-1.0f, 1.0f, 1.0f),
(-1.0f, -1.0f, -1.0f),
(1.0f, -1.0f, -1.0f),
(1.0f, 1.0f, -1.0f),
(-1.0f, 1.0f, -1.0f))
};

static const GLsizeiptr ColorSize = 8 * 3 * sizeof(GLubyte);
static const GLubyte _color[] =
{
255, 0, 0,
255, 255, 0,
0, 255, 0,
0, 255, 0,
0, 0, 255,
255, 0, 0,
255, 255, 0,
0, 255, 0
};
// Element Indicies for the Cube
unsigned int _index[] =
{
// 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,
};

const unsigned _vertexSize = 24;

glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);

//gerando a vbo para as posições
glGenBuffers(1, &_positionVBO);
glBindBuffer(GL_ARRAY_BUFFER, _positionVBO);
glBufferData(GL_ARRAY_BUFFER, _vertexSize * sizeof(float), NULL, GL_STATIC_DRAW);
void* positionPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(positionPtr, (float*) _vertex, 24 * sizeof(float));
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para as normais
glGenBuffers(1, &_normalVBO);
glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float) , NULL, GL_STATIC_DRAW);
void* normalPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(normalPtr, (float*) _normal, 24 * sizeof(float));
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para as cores
glGenBuffers(1, &_colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, _colorVBO);
glBufferData(GL_ARRAY_BUFFER, ColorSize , NULL, GL_STATIC_DRAW);
void* colorPtr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(colorPtr, (GLubyte*) _color, ColorSize);
glUnmapBuffer(GL_ARRAY_BUFFER);

//gerando a vbo para os indices
glGenBuffers(1, &_indexVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(unsigned int), NULL, GL_STATIC_DRAW);

//setando os dados para index
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
void* indexPtr = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(indexPtr,(unsigned int*) _index, 36 * sizeof(unsigned int));
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glBindVertexArray(0);
}

carsten neumann
09-24-2014, 03:14 AM
Hmm, only thing I see is that your draw function has a glPushMatrix with no matching glPopMatrix, not sure why that would not affect the front face, but it'll likely mess things up when you move the camera around.

Romulo
09-24-2014, 07:37 AM
Well i put glPopMatrix here after glDrawElements but nothing changes




glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glPopMatrix();
glBindVertexArray(0);