-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
183 lines (131 loc) · 4.8 KB
/
Copy pathWindow.cpp
File metadata and controls
183 lines (131 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "Window.h"
//
// Print GLFW errors to stderr
//
static void error(int error,const char* text)
{
fprintf(stderr,"GLFW error %d: %s\n",error,text);
}
namespace Window
{
//private
namespace
{
GLFWwindow* window;
Scene* scene;
double aspectRatio = 1;
int width, height;
glm::mat4 projection;
float fov = 55;
void Reshape(GLFWwindow* window,int width,int height)
{
//taken from ex06
// Get framebuffer dimensions (makes Apple work right)
glfwGetFramebufferSize(window, &width, &height);
// Ratio of the width to the height of the window
aspectRatio = (height>0) ? (double)width/height : 1;
// Set the viewport to the entire window
glViewport(0,0, width,height);
Window::width = width;
Window::height = height;
// Set projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Perspective transformation
gluPerspective(fov,aspectRatio,NEAR_WORLD_PLANE, FAR_WORLD_PLANE);
// Reset modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
projection = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_WORLD_PLANE, FAR_WORLD_PLANE);
}
//
// Initialize GLFW, GLEW and launch window
//
GLFWwindow* InitWindow(const char* title,int sync,int width,int height , void (*reshape)(GLFWwindow*,int,int) , void (*key)(GLFWwindow*,int,int,int,int))
{
// Initialize GLFW
if (!glfwInit()) ErrorHandler::Fatal("Cannot initialize glfw\n");
// Error callback
glfwSetErrorCallback(error);
// Set window properties
glfwWindowHint(GLFW_RESIZABLE,1); // Window can be resized
glfwWindowHint(GLFW_DOUBLEBUFFER,1); // Window has double buffering
glfwWindowHint(GLFW_DEPTH_BITS,24); // Prefer 24 depth bits
glfwWindowHint(GLFW_ALPHA_BITS,8); // Prefer 8 alpha bits
#ifdef APPLE_GL4
// Apple specific window properties to use with GLEW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
#endif
// Create window and make current
GLFWwindow* window = glfwCreateWindow(width,height,title, NULL, NULL);
if (!window) ErrorHandler::Fatal("Cannot create GLFW window\n");
glfwMakeContextCurrent(window);
// Enable VSYNC (if selected)
glfwSwapInterval(sync);
#ifdef USEGLEW
// Initialize GLEW (if compiled in)
if (glewInit()!=GLEW_OK) ErrorHandler::Fatal("Error initializing GLEW\n");
#endif
// Set callback for window resize and make initial call
if (reshape)
{
glfwSetWindowSizeCallback(window,reshape);
// Set initial window size and call reshape
glfwGetWindowSize(window,&width,&height);
reshape(window,width,height);
}
// Set callback for keyboard input
if (key) glfwSetKeyCallback(window,key);
// Return window
return window;
}
}
//public
void Init(std::string name, bool vSync, int width, int height)
{
Window::width = width;
Window::height = height;
window = InitWindow(name.c_str(), vSync, width, height, &Reshape, NULL);
glfwMakeContextCurrent(window);
scene = new Scene();
Camera::Init(window, glm::vec3(32.0, 104.0, 40.0));
Time::Init(true);
Input::InitInput(window);
}
void Start()
{
//TextureHandler::Preload();
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
ErrorHandler::ErrCheck("init");
while(!glfwWindowShouldClose(window))
{
//update time to get fps and delta time
Time::Update();
//handle input
Input::ProcessInput();
Camera::Update();
// Process any events
scene->Render(projection);
scene->RenderUI();
Input::ClearOldInputs();
ErrorHandler::ErrCheck("display");
glFlush();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
}
int GetWidth()
{
return width;
}
int GetHeight()
{
return height;
}
}