-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
123 lines (90 loc) · 2.8 KB
/
window.cpp
File metadata and controls
123 lines (90 loc) · 2.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
#include "window.h"
#include <iostream>
#include <cstdlib>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "glerror.h"
Window::Window(const char * title, int width, int height){
strcpy(title_, title);
width_ = width;
height_ = height;
}
void Window::Initialize(int argc, char * argv[], int major_gl_version, int minor_gl_version){
InitGlutOrDie(argc, argv, major_gl_version, minor_gl_version);
InitGlewOrDie();
std::cout << "OpenGL initialized: OpenGL version: " << glGetString(GL_VERSION) << " GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
InitModels();
InitPrograms();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
void Window::InitGlutOrDie(int argc, char * argv[], int major_gl_version, int minor_gl_version){
glutInit(&argc, argv);
glutInitContextVersion(major_gl_version, minor_gl_version);
glutInitContextProfile(GLUT_CORE_PROFILE);
#ifdef DEBUG
glutInitContextFlags(GLUT_DEBUG);
#endif
glutSetOption(
GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS
);
glutInitWindowSize(width_, height_);
glutInitDisplayMode(/*GLUT_DEPTH|*/ GLUT_DOUBLE | GLUT_RGBA);
int window_handle = glutCreateWindow(title_);
if( window_handle < 1) {
std::cerr << "ERROR: Could not create a new rendering window" << std::endl;
exit(EXIT_FAILURE);
}
}
void Window::InitGlewOrDie(){
GLenum glew_init_result;
glewExperimental = GL_TRUE;
glew_init_result = glewInit();
if (GLEW_OK != glew_init_result) {
std::cerr << "Glew ERROR: " << glewGetErrorString(glew_init_result) << std::endl;
exit(EXIT_FAILURE);
}
#ifdef DEBUG
if(glDebugMessageCallback){
std::cout << "Register OpenGL debug callback " << std::endl;
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback((GLDEBUGPROC) OpenglCallbackFunction, NULL);
GLuint unused_ids = 0;
glDebugMessageControl(GL_DONT_CARE,
GL_DONT_CARE,
GL_DONT_CARE,
0,
&unused_ids,
GL_FALSE);
}
else
std::cout << "glDebugMessageCallback not available" << std::endl;
#endif
}
void Window::InitModels(){
triangle_.Initialize();
}
void Window::InitPrograms(){
program_.Initialize();
}
void Window::Resize(int new_width, int new_height){
width_ = new_width;
height_ = new_height;
glViewport(0, 0, width_, height_);
glutPostRedisplay();
}
void Window::KeyPressed(unsigned char key, int /*x_coord*/, int /*y_coord*/){
switch (key){
case 27:
glutLeaveMainLoop();
break;
default:
break;
}
}
void Window::Render(){
glClear(GL_COLOR_BUFFER_BIT /*| GL_DEPTH_BUFFER_BIT*/);
triangle_.Draw(program_);
glutSwapBuffers();
// glutPostRedisplay();
}