adding camera to 3d render. angles not yet working.

This commit is contained in:
Andreas Ortmann 2016-10-04 16:09:15 +02:00
parent 14e08db4ca
commit 602d0bb342
12 changed files with 197 additions and 29 deletions

View file

View file

@ -0,0 +1,110 @@
#pragma once
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/random.hpp>
#include <glm/vec3.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace endofthejedi {
// TODO:
// add shaking attributes to camera too
// and dirt effects
class Camera {
public:
Camera() : m_aspectRatio(1.0), m_zoom(1.0), m_position(0.0, 0.0, 0.0), m_angles(0.0, 0.0, 0.0), m_dirty(true)
{
}
// set camera zoom:
// 0 makes no sense,
// << 1 scales down
// 1 is neutral
// >> 1 makes things bigger
void setCameraZoom(float zoom)
{
if (zoom != m_zoom) {
m_zoom = zoom;
m_dirty = true;
}
}
// position of the camera in world space
void setCameraPosition(const glm::vec3 &pos)
{
if (pos != m_position) {
m_position = pos;
m_dirty = true;
}
}
// some angles (in rad) for the camera.
void setCameraAngles(const glm::vec3 &angles)
{
if (angles != m_angles) {
m_angles = angles;
m_dirty = true;
}
}
// all three arguments.
void setCameraPositionAnglesZoom(const glm::vec3 &position, const glm::vec3 &angles, float zoom)
{
setCameraZoom(zoom);
setCameraPosition(position);
setCameraAngles(angles);
}
float zoom() const { return m_zoom; }
const glm::vec3 &position() const { return m_position; }
const glm::vec3 &angles() const { return m_angles; }
float aspectRatio() const { return m_aspectRatio; }
void setAspectRatio(float r)
{
if (r != m_aspectRatio) {
m_aspectRatio = r;
m_dirty = true;
}
}
// recompute view matrix. return true if it was dirty and is now clean
// and recomputed.
bool refresh()
{
if (m_dirty) {
m_dirty = false;
recomputeMatrix();
return true;
}
return false;
}
const glm::mat4 &viewMatrix() const { return m_viewMatrix; }
void recomputeMatrix()
{
glm::mat4 view;
// TODO: rest is smissing
//float a = 2.0*M_PI*m_lastTime/10.0;
//float s = glm::mod(m_lastTime, 5.0f)/1.0f;
view = glm::translate(view, m_position);
view = glm::scale(view, glm::vec3(m_zoom/m_aspectRatio, m_zoom, m_zoom));
m_viewMatrix = view;
}
private:
float m_aspectRatio;
float m_zoom;
glm::vec3 m_position;
glm::vec3 m_angles;
bool m_dirty;
glm::mat4 m_viewMatrix;
};
}

View file

@ -127,7 +127,7 @@ namespace endofthejedi {
glClearColor(0.0, 0.0, 0.0, 1.0);
renderBackgroundImage();
//renderBackgroundImage();
//float s = 0.1;
//glClearColor(s, s, 1.2*s, 1.0);
@ -142,6 +142,12 @@ namespace endofthejedi {
//std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl;
glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio);
m_camera.refresh();
auto *ptr = glm::value_ptr(m_camera.viewMatrix());
glUniformMatrix4fv(m_shader_game_objects.location("view"), 1, GL_FALSE, ptr);
glUniformMatrix4fv(m_shader_particles.location("view"), 1, GL_FALSE, ptr);
renderPlanets();
renderShips();
renderMissiles();
@ -601,7 +607,7 @@ namespace endofthejedi {
}
}
glUniform1i( shader->location("explLightsNum"), numExplLights);
glUniform1i(shader->location("explLightsNum"), numExplLights);
if (numExplLights != 0) {
glUniform3fv(shader->location("explLightsPos"), numExplLights, glm::value_ptr(positions[0]));
@ -611,11 +617,6 @@ namespace endofthejedi {
void RendererPolygon3d::setWindowSize(int px, int py)
{
m_aspectRatio = (float) px / (float) py;
}
void RendererPolygon3d::setCameraMatrix(const glm::mat4 &cam)
{
(void) cam;
m_camera.setAspectRatio((float) px / (float) py);
}
}

View file

@ -3,7 +3,6 @@
#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>
@ -24,6 +23,7 @@
#include "particle_batch.hpp"
#include "polygon_model.hpp"
#include "image_texture.hpp"
#include "camera.hpp"
namespace endofthejedi {
@ -33,7 +33,11 @@ namespace endofthejedi {
void render(const game::State *state) override;
void setWindowSize(int px, int py);
void setCameraMatrix(const glm::mat4 &cam);
//void setCameraMatrix(const glm::mat4 &cam);
// TODO: fov as argument?
Camera &camera();
private:
void renderPlanets();
@ -96,5 +100,7 @@ namespace endofthejedi {
std::string m_backgroundTexturePath;
ImageTexture *m_texture;
Camera m_camera;
};
}