5#include <glm/gtc/matrix_transform.hpp>
32 : resolution(resx,resy), zoomLevel(1.0f), position(glm::vec2(0.0,0.0))
45 : resolution(resx,resy), zoomLevel(1.0f), position(pos)
60 2.0*x/resolution.x-1.0,
61 2.0*(resolution.y-y)/resolution.y-1.0,
66 return invProjection*ndc;
78 glm::vec4 pos = vp*glm::vec4(x, y, 0.0, 1.0);
79 return glm::vec2( (pos.x+1.0)*resolution.x*0.5, -1.0*((pos.y+1.0)*resolution.y*0.5-resolution.y) );
82 const glm::mat4 &
getVP()
const {
return vp;}
96 zoomLevel < 1.0 ? zoomLevel = 1.0 : 0;
105 void setPosition(glm::vec2 newPosition){position=newPosition; update();}
106 void setPosition(
float x,
float y){position=glm::vec2(x,y); update();}
107 void move(
float dx,
float dy){position += glm::vec2(dx,dy); update();}
114 double maxRes = std::max(resolution.x,resolution.y);
115 modelView = glm::scale(glm::mat4(1.0),glm::vec3(maxRes,maxRes,1.0)) *
118 glm::vec3(position.x,position.y,1.0),
119 glm::vec3(position.x,position.y,0.0),
120 glm::vec3(0.0,1.0,0.0)
123 glm::vec3 center(position.x+0.5,position.y+0.5, 1.0);
124 modelView *= glm::translate(glm::mat4(1.0), center) *
125 glm::scale(glm::mat4(1.0),glm::vec3(zoomLevel,zoomLevel,1.0))*
126 glm::translate(glm::mat4(1.0), -center);
129 projection = glm::ortho(
131 double(resolution.x),
137 vp = projection*modelView;
138 invProjection = glm::inverse(vp);
141 glm::vec2 resolution;
143 glm::mat4 projection;
144 glm::mat4 invProjection;
An orthographic camera for 2D.
Definition orthoCam.h:21
OrthoCam(int resx, int resy)
Construct a new Ortho Cam object.
Definition orthoCam.h:31
float getZoomLevel() const
Definition orthoCam.h:90
glm::vec2 worldToScreen(float x, float y) const
Convert world position to screen coordinate.
Definition orthoCam.h:76
glm::vec4 screenToWorld(float x, float y) const
Convert screen position to world position.
Definition orthoCam.h:57
const glm::mat4 getProjection() const
Definition orthoCam.h:84
void move(float dx, float dy)
Definition orthoCam.h:107
void incrementZoom(float dz)
Definition orthoCam.h:91
glm::vec2 getPosition() const
Definition orthoCam.h:88
void setPosition(float x, float y)
Definition orthoCam.h:106
glm::vec2 getResolution() const
Definition orthoCam.h:86
void setPosition(glm::vec2 newPosition)
Definition orthoCam.h:105
const glm::mat4 & getVP() const
Definition orthoCam.h:82
OrthoCam(int resx, int resy, glm::vec2 pos)
Construct a new Ortho Cam object.
Definition orthoCam.h:44
A drawable graphic.
Definition id.h:10