playing more with camera.

This commit is contained in:
Andreas Ortmann 2016-10-04 16:25:23 +02:00
parent 602d0bb342
commit 55274ec3cf
3 changed files with 60 additions and 16 deletions

View file

@ -19,7 +19,7 @@ void main()
vec3 Eye = normalize(-vertex);
vec3 Reflected = normalize(reflect( -lightPosition, normal));
vec3 IAmbient = vec3(0.05f);
vec3 IAmbient = vec3(0.2f);
if (materialKind == 6) {
// sun: shines by itself

View file

@ -13,7 +13,10 @@ namespace endofthejedi {
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)
Camera()
: m_aspectRatio(1.0), m_fov(util::deg2rad(90.0))
, m_zoom(1.0), m_position(0.0, 0.0, 0.0), m_angles(0.0, 0.0, 0.0)
, m_dirty(true)
{
}
@ -22,7 +25,7 @@ namespace endofthejedi {
// << 1 scales down
// 1 is neutral
// >> 1 makes things bigger
void setCameraZoom(float zoom)
void setZoom(float zoom)
{
if (zoom != m_zoom) {
m_zoom = zoom;
@ -30,8 +33,18 @@ namespace endofthejedi {
}
}
// set field of view.
// degree or radian? find it out.
void setFov(float fov)
{
if (fov != m_fov) {
m_fov = fov;
m_dirty = true;
}
}
// position of the camera in world space
void setCameraPosition(const glm::vec3 &pos)
void setPosition(const glm::vec3 &pos)
{
if (pos != m_position) {
m_position = pos;
@ -40,7 +53,7 @@ namespace endofthejedi {
}
// some angles (in rad) for the camera.
void setCameraAngles(const glm::vec3 &angles)
void setAngles(const glm::vec3 &angles)
{
if (angles != m_angles) {
m_angles = angles;
@ -49,18 +62,18 @@ namespace endofthejedi {
}
// all three arguments.
void setCameraPositionAnglesZoom(const glm::vec3 &position, const glm::vec3 &angles, float zoom)
void setPositionAnglesZoom(const glm::vec3 &position, const glm::vec3 &angles, float zoom)
{
setCameraZoom(zoom);
setCameraPosition(position);
setCameraAngles(angles);
setZoom(zoom);
setPosition(position);
setAngles(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; }
float fov() const { return m_fov; }
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)
{
@ -92,14 +105,33 @@ namespace endofthejedi {
//float a = 2.0*M_PI*m_lastTime/10.0;
//float s = glm::mod(m_lastTime, 5.0f)/1.0f;
view = glm::scale(view, glm::vec3(m_zoom, m_zoom, m_zoom));
view = glm::translate(view, m_position);
view = glm::scale(view, glm::vec3(m_zoom/m_aspectRatio, m_zoom, m_zoom));
view = glm::rotate(view, m_angles.x, glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, m_angles.y, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::rotate(view, m_angles.z, glm::vec3(0.0f, 0.0f, 1.0f));
m_viewMatrix = view;
// Generates a really hard-to-read matrix, but a normal, standard 4x4 matrix nonetheless
glm::mat4 projectionMatrix = glm::perspective(
// The horizontal Field of View, in degrees : the amount of
// "zoom". Think "camera lens". Usually between 90° (extra
// wide) and 30° (quite zoomed in)
m_fov,
m_aspectRatio, // aspect ratio
0.1f, // near clipping plane
100.0f // far clipping plane
);
//m_viewMatrix = view;
m_viewMatrix = projectionMatrix * view;
}
private:
float m_aspectRatio;
float m_fov;
float m_zoom;
glm::vec3 m_position;
glm::vec3 m_angles;

View file

@ -142,7 +142,19 @@ namespace endofthejedi {
//std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl;
glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio);
// test code to move the camera
float t = 2.0*M_PI*m_lastTime/10.0;
float r = 0.5;
glm::vec3 pos = glm::vec3(r*sin(t), r*cos(t), 0.0f);
m_camera.setPosition(pos);
// test code to rotate the camera
float a = t;
m_camera.setAngles(glm::vec3(0.0f, a, 0.0f));
// must be done once each frame
m_camera.refresh();
auto *ptr = glm::value_ptr(m_camera.viewMatrix());
glUniformMatrix4fv(m_shader_game_objects.location("view"), 1, GL_FALSE, ptr);