KlassischeKeplerKriege/game/renderer_polygon_3d/renderer_polygon_3d.cpp
Andreas Ortmann c06e138b3e merged
2016-09-29 17:54:01 +02:00

264 lines
7.7 KiB
C++

#include "renderer_polygon_3d.hpp"
#include <iostream>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp>
namespace endofthejedi {
void RendererPolygon3d::setup()
{
m_lastTime = -1.0;
std::cout<<"setup polygon 3d" << std::endl;
m_shader.init();
#if 0
std::string vss_simple = "../data/shader/simple.vert";
std::string fss_simple = "../data/shader/simple.frag";
m_shader.loadFile(vss_simple, GL_VERTEX_SHADER);
m_shader.loadFile(fss_simple, GL_FRAGMENT_SHADER);
#else
std::string vss_game_objects = "../data/shader/gameobjects.vert";
std::string fss_game_objects = "../data/shader/gameobjects.frag";
m_shader.loadFile(vss_game_objects, GL_VERTEX_SHADER);
m_shader.loadFile(fss_game_objects, GL_FRAGMENT_SHADER);
#endif
addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
addModel("../data/mesh/planet_64.stl", &m_planetModel);
addModel("../data/mesh/ship_ufo.stl", &m_shipModel);
}
void RendererPolygon3d::render(const game::State *state)
{
if (m_lastTime == -1.0) {
m_lastTime = state->timestamp();
}
float dt = state->timestamp() - m_lastTime;
if (dt < 0.0) {
dt = 0.0;
}
m_state = state;
advanceGraphicObjects(dt);
// TODO: add stars (texture)
// TODO: add dust particles
// TODO: add little rocks flying around
glClearColor(0.0, 0.0, 0.0, 1.0);
m_shader.bind();
glm::vec3 c = glm::vec3(1.0, 1.0, 0.5);
glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z);
//glm::vec3 p = glm::vec3(1.0, 0.0, 0.0);
glm::vec3 p = glm::vec3(0.5f, 0.5f, -4.0f);
glUniform3f(m_shader.location("lightPosition"), p.x, p.y, p.z);
renderPlanets();
renderShips();
renderMissiles();
//glDisable(GL_DEPTH_TEST);
//renderParticles();
//glEnable(GL_DEPTH_TEST);
renderTraces();
//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();
m_lastTime = state->timestamp();
}
void RendererPolygon3d::renderParticles()
{
for (ParticleBatch *batch : m_particles) {
batch->bind();
batch->render();
}
}
void RendererPolygon3d::addExplosionEffect(const glm::vec2 &pos, size_t n, float duration)
{
ParticleBatch *batch = new ParticleBatch(n, 0.005, duration);
for (size_t i=0; i<n; i++) {
// distribute in a circle
float t = 4.0 * M_PI * i / (float) n;
t += 0.2*util::randf_m1_1();
// with random velocities
glm::vec2 v = 0.2f*glm::vec2(sin(t), cos(t));
v *= 0.2+0.8*util::randf_0_1();
batch->setParticle(i, pos, v);
}
batch->upload();
m_particles.push_back(batch);
}
void RendererPolygon3d::advanceGraphicObjects(float dt)
{
for (const game::Explosion *expl : m_state->explosions) {
if (expl->age == 0.0) {
addExplosionEffect(expl->position, 200, 3.0);
}
}
std::vector<ParticleBatch*> rm;
for (ParticleBatch *batch : m_particles) {
batch->tick(dt);
if (batch->done()) {
rm.push_back(batch);
}
}
for (ParticleBatch *batch : rm) {
m_particles.remove(batch);
delete(batch);
}
}
void RendererPolygon3d::renderPlanets()
{
m_planetModel->bind();
for (const game::Planet *planet : m_state->planets) {
glm::mat4 model = computeModelMatrix(planet);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
glm::vec3 c = planet->getColor();
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
m_planetModel->render();
}
}
void RendererPolygon3d::renderMissiles()
{
m_missileModel->bind();
for (const game::Player *player : m_state->players) {
for (const game::Missile *missile : player->missiles) {
glm::vec3 c = glm::vec3(1.0, 1.0, 0.3);
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
glm::mat4 model = computeModelMatrix(missile);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
m_missileModel->render();
}
}
}
void RendererPolygon3d::renderShips()
{
m_shipModel->bind();
for (const game::Ship *ship : m_state->ships) {
glm::mat4 model = computeModelMatrix(ship);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
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)->setup(&m_shader);
(*dest)->uploadToOpenGl();
m_models.push_back(*dest);
}
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.03f, a);
}
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Ship *ship)
{
// TODO: rotate them before shooting, that looks better
glm::mat4 mat = computeModelMatrix(ship->position, m_state->shipRadius());
// XXX model is flipped
//glm::mat4 mat = computeModelMatrix(ship->position, 0.3);
mat = glm::rotate(mat, (float) M_PI, glm::vec3(0.0f, 1.0f, 0.0f));
return mat;
}
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;
}
void RendererPolygon3d::renderTraces()
{
// revert to default
glUseProgram(0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
for (const game::Trace *trace : m_state->traces) {
float fade_out = 1.0;
if (trace->missile == nullptr) {
fade_out = 1.0 - (trace->age / trace->maxAge);
}
glColor3f(0.0, 0.0, 1.0*fade_out);
glBegin(GL_LINE_STRIP);
for (const game::Trace::TracePoint &tp : trace->points) {
glVertex2f(tp.position.x, tp.position.y);
}
glEnd();
}
}
}