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" "varying vec3 vertex;\n"
"void main()\n" "void main()\n"
"{\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(0.5+0.5*vertex.xyz, 1.0);\n"
"}\n" "}\n"
; ;
std::string vss_game_objects = std::string vss_game_objects =
"#version 120\n" "#version 120\n"
"uniform vec3 position;\n"
"uniform float scale;\n"
"varying vec3 vertex;\n" "varying vec3 vertex;\n"
"uniform mat4 model;\n" "uniform mat4 model;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
//" vec3 p = position + scale*gl_Vertex.xyz;\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" " gl_Position = vec4(p, 1.0);\n"
" vertex = p.xyz;\n" " vertex = p.xyz;\n"
"}\n" "}\n"
@ -84,13 +81,15 @@ namespace endofthejedi {
void RendererPolygon3d::render(const game::State *state) void RendererPolygon3d::render(const game::State *state)
{ {
m_state = state;
m_shader.bind(); m_shader.bind();
// TODO :Z? // TODO :Z?
renderPlanets(state); renderPlanets();
renderShips(state); renderShips();
renderMissiles(state); renderMissiles();
//glColor3f(1.0, 0.0, 0.0); //glColor3f(1.0, 0.0, 0.0);
//glBegin(GL_QUADS); //glBegin(GL_QUADS);
@ -101,47 +100,31 @@ namespace endofthejedi {
//glEnd(); //glEnd();
} }
void RendererPolygon3d::renderPlanets(const game::State *state) void RendererPolygon3d::renderPlanets()
{ {
m_planetModel->bind(); m_planetModel->bind();
for (const game::Planet *planet : state->planets) { for (const game::Planet *planet : m_state->planets) {
glm::vec3 c = glm::vec3(0.7, 0.2, 0.1); glm::mat4 model = computeModelMatrix(planet);
const auto &p = planet->position;
glm::mat4 model(1.0f);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model)); 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); glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
glUniform1f(m_shader.location("scale"), planet->radius);
m_planetModel->render(); m_planetModel->render();
} }
} }
void RendererPolygon3d::renderMissiles(const game::State *state) void RendererPolygon3d::renderMissiles()
{ {
m_missileModel->bind(); 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) { for (const game::Missile *missile : player->missiles) {
glm::vec3 c = glm::vec3(1.0, 1.0, 0.3); 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); 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)); glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
m_missileModel->render(); m_missileModel->render();
@ -149,20 +132,16 @@ namespace endofthejedi {
} }
} }
void RendererPolygon3d::renderShips(const game::State *state) void RendererPolygon3d::renderShips()
{ {
m_shipModel->bind(); m_shipModel->bind();
for (const game::Ship *ship : state->ships) { for (const game::Ship *ship : m_state->ships) {
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2); glm::mat4 model = computeModelMatrix(ship);
const auto &p = ship->position;
glm::mat4 model(1.0f);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model)); 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); glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
glUniform1f(m_shader.location("scale"), 0.02);
m_shipModel->render(); m_shipModel->render();
} }
@ -182,4 +161,43 @@ namespace endofthejedi {
m_models.push_back(*dest); 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/ship.hpp"
#include "state/explosion.hpp" #include "state/explosion.hpp"
#include <glm/gtc/matrix_transform.hpp>
class PolygonModel; class PolygonModel;
namespace endofthejedi { namespace endofthejedi {
@ -22,12 +24,20 @@ namespace endofthejedi {
void render(const game::State *state) override; void render(const game::State *state) override;
private: private:
void renderPlanets(const game::State *state); void renderPlanets();
void renderMissiles(const game::State *state); void renderMissiles();
void renderShips(const game::State *state); void renderShips();
void addModel(const std::string &filename, PolygonModel **dest); 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: private:
// all models are also here (for easy reloading etc.) // all models are also here (for easy reloading etc.)
std::vector<PolygonModel*> m_models; std::vector<PolygonModel*> m_models;
@ -39,5 +49,8 @@ namespace endofthejedi {
// for rendering everything // for rendering everything
Shader m_shader; Shader m_shader;
// for accessing
const game::State *m_state;
}; };
} }

View file

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