KlassischeKeplerKriege/game/renderer_polygon_3d/renderer_polygon_3d.cpp
2016-09-28 15:22:16 +02:00

182 lines
5.5 KiB
C++

#include "renderer_polygon_3d.hpp"
#include <iostream>
#include "glm/gtc/type_ptr.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include "polygon_model.hpp"
namespace endofthejedi {
void RendererPolygon3d::setup()
{
std::cout<<"setup polygon 3d" << std::endl;
//m_missileModel = new PolygonModel("../data/mesh/quad_screen_coords.stl");
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);
addModel("../data/mesh/ship.stl", &m_shipModel);
std::string vss_simple =
"#version 120\n"
"varying vec3 vertex;\n"
"void main()\n"
"{\n"
" gl_Position = gl_Vertex;\n"
" vertex = gl_Position.xyz;\n"
"}\n"
;
std::string fss_simple =
"#version 120\n"
"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"
;
std::string vss_game_objects =
"#version 120\n"
"uniform vec3 position;\n"
"uniform float scale;\n"
"varying vec3 vertex;\n"
"uniform mat4 model;\n"
"void main()\n"
"{\n"
//" vec3 p = position + scale*gl_Vertex.xyz;\n"
" vec3 p = position + scale*(model*gl_Vertex).xyz;\n"
" gl_Position = vec4(p, 1.0);\n"
" vertex = p.xyz;\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"
;
m_shader.init();
#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
}
void RendererPolygon3d::render(const game::State *state)
{
m_shader.bind();
// TODO :Z?
renderPlanets(state);
renderShips(state);
renderMissiles(state);
//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();
}
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;
glm::mat4 model(1.0f);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
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)
{
m_missileModel->bind();
for (const game::Player *player : state->players) {
for (const game::Missile *missile : player->missiles) {
glm::vec3 c = glm::vec3(1.0, 1.0, 0.3);
const auto &p = missile->position;
glm::mat4 model(1.0f);
static float a = 0.0;
a += 2.0*M_PI * 0.01;
model = glm::rotate(model, a, glm::vec3(0.0f, 0.0f, 1.0f));
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"), 0.05);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
m_missileModel->render();
}
}
}
void RendererPolygon3d::renderShips(const game::State *state)
{
m_shipModel->bind();
for (const game::Ship *ship : state->ships) {
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
const auto &p = ship->position;
glm::mat4 model(1.0f);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
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"), 0.02);
m_shipModel->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();
m_models.push_back(*dest);
}
}