refactored code for positions to use matrix only.

This commit is contained in:
Andreas Ortmann 2016-09-28 16:12:25 +02:00
parent ff74da1c03
commit b71cb5cc52
3 changed files with 73 additions and 42 deletions

View file

@ -35,21 +35,18 @@ namespace endofthejedi {
"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"
" vec3 p = (model*gl_Vertex).xyz;\n"
" gl_Position = vec4(p, 1.0);\n"
" vertex = p.xyz;\n"
"}\n"
@ -84,13 +81,15 @@ namespace endofthejedi {
void RendererPolygon3d::render(const game::State *state)
{
m_state = state;
m_shader.bind();
// TODO :Z?
renderPlanets(state);
renderShips(state);
renderMissiles(state);
renderPlanets();
renderShips();
renderMissiles();
//glColor3f(1.0, 0.0, 0.0);
//glBegin(GL_QUADS);
@ -101,47 +100,31 @@ namespace endofthejedi {
//glEnd();
}
void RendererPolygon3d::renderPlanets(const game::State *state)
void RendererPolygon3d::renderPlanets()
{
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);
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));
glUniform3f(m_shader.location("position"), p.x, p.y, 0.0);
glm::vec3 c = glm::vec3(0.7, 0.2, 0.1);
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 RendererPolygon3d::renderMissiles()
{
m_missileModel->bind();
for (const game::Player *player : state->players) {
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);
const auto &p = missile->position;
glm::mat4 model(1.0f);
//static float a = 0.0;
//a += 2.0*M_PI * 0.01;
glm::vec2 vn = glm::normalize(missile->velocity);
float a = std::atan2((float) vn.y, (float) vn.x);
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);
glm::mat4 model = computeModelMatrix(missile);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
m_missileModel->render();
@ -149,20 +132,16 @@ namespace endofthejedi {
}
}
void RendererPolygon3d::renderShips(const game::State *state)
void RendererPolygon3d::renderShips()
{
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);
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));
glUniform3f(m_shader.location("position"), p.x, p.y, 0.0);
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
glUniform1f(m_shader.location("scale"), 0.02);
m_shipModel->render();
}
@ -182,4 +161,43 @@ namespace endofthejedi {
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.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;
}
}

View file

@ -12,6 +12,8 @@
#include "state/ship.hpp"
#include "state/explosion.hpp"
#include <glm/gtc/matrix_transform.hpp>
class PolygonModel;
namespace endofthejedi {
@ -22,12 +24,20 @@ namespace endofthejedi {
void render(const game::State *state) override;
private:
void renderPlanets(const game::State *state);
void renderMissiles(const game::State *state);
void renderShips(const game::State *state);
void renderPlanets();
void renderMissiles();
void renderShips();
void addModel(const std::string &filename, PolygonModel **dest);
// shortcuts which use the lower versions
glm::mat4 computeModelMatrix(const game::Planet *planet);
glm::mat4 computeModelMatrix(const game::Missile *missile);
glm::mat4 computeModelMatrix(const game::Ship *ship);
// do the actual computation with these values
glm::mat4 computeModelMatrix(const glm::vec2 &pos, float scale, float angle=0.0);
private:
// all models are also here (for easy reloading etc.)
std::vector<PolygonModel*> m_models;
@ -39,5 +49,8 @@ namespace endofthejedi {
// for rendering everything
Shader m_shader;
// for accessing
const game::State *m_state;
};
}

View file

@ -23,7 +23,7 @@ using namespace std;
namespace game {
void State::init()
{
m_shipRadius = 0.05;
m_shipRadius = 0.02;
m_maxMissileDistance = 2.0;
m_playerRespawnTime = 2.0;
m_defaultEnergy = 10.0;