improving aspect ratio and window resize/scaling. done for game objects.

This commit is contained in:
Andreas Ortmann 2016-10-02 11:00:03 +02:00
parent 1562dbe8a7
commit 4353609580
14 changed files with 209 additions and 109 deletions

View file

@ -33,14 +33,16 @@ namespace endofthejedi {
m_data_velocity.resize(m_numParticles);
m_data_kind.resize(m_numParticles);
m_data_max_age.resize(m_numParticles);
}
std::string vss_particles = "../data/shader/particle.vert";
std::string fss_particles = "../data/shader/particle.frag";
m_shader.init();
m_shader.loadFile(vss_particles, GL_VERTEX_SHADER);
m_shader.loadFile(fss_particles, GL_FRAGMENT_SHADER);
ParticleBatch::~ParticleBatch()
{
// TODO: find out if stuff must be deallocated
glDeleteBuffers(5, m_data_vbos);
}
void ParticleBatch::setup(Shader *shader)
{
const char *names[] = {
"in_vertex",
"in_position",
@ -51,18 +53,12 @@ namespace endofthejedi {
for (int i=0; i<5; i++) {
const char *name = names[i];
GLint loc = glGetAttribLocation(m_shader.program(), name);
GLint loc = glGetAttribLocation(shader->program(), name);
m_attr_locations[i] = loc;
//std::cout<<"attr location " << i << " " << loc << " " << name << std::endl;
}
}
ParticleBatch::~ParticleBatch()
{
// TODO: find out if stuff must be deallocated
glDeleteBuffers(5, m_data_vbos);
}
void ParticleBatch::setCenter(const glm::vec3 &center)
{
m_center = center;
@ -130,16 +126,14 @@ namespace endofthejedi {
}
}
void ParticleBatch::render()
void ParticleBatch::render(Shader *shader)
{
//std::cout<<"[ParticleBatch] render " << std::endl;
m_shader.bind();
glUniform1f(m_shader.location("age"), m_age);
glUniform1f(m_shader.location("maxVelocity"), m_maxVelocity);
glUniform1f(m_shader.location("halfAge"), m_halfAge);
glUniform1f(m_shader.location("size"), m_particleRadius);
glUniform3f(m_shader.location("explCenter"), m_center.x, m_center.y, m_center.z);
glUniform1f(shader->location("age"), m_age);
glUniform1f(shader->location("maxVelocity"), m_maxVelocity);
glUniform1f(shader->location("halfAge"), m_halfAge);
glUniform1f(shader->location("size"), m_particleRadius);
glUniform3f(shader->location("explCenter"), m_center.x, m_center.y, m_center.z);
bind();

View file

@ -22,15 +22,14 @@ namespace endofthejedi {
void setCenter(const glm::vec3 &center);
void setMaxVelocity(float maxVelocity);
void setup(Shader *shader);
void bind();
void upload();
void render();
void render(Shader *shader);
void tick(float dt);
bool done() const;
Shader *shader() { return &m_shader; }
size_t id() const { return m_id; }
private:
@ -55,7 +54,5 @@ namespace endofthejedi {
std::vector<float> m_data_max_age;
GLuint m_attr_locations[5];
Shader m_shader;
};
}

View file

@ -2,11 +2,6 @@
#include <iostream>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/random.hpp>
namespace endofthejedi {
void RendererPolygon3d::setup()
{
@ -14,19 +9,21 @@ namespace endofthejedi {
std::cout<<"setup polygon 3d" << std::endl;
m_shader.init();
#if 0
std::string vss_simple = "../data/shader/simple.vert";
std::string fss_simple = "../data/shader/simple.frag";
m_shader.loadFile(vss_simple, GL_VERTEX_SHADER);
m_shader.loadFile(fss_simple, GL_FRAGMENT_SHADER);
#else
std::string vss_game_objects = "../data/shader/gameobjects.vert";
std::string fss_game_objects = "../data/shader/gameobjects.frag";
m_shader.loadFile(vss_game_objects, GL_VERTEX_SHADER);
m_shader.loadFile(fss_game_objects, GL_FRAGMENT_SHADER);
#endif
m_shader_game_objects.init();
m_shader_game_objects.loadFile(vss_game_objects, GL_VERTEX_SHADER);
m_shader_game_objects.loadFile(fss_game_objects, GL_FRAGMENT_SHADER);
std::string vss_particles = "../data/shader/particle.vert";
std::string fss_particles = "../data/shader/particle.frag";
m_shader_particles.init();
m_shader_particles.loadFile(vss_particles, GL_VERTEX_SHADER);
m_shader_particles.loadFile(fss_particles, GL_FRAGMENT_SHADER);
//addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
addModel("../data/mesh/rocket.stl", &m_missileModel);
@ -56,17 +53,22 @@ namespace endofthejedi {
//float s = 0.1;
//glClearColor(s, s, s, 1.0);
m_shader.bind();
m_shader_game_objects.bind();
// TODO: add ONE sun planet
// TODO: add lights for explosions
configureLightningInShader();
//std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl;
glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio);
renderPlanets();
renderShips();
renderMissiles();
renderParticles();
renderTraces();
//glColor3f(1.0, 0.0, 0.0);
@ -82,9 +84,13 @@ namespace endofthejedi {
void RendererPolygon3d::renderParticles()
{
m_shader_particles.bind();
glUniform1f(m_shader_particles.location("aspectRatio"), m_aspectRatio);
for (ParticleBatch *batch : m_particles) {
batch->bind();
batch->render();
batch->render(&m_shader_particles);
}
}
@ -100,6 +106,7 @@ namespace endofthejedi {
float maxVelocity = 0.4f;
ParticleBatch *batch = new ParticleBatch(id, n, particleRadius, duration);
batch->setup(&m_shader_particles);
batch->setCenter(glm::vec3(pos, 0.0));
batch->setMaxVelocity(maxVelocity);
@ -168,12 +175,12 @@ namespace endofthejedi {
// too (same for missiles)
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));
glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE, glm::value_ptr(model));
glm::vec3 c = planet->getColor();
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
glUniform1i(m_shader.location("materialSeed"), planet->seed);
glUniform1i(m_shader.location("materialKind"), (int) planet->material);
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, c.z);
glUniform1i(m_shader_game_objects.location("materialSeed"), planet->seed);
glUniform1i(m_shader_game_objects.location("materialKind"), (int) planet->material);
m_planetModel->render();
}
@ -186,10 +193,10 @@ namespace endofthejedi {
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);
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, c.z);
glm::mat4 model = computeModelMatrix(missile);
glUniformMatrix4fv(m_shader.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();
}
@ -202,10 +209,10 @@ namespace endofthejedi {
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));
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.location("materialColor"), c.x, c.y, c.z);
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, c.z);
m_shipModel->render();
}
@ -221,7 +228,7 @@ namespace endofthejedi {
exit(-1);
}
(*dest)->setup(&m_shader);
(*dest)->setup(&m_shader_game_objects);
(*dest)->uploadToOpenGl();
m_models.push_back(*dest);
@ -319,7 +326,17 @@ namespace endofthejedi {
}
}
glUniform3f(m_shader.location("lightPosition"), p.x, p.y, p.z);
glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z);
glUniform3f(m_shader_game_objects.location("lightPosition"), p.x, p.y, p.z);
glUniform3f(m_shader_game_objects.location("lightColor"), c.x, c.y, c.z);
}
void RendererPolygon3d::setWindowSize(int px, int py)
{
m_aspectRatio = (float) px / (float) py;
}
void RendererPolygon3d::setCameraMatrix(const glm::mat4 &cam)
{
(void) cam;
}
}

View file

@ -2,6 +2,13 @@
#include <list>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/random.hpp>
#include <glm/vec3.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "glclasses.hpp"
#include "game.hpp"
@ -17,9 +24,6 @@
#include "particle_batch.hpp"
#include "polygon_model.hpp"
#include <glm/vec3.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace endofthejedi {
class RendererPolygon3d : public Renderer {
@ -27,6 +31,9 @@ namespace endofthejedi {
void setup();
void render(const game::State *state) override;
void setWindowSize(int px, int py);
void setCameraMatrix(const glm::mat4 &cam);
private:
void renderPlanets();
void renderMissiles();
@ -61,7 +68,8 @@ namespace endofthejedi {
PolygonModel *m_shipModel;
// for rendering everything
Shader m_shader;
Shader m_shader_game_objects;
Shader m_shader_particles;
// for accessing
const game::State *m_state;
@ -70,5 +78,7 @@ namespace endofthejedi {
// time value for last rendering cycle (-1 after setup/startup)
float m_lastTime;
float m_aspectRatio;
};
}