2016-09-28 09:50:35 +00:00
|
|
|
#include "renderer_polygon_3d.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
#include "glm/gtc/type_ptr.hpp"
|
|
|
|
|
2016-09-28 11:00:40 +00:00
|
|
|
#include "polygon_model.hpp"
|
|
|
|
|
2016-09-28 09:50:35 +00:00
|
|
|
namespace endofthejedi {
|
|
|
|
void RendererPolygon3d::setup()
|
|
|
|
{
|
2016-09-28 12:19:05 +00:00
|
|
|
std::cout<<"setup polygon 3d" << std::endl;
|
2016-09-28 11:00:40 +00:00
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
//m_missileModel = new PolygonModel("../data/mesh/quad_screen_coords.stl");
|
2016-09-28 11:16:11 +00:00
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
|
|
|
|
//addModel("../data/mesh/planet_128.stl", &m_planetModel);
|
|
|
|
addModel("../data/mesh/planet_12.stl", &m_planetModel);
|
2016-09-28 11:36:22 +00:00
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
std::string vss_simple =
|
|
|
|
"#version 120\n"
|
2016-09-28 11:36:22 +00:00
|
|
|
"varying vec3 vertex;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_Position = gl_Vertex;\n"
|
2016-09-28 12:19:05 +00:00
|
|
|
" vertex = gl_Position.xyz;\n"
|
2016-09-28 11:36:22 +00:00
|
|
|
"}\n"
|
|
|
|
;
|
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
std::string fss_simple =
|
|
|
|
"#version 120\n"
|
2016-09-28 11:36:22 +00:00
|
|
|
"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"
|
|
|
|
;
|
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
std::string vss_game_objects =
|
|
|
|
"#version 120\n"
|
|
|
|
"uniform vec3 position;\n"
|
|
|
|
"uniform float scale;\n"
|
|
|
|
"varying vec3 vertex;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec3 p = scale*gl_Vertex.xyz;\n"
|
|
|
|
" p += position;\n"
|
|
|
|
" gl_Position = vec4(p, gl_Vertex.w);\n"
|
|
|
|
" vertex = p;\n"
|
|
|
|
"}\n"
|
|
|
|
;
|
|
|
|
|
|
|
|
std::string fss_game_objects =
|
|
|
|
"#version 120\n"
|
|
|
|
"varying vec3 vertex;\n"
|
|
|
|
"uniform vec3 color;\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"
|
|
|
|
" gl_FragColor = vec4(color, 1.0);\n"
|
|
|
|
"}\n"
|
|
|
|
;
|
|
|
|
|
2016-09-28 11:36:22 +00:00
|
|
|
m_shader.init();
|
2016-09-28 12:19:05 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
(void) vss_game_objects;
|
|
|
|
(void) fss_game_objects;
|
|
|
|
m_shader.load(vss_simple.c_str(), GL_VERTEX_SHADER);
|
|
|
|
m_shader.load(fss_simple.c_str(), GL_FRAGMENT_SHADER);
|
|
|
|
#else
|
|
|
|
(void) vss_simple;
|
|
|
|
(void) fss_simple;
|
|
|
|
m_shader.load(vss_game_objects.c_str(), GL_VERTEX_SHADER);
|
|
|
|
m_shader.load(fss_game_objects.c_str(), GL_FRAGMENT_SHADER);
|
|
|
|
#endif
|
2016-09-28 09:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RendererPolygon3d::render(const game::State *state)
|
|
|
|
{
|
2016-09-28 11:36:22 +00:00
|
|
|
m_shader.bind();
|
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
renderPlanets(state);
|
2016-09-28 11:36:22 +00:00
|
|
|
|
2016-09-28 11:38:31 +00:00
|
|
|
//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();
|
2016-09-28 12:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RendererPolygon3d::renderPlanets(const game::State *state)
|
|
|
|
{
|
|
|
|
m_planetModel->bind();
|
|
|
|
|
|
|
|
for (const game::Planet *planet : state->planets) {
|
|
|
|
glm::vec3 c = glm::vec3(0.7, 0.2, 0.1);
|
|
|
|
const auto &p = planet->position;
|
|
|
|
|
|
|
|
glUniform3f(m_shader.location("position"), p.x, p.y, 0.0);
|
|
|
|
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
|
|
|
|
glUniform1f(m_shader.location("scale"), planet->radius);
|
|
|
|
|
|
|
|
m_planetModel->render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RendererPolygon3d::renderMissiles(const game::State *state)
|
|
|
|
{
|
|
|
|
(void) state;
|
|
|
|
//m_missileModel->bind();
|
|
|
|
// TODO
|
|
|
|
//m_missileModel->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RendererPolygon3d::addModel(const std::string &filename, PolygonModel **dest)
|
|
|
|
{
|
|
|
|
std::cout<<"adding a model: " << filename << std::endl;
|
|
|
|
|
|
|
|
*dest = new PolygonModel(filename);
|
|
|
|
if (!(*dest)->import()) {
|
|
|
|
std::cout<<"error: failed to load needed model!!!" << std::endl << std::endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*dest)->uploadToOpenGl();
|
2016-09-28 11:16:11 +00:00
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
m_models.push_back(*dest);
|
2016-09-28 09:50:35 +00:00
|
|
|
}
|
|
|
|
}
|