-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.cpp
More file actions
350 lines (292 loc) · 8.61 KB
/
graphics.cpp
File metadata and controls
350 lines (292 loc) · 8.61 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <unistd.h>
#include <iostream>
#include "bcm_host.h"
#include "graphics.h"
#define check() assert(glGetError() == 0)
uint32_t GScreenWidth;
uint32_t GScreenHeight;
EGLDisplay GDisplay;
EGLSurface GSurface;
EGLContext GContext;
GfxShader GSimpleVS;
GfxShader GSimpleFS;
GfxProgram GSimpleProg;
GLuint GQuadVertexBuffer;
void InitGraphics()
{
bcm_host_init();
int32_t success = 0;
EGLBoolean result;
EGLint num_config;
static EGL_DISPMANX_WINDOW_T nativewindow;
DISPMANX_ELEMENT_HANDLE_T dispman_element;
DISPMANX_DISPLAY_HANDLE_T dispman_display;
DISPMANX_UPDATE_HANDLE_T dispman_update;
VC_RECT_T dst_rect;
VC_RECT_T src_rect;
static const EGLint attribute_list[] =
{
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
static const EGLint context_attributes[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLConfig config;
// get an EGL display connection
GDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
assert(GDisplay!=EGL_NO_DISPLAY);
check();
// initialize the EGL display connection
result = eglInitialize(GDisplay, NULL, NULL);
assert(EGL_FALSE != result);
check();
// get an appropriate EGL frame buffer configuration
result = eglChooseConfig(GDisplay, attribute_list, &config, 1, &num_config);
assert(EGL_FALSE != result);
check();
// get an appropriate EGL frame buffer configuration
result = eglBindAPI(EGL_OPENGL_ES_API);
assert(EGL_FALSE != result);
check();
// create an EGL rendering context
GContext = eglCreateContext(GDisplay, config, EGL_NO_CONTEXT, context_attributes);
assert(GContext!=EGL_NO_CONTEXT);
check();
// create an EGL window surface
success = graphics_get_display_size(0 /* LCD */, &GScreenWidth, &GScreenHeight);
assert( success >= 0 );
dst_rect.x = 0;
dst_rect.y = 0;
dst_rect.width = GScreenWidth;
dst_rect.height = GScreenHeight;
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = GScreenWidth << 16;
src_rect.height = GScreenHeight << 16;
dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
dispman_update = vc_dispmanx_update_start( 0 );
dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display,
0/*layer*/, &dst_rect, 0/*src*/,
&src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, (DISPMANX_TRANSFORM_T)0/*transform*/);
nativewindow.element = dispman_element;
nativewindow.width = GScreenWidth;
nativewindow.height = GScreenHeight;
vc_dispmanx_update_submit_sync( dispman_update );
check();
GSurface = eglCreateWindowSurface( GDisplay, config, &nativewindow, NULL );
assert(GSurface != EGL_NO_SURFACE);
check();
// connect the context to the surface
result = eglMakeCurrent(GDisplay, GSurface, GSurface, GContext);
assert(EGL_FALSE != result);
check();
// Set background color and clear buffers
glClearColor(0.15f, 0.25f, 0.35f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT );
//load the test shaders
GSimpleVS.LoadVertexShader("simplevertshader.glsl");
GSimpleFS.LoadFragmentShader("simplefragshader.glsl");
GSimpleProg.Create(&GSimpleVS,&GSimpleFS);
check();
glUseProgram(GSimpleProg.GetId());
check();
//create an ickle vertex buffer
static const GLfloat quad_vertex_positions[] = {
0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
glGenBuffers(1, &GQuadVertexBuffer);
check();
glBindBuffer(GL_ARRAY_BUFFER, GQuadVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertex_positions), quad_vertex_positions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
check();
}
void BeginFrame()
{
// Prepare viewport
glViewport ( 0, 0, GScreenWidth, GScreenHeight );
check();
// Clear the background
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
check();
}
void EndFrame()
{
eglSwapBuffers(GDisplay,GSurface);
check();
}
void ReleaseGraphics()
{
}
// printShaderInfoLog
// From OpenGL Shading Language 3rd Edition, p215-216
// Display (hopefully) useful error messages if shader fails to compile
void printShaderInfoLog(GLint shader)
{
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLen);
if (infoLogLen > 0)
{
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetShaderInfoLog(shader, infoLogLen, &charsWritten, infoLog);
std::cout << "InfoLog : " << std::endl << infoLog << std::endl;
delete [] infoLog;
}
}
bool GfxShader::LoadVertexShader(const char* filename)
{
//cheeky bit of code to read the whole file into memory
assert(!Src);
FILE* f = fopen(filename, "rb");
assert(f);
fseek(f,0,SEEK_END);
int sz = ftell(f);
fseek(f,0,SEEK_SET);
Src = new GLchar[sz+1];
fread(Src,1,sz,f);
Src[sz] = 0; //null terminate it!
fclose(f);
//now create and compile the shader
GlShaderType = GL_VERTEX_SHADER;
Id = glCreateShader(GlShaderType);
glShaderSource(Id, 1, (const GLchar**)&Src, 0);
glCompileShader(Id);
check();
//compilation check
GLint compiled;
glGetShaderiv(Id, GL_COMPILE_STATUS, &compiled);
if(compiled==0)
{
printf("Failed to compile vertex shader %s:\n%s\n", filename, Src);
printShaderInfoLog(Id);
glDeleteShader(Id);
return false;
}
else
{
printf("Compiled vertex shader %s:\n%s\n", filename, Src);
}
return true;
}
bool GfxShader::LoadFragmentShader(const char* filename)
{
//cheeky bit of code to read the whole file into memory
assert(!Src);
FILE* f = fopen(filename, "rb");
assert(f);
fseek(f,0,SEEK_END);
int sz = ftell(f);
fseek(f,0,SEEK_SET);
Src = new GLchar[sz+1];
fread(Src,1,sz,f);
Src[sz] = 0; //null terminate it!
fclose(f);
//now create and compile the shader
GlShaderType = GL_FRAGMENT_SHADER;
Id = glCreateShader(GlShaderType);
glShaderSource(Id, 1, (const GLchar**)&Src, 0);
glCompileShader(Id);
check();
//compilation check
GLint compiled;
glGetShaderiv(Id, GL_COMPILE_STATUS, &compiled);
if(compiled==0)
{
printf("Failed to compile fragment shader %s:\n%s\n", filename, Src);
printShaderInfoLog(Id);
glDeleteShader(Id);
return false;
}
else
{
printf("Compiled fragment shader %s:\n%s\n", filename, Src);
}
return true;
}
bool GfxProgram::Create(GfxShader* vertex_shader, GfxShader* fragment_shader)
{
VertexShader = vertex_shader;
FragmentShader = fragment_shader;
Id = glCreateProgram();
glAttachShader(Id, VertexShader->GetId());
glAttachShader(Id, FragmentShader->GetId());
glLinkProgram(Id);
check();
printf("Created program id %d from vs %d and fs %d\n", GetId(), VertexShader->GetId(), FragmentShader->GetId());
// Prints the information log for a program object
char log[1024];
glGetProgramInfoLog(Id,sizeof log,NULL,log);
printf("%d:program:\n%s\n", Id, log);
return true;
}
void DrawTextureRect(GfxTexture* texture, float x0, float y0, float x1, float y1)
{
glUseProgram(GSimpleProg.GetId());
check();
glUniform2f(glGetUniformLocation(GSimpleProg.GetId(),"offset"),x0,y0);
glUniform2f(glGetUniformLocation(GSimpleProg.GetId(),"scale"),x1-x0,y1-y0);
glUniform1i(glGetUniformLocation(GSimpleProg.GetId(),"tex"), 0);
check();
glBindBuffer(GL_ARRAY_BUFFER, GQuadVertexBuffer);
check();
glBindTexture(GL_TEXTURE_2D,texture->GetId());
check();
GLuint loc = glGetAttribLocation(GSimpleProg.GetId(),"vertex");
check();
glVertexAttribPointer(loc, 4, GL_FLOAT, 0, 16, 0);
check();
glEnableVertexAttribArray(loc);
check();
glDrawArrays ( GL_TRIANGLE_STRIP, 0, 4 );
check();
glFinish();
check();
glFlush();
check();
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
bool GfxTexture::Create(int width, int height, const void* data)
{
Width = width;
Height = height;
glGenTextures(1, &Id);
check();
glBindTexture(GL_TEXTURE_2D, Id);
check();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
check();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLfloat)GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLfloat)GL_NEAREST);
check();
glBindTexture(GL_TEXTURE_2D, 0);
return true;
}
void GfxTexture::SetPixels(const void* data)
{
glBindTexture(GL_TEXTURE_2D, Id);
check();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, data);
check();
glBindTexture(GL_TEXTURE_2D, 0);
check();
}