* refactored

This commit is contained in:
end 2016-09-28 07:03:29 +02:00
parent 959202e111
commit ed3990710e
10 changed files with 226 additions and 179 deletions

View file

@ -10,6 +10,8 @@ set(GAME_SRC
glclasses.cpp
renderer.cpp
game_window.cpp
renderer_simple.cpp
renderer_shader.cpp
util.cpp
game.cpp
@ -28,6 +30,8 @@ set(GAME_HEADERS
opengl.hpp
glclasses.hpp
renderer.hpp
renderer_simple.hpp
renderer_shader.hpp
)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

View file

@ -2,21 +2,15 @@
#include "opengl.hpp"
#include "renderer.hpp"
#include "renderer_simple.hpp"
#include "renderer_shader.hpp"
#include "game.hpp"
#include "state/trace.hpp"
#include "state/object.hpp"
#include "state/missile.hpp"
#include "state/player.hpp"
#include "state/planet.hpp"
#include "state/ship.hpp"
#include "state/explosion.hpp"
class GameWindow : public endofthejedi::GLWindow {
private:
Game m_game;
endofthejedi::Renderer m_renderer;
endofthejedi::RendererSimple m_renderer;
protected:
void init() override {
@ -41,77 +35,12 @@ class GameWindow : public endofthejedi::GLWindow {
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*
for (const game::Planet *planet : m_game.state()->planets) {
drawPlanet(planet->position, planet->radius);
}
for (const game::Trace *trace : m_game.state()->traces) {
drawTrace(trace);
}
for (const game::Explosion *explosion : m_game.state()->explosions) {
drawExplosion(explosion);
}
for (const game::Ship *ship : m_game.state()->ships) {
drawShip(ship->position);
}
for (const game::Player *player : m_game.state()->players) {
for (const game::Missile *missile : player->missiles) {
drawMissile(missile->position);
}
}
*/
m_renderer.render(m_game.state());
}
void resize() override { glViewport(0, 0, getwidth(), getheight()); }
void drawShip(const glm::vec2 &pos) {
// std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl;
glm::vec3 color = glm::vec3(0.2, 1.0, 0.3);
float radius = m_game.state()->shipRadius();
m_renderer.drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z,
12);
}
void drawPlanet(const glm::vec2 &pos, float radius) {
glm::vec3 color = glm::vec3(0.7, 0.1, 0.2);
// std::cout<<"draw planet @ " << pos.x << ", " << pos.y << std::endl;
m_renderer.drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z,
32);
}
void drawMissile(const glm::vec2 &pos) {
glm::vec3 color = glm::vec3(1.0, 1.0, 0.3);
m_renderer.drawCircle(pos.x, pos.y, 0.01, color.x, color.y, color.z, 6);
}
void drawTrace(const game::Trace *trace) {
for (const game::Trace::TracePoint &p : trace->points) {
glm::vec3 color =
glm::vec3(0.1, 0.3, 1.0) / (1.0f + 500.0f * p.speed);
m_renderer.drawCircle(p.position.x, p.position.y, 0.005, color.x,
color.y, color.z, 3);
}
}
void drawExplosion(const game::Explosion *explosion) {
// TODO: transparent
// TODO: with glow in the middle
float r = explosion->maxRadius * (explosion->age / explosion->maxAge);
// TODO: transparency?
glm::vec3 color = glm::vec3(1.0, 0.9, 0.1);
m_renderer.drawCircle(explosion->position.x, explosion->position.y, r,
color.x, color.y, color.z, 64);
}
public:
GameWindow(unsigned int width, unsigned int height)
: endofthejedi::GLWindow(width, height) {}

View file

@ -1,96 +1,4 @@
#include "renderer.hpp"
static const char *vss =
#include "main.vs"
;
static const char *fss =
#include "main.fs"
;
#include <vector>
#include "glm/glm.hpp"
#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include "glm/gtc/type_ptr.hpp"
endofthejedi::Renderer::Renderer() {
m_shader.load(vss, GL_VERTEX_SHADER);
m_shader.load(fss, GL_FRAGMENT_SHADER);
}
endofthejedi::Renderer::~Renderer() {}
void endofthejedi::Renderer::render(const game::State *state) {
m_shader.bind();
std::vector<glm::vec3> positions;
std::vector<glm::vec4> colors;
std::vector<GLfloat> radii;
std::vector<GLfloat> reflections;
for (const game::Planet *planet : state->planets) {
positions.push_back(glm::vec3(planet->position, -5.0f));
radii.push_back(planet->radius);
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
}
for (const game::Ship *ship : state->ships) {
positions.push_back(glm::vec3(ship->position, -3.0f));
radii.push_back(ship->radius);
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
}
for (const game::Player *player : state->players) {
for (const game::Missile *missile : player->missiles) {
positions.push_back(glm::vec3(missile->position, -3.0f));
radii.push_back(0.01f);
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
}
}
for (auto& pos : positions) {
pos.x *= 5;
pos.y *= 5;
}
for (auto& rad : radii) {
rad *= 5;
}
/*
for (const game::Trace *trace : state->traces) {
positions.push_back(glm::vec3(trace->position, -3.0f));
radii.push_back(trace->radius);
colors.push_back(glm::vec4(0.0f, 1.0f, 0.0f, 0.5f));
}*/
glUniform1f(m_shader.location("aspectratio"), 1.0f);
glUniform1i(m_shader.location("reflections"), 1);
glUniform3f(m_shader.location("l_position"), 0.6f, 0.1f, 0.0f);
glUniform1f(m_shader.location("l_radius"), 1.0f);
glUniform3fv(m_shader.location("s_positions"), positions.size(),
glm::value_ptr(positions[0]));
glUniform4fv(m_shader.location("s_colors"), colors.size(),
glm::value_ptr(colors[0]));
glUniform1fv(m_shader.location("s_radii"), radii.size(), &radii[0]);
glUniform1i(m_shader.location("s_length"), positions.size());
// render fullscreen quad with legacy opengl (too lazy, sorry)
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 endofthejedi::Renderer::drawCircle(float x, float y, float radius, float r,
float g, float b, int numSides) {
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y); // center of circle
for (int i = 0; i <= numSides; i++) {
glColor3f(r, g, b);
glVertex2f(x + (radius * cos(i * 2 * M_PI / numSides)),
y + (radius * sin(i * 2 * M_PI / numSides)));
}
glEnd();
}

View file

@ -5,26 +5,15 @@
#include <epoxy/gl.h>
#include <epoxy/glx.h>
#include "glclasses.hpp"
#include "game.hpp"
#include "state/planet.hpp"
#include "state/trace.hpp"
#include "state/player.hpp"
#include "state/ship.hpp"
#include "state/missile.hpp"
namespace endofthejedi {
class Renderer {
private:
Shader m_shader;
protected:
public:
Renderer();
~Renderer();
void render(const game::State* state);
void drawCircle(float x, float y, float radius, float r, float g, float b,
int numSides = 12);
virtual void render(const game::State *state);
};
}

75
game/renderer_shader.cpp Normal file
View file

@ -0,0 +1,75 @@
#include "renderer_shader.hpp"
#include <vector>
#include "glm/glm.hpp"
#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include "glm/gtc/type_ptr.hpp"
endofthejedi::RendererShader::RendererShader() {
m_shader.load(vss, GL_VERTEX_SHADER);
m_shader.load(fss, GL_FRAGMENT_SHADER);
}
void endofthejedi::RendererShader::render(const game::State *state) {
m_shader.bind();
std::vector<glm::vec3> positions;
std::vector<glm::vec4> colors;
std::vector<GLfloat> radii;
std::vector<GLfloat> reflections;
for (const game::Planet *planet : state->planets) {
positions.push_back(glm::vec3(planet->position, -5.0f));
radii.push_back(planet->radius);
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
}
for (const game::Ship *ship : state->ships) {
positions.push_back(glm::vec3(ship->position, -3.0f));
radii.push_back(ship->radius);
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
}
for (const game::Player *player : state->players) {
for (const game::Missile *missile : player->missiles) {
positions.push_back(glm::vec3(missile->position, -3.0f));
radii.push_back(0.01f);
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
}
}
for (auto &pos : positions) {
pos.x *= 5;
pos.y *= 5;
}
for (auto &rad : radii) {
rad *= 5;
}
/*
for (const game::Trace *trace : state->traces) {
positions.push_back(glm::vec3(trace->position, -3.0f));
radii.push_back(trace->radius);
colors.push_back(glm::vec4(0.0f, 1.0f, 0.0f, 0.5f));
}*/
glUniform1f(m_shader.location("aspectratio"), 1.0f);
glUniform1i(m_shader.location("reflections"), 1);
glUniform3f(m_shader.location("l_position"), 0.6f, 0.1f, 0.0f);
glUniform1f(m_shader.location("l_radius"), 1.0f);
glUniform3fv(m_shader.location("s_positions"), positions.size(),
glm::value_ptr(positions[0]));
glUniform4fv(m_shader.location("s_colors"), colors.size(),
glm::value_ptr(colors[0]));
glUniform1fv(m_shader.location("s_radii"), radii.size(), &radii[0]);
glUniform1i(m_shader.location("s_length"), positions.size());
// render fullscreen quad with legacy opengl (too lazy, sorry)
glBegin(GL_QUADS);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
glVertex2f(1.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();
}

30
game/renderer_shader.hpp Normal file
View file

@ -0,0 +1,30 @@
#pragma once
static const char *vss =
#include "raycaster.vs"
;
static const char *fss =
#include "raycaster.fs"
;
#include "renderer.hpp"
#include "glclasses.hpp"
#include "game.hpp"
#include "state/planet.hpp"
#include "state/trace.hpp"
#include "state/player.hpp"
#include "state/ship.hpp"
namespace endofthejedi {
class RendererShader : Renderer {
private:
Shader m_shader;
protected:
public:
RendererShader();
void render(const game::State *state) override;
};
}

79
game/renderer_simple.cpp Normal file
View file

@ -0,0 +1,79 @@
#include "renderer_simple.hpp"
void endofthejedi::RendererSimple::drawCircle(float x, float y, float radius,
float r, float g, float b,
int numSides) {
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y); // center of circle
for (int i = 0; i <= numSides; i++) {
glColor3f(r, g, b);
glVertex2f(x + (radius * cos(i * 2 * M_PI / numSides)),
y + (radius * sin(i * 2 * M_PI / numSides)));
}
glEnd();
}
void endofthejedi::RendererSimple::drawShip(const glm::vec2 &pos,
float radius) {
// std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl;
glm::vec3 color = glm::vec3(0.2, 1.0, 0.3);
drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z, 12);
}
void endofthejedi::RendererSimple::drawPlanet(const glm::vec2 &pos,
float radius) {
glm::vec3 color = glm::vec3(0.7, 0.1, 0.2);
// std::cout<<"draw planet @ " << pos.x << ", " << pos.y << std::endl;
drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z, 32);
}
void endofthejedi::RendererSimple::drawMissile(const glm::vec2 &pos) {
glm::vec3 color = glm::vec3(1.0, 1.0, 0.3);
drawCircle(pos.x, pos.y, 0.01, color.x, color.y, color.z, 6);
}
void endofthejedi::RendererSimple::drawTrace(const game::Trace *trace) {
for (const game::Trace::TracePoint &p : trace->points) {
glm::vec3 color = glm::vec3(0.1, 0.3, 1.0) / (1.0f + 500.0f * p.speed);
drawCircle(p.position.x, p.position.y, 0.005, color.x, color.y, color.z,
3);
}
}
void endofthejedi::RendererSimple::drawExplosion(
const game::Explosion *explosion) {
// TODO: transparent
// TODO: with glow in the middle
float r = explosion->maxRadius * (explosion->age / explosion->maxAge);
// TODO: transparency?
glm::vec3 color = glm::vec3(1.0, 0.9, 0.1);
drawCircle(explosion->position.x, explosion->position.y, r, color.x,
color.y, color.z, 64);
}
void endofthejedi::RendererSimple::render(const game::State *state) {
for (const game::Planet *planet : state->planets) {
drawPlanet(planet->position, planet->radius);
}
for (const game::Trace *trace : state->traces) {
drawTrace(trace);
}
for (const game::Explosion *explosion : state->explosions) {
drawExplosion(explosion);
}
for (const game::Ship *ship : state->ships) {
drawShip(ship->position, ship->radius);
}
for (const game::Player *player : state->players) {
for (const game::Missile *missile : player->missiles) {
drawMissile(missile->position);
}
}
}

33
game/renderer_simple.hpp Normal file
View file

@ -0,0 +1,33 @@
#pragma once
#include "renderer.hpp"
#include "glclasses.hpp"
#include "game.hpp"
#include "state/trace.hpp"
#include "state/object.hpp"
#include "state/missile.hpp"
#include "state/player.hpp"
#include "state/planet.hpp"
#include "state/ship.hpp"
#include "state/explosion.hpp"
namespace endofthejedi {
class RendererSimple : Renderer {
private:
void drawCircle(float x, float y, float radius, float r, float g, float b,
int numSides = 12);
void drawShip(const glm::vec2 &pos, float radius);
void drawPlanet(const glm::vec2 &pos, float radius);
void drawMissile(const glm::vec2 &pos);
void drawTrace(const game::Trace *trace);
void drawExplosion(const game::Explosion *explosion);
protected:
public:
void render(const game::State *state) override;
};
}