-
Notifications
You must be signed in to change notification settings - Fork 1
/
main-ogl.cpp
executable file
·234 lines (179 loc) · 6.51 KB
/
main-ogl.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
#define MLSDK20
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <cmath>
#ifndef EGL_EGLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#endif
#include <EGL/egl.h>
#include <EGL/eglext.h>
#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#include <ml_logging.h>
#include <ml_graphics.h>
#include <ml_lifecycle.h>
#include <ml_perception.h>
// -----------------------------------------------------------------------------
// 1. Types and definitions
const char APP_TAG[] = "MAIN";
struct graphics_context_t {
EGLDisplay egl_display;
EGLContext egl_context;
GLuint framebuffer_id;
graphics_context_t();
~graphics_context_t();
void makeCurrent();
void unmakeCurrent();
};
// -----------------------------------------------------------------------------
// 2. OpenGL context functions
graphics_context_t::graphics_context_t() {
egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint major = 4;
EGLint minor = 0;
eglInitialize(egl_display, &major, &minor);
eglBindAPI(EGL_OPENGL_API);
EGLint config_attribs[] = {
EGL_RED_SIZE, 5,
EGL_GREEN_SIZE, 6,
EGL_BLUE_SIZE, 5,
EGL_ALPHA_SIZE, 0,
EGL_DEPTH_SIZE, 24,
EGL_STENCIL_SIZE, 8,
EGL_NONE
};
EGLConfig egl_config = nullptr;
EGLint config_size = 0;
eglChooseConfig(egl_display, config_attribs, &egl_config, 1, &config_size);
EGLint context_attribs[] = {
EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
EGL_CONTEXT_MINOR_VERSION_KHR, 0,
EGL_NONE
};
egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, context_attribs);
}
void graphics_context_t::makeCurrent() {
eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context);
}
void graphics_context_t::unmakeCurrent() {
eglMakeCurrent(NULL, EGL_NO_SURFACE, EGL_NO_SURFACE, NULL);
}
graphics_context_t::~graphics_context_t() {
eglDestroyContext(egl_display, egl_context);
eglTerminate(egl_display);
}
// -----------------------------------------------------------------------------
// 3. App Lifecycle callback functions
static void on_stop(void* user_data) {
ML_LOG_TAG(Info, APP_TAG, "Lifecycle call on_stop()");
}
static void on_pause(void* user_data) {
ML_LOG_TAG(Info, APP_TAG, "Lifecycle call on_pause()");
}
static void on_resume(void* user_data) {
ML_LOG_TAG(Info, APP_TAG, "Lifecycle call on_resume()");
}
// Structures
struct application_context_t {
int dummy_value;
};
// -----------------------------------------------------------------------------
// 4. Main
int main() {
graphics_context_t graphics_context;
MLLoggingEnableLogLevel(MLLogLevel_Debug);
// 5. Assign call application lifecycle callback functions
MLLifecycleCallbacks lifecycle_callbacks = {};
lifecycle_callbacks.on_stop = on_stop;
lifecycle_callbacks.on_pause = on_pause;
lifecycle_callbacks.on_resume = on_resume;
struct application_context_t application_context;
application_context.dummy_value = 2;
// 6. Initialize application lifecycle
MLResult result = MLLifecycleInit(&lifecycle_callbacks, (void*)&application_context);
if (result != MLResult_Ok) {
ML_LOG_TAG(Error, APP_TAG, "Failed to initialize lifecycle system");
return -1;
}
else {
ML_LOG_TAG(Debug, APP_TAG, "Lifecycle system started");
}
// 7. Initialize perception system
MLPerceptionSettings perception_settings;
if (MLResult_Ok != MLPerceptionInitSettings(&perception_settings)) {
ML_LOG_TAG(Error, APP_TAG, "Failed to initialize perception");
return -1;
}
if (MLResult_Ok != MLPerceptionStartup(&perception_settings)) {
ML_LOG_TAG(Error, APP_TAG, "Failed to startup perception");
return -1;
}
// 8. Create OpenGL context and create framebuffer
graphics_context.makeCurrent();
glGenFramebuffers(1, &graphics_context.framebuffer_id);
MLGraphicsOptions graphics_options = { 0, MLSurfaceFormat_RGBA8UNorm, MLSurfaceFormat_D32Float };
MLHandle opengl_context = reinterpret_cast<MLHandle>(graphics_context.egl_context);
MLHandle graphics_client = ML_INVALID_HANDLE;
MLGraphicsCreateClientGL(&graphics_options, opengl_context, &graphics_client);
// 9. Ready for application lifecycle
if (MLLifecycleSetReadyIndication() != MLResult_Ok) {
ML_LOG_TAG(Error, APP_TAG, "Failed to indicate lifecycle readyness");
return -1;
}
else {
ML_LOG_TAG(Debug, APP_TAG, "Lifecycle system ready");
}
// 10. The main/game loop
while (true) {
// 11. Initialize a frame
MLGraphicsFrameParams frame_params;
result = MLGraphicsInitFrameParams(&frame_params);
if (MLResult_Ok != result) {
ML_LOG_TAG(Error, APP_TAG, "MLGraphicsInitFrameParams() error: %d", result);
}
frame_params.surface_scale = 1.0f;
frame_params.projection_type = MLGraphicsProjectionType_ReversedInfiniteZ;
frame_params.near_clip = 0.38;
frame_params.focus_distance = 1.0f;
MLHandle frame_handle;
MLGraphicsVirtualCameraInfoArray virtual_camera_array;
// 12. Begin the frame
MLResult frame_result = MLGraphicsBeginFrame(graphics_client, &frame_params, &frame_handle, &virtual_camera_array);
if (frame_result == MLResult_Ok) {
// 13. Prepare rendering for each camera/eye
for (int camera = 0; camera < 2; ++camera) {
glBindFramebuffer(GL_FRAMEBUFFER, graphics_context.framebuffer_id);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, virtual_camera_array.color_id, 0, camera);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, virtual_camera_array.depth_id, 0, camera);
const MLRectf& viewport = virtual_camera_array.viewport;
glViewport((GLint)viewport.x, (GLint)viewport.y, (GLsizei)viewport.w, (GLsizei)viewport.h);
// 14. TODO: Here we display later our content
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 15. Bind the frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
MLGraphicsSignalSyncObjectGL(graphics_client, virtual_camera_array.virtual_cameras[camera].sync_object);
}
// 16. Finish the frame
result = MLGraphicsEndFrame(graphics_client, frame_handle);
if (MLResult_Ok != result) {
ML_LOG_TAG(Error, APP_TAG, "MLGraphicsEndFrame() error: %d", result);
}
}
else if (frame_result != MLResult_Timeout) {
// Sometimes it fails with timeout when device is busy
ML_LOG_TAG(Error, APP_TAG, "MLGraphicsBeginFrame() error: %d", frame_result);
}
}
// 17. End of game loop, clean app and exit
ML_LOG_TAG(Debug, APP_TAG, "End application loop");
graphics_context.unmakeCurrent();
glDeleteFramebuffers(1, &graphics_context.framebuffer_id);
MLGraphicsDestroyClient(&graphics_client);
ML_LOG_TAG(Debug, APP_TAG, "System cleanup done");
return 0;
}