2016-09-28 09:50:35 +00:00
|
|
|
#include "renderer_polygon_3d.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2016-09-28 13:33:35 +00:00
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
2016-09-28 13:10:57 +00:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
2016-09-28 13:33:35 +00:00
|
|
|
#include <glm/gtx/euler_angles.hpp>
|
2016-09-28 12:19:05 +00:00
|
|
|
|
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);
|
2016-09-28 12:41:15 +00:00
|
|
|
addModel("../data/mesh/planet_12.stl", &m_planetModel);
|
2016-09-28 22:50:14 +00:00
|
|
|
addModel("../data/mesh/planet_12.stl", &m_shipModel);
|
2016-09-28 11:36:22 +00:00
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
size_t n = 100;
|
|
|
|
m_particles = new ParticleBatch(n, 0.05, 3.0);
|
2016-09-28 19:26:11 +00:00
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
// distribute in a circle
|
2016-09-28 19:26:11 +00:00
|
|
|
for (size_t i=0; i<n; i++) {
|
2016-09-28 22:50:14 +00:00
|
|
|
float t = 2.0 * M_PI * i / (float) n;
|
|
|
|
t += 0.2*util::randf_m1_1();
|
|
|
|
|
|
|
|
glm::vec2 v = 0.2f*glm::vec2(sin(t), cos(t));
|
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
m_particles->setParticle(
|
|
|
|
i,
|
2016-09-28 22:50:14 +00:00
|
|
|
//glm::vec2(i/(float) n, 0.0),
|
|
|
|
glm::vec2(0.0, 0.0),
|
|
|
|
v);
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_particles->upload();
|
|
|
|
|
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.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"
|
|
|
|
"varying vec3 vertex;\n"
|
2016-09-28 13:22:16 +00:00
|
|
|
"uniform mat4 model;\n"
|
2016-09-28 12:19:05 +00:00
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
2016-09-28 13:22:16 +00:00
|
|
|
//" vec3 p = position + scale*gl_Vertex.xyz;\n"
|
2016-09-28 14:12:25 +00:00
|
|
|
" vec3 p = (model*gl_Vertex).xyz;\n"
|
2016-09-28 13:22:16 +00:00
|
|
|
" gl_Position = vec4(p, 1.0);\n"
|
2016-09-28 13:10:57 +00:00
|
|
|
" vertex = p.xyz;\n"
|
2016-09-28 12:19:05 +00:00
|
|
|
"}\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 19:26:11 +00:00
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
m_state = state;
|
|
|
|
|
2016-09-28 11:36:22 +00:00
|
|
|
m_shader.bind();
|
|
|
|
|
2016-09-28 12:41:15 +00:00
|
|
|
// TODO :Z?
|
|
|
|
|
2016-09-28 22:52:58 +00:00
|
|
|
renderPlanets();
|
|
|
|
renderShips();
|
|
|
|
renderMissiles();
|
2016-09-28 12:41:15 +00:00
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
renderParticles();
|
2016-09-28 19:26:11 +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
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
void RendererPolygon3d::renderParticles()
|
|
|
|
{
|
|
|
|
//m_particles->bind();
|
|
|
|
m_particles->render();
|
|
|
|
}
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
void RendererPolygon3d::renderPlanets()
|
2016-09-28 12:19:05 +00:00
|
|
|
{
|
|
|
|
m_planetModel->bind();
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
for (const game::Planet *planet : m_state->planets) {
|
|
|
|
glm::mat4 model = computeModelMatrix(planet);
|
2016-09-28 13:22:16 +00:00
|
|
|
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
glm::vec3 c = glm::vec3(0.7, 0.2, 0.1);
|
2016-09-28 12:19:05 +00:00
|
|
|
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
|
|
|
|
|
|
|
|
m_planetModel->render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
void RendererPolygon3d::renderMissiles()
|
2016-09-28 12:19:05 +00:00
|
|
|
{
|
2016-09-28 12:41:15 +00:00
|
|
|
m_missileModel->bind();
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
for (const game::Player *player : m_state->players) {
|
2016-09-28 12:41:15 +00:00
|
|
|
for (const game::Missile *missile : player->missiles) {
|
|
|
|
glm::vec3 c = glm::vec3(1.0, 1.0, 0.3);
|
|
|
|
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
|
2016-09-28 13:10:57 +00:00
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
glm::mat4 model = computeModelMatrix(missile);
|
2016-09-28 13:22:16 +00:00
|
|
|
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
2016-09-28 12:41:15 +00:00
|
|
|
|
|
|
|
m_missileModel->render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
void RendererPolygon3d::renderShips()
|
2016-09-28 12:41:15 +00:00
|
|
|
{
|
|
|
|
m_shipModel->bind();
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
for (const game::Ship *ship : m_state->ships) {
|
|
|
|
glm::mat4 model = computeModelMatrix(ship);
|
2016-09-28 13:22:16 +00:00
|
|
|
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
|
2016-09-28 12:41:15 +00:00
|
|
|
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
|
|
|
|
|
|
|
|
m_shipModel->render();
|
|
|
|
}
|
2016-09-28 12:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RendererPolygon3d::addModel(const std::string &filename, PolygonModel **dest)
|
|
|
|
{
|
2016-09-28 13:10:57 +00:00
|
|
|
//std::cout<<"adding a model: " << filename << std::endl;
|
2016-09-28 12:19:05 +00:00
|
|
|
|
|
|
|
*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
|
|
|
}
|
2016-09-28 14:12:25 +00:00
|
|
|
|
|
|
|
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Planet *planet)
|
|
|
|
{
|
|
|
|
return computeModelMatrix(planet->position, planet->radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Missile *missile)
|
|
|
|
{
|
|
|
|
glm::vec2 vn = glm::normalize(missile->velocity);
|
|
|
|
float a = std::atan2(vn.y, vn.x);
|
|
|
|
|
|
|
|
// TODO: which visual size has the rocket? in game its just a point with
|
|
|
|
// no size because all others have size.
|
|
|
|
return computeModelMatrix(missile->position, 0.02f, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Ship *ship)
|
|
|
|
{
|
|
|
|
// TODO: rotate them before shooting, that looks better
|
|
|
|
return computeModelMatrix(ship->position, m_state->shipRadius());
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::mat4 RendererPolygon3d::computeModelMatrix(const glm::vec2 &pos, float scale, float angle)
|
|
|
|
{
|
|
|
|
// init as identity matrix
|
|
|
|
glm::mat4 model;
|
|
|
|
|
|
|
|
model = glm::translate(model, glm::vec3(pos, 0.0));
|
|
|
|
|
|
|
|
if (scale != 1.0) {
|
|
|
|
model = glm::scale(model, glm::vec3(scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (angle != 0.0) {
|
|
|
|
model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
2016-09-28 09:50:35 +00:00
|
|
|
}
|