got minimal vertex shader working.

This commit is contained in:
Andreas Ortmann 2016-09-28 13:36:22 +02:00
parent 92c27a1ef8
commit 85b8139872
3 changed files with 93 additions and 20 deletions

View file

@ -14,19 +14,27 @@ class PolygonModel {
public:
PolygonModel(const std::string &filename) : m_filename(filename)
{
if (!import()) {
m_loaded = false;
clear();
}
} else {
m_loaded = true;
}
void clear()
{
// TODO: delete buffers if there's data
m_loaded_from_file = false;
m_loaded_to_opengl = false;
m_data_position.clear();
m_data_normal.clear();
m_vbo_id_position = 0;
m_vbo_id_normal = 0;
m_numVertices = 0;
}
bool import()
{
m_vbo_id_position = 0;
m_vbo_id_normal = 0;
m_numVertices = 0;
clear();
// Create an instance of the Importer class
Assimp::Importer importer;
@ -51,12 +59,21 @@ class PolygonModel {
// Now we can access the file's contents.
copyVertices(scene);
m_loaded_from_file = true;
// We're done. Everything will be cleaned up by the importer destructor
return true;
}
bool loadIntoOpenGl()
bool uploadToOpenGl()
{
if (!m_loaded_from_file) {
std::cerr<<"[polygonmodel] warning: try to upload model data "
<< "to OpenGL but no data is loaded!" << std::endl;
return false;
}
glEnableVertexAttribArray(0);
glGenBuffers(1, &m_vbo_id_position); // Generate buffer
@ -97,11 +114,21 @@ class PolygonModel {
model->normals, // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
#endif
m_loaded_to_opengl = true;
return true;
}
bool bind()
{
// TODO: check whether this vbo is bound!
if (!m_loaded_to_opengl) {
std::cout<<"[polygonmodel] warning: try to bind model vbo "
<< "which was not uploaded to OpenGL!" << std::endl;
return false;
}
// bind position vbo
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position);
@ -116,12 +143,20 @@ class PolygonModel {
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
#endif
m_binding_active = true;
return true;
}
void render()
bool render()
{
if (!m_binding_active || !m_loaded_to_opengl) {
std::cout<<"[polygonmodel] warning: try to render model without bind()" << std::endl;
return false;
}
glDrawArrays(GL_TRIANGLES, 0, m_numVertices);
return true;
}
private:
@ -171,11 +206,12 @@ class PolygonModel {
}
const std::string &filename() const { return m_filename; }
bool ready() const { return m_loaded; }
private:
std::string m_filename;
bool m_loaded;
bool m_loaded_from_file;
bool m_loaded_to_opengl;
bool m_binding_active;
// both will hold 3 * numVertices floats
std::vector<float> m_data_position;

View file

@ -9,18 +9,52 @@ namespace endofthejedi {
{
std::cout<<"setup 3d" << std::endl;
m_atomicBomb = new PolygonModel("../data/mesh/small_atomic_bomb.stl");
m_atomicBomb->loadIntoOpenGl();
//m_atomicBomb = new PolygonModel("../data/mesh/small_atomic_bomb.stl");
m_atomicBomb = new PolygonModel("../data/mesh/quad_screen_coords.stl");
m_atomicBomb->import();
m_atomicBomb->uploadToOpenGl();
m_models.push_back(m_atomicBomb);
std::string vss =
"varying vec3 vertex;\n"
"void main()\n"
"{\n"
" gl_Position = gl_Vertex;\n"
" vertex = gl_Vertex.xyz;\n"
"}\n"
;
std::string fss =
"varying vec3 vertex;\n"
"void main()\n"
"{\n"
//" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
" gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0);\n"
"}\n"
;
m_shader.init();
m_shader.load(vss.c_str(), GL_VERTEX_SHADER);
m_shader.load(fss.c_str(), GL_FRAGMENT_SHADER);
}
void RendererPolygon3d::render(const game::State *state)
{
m_shader.bind();
(void) state;
m_atomicBomb->bind();
m_atomicBomb->render();
//m_atomicBomb->bind();
//m_atomicBomb->render();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
glVertex2f(1.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();
//std::cout<<"render 3d" << std::endl;
}

View file

@ -17,15 +17,18 @@ class PolygonModel;
namespace endofthejedi {
class RendererPolygon3d : public Renderer {
private:
protected:
public:
void setup();
void render(const game::State *state) override;
private:
std::vector<PolygonModel*> m_models;
// all models are also here (for easy reloading etc.)
std::vector<PolygonModel*> m_models;
PolygonModel *m_atomicBomb;
// and with a specific variable name here
PolygonModel *m_atomicBomb;
// for rendering everything
Shader m_shader;
};
}