jeudi 5 mars 2015

Objects placed inside classes to be rendered with OpenGL

I began to learn OpenGL about a week ago and I now want to create a mesh class. The code I'm about to show gives me a black screen (that's the color I fill it with).


I basically stripped the code from my main function and placed it inside a class, it worked inside main.


mesh.cpp



#include "mesh.h"

mesh::mesh(std::vector<GLfloat> vertices, std::vector<GLuint> triangles)
{
this->vertices = vertices;
this->triangles = triangles;
glGenVertexArrays(1, &this->vertexArrayObject);
glBindVertexArray(this->vertexArrayObject);

glGenBuffers(1, &this->vertexBuffer);
glGenBuffers(1, &this->triangleBuffer);

glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(GLfloat), this->vertices.data(), GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->triangleBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->triangles.size() * sizeof(GLfloat), this->triangles.data(), GL_STATIC_DRAW);

//ctor
}

mesh::~mesh()
{
glDeleteBuffers(1, &this->triangleBuffer);
glDeleteBuffers(1, &this->vertexBuffer);

glDeleteVertexArrays(1, &this->vertexArrayObject);
//dtor
}

void mesh::update(){
glBindVertexArray(this->vertexArrayObject);
glDrawElements(GL_TRIANGLES, this->vertices.size(), GL_UNSIGNED_INT, 0);
}


mesh.h



#ifndef MESH_H
#define MESH_H

#include <iostream>
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>

class mesh
{
public:
mesh(std::vector<GLfloat> vertices, std::vector<GLuint> triangles);
~mesh();
void update();
protected:
private:
GLuint vertexArrayObject, vertexBuffer, triangleBuffer;
std::vector<GLfloat> vertices;
std::vector<GLuint> triangles;
};

#endif // MESH_H

Aucun commentaire:

Enregistrer un commentaire