KlassischeKeplerKriege/game/renderer.cpp

68 lines
2.1 KiB
C++
Raw Normal View History

2016-09-27 17:01:21 +00:00
#include "renderer.hpp"
2016-09-15 15:23:02 +00:00
2016-09-27 18:56:17 +00:00
static const char *vss =
#include "main.vs"
;
static const char *fss =
#include "main.fs"
;
2016-09-27 15:57:41 +00:00
2016-09-28 01:33:01 +00:00
#include <vector>
#include "glm/glm.hpp"
#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include "glm/gtc/type_ptr.hpp"
2016-09-27 15:57:41 +00:00
2016-09-27 18:56:17 +00:00
endofthejedi::Renderer::Renderer() {
m_shader.load(vss, GL_VERTEX_SHADER);
m_shader.load(fss, GL_FRAGMENT_SHADER);
2016-09-27 15:57:41 +00:00
}
2016-09-25 21:05:27 +00:00
endofthejedi::Renderer::~Renderer() {}
2016-09-28 01:33:01 +00:00
void endofthejedi::Renderer::render(const game::State *state) {
2016-09-27 18:56:17 +00:00
m_shader.bind();
2016-09-28 01:33:01 +00:00
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, -10.0f));
radii.push_back(planet->radius);
colors.push_back(glm::vec4(1.0f, 0.0f, 0.0f, 0.5f));
}
glUniform1f(m_shader.location("aspectratio"), 16.0f/9.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();
2016-09-27 18:56:17 +00:00
}
2016-09-25 21:05:27 +00:00
void endofthejedi::Renderer::drawCircle(float x, float y, float radius, float r,
float g, float b, int numSides) {
2016-09-27 18:56:17 +00:00
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();
2016-09-15 15:23:02 +00:00
}