jGL
Loading...
Searching...
No Matches
orthoCam.h
Go to the documentation of this file.
1#ifndef ORTHOCAM_H
2#define ORTHOCAM_H
3
4#include <glm/glm.hpp>
5#include <glm/gtc/matrix_transform.hpp>
6
7namespace jGL
8{
20 class OrthoCam
21 {
22
23 public:
24
31 OrthoCam(int resx, int resy)
32 : resolution(resx,resy), zoomLevel(1.0f), position(glm::vec2(0.0,0.0))
33 {
34 update();
35 }
36
44 OrthoCam(int resx, int resy, glm::vec2 pos)
45 : resolution(resx,resy), zoomLevel(1.0f), position(pos)
46 {
47 update();
48 }
49
57 glm::vec4 screenToWorld(float x, float y) const
58 {
59 glm::vec4 ndc(
60 2.0*x/resolution.x-1.0,
61 2.0*(resolution.y-y)/resolution.y-1.0,
62 0.0,
63 1.0
64 );
65
66 return invProjection*ndc;
67 }
68
76 glm::vec2 worldToScreen(float x, float y) const
77 {
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) );
80 }
81
82 const glm::mat4 & getVP() const {return vp;}
83
84 const glm::mat4 getProjection() const {return projection;}
85
86 glm::vec2 getResolution() const {return resolution;}
87
88 glm::vec2 getPosition() const {return position;}
89
90 float getZoomLevel() const {return zoomLevel;}
91 void incrementZoom(float dz)
92 {
93 if (zoomLevel >= 1.0)
94 {
95 zoomLevel += dz;
96 zoomLevel < 1.0 ? zoomLevel = 1.0 : 0;
97 }
98 else
99 {
100 zoomLevel += 1.0/dz;
101 }
102 update();
103
104 }
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();}
108
109 private:
110
111 void update()
112 {
113 // scale equally by screen width (all lengths relative to this)
114 double maxRes = std::max(resolution.x,resolution.y);
115 modelView = glm::scale(glm::mat4(1.0),glm::vec3(maxRes,maxRes,1.0)) *
116 // move to position and look at x-y plane from z=1, with up = y axis
117 glm::lookAt(
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)
121 );
122
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);
127
128 // finally, project to the screen (ndc)
129 projection = glm::ortho(
130 0.0,
131 double(resolution.x),
132 0.0,
133 double(resolution.y)
134 );
135
136
137 vp = projection*modelView;
138 invProjection = glm::inverse(vp);
139 }
140
141 glm::vec2 resolution;
142 glm::mat4 modelView;
143 glm::mat4 projection;
144 glm::mat4 invProjection;
145 glm::mat4 vp;
146
147 float zoomLevel;
148
149 glm::vec2 position;
150 };
151}
152
153#endif
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