-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRenderWindow_GLUT.cpp
235 lines (192 loc) · 6.44 KB
/
RenderWindow_GLUT.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "stdafx.h"
#include <ctime>
#include <exception>
#include <gl/glew.h>
#include <gl/glut.h>
#include <windows.h>
#include "RenderWindow.h"
static RenderWindow *gWindow = nullptr;
GLuint gPboArray[2] = {0};
int gPboIndex = 0;
GLuint gTexture = 0;
static void SetupTextureAndPbo(int width, int height)
{
if (gTexture != 0)
{
glDeleteTextures(1, &gTexture);
glDeleteBuffers(sizeof(gPboArray) / sizeof(gPboArray[0]), gPboArray);
gTexture = 0;
}
glGenTextures(1, &gTexture);
glBindTexture(GL_TEXTURE_2D, gTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
glGenBuffers(sizeof(gPboArray) / sizeof(gPboArray[0]), gPboArray);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gPboArray[0]);
glBufferData(GL_PIXEL_UNPACK_BUFFER, width * height * 4, nullptr, GL_STREAM_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gPboArray[1]);
glBufferData(GL_PIXEL_UNPACK_BUFFER, width * height * 4, nullptr, GL_STREAM_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}
static void OnDisplay()
{
if (gWindow->RenderToBuffer())
{
int prevIndex = gPboIndex;
gPboIndex = 1 - gPboIndex;
glBindTexture(GL_TEXTURE_2D, gTexture);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gPboArray[prevIndex]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, gWindow->GetWidth(), gWindow->GetHeight(), GL_BGRA, GL_UNSIGNED_BYTE, nullptr);
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glNormal3f(0, 0, 1);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
int size = gWindow->GetWidth() * gWindow->GetHeight() * 4;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gPboArray[gPboIndex]);
glBufferData(GL_PIXEL_UNPACK_BUFFER, size, nullptr, GL_STREAM_DRAW);
auto ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
if (ptr)
{
gWindow->Render(reinterpret_cast<int*>(ptr));
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); // release pointer to mapping buffer
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
else
{
gWindow->Render(nullptr);
}
glutSwapBuffers();
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
GLubyte const *message = gluErrorString(err);
throw std::exception(reinterpret_cast<char const *>(message));
}
}
static clock_t gLastTick = clock();
static void OnIdle()
{
clock_t now = clock();
float elapse = float(now - gLastTick) / CLOCKS_PER_SEC;
gLastTick = now;
gWindow->Update(elapse);
glutPostRedisplay();
}
static void OnReshape(int width, int height)
{
width = max(width, 1);
height = max(height, 1);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
gWindow->Resize(width, height);
SetupTextureAndPbo(width, height);
}
static void OnKeyDown(unsigned char key, int x, int y)
{
gWindow->KeyDown(key >= 'a' && key <= 'z' ? key - 'a' + 'A' : key);
}
static void OnKeyUp(unsigned char key, int x, int y)
{
// esc
if (key == 0x1b)
{
gWindow->Cleanup();
exit(0);
}
gWindow->KeyUp(key >= 'a' && key <= 'z' ? key - 'a' + 'A' : key);
}
static void OnMouseFunc(int button, int state, int x, int y)
{
float fx = x / float(gWindow->GetWidth()), fy = y / float(gWindow->GetHeight());
if (state == GLUT_UP)
{
if (button == GLUT_LEFT_BUTTON)
{
gWindow->MouseButtonUp(RenderWindow::MouseButton::Left, fx, fy);
}
else if (button == GLUT_RIGHT_BUTTON)
{
gWindow->MouseButtonUp(RenderWindow::MouseButton::Right, fx, fy);
}
}
else if (state == GLUT_DOWN)
{
if (button == GLUT_LEFT_BUTTON)
{
gWindow->MouseButtonDown(RenderWindow::MouseButton::Left, fx, fy);
}
else if (button == GLUT_RIGHT_BUTTON)
{
gWindow->MouseButtonDown(RenderWindow::MouseButton::Right, fx, fy);
}
}
}
static void OnMouseMove(int x, int y)
{
gWindow->MouseMove(x / float(gWindow->GetWidth()), y / float(gWindow->GetHeight()));
}
RenderWindow::RenderWindow(char const *title, int width, int height)
: mTitle(title), mWidth(width), mHeight(height)
{
gWindow = this;
}
RenderWindow::~RenderWindow()
{
gWindow = nullptr;
}
void RenderWindow::Run(char *argv[], int argc)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(mWidth, mHeight);
glutInitWindowPosition(
(GetSystemMetrics(SM_CXSCREEN) - mWidth) / 2,
(GetSystemMetrics(SM_CYSCREEN) - mHeight) / 2);
glutCreateWindow(mTitle.c_str());
cout << "GL Version: " << glGetString(GL_VERSION) << endl;
cout << "GL Shading Language Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl;
glewInit();
glutDisplayFunc(OnDisplay);
glutIdleFunc(OnIdle);
glutReshapeFunc(OnReshape);
glutKeyboardFunc(OnKeyDown);
glutKeyboardUpFunc(OnKeyUp);
glutMouseFunc(OnMouseFunc);
glutMotionFunc(OnMouseMove);
typedef void(__stdcall *PFNWGLEXTSWAPCONTROLPROC) (int);
PFNWGLEXTSWAPCONTROLPROC wglSwapIntervalEXT = NULL;
if (strstr(const_cast<char*>(reinterpret_cast<char const*>(glGetString(GL_EXTENSIONS))), "WGL_EXT_swap_control") != nullptr)
{
wglSwapIntervalEXT = reinterpret_cast<PFNWGLEXTSWAPCONTROLPROC>(wglGetProcAddress("wglSwapIntervalEXT"));
wglSwapIntervalEXT(0);
}
glEnable(GL_TEXTURE_2D);
SetupTextureAndPbo(mWidth, mHeight);
Setup();
try
{
glutMainLoop();
}
catch (std::exception const &e)
{
cout << "Found exception : " << e.what() << endl;
cin.get();
}
Cleanup();
glDeleteTextures(1, &gTexture);
glDeleteBuffers(sizeof(gPboArray) / sizeof(gPboArray[0]), gPboArray);
gTexture = 0;
}