-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcube.c
More file actions
310 lines (263 loc) · 8.62 KB
/
cube.c
File metadata and controls
310 lines (263 loc) · 8.62 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
/*
* This proprietary software may be used only as
* authorised by a licensing agreement from ARM Limited
* (C) COPYRIGHT 2009 - 2011 ARM Limited
* ALL RIGHTS RESERVED
* The entire notice above must be reproduced on all authorised
* copies and copies may only be made to the extent permitted
* by a licensing agreement from ARM Limited.
*/
#include "cube.h"
#include "shader.h"
#include "window.h"
#include "matrix.h"
#include "GLES2/gl2.h"
#include "EGL/egl.h"
HWND hWindow;
HDC hDisplay;
/* 3D data. Vertex range -0.5..0.5 in all axes.
* Z -0.5 is near, 0.5 is far. */
const float aVertices[] =
{
/* Front face. */
/* Bottom left */
-0.5, 0.5, -0.5,
0.5, -0.5, -0.5,
-0.5, -0.5, -0.5,
/* Top right */
-0.5, 0.5, -0.5,
0.5, 0.5, -0.5,
0.5, -0.5, -0.5,
/* Left face */
/* Bottom left */
-0.5, 0.5, 0.5,
-0.5, -0.5, -0.5,
-0.5, -0.5, 0.5,
/* Top right */
-0.5, 0.5, 0.5,
-0.5, 0.5, -0.5,
-0.5, -0.5, -0.5,
/* Top face */
/* Bottom left */
-0.5, 0.5, 0.5,
0.5, 0.5, -0.5,
-0.5, 0.5, -0.5,
/* Top right */
-0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, -0.5,
/* Right face */
/* Bottom left */
0.5, 0.5, -0.5,
0.5, -0.5, 0.5,
0.5, -0.5, -0.5,
/* Top right */
0.5, 0.5, -0.5,
0.5, 0.5, 0.5,
0.5, -0.5, 0.5,
/* Back face */
/* Bottom left */
0.5, 0.5, 0.5,
-0.5, -0.5, 0.5,
0.5, -0.5, 0.5,
/* Top right */
0.5, 0.5, 0.5,
-0.5, 0.5, 0.5,
-0.5, -0.5, 0.5,
/* Bottom face */
/* Bottom left */
-0.5, -0.5, -0.5,
0.5, -0.5, 0.5,
-0.5, -0.5, 0.5,
/* Top right */
-0.5, -0.5, -0.5,
0.5, -0.5, -0.5,
0.5, -0.5, 0.5,
};
const float aColours[] =
{
/* Front face */
/* Bottom left */
1.0, 0.0, 0.0, /* red */
0.0, 0.0, 1.0, /* blue */
0.0, 1.0, 0.0, /* green */
/* Top right */
1.0, 0.0, 0.0, /* red */
1.0, 1.0, 0.0, /* yellow */
0.0, 0.0, 1.0, /* blue */
/* Left face */
/* Bottom left */
1.0, 1.0, 1.0, /* white */
0.0, 1.0, 0.0, /* green */
0.0, 1.0, 1.0, /* cyan */
/* Top right */
1.0, 1.0, 1.0, /* white */
1.0, 0.0, 0.0, /* red */
0.0, 1.0, 0.0, /* green */
/* Top face */
/* Bottom left */
1.0, 1.0, 1.0, /* white */
1.0, 1.0, 0.0, /* yellow */
1.0, 0.0, 0.0, /* red */
/* Top right */
1.0, 1.0, 1.0, /* white */
0.0, 0.0, 0.0, /* black */
1.0, 1.0, 0.0, /* yellow */
/* Right face */
/* Bottom left */
1.0, 1.0, 0.0, /* yellow */
1.0, 0.0, 1.0, /* magenta */
0.0, 0.0, 1.0, /* blue */
/* Top right */
1.0, 1.0, 0.0, /* yellow */
0.0, 0.0, 0.0, /* black */
1.0, 0.0, 1.0, /* magenta */
/* Back face */
/* Bottom left */
0.0, 0.0, 0.0, /* black */
0.0, 1.0, 1.0, /* cyan */
1.0, 0.0, 1.0, /* magenta */
/* Top right */
0.0, 0.0, 0.0, /* black */
1.0, 1.0, 1.0, /* white */
0.0, 1.0, 1.0, /* cyan */
/* Bottom face */
/* Bottom left */
0.0, 1.0, 0.0, /* green */
1.0, 0.0, 1.0, /* magenta */
0.0, 1.0, 1.0, /* cyan */
/* Top right */
0.0, 1.0, 0.0, /* green */
0.0, 0.0, 1.0, /* blue */
1.0, 0.0, 1.0, /* magenta */
};
int main(int argc, char **argv) {
EGLDisplay sEGLDisplay;
EGLContext sEGLContext;
EGLSurface sEGLSurface;
/* EGL Configuration */
EGLint aEGLAttributes[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLint aEGLContextAttributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLConfig aEGLConfigs[1];
EGLint cEGLConfigs;
MSG sMessage;
GLint iLocPosition = 0;
GLint iLocColour, iLocMVP;
GLuint uiProgram, uiFragShader, uiVertShader;
int bDone = 0;
const unsigned int uiWidth = 640;
const unsigned int uiHeight = 480;
int iXangle = 0, iYangle = 0, iZangle = 0;
float aLightPos[] = { 0.0f, 0.0f, -1.0f }; // Light is nearest camera.
unsigned char *myPixels = calloc(1, 128*128*4); // Holds texture data.
unsigned char *myPixels2 = calloc(1, 128*128*4); // Holds texture data.
float aRotate[16], aModelView[16], aPerspective[16], aMVP[16];
hDisplay = EGL_DEFAULT_DISPLAY;
sEGLDisplay = EGL_CHECK(eglGetDisplay(hDisplay));
EGL_CHECK(eglInitialize(sEGLDisplay, NULL, NULL));
EGL_CHECK(eglChooseConfig(sEGLDisplay, aEGLAttributes, aEGLConfigs, 1, &cEGLConfigs));
if (cEGLConfigs == 0) {
printf("No EGL configurations were returned.\n");
exit(-1);
}
hWindow = create_window(uiWidth, uiHeight);
sEGLSurface = EGL_CHECK(eglCreateWindowSurface(sEGLDisplay,
aEGLConfigs[0], (EGLNativeWindowType)hWindow, NULL));
if (sEGLSurface == EGL_NO_SURFACE) {
printf("Failed to create EGL surface.\n");
exit(-1);
}
sEGLContext = EGL_CHECK(eglCreateContext(sEGLDisplay,
aEGLConfigs[0], EGL_NO_CONTEXT, aEGLContextAttributes));
if (sEGLContext == EGL_NO_CONTEXT) {
printf("Failed to create EGL context.\n");
exit(-1);
}
EGL_CHECK(eglMakeCurrent(sEGLDisplay, sEGLSurface, sEGLSurface, sEGLContext));
/* Shader Initialisation */
process_shader(&uiVertShader, "shader.vert", GL_VERTEX_SHADER);
process_shader(&uiFragShader, "shader.frag", GL_FRAGMENT_SHADER);
/* Create uiProgram (ready to attach shaders) */
uiProgram = GL_CHECK(glCreateProgram());
/* Attach shaders and link uiProgram */
GL_CHECK(glAttachShader(uiProgram, uiVertShader));
GL_CHECK(glAttachShader(uiProgram, uiFragShader));
GL_CHECK(glLinkProgram(uiProgram));
/* Get attribute locations of non-fixed attributes like colour and texture coordinates. */
iLocPosition = GL_CHECK(glGetAttribLocation(uiProgram, "av4position"));
iLocColour = GL_CHECK(glGetAttribLocation(uiProgram, "av3colour"));
#ifdef DEBUG
printf("iLocPosition = %i\n", iLocPosition);
printf("iLocColour = %i\n", iLocColour);
#endif
/* Get uniform locations */
iLocMVP = GL_CHECK(glGetUniformLocation(uiProgram, "mvp"));
#ifdef DEBUG
printf("iLocMVP = %i\n", iLocMVP);
#endif
GL_CHECK(glUseProgram(uiProgram));
/* Enable attributes for position, colour and texture coordinates etc. */
GL_CHECK(glEnableVertexAttribArray(iLocPosition));
GL_CHECK(glEnableVertexAttribArray(iLocColour));
/* Populate attributes for position, colour and texture coordinates etc. */
GL_CHECK(glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, 0, aVertices));
GL_CHECK(glVertexAttribPointer(iLocColour, 3, GL_FLOAT, GL_FALSE, 0, aColours));
GL_CHECK(glEnable(GL_CULL_FACE));
GL_CHECK(glEnable(GL_DEPTH_TEST));
/* Enter event loop */
while (!bDone) {
if(PeekMessage(&sMessage, NULL, 0, 0, PM_REMOVE)) {
if(sMessage.message == WM_QUIT) {
bDone = 1;
} else {
TranslateMessage(&sMessage);
DispatchMessage(&sMessage);
}
}
rotate_matrix(iXangle, 1.0, 0.0, 0.0, aModelView);
rotate_matrix(iYangle, 0.0, 1.0, 0.0, aRotate);
multiply_matrix(aRotate, aModelView, aModelView);
rotate_matrix(iZangle, 0.0, 1.0, 0.0, aRotate);
multiply_matrix(aRotate, aModelView, aModelView);
aModelView[14] -= 2.5;
perspective_matrix(45.0, (double)uiWidth/(double)uiHeight, 0.01, 100.0, aPerspective);
multiply_matrix(aPerspective, aModelView, aMVP);
GL_CHECK(glUniformMatrix4fv(iLocMVP, 1, GL_FALSE, aMVP));
iXangle += 3;
iYangle += 2;
iZangle += 1;
if(iXangle >= 360) iXangle -= 360;
if(iXangle < 0) iXangle += 360;
if(iYangle >= 360) iYangle -= 360;
if(iYangle < 0) iYangle += 360;
if(iZangle >= 360) iZangle -= 360;
if(iZangle < 0) iZangle += 360;
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36));
if (!eglSwapBuffers(sEGLDisplay, sEGLSurface)) {
printf("Failed to swap buffers.\n");
}
Sleep(20);
}
/* Cleanup shaders */
GL_CHECK(glUseProgram(0));
GL_CHECK(glDeleteShader(uiVertShader));
GL_CHECK(glDeleteShader(uiFragShader));
GL_CHECK(glDeleteProgram(uiProgram));
/* EGL clean up */
EGL_CHECK(eglMakeCurrent(sEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EGL_CHECK(eglDestroySurface(sEGLDisplay, sEGLSurface));
EGL_CHECK(eglDestroyContext(sEGLDisplay, sEGLContext));
EGL_CHECK(eglTerminate(sEGLDisplay));
return 0;
}