-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleFrameworkApp.cpp
342 lines (270 loc) · 8.3 KB
/
SimpleFrameworkApp.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
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
#include "framework.h"
#include "SimpleFrameworkApp.h"
#include "OpenGLWindow/ShapeData.h"
#include "Bullet3Common/b3Vector3.h"
#include "Bullet3Common/b3Logging.h"
#include "Bullet3Common/b3Quaternion.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "Framework2dCanvasInterface.h"
#include "FrameworkParameterInterface.h"
#include "FrameworkRenderInterface.h"
#include "FrameworkWindow.h"
struct SimpleInternalData
{
int m_upAxis; //y=1 or z=2 is supported
int m_customViewPortWidth;
int m_customViewPortHeight;
SimpleInternalData()
: m_upAxis(1)
, m_customViewPortWidth(-1)
, m_customViewPortHeight(-1)
{
}
};
static SimpleFrameworkApp * gApp = nullptr;
static void SimpleResizeCallback(float widthf, float heightf)
{
int width = (int)widthf;
int height = (int)heightf;
if (gApp && gApp->m_renderer)
gApp->m_renderer->resize(width, height);
}
static void SimpleKeyboardCallback(int key, int state)
{
if (key == SDLK_ESCAPE && gApp && gApp->m_window)
{
gApp->m_window->setRequestExit();
}
else
{
//gApp->defaultKeyboardCallback(key,state);
}
}
static void SimpleMouseButtonCallback(int button, int state, float x, float y)
{
gApp->defaultMouseButtonCallback(button, state, x, y);
}
static void SimpleMouseMoveCallback(float x, float y)
{
gApp->defaultMouseMoveCallback(x, y);
}
static void SimpleWheelCallback(float deltax, float deltay)
{
gApp->defaultWheelCallback(deltax, deltay);
}
SimpleFrameworkApp::SimpleFrameworkApp(
const char * title,
int width,
int height,
bool allowRetina,
int windowType,
int renderDevice,
int maxNumObjectCapacity,
int maxShapeCapacityInBytes)
{
gApp = this;
m_data = new SimpleInternalData();
m_window = new FrameworkWindow();
m_window->setAllowRetina(allowRetina);
b3gWindowConstructionInfo ci;
ci.m_title = title;
ci.m_width = width;
ci.m_height = height;
ci.m_renderDevice = renderDevice;
m_window->createWindow(ci);
m_window->setWindowTitle(title);
static_cast<FrameworkWindow*>(m_window)->setBackgroundColor(m_backgroundColorRGB);
m_window->startRendering();
width = m_window->getWidth();
height = m_window->getHeight();
m_renderer = new FrameworkRenderInterface();
m_parameterInterface = new FrameworkParameterInterface();
m_2dCanvasInterface = new Framework2dCanvasInterface();
m_window->setResizeCallback(SimpleResizeCallback);
m_window->setMouseMoveCallback(SimpleMouseMoveCallback);
m_window->setMouseButtonCallback(SimpleMouseButtonCallback);
m_window->setKeyboardCallback(SimpleKeyboardCallback);
m_window->setWheelCallback(SimpleWheelCallback);
m_wheelMultiplier = .1f;
}
// todo : optimize text drawing. currently it does too many state switches
static void beginTextDrawing()
{
gxMatrixMode(GX_PROJECTION);
gxPushMatrix();
gxMatrixMode(GX_MODELVIEW);
gxPushMatrix();
//
projectScreen2d();
pushDepthTest(false, DEPTH_LESS);
pushBlend(BLEND_ALPHA);
setFont("calibri.ttf");
}
static void endTextDrawing()
{
popDepthTest();
popBlend();
setTransform(TRANSFORM_3D);
//
gxMatrixMode(GX_PROJECTION);
gxPopMatrix();
gxMatrixMode(GX_MODELVIEW);
gxPopMatrix();
}
void SimpleFrameworkApp::drawText3D(const char* txt, float position[3], float orientation[4], float color[4], float size, int optionFlag)
{
float w;
const Vec2 p = transformToScreen(Vec3(position[0], position[1], position[2]), w);
if (w > 0.f)
{
beginTextDrawing();
{
::setColorf(color[0], color[1], color[2], color[3]);
::drawText(p[0], p[1], size * 12.f, 0, 0, "%s", txt);
}
endTextDrawing();
}
}
void SimpleFrameworkApp::drawText3D(const char* txt, float worldPosX, float worldPosY, float worldPosZ, float size1)
{
float position[3] = {worldPosX, worldPosY, worldPosZ};
float orientation[4] = {0, 0, 0, 1};
float color[4] = {0, 0, 0, 1};
int optionFlags = CommonGraphicsApp::eDrawText3D_OrtogonalFaceCamera;
drawText3D(txt, position, orientation, color, size1, optionFlags);
}
void SimpleFrameworkApp::drawText(const char* txt, int posX, int posY, float size, float colorRGBA[4])
{
beginTextDrawing();
{
::setColorf(colorRGBA[0], colorRGBA[1], colorRGBA[2], colorRGBA[3]);
::drawText(posX, posY, size, +1, -1, "%s", txt);
}
endTextDrawing();
}
void SimpleFrameworkApp::drawTexturedRect(float x0, float y0, float x1, float y1, float color[4], float u0, float v0, float u1, float v1, int useRGBA)
{
beginTextDrawing();
{
// todo : use custom uv's
// todo : honor useRGBA
Assert(false);
::setColorf(color[0], color[1], color[2], color[3]);
drawRect(x0, y0, x1, y1);
}
endTextDrawing();
}
int SimpleFrameworkApp::registerCubeShape(float halfExtentsX, float halfExtentsY, float halfExtentsZ, int textureIndex, float textureScaling)
{
const int vertexStrideInBytes = 9 * sizeof(float);
const int numVertices = sizeof(cube_vertices_textured) / vertexStrideInBytes;
btAlignedObjectArray<float> vertices;
vertices.resize(numVertices * 9);
for (int i = 0; i < numVertices * 9; ++i)
vertices[i] = cube_vertices_textured[i];
for (int i = 0; i < numVertices; ++i)
{
vertices[i * 9 + 0] *= halfExtentsX;
vertices[i * 9 + 1] *= halfExtentsY;
vertices[i * 9 + 2] *= halfExtentsZ;
}
return m_renderer->registerShape(
&vertices[0],
numVertices,
cube_indices,
sizeof(cube_indices) / sizeof(cube_indices[0]),
B3_GL_TRIANGLES,
textureIndex);
}
void SimpleFrameworkApp::registerGrid(int cells_x, int cells_z, float color0[4], float color1[4])
{
const double halfHeight = 0.1;
b3Vector3 cubeExtents = b3MakeVector3(0.5, 0.5, 0.5);
cubeExtents[m_data->m_upAxis] = halfHeight;
const int cubeId = registerCubeShape(cubeExtents[0], cubeExtents[1], cubeExtents[2]);
const b3Quaternion orientation(0, 0, 0, 1);
const b3Vector3 scaling = b3MakeVector3(1, 1, 1, 1);
for (int i = 0; i < cells_x; i++)
{
for (int j = 0; j < cells_z; j++)
{
float * color = nullptr;
if ((i + j) % 2 == 0)
color = (float*)color0;
else
color = (float*)color1;
const b3Vector3 center = m_data->m_upAxis == 1
? b3MakeVector3((i + 0.5f) - cells_x * 0.5f, -halfHeight, (j + 0.5f) - cells_z * 0.5f)
: b3MakeVector3((i + 0.5f) - cells_x * 0.5f, (j + 0.5f) - cells_z * 0.5f, -halfHeight);
m_renderer->registerGraphicsInstance(cubeId, center, orientation, color, scaling);
}
}
}
int SimpleFrameworkApp::registerGraphicsUnitSphereShape(EnumSphereLevelOfDetail lod, int textureId)
{
const int vertexStrideInBytes = 9 * sizeof(float);
return m_renderer->registerShape(
textured_detailed_sphere_vertices,
sizeof(textured_detailed_sphere_vertices) / vertexStrideInBytes,
textured_detailed_sphere_indices,
sizeof(textured_detailed_sphere_indices) / sizeof(textured_detailed_sphere_indices[0]),
B3_GL_TRIANGLES,
textureId);
}
void SimpleFrameworkApp::drawGrid(DrawGridData data)
{
gxPushMatrix();
{
Vec3 offset;
offset[data.upAxis] = data.upOffset;
gxTranslatef(offset[0], offset[1], offset[2]);
gxScalef(data.gridSize, data.gridSize, data.gridSize);
int axis1 = (data.upAxis + 1) % 3;
int axis2 = (data.upAxis + 2) % 3;
::setColorf(data.gridColor[0], data.gridColor[1], data.gridColor[2], data.gridColor[3]);
::drawGrid3dLine(20, 20, axis1, axis2);
}
gxPopMatrix();
}
void SimpleFrameworkApp::setBackgroundColor(float red, float green, float blue)
{
CommonGraphicsApp::setBackgroundColor(red, green, blue);
static_cast<FrameworkWindow*>(m_window)->setBackgroundColor(m_backgroundColorRGB);
}
SimpleFrameworkApp::~SimpleFrameworkApp()
{
delete m_2dCanvasInterface;
m_2dCanvasInterface = nullptr;
delete m_parameterInterface;
m_parameterInterface = nullptr;
delete m_renderer;
m_renderer = nullptr;
m_window->closeWindow();
delete m_window;
m_window = nullptr;
delete m_data;
m_data = nullptr;
}
void SimpleFrameworkApp::setViewport(int width, int height)
{
m_data->m_customViewPortWidth = width;
m_data->m_customViewPortHeight = height;
}
void SimpleFrameworkApp::getScreenPixels(unsigned char* rgbaBuffer, int bufferSizeInBytes, float* depthBuffer, int depthBufferSizeInBytes)
{
}
void SimpleFrameworkApp::swapBuffer()
{
m_window->endRendering();
m_window->runMainLoop();
m_window->startRendering();
}
void SimpleFrameworkApp::setUpAxis(int axis)
{
b3Assert((axis == 1) || (axis == 2)); //only Y or Z is supported at the moment
m_data->m_upAxis = axis;
}
int SimpleFrameworkApp::getUpAxis() const
{
return m_data->m_upAxis;
}