+ added line width of 2

This commit is contained in:
end 2016-10-05 23:37:46 +02:00
parent 70b0fee526
commit 9e4bda9242

View file

@ -9,8 +9,7 @@
#include "developer_console.hpp"
namespace endofthejedi {
void RendererPolygon3d::setup()
{
void RendererPolygon3d::setup() {
m_lastTime = -1.0;
// std::cout << "setup polygon 3d" << std::endl;
@ -75,8 +74,7 @@ namespace endofthejedi {
});
}
void RendererPolygon3d::loadBackgroundTexture()
{
void RendererPolygon3d::loadBackgroundTexture() {
// m_texture = new ImageTexture("../data/img/test.png");
if (m_texture != nullptr) {
delete (m_texture);
@ -99,11 +97,11 @@ namespace endofthejedi {
std::cout << "texture size is " << s.x << " X " << s.y << std::endl;
}
void RendererPolygon3d::renderBackgroundImage()
{
void RendererPolygon3d::renderBackgroundImage() {
m_shader_background.bind();
glUniform2fv(m_shader_background.location("uvScale"), 1, glm::value_ptr(m_texture->uvScale()));
glUniform2fv(m_shader_background.location("uvScale"), 1,
glm::value_ptr(m_texture->uvScale()));
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
@ -119,8 +117,7 @@ namespace endofthejedi {
glEnd();
}
void RendererPolygon3d::render(const game::State *state)
{
void RendererPolygon3d::render(const game::State *state) {
if (m_lastTime == -1.0) {
m_lastTime = state->timestamp();
}
@ -156,18 +153,10 @@ namespace endofthejedi {
m_postprocess_tex0.getName());
renderBackgroundImage();
//float s = 0.1;
//glClearColor(s, s, 1.2*s, 1.0);
m_shader_game_objects.bind();
// TODO: add ONE sun planet
// TODO: add lights for explosions
configureLightningInShader(&m_shader_game_objects);
//std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl;
renderPlanets();
renderShips();
renderMissiles();
@ -195,7 +184,6 @@ namespace endofthejedi {
// postprocessing
m_postprocess_shader.bind();
//glColor3f(1.0, 0.0, 0.0);
glActiveTexture(GL_TEXTURE0);
m_postprocess_tex1.bind();
@ -203,19 +191,17 @@ namespace endofthejedi {
glUniform2f(m_postprocess_shader.location("uvScale"), 1, 1);
for (int i = 0; i < 2; i++) {
glUniform2f(m_postprocess_shader.location("scale"), 2.0f / m_w,
0.0f);
//glVertex2f(1.0f, -1.0f);
glUniform2f(m_postprocess_shader.location("scale"), 2.0f / m_w, 0.0f);
glBegin(GL_QUADS);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
glVertex2f(1.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();
//glVertex2f(1.0f, 1.0f);
glUniform2f(m_postprocess_shader.location("scale"), 0.0f,
2.0f / m_h);
//glVertex2f(-1.0f, 1.0f);
glUniform2f(m_postprocess_shader.location("scale"), 0.0f, 2.0f / m_h);
glBegin(GL_QUADS);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
@ -238,7 +224,7 @@ namespace endofthejedi {
m_postprocess_tex0.bind();
glActiveTexture(GL_TEXTURE1);
m_postprocess_tex1.bind();
//glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(-1.0f, -1.0f);
@ -250,8 +236,7 @@ namespace endofthejedi {
m_lastTime = state->timestamp();
}
void RendererPolygon3d::renderParticles()
{
void RendererPolygon3d::renderParticles() {
m_shader_particles.bind();
glUniform1f(m_shader_particles.location("aspectRatio"), m_aspectRatio);
@ -262,11 +247,11 @@ namespace endofthejedi {
}
}
void RendererPolygon3d::addExplosionEffect(
size_t id, const glm::vec2 &explCenter, const glm::vec2 &missileVelocity,
bool isPlanetHit,
size_t n, float duration)
{
void RendererPolygon3d::addExplosionEffect(size_t id,
const glm::vec2 &explCenter,
const glm::vec2 &missileVelocity,
bool isPlanetHit, size_t n,
float duration) {
// float particleRadius = 0.005;
// float particleRadius = 0.003;
float particleRadius = 0.02;
@ -292,7 +277,8 @@ namespace endofthejedi {
// especially in 3d this would look bad without 3d velocity vector.
// glm::vec3 v = 0.5f*glm::vec3(sin(t), cos(t), util::randf_m1_1());
glm::vec3 pos = glm::vec3(explCenter, 0.0) + glm::ballRand(explCoreSize);
glm::vec3 pos =
glm::vec3(explCenter, 0.0) + glm::ballRand(explCoreSize);
glm::vec3 v = glm::ballRand(maxVelocity);
@ -340,19 +326,21 @@ namespace endofthejedi {
if (isInsidePlanet && isPlanetHit) {
util::IntersectionTest intersect;
if (!intersect.raySphere(
glm::vec3(explCenter, 0.0f), v,
glm::vec3(nearestPlanet->position, 0.0f), nearestPlanet->radius))
{
if (!intersect.raySphere(glm::vec3(explCenter, 0.0f), v,
glm::vec3(nearestPlanet->position, 0.0f),
nearestPlanet->radius)) {
makeStationary = true;
//std::cout<<"warning: intersection should be valid!" << std::endl;
// std::cout<<"warning: intersection should be valid!" <<
// std::endl;
// TODO: must be as they lie on a plane and the dist is < as
// the radius.
// handle if this is wrong.
} else {
// simple reflection, ignoring the missile velocity: this looks good enough
glm::vec3 planetNormal = glm::normalize(pos - glm::vec3(nearestPlanet->position, 0.0f));
// simple reflection, ignoring the missile velocity: this looks
// good enough
glm::vec3 planetNormal = glm::normalize(
pos - glm::vec3(nearestPlanet->position, 0.0f));
v = glm::length(v) * planetNormal;
// TODO
@ -367,13 +355,17 @@ namespace endofthejedi {
// maxParticleDist = 100.0;
// v = -v;
//pos = glm::vec3(nearestPlanet->position, 0.0f) + nearestPlanet->radius*planetNormal;
// pos = glm::vec3(nearestPlanet->position, 0.0f) +
// nearestPlanet->radius*planetNormal;
// set position to the intersection point between explosion
// center and planet surface
//const glm::vec3 planetNormal = glm::vec3(glm::normalize(explCenter - nearestPlanet->position), 0.0f);
// const glm::vec3 planetNormal =
// glm::vec3(glm::normalize(explCenter -
// nearestPlanet->position), 0.0f);
//pos = glm::vec3(nearestPlanet->position, 0.0f) + nearestPlanet->radius*planetNormal;
// pos = glm::vec3(nearestPlanet->position, 0.0f) +
// nearestPlanet->radius*planetNormal;
// build new velocity by reflecting the old velocity on the
// planet normal
@ -387,12 +379,15 @@ namespace endofthejedi {
// v = v - 2.0f*glm::dot(v, planetNormal) * planetNormal;
// v = glm::length(v) * planetNormal;
//glm::vec3 r = v - 2.0f*glm::dot(v, planetNormal) * planetNormal;
// glm::vec3 r = v - 2.0f*glm::dot(v, planetNormal) *
// planetNormal;
// glm::vec3 vn = glm::length(v) * planetNormal;
// v = (r+vn) / 2.0f;
//glm::vec3 vc = glm::vec3(nearestPlanet->position-explCenter, 0.0f);
//glm::vec3 r = vc - 2.0f*glm::dot(vc, planetNormal) * planetNormal;
// glm::vec3 vc = glm::vec3(nearestPlanet->position-explCenter,
// 0.0f);
// glm::vec3 r = vc - 2.0f*glm::dot(vc, planetNormal) *
// planetNormal;
// v = r;
}
} else if (isInsidePlanet && !isPlanetHit) {
@ -404,7 +399,8 @@ namespace endofthejedi {
if (makeStationary) {
v = glm::vec3(0.0f, 0.0f, 0.0f);
pos = glm::vec3(explCenter, 0.0f);;
pos = glm::vec3(explCenter, 0.0f);
;
}
batch->setParticle(i, pos, v, maxParticleDist);
@ -415,8 +411,7 @@ namespace endofthejedi {
m_particles.push_back(batch);
}
void RendererPolygon3d::advanceGraphicObjects(float dt)
{
void RendererPolygon3d::advanceGraphicObjects(float dt) {
#if 0
for (const game::Explosion *expl : m_state->explosions) {
bool gotIt = false;
@ -443,14 +438,14 @@ namespace endofthejedi {
if (type == game::EventType::Explosion) {
if (cycle == game::LifeCycle::Create) {
game::ExplosionEvent *ee = static_cast<game::ExplosionEvent*>(evt);
game::Explosion *expl = static_cast<game::Explosion*>(ee->object());
game::ExplosionEvent *ee =
static_cast<game::ExplosionEvent *>(evt);
game::Explosion *expl =
static_cast<game::Explosion *>(ee->object());
addExplosionEffect(
expl->id, expl->position,
addExplosionEffect(expl->id, expl->position,
glm::vec3(expl->missileVelocity, 1.0),
(expl->hit == game::Hit::Planet),
1000, 1.0);
(expl->hit == game::Hit::Planet), 1000, 1.0);
}
} else if (type == game::EventType::Ship) {
game::ShipEvent *me = static_cast<game::ShipEvent *>(evt);
@ -458,11 +453,13 @@ namespace endofthejedi {
// is always modificated
if (cycle == game::LifeCycle::Create) {
//std::cout<<"[renderer] adding missile #" << missile->id << std::endl;
// std::cout<<"[renderer] adding missile #" << missile->id <<
// std::endl;
m_ships.push_back(ship);
} else if (cycle == game::LifeCycle::Destroy) {
//std::cout<<"[renderer] removing missile #" << missile->id << std::endl;
// std::cout<<"[renderer] removing missile #" << missile->id <<
// std::endl;
m_ships.remove(ship);
}
} else if (type == game::EventType::Missile) {
@ -471,18 +468,21 @@ namespace endofthejedi {
// is always modificated
if (cycle == game::LifeCycle::Create) {
//std::cout<<"[renderer] adding missile #" << missile->id << std::endl;
// std::cout<<"[renderer] adding missile #" << missile->id <<
// std::endl;
m_missiles.push_back(missile);
} else if (cycle == game::LifeCycle::Destroy) {
//std::cout<<"[renderer] removing missile #" << missile->id << std::endl;
// std::cout<<"[renderer] removing missile #" << missile->id <<
// std::endl;
m_missiles.remove(missile);
}
}
}
// if (m_particles.size() == 0) {
// addExplosionEffect(0, glm::vec2(0.0, 0.0), glm::vec2(0.0, 0.0), false, 10000, 2.0);
// addExplosionEffect(0, glm::vec2(0.0, 0.0), glm::vec2(0.0, 0.0), false,
// 10000, 2.0);
//}
std::vector<ParticleBatch *> rm;
@ -555,34 +555,37 @@ void RendererPolygon3d::renderMissiles() {
for (const game::Missile *missile : m_missiles) {
glm::vec3 c = glm::vec3(1.0, 1.0, 0.3);
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, c.z);
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y,
c.z);
// TODO: rename functions so their name represents what args they
// take
glm::mat4 model = computeModelMatrix(missile);
glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE,
glm::value_ptr(model));
m_missileModel->render();
}
}
void RendererPolygon3d::renderShips()
{
void RendererPolygon3d::renderShips() {
m_shipModel->bind();
for (const game::Ship *ship : m_ships) {
glm::mat4 model = computeModelMatrix(ship);
glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE,
glm::value_ptr(model));
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, c.z);
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y,
c.z);
m_shipModel->render();
}
}
void RendererPolygon3d::addModel(const std::string &filename, PolygonModel **dest)
{
void RendererPolygon3d::addModel(const std::string &filename,
PolygonModel **dest) {
// std::cout<<"adding a model: " << filename << std::endl;
*dest = new PolygonModel(filename);
@ -608,13 +611,11 @@ void RendererPolygon3d::renderMissiles() {
m_models.push_back(*dest);
}
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Planet *planet)
{
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Planet *planet) {
return computeModelMatrix(planet->position, planet->radius);
}
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Missile *missile)
{
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Missile *missile) {
glm::vec2 vn = glm::normalize(missile->velocity);
float a = std::atan2(vn.y, vn.x);
@ -631,8 +632,7 @@ void RendererPolygon3d::renderMissiles() {
return mat;
}
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Ship *ship)
{
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());
@ -642,8 +642,8 @@ void RendererPolygon3d::renderMissiles() {
return mat;
}
glm::mat4 RendererPolygon3d::computeModelMatrix(const glm::vec2 &pos, float scale, float angle)
{
glm::mat4 RendererPolygon3d::computeModelMatrix(const glm::vec2 &pos,
float scale, float angle) {
// init as identity matrix
glm::mat4 model;
@ -660,13 +660,13 @@ void RendererPolygon3d::renderMissiles() {
return model;
}
void RendererPolygon3d::renderTraces()
{
void RendererPolygon3d::renderTraces() {
// revert to default
glUseProgram(0);
// TODO dont use line mode. make that with own quads
glPolygonMode(GL_FRONT, GL_LINE);
glLineWidth(2.0f);
for (const game::Trace *trace : m_state->traces) {
float fade_out = 1.0;
if (trace->missile == nullptr) {
@ -680,11 +680,11 @@ void RendererPolygon3d::renderMissiles() {
}
glEnd();
}
glLineWidth(1.0f);
glPolygonMode(GL_FRONT, GL_FILL);
}
void RendererPolygon3d::configureLightningInShader(Shader *shader) const
{
void RendererPolygon3d::configureLightningInShader(Shader *shader) const {
// TODO: add a few small lights for explosions so they lit the
// surroundsings
@ -731,8 +731,10 @@ void RendererPolygon3d::renderMissiles() {
glUniform1i(shader->location("explLightsNum"), numExplLights);
if (numExplLights != 0) {
glUniform3fv(shader->location("explLightsPos"), numExplLights, glm::value_ptr(positions[0]));
glUniform1fv(shader->location("explLightsIntensities"), numExplLights, intensities.data());
glUniform3fv(shader->location("explLightsPos"), numExplLights,
glm::value_ptr(positions[0]));
glUniform1fv(shader->location("explLightsIntensities"), numExplLights,
intensities.data());
}
}
@ -742,8 +744,5 @@ void RendererPolygon3d::setWindowSize(int px, int py) {
m_h = py;
}
void RendererPolygon3d::setCameraMatrix(const glm::mat4 &cam)
{
(void) cam;
}
void RendererPolygon3d::setCameraMatrix(const glm::mat4 &cam) { (void)cam; }
}