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: public:
PolygonModel(const std::string &filename) : m_filename(filename) PolygonModel(const std::string &filename) : m_filename(filename)
{ {
if (!import()) { clear();
m_loaded = false; }
} else { void clear()
m_loaded = true; {
} // 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() bool import()
{ {
m_vbo_id_position = 0; clear();
m_vbo_id_normal = 0;
m_numVertices = 0;
// Create an instance of the Importer class // Create an instance of the Importer class
Assimp::Importer importer; Assimp::Importer importer;
@ -51,12 +59,21 @@ class PolygonModel {
// Now we can access the file's contents. // Now we can access the file's contents.
copyVertices(scene); copyVertices(scene);
m_loaded_from_file = true;
// We're done. Everything will be cleaned up by the importer destructor // We're done. Everything will be cleaned up by the importer destructor
return true; 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); glEnableVertexAttribArray(0);
glGenBuffers(1, &m_vbo_id_position); // Generate buffer glGenBuffers(1, &m_vbo_id_position); // Generate buffer
@ -97,11 +114,21 @@ class PolygonModel {
model->normals, // Buffer data pointer model->normals, // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes; GL_STATIC_DRAW); // Usage - Data never changes;
#endif #endif
m_loaded_to_opengl = true;
return true; return true;
} }
bool bind() 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 // bind position vbo
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position); glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position);
@ -116,12 +143,20 @@ class PolygonModel {
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
#endif #endif
m_binding_active = true;
return 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); glDrawArrays(GL_TRIANGLES, 0, m_numVertices);
return true;
} }
private: private:
@ -171,11 +206,12 @@ class PolygonModel {
} }
const std::string &filename() const { return m_filename; } const std::string &filename() const { return m_filename; }
bool ready() const { return m_loaded; }
private: private:
std::string m_filename; 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 // both will hold 3 * numVertices floats
std::vector<float> m_data_position; std::vector<float> m_data_position;

View file

@ -9,18 +9,52 @@ namespace endofthejedi {
{ {
std::cout<<"setup 3d" << std::endl; std::cout<<"setup 3d" << std::endl;
m_atomicBomb = new PolygonModel("../data/mesh/small_atomic_bomb.stl"); //m_atomicBomb = new PolygonModel("../data/mesh/small_atomic_bomb.stl");
m_atomicBomb->loadIntoOpenGl(); m_atomicBomb = new PolygonModel("../data/mesh/quad_screen_coords.stl");
m_atomicBomb->import();
m_atomicBomb->uploadToOpenGl();
m_models.push_back(m_atomicBomb); 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) void RendererPolygon3d::render(const game::State *state)
{ {
m_shader.bind();
(void) state; (void) state;
m_atomicBomb->bind(); //m_atomicBomb->bind();
m_atomicBomb->render(); //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; //std::cout<<"render 3d" << std::endl;
} }

View file

@ -17,15 +17,18 @@ class PolygonModel;
namespace endofthejedi { namespace endofthejedi {
class RendererPolygon3d : public Renderer { class RendererPolygon3d : public Renderer {
private:
protected:
public: public:
void setup(); void setup();
void render(const game::State *state) override; void render(const game::State *state) override;
private: 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;
}; };
} }