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 void setResolution(glm::ivec2 wh){resolution = wh; update();}
109
110 private:
111
112 void update()
113 {
114 // scale equally by screen width (all lengths relative to this)
115 double maxRes = std::max(resolution.x,resolution.y);
116 modelView = glm::scale(glm::mat4(1.0),glm::vec3(maxRes,maxRes,1.0)) *
117 // move to position and look at x-y plane from z=1, with up = y axis
118 glm::lookAt(
119 glm::vec3(position.x,position.y,1.0),
120 glm::vec3(position.x,position.y,0.0),
121 glm::vec3(0.0,1.0,0.0)
122 );
123
124 glm::vec3 center(position.x+0.5,position.y+0.5, 1.0);
125 modelView *= glm::translate(glm::mat4(1.0), center) *
126 glm::scale(glm::mat4(1.0),glm::vec3(zoomLevel,zoomLevel,1.0))*
127 glm::translate(glm::mat4(1.0), -center);
128
129 // finally, project to the screen (ndc)
130 projection = glm::ortho(
131 0.0,
132 double(resolution.x),
133 0.0,
134 double(resolution.y)
135 );
136
137
138 vp = projection*modelView;
139 invProjection = glm::inverse(vp);
140 }
141
142 glm::vec2 resolution;
143 glm::mat4 modelView;
144 glm::mat4 projection;
145 glm::mat4 invProjection;
146 glm::mat4 vp;
147
148 float zoomLevel;
149
150 glm::vec2 position;
151 };
152}
153
154#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 setResolution(glm::ivec2 wh)
Definition orthoCam.h:108
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