sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description: Rendering context function implementations
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
/*!
|
sl@0
|
20 |
* \internal
|
sl@0
|
21 |
* \file
|
sl@0
|
22 |
* \brief Rendering context function implementations
|
sl@0
|
23 |
*/
|
sl@0
|
24 |
|
sl@0
|
25 |
#ifndef M3G_CORE_INCLUDE
|
sl@0
|
26 |
# error included by m3g_core.c; do not compile separately.
|
sl@0
|
27 |
#endif
|
sl@0
|
28 |
|
sl@0
|
29 |
#include "m3g_rendercontext.h"
|
sl@0
|
30 |
#include "m3g_object.h"
|
sl@0
|
31 |
|
sl@0
|
32 |
#include "m3g_gl.h"
|
sl@0
|
33 |
#include "m3g_memory.h"
|
sl@0
|
34 |
#include "m3g_appearance.h"
|
sl@0
|
35 |
#include "m3g_indexbuffer.h"
|
sl@0
|
36 |
#include "m3g_lightmanager.h"
|
sl@0
|
37 |
#include "m3g_vertexbuffer.h"
|
sl@0
|
38 |
#include "m3g_world.h"
|
sl@0
|
39 |
|
sl@0
|
40 |
/*----------------------------------------------------------------------
|
sl@0
|
41 |
* Private data types
|
sl@0
|
42 |
*--------------------------------------------------------------------*/
|
sl@0
|
43 |
|
sl@0
|
44 |
#if defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
45 |
/*!
|
sl@0
|
46 |
* \internal
|
sl@0
|
47 |
* \brief Depth buffer data
|
sl@0
|
48 |
*/
|
sl@0
|
49 |
typedef struct
|
sl@0
|
50 |
{
|
sl@0
|
51 |
M3GMemObject handle;
|
sl@0
|
52 |
M3Gsizei size;
|
sl@0
|
53 |
} DepthBuffer;
|
sl@0
|
54 |
#endif /*M3G_NGL_CONTEXT_API*/
|
sl@0
|
55 |
|
sl@0
|
56 |
#if !defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
57 |
/*! \internal \brief OpenGL rendering context record */
|
sl@0
|
58 |
typedef struct {
|
sl@0
|
59 |
EGLContext handle;
|
sl@0
|
60 |
M3GPixelFormat format;
|
sl@0
|
61 |
M3Gbitmask bufferBits;
|
sl@0
|
62 |
M3Gbitmask surfaceTypeBits;
|
sl@0
|
63 |
M3Gbitmask modeBits;
|
sl@0
|
64 |
M3Guint lastUseTime;
|
sl@0
|
65 |
} GLContextRecord;
|
sl@0
|
66 |
|
sl@0
|
67 |
/*! \internal \brief OpenGL surface record */
|
sl@0
|
68 |
typedef struct {
|
sl@0
|
69 |
EGLSurface handle;
|
sl@0
|
70 |
M3Gbitmask bufferBits;
|
sl@0
|
71 |
M3Gbitmask type;
|
sl@0
|
72 |
M3Guint width;
|
sl@0
|
73 |
M3Guint height;
|
sl@0
|
74 |
M3Guint format;
|
sl@0
|
75 |
M3Guint targetHandle;
|
sl@0
|
76 |
void* pixels;
|
sl@0
|
77 |
M3Guint lastUseTime;
|
sl@0
|
78 |
} GLSurfaceRecord;
|
sl@0
|
79 |
#endif /*!M3G_NGL_CONTEXT_API*/
|
sl@0
|
80 |
|
sl@0
|
81 |
/*!
|
sl@0
|
82 |
* \internal \brief Rendering target data
|
sl@0
|
83 |
*/
|
sl@0
|
84 |
typedef struct
|
sl@0
|
85 |
{
|
sl@0
|
86 |
M3Gbitmask type;
|
sl@0
|
87 |
M3GPixelFormat format;
|
sl@0
|
88 |
M3Gint width, height;
|
sl@0
|
89 |
M3Guint stride;
|
sl@0
|
90 |
/*@shared@*/ void *pixels, *lockedPixels;
|
sl@0
|
91 |
EGLSurface surface;
|
sl@0
|
92 |
M3Guint handle;
|
sl@0
|
93 |
M3Guint userData;
|
sl@0
|
94 |
|
sl@0
|
95 |
/*!
|
sl@0
|
96 |
* \internal
|
sl@0
|
97 |
* \brief Flag set to indicate back buffer rendering
|
sl@0
|
98 |
*
|
sl@0
|
99 |
* The final target is only written to, via a format
|
sl@0
|
100 |
* conversion, when releasing the target.
|
sl@0
|
101 |
*/
|
sl@0
|
102 |
M3Gbool buffered;
|
sl@0
|
103 |
} RenderTarget;
|
sl@0
|
104 |
|
sl@0
|
105 |
/*!
|
sl@0
|
106 |
* \internal
|
sl@0
|
107 |
* \brief Back color buffer data
|
sl@0
|
108 |
*/
|
sl@0
|
109 |
typedef struct {
|
sl@0
|
110 |
# if defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
111 |
M3GMemObject handle;
|
sl@0
|
112 |
M3Gsizei size;
|
sl@0
|
113 |
# else
|
sl@0
|
114 |
M3Gint width, height;
|
sl@0
|
115 |
EGLSurface glSurface;
|
sl@0
|
116 |
# endif /* M3G_NGL_CONTEXT_API */
|
sl@0
|
117 |
M3Gbool contentsValid;
|
sl@0
|
118 |
} BackBuffer;
|
sl@0
|
119 |
|
sl@0
|
120 |
/*!
|
sl@0
|
121 |
* \internal
|
sl@0
|
122 |
* \brief Rendering context data structure
|
sl@0
|
123 |
*
|
sl@0
|
124 |
* This includes data related to a specific rendering context,
|
sl@0
|
125 |
* including e.g. viewport settings, and active lights and
|
sl@0
|
126 |
* camera. This is equivalent to the Graphics3D class in the Java API.
|
sl@0
|
127 |
*/
|
sl@0
|
128 |
struct M3GRenderContextImpl
|
sl@0
|
129 |
{
|
sl@0
|
130 |
Object object;
|
sl@0
|
131 |
|
sl@0
|
132 |
RenderTarget target;
|
sl@0
|
133 |
BackBuffer backBuffer;
|
sl@0
|
134 |
# if defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
135 |
DepthBuffer depthBuffer;
|
sl@0
|
136 |
# endif
|
sl@0
|
137 |
|
sl@0
|
138 |
# if !defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
139 |
|
sl@0
|
140 |
/* OpenGL context and surface caches */
|
sl@0
|
141 |
|
sl@0
|
142 |
GLContextRecord glContext[M3G_MAX_GL_CONTEXTS];
|
sl@0
|
143 |
GLSurfaceRecord glSurface[M3G_MAX_GL_SURFACES];
|
sl@0
|
144 |
M3Guint cacheTimeStamp;
|
sl@0
|
145 |
|
sl@0
|
146 |
# endif /* M3G_NGL_CONTEXT_API */
|
sl@0
|
147 |
|
sl@0
|
148 |
/*! \internal \brief Current/last rendering mode */
|
sl@0
|
149 |
M3Genum renderMode;
|
sl@0
|
150 |
|
sl@0
|
151 |
/*! \internal \brief OpenGL viewing transformation */
|
sl@0
|
152 |
GLfloat viewTransform[16];
|
sl@0
|
153 |
|
sl@0
|
154 |
/*! \internal \brief Current camera */
|
sl@0
|
155 |
const Camera *camera;
|
sl@0
|
156 |
|
sl@0
|
157 |
/*! \internal \brief Light manager component */
|
sl@0
|
158 |
LightManager lightManager;
|
sl@0
|
159 |
|
sl@0
|
160 |
/*! \internal \brief Last used scope, to speed up light selection */
|
sl@0
|
161 |
M3Gint lastScope;
|
sl@0
|
162 |
|
sl@0
|
163 |
M3Gfloat depthNear;
|
sl@0
|
164 |
M3Gfloat depthFar;
|
sl@0
|
165 |
|
sl@0
|
166 |
/*! \internal \brief Clipping rectangle parameters */
|
sl@0
|
167 |
struct { M3Gint x0, y0, x1, y1; } clip;
|
sl@0
|
168 |
|
sl@0
|
169 |
/*! \internal \brief Scissor and viewport rectangles */
|
sl@0
|
170 |
struct { GLint x, y, width, height; } scissor, viewport;
|
sl@0
|
171 |
|
sl@0
|
172 |
/*! \internal \brief Physical display size */
|
sl@0
|
173 |
struct { M3Gint width, height; } display;
|
sl@0
|
174 |
|
sl@0
|
175 |
M3Gbitmask bufferBits; /*!< \brief Rendering buffer bits */
|
sl@0
|
176 |
M3Gbitmask modeBits; /*!< \brief Rendering mode bits */
|
sl@0
|
177 |
|
sl@0
|
178 |
/*! \internal \brief OpenGL subsystem initialization flag */
|
sl@0
|
179 |
M3Gbool glInitialized;
|
sl@0
|
180 |
|
sl@0
|
181 |
/*! \internal \brief HW acceleration status flag */
|
sl@0
|
182 |
M3Gbool accelerated;
|
sl@0
|
183 |
|
sl@0
|
184 |
/*! \internal \brief Render queue for this context */
|
sl@0
|
185 |
RenderQueue *renderQueue;
|
sl@0
|
186 |
|
sl@0
|
187 |
M3Gbool currentColorWrite;
|
sl@0
|
188 |
M3Gbool currentAlphaWrite;
|
sl@0
|
189 |
M3Gbool inSplitDraw;
|
sl@0
|
190 |
M3Gbool alphaWrite;
|
sl@0
|
191 |
};
|
sl@0
|
192 |
|
sl@0
|
193 |
/*
|
sl@0
|
194 |
* Rendering target types; note that the values here MUST match the
|
sl@0
|
195 |
* respective EGL bit values
|
sl@0
|
196 |
*/
|
sl@0
|
197 |
enum SurfaceType {
|
sl@0
|
198 |
SURFACE_NONE = 0,
|
sl@0
|
199 |
SURFACE_IMAGE = 0x01, /* EGL_PBUFFER_BIT */
|
sl@0
|
200 |
SURFACE_BITMAP = 0x02, /* EGL_PIXMAP_BIT */
|
sl@0
|
201 |
SURFACE_WINDOW = 0x04, /* EGL_WINDOW_BIT */
|
sl@0
|
202 |
SURFACE_MEMORY = SURFACE_IMAGE | SURFACE_BITMAP | SURFACE_WINDOW,
|
sl@0
|
203 |
SURFACE_EGL = 0x80
|
sl@0
|
204 |
};
|
sl@0
|
205 |
|
sl@0
|
206 |
enum RenderMode {
|
sl@0
|
207 |
RENDER_IMMEDIATE,
|
sl@0
|
208 |
RENDER_NODES,
|
sl@0
|
209 |
RENDER_WORLD
|
sl@0
|
210 |
};
|
sl@0
|
211 |
|
sl@0
|
212 |
/*----------------------------------------------------------------------
|
sl@0
|
213 |
* Platform specific code
|
sl@0
|
214 |
*--------------------------------------------------------------------*/
|
sl@0
|
215 |
|
sl@0
|
216 |
static M3Gbool m3gBindRenderTarget(RenderContext *ctx,
|
sl@0
|
217 |
M3Genum targetType,
|
sl@0
|
218 |
M3Gint width, M3Gint height,
|
sl@0
|
219 |
M3GPixelFormat format,
|
sl@0
|
220 |
M3Guint handle);
|
sl@0
|
221 |
static void m3gResetRectangles(RenderContext *ctx);
|
sl@0
|
222 |
static void m3gSetGLDefaults(void);
|
sl@0
|
223 |
static void m3gUpdateScissor(RenderContext *ctx);
|
sl@0
|
224 |
static void m3gValidateBuffers(RenderContext *ctx);
|
sl@0
|
225 |
static M3Gbool m3gValidTargetFormat(M3GPixelFormat format);
|
sl@0
|
226 |
|
sl@0
|
227 |
#include "m3g_rendercontext.inl"
|
sl@0
|
228 |
|
sl@0
|
229 |
/*----------------------------------------------------------------------
|
sl@0
|
230 |
* Internal functions
|
sl@0
|
231 |
*--------------------------------------------------------------------*/
|
sl@0
|
232 |
|
sl@0
|
233 |
/*!
|
sl@0
|
234 |
* \internal
|
sl@0
|
235 |
* \brief Rendering context destructor
|
sl@0
|
236 |
*
|
sl@0
|
237 |
*/
|
sl@0
|
238 |
static void m3gDestroyContext(/*@only@*/ Object *obj)
|
sl@0
|
239 |
{
|
sl@0
|
240 |
RenderContext *ctx = (RenderContext *) obj;
|
sl@0
|
241 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
242 |
|
sl@0
|
243 |
M3G_ASSIGN_REF(ctx->camera, NULL);
|
sl@0
|
244 |
|
sl@0
|
245 |
# if defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
246 |
if (ctx->target.type == SURFACE_MEMORY && ctx->target.pixels == NULL) {
|
sl@0
|
247 |
m3gSignalTargetRelease(M3G_INTERFACE(ctx), ctx->target.handle);
|
sl@0
|
248 |
}
|
sl@0
|
249 |
|
sl@0
|
250 |
m3gFreeObject(M3G_INTERFACE(ctx), ctx->depthBuffer.handle);
|
sl@0
|
251 |
m3gFreeObject(M3G_INTERFACE(ctx), ctx->backBuffer.handle);
|
sl@0
|
252 |
|
sl@0
|
253 |
# else /* !M3G_NGL_CONTEXT_API */
|
sl@0
|
254 |
|
sl@0
|
255 |
{
|
sl@0
|
256 |
int i;
|
sl@0
|
257 |
for (i = 0; i < M3G_MAX_GL_CONTEXTS; ++i) {
|
sl@0
|
258 |
if (ctx->glContext[i].handle != 0) {
|
sl@0
|
259 |
m3gDeleteGLContext(ctx->glContext[i].handle);
|
sl@0
|
260 |
}
|
sl@0
|
261 |
}
|
sl@0
|
262 |
for (i = 0; i < M3G_MAX_GL_SURFACES; ++i) {
|
sl@0
|
263 |
if (ctx->glSurface[i].handle != 0) {
|
sl@0
|
264 |
m3gDeleteGLSurface(ctx->glSurface[i].handle);
|
sl@0
|
265 |
}
|
sl@0
|
266 |
}
|
sl@0
|
267 |
}
|
sl@0
|
268 |
|
sl@0
|
269 |
# endif /* M3G_NGL_CONTEXT_API */
|
sl@0
|
270 |
|
sl@0
|
271 |
if (ctx->glInitialized) {
|
sl@0
|
272 |
m3gShutdownGL(M3G_INTERFACE(ctx));
|
sl@0
|
273 |
}
|
sl@0
|
274 |
|
sl@0
|
275 |
m3gDestroyLightManager(&ctx->lightManager, M3G_INTERFACE(ctx));
|
sl@0
|
276 |
m3gDestroyRenderQueue(M3G_INTERFACE(ctx), ctx->renderQueue);
|
sl@0
|
277 |
m3gDestroyObject(obj);
|
sl@0
|
278 |
}
|
sl@0
|
279 |
|
sl@0
|
280 |
/*!
|
sl@0
|
281 |
* \internal
|
sl@0
|
282 |
* \brief Resets the clipping and viewport rectangles to defaults
|
sl@0
|
283 |
*
|
sl@0
|
284 |
* This is called after binding a new target.
|
sl@0
|
285 |
*/
|
sl@0
|
286 |
static void m3gResetRectangles(RenderContext *ctx)
|
sl@0
|
287 |
{
|
sl@0
|
288 |
int w = ctx->display.width;
|
sl@0
|
289 |
int h = ctx->display.height;
|
sl@0
|
290 |
|
sl@0
|
291 |
ctx->clip.x0 = 0;
|
sl@0
|
292 |
ctx->clip.y0 = ctx->target.height - ctx->display.height;
|
sl@0
|
293 |
ctx->clip.x1 = w;
|
sl@0
|
294 |
ctx->clip.y1 = ctx->clip.y0 + h;
|
sl@0
|
295 |
|
sl@0
|
296 |
ctx->viewport.x = 0;
|
sl@0
|
297 |
ctx->viewport.y = 0;
|
sl@0
|
298 |
ctx->viewport.width = M3G_MIN(w, M3G_MAX_VIEWPORT_DIMENSION);
|
sl@0
|
299 |
ctx->viewport.height = M3G_MIN(h, M3G_MAX_VIEWPORT_DIMENSION);
|
sl@0
|
300 |
}
|
sl@0
|
301 |
|
sl@0
|
302 |
/*!
|
sl@0
|
303 |
* \internal
|
sl@0
|
304 |
* \brief Constrains the clip rectangle to the rendering target.
|
sl@0
|
305 |
*/
|
sl@0
|
306 |
static void m3gValidateClipRect(RenderContext *ctx)
|
sl@0
|
307 |
{
|
sl@0
|
308 |
int xMin = 0;
|
sl@0
|
309 |
int xMax = ctx->display.width;
|
sl@0
|
310 |
int yMin = ctx->target.height - ctx->display.height;
|
sl@0
|
311 |
int yMax = yMin + ctx->display.height;
|
sl@0
|
312 |
|
sl@0
|
313 |
ctx->clip.x0 = m3gClampInt(ctx->clip.x0, xMin, xMax);
|
sl@0
|
314 |
ctx->clip.y0 = m3gClampInt(ctx->clip.y0, yMin, yMax);
|
sl@0
|
315 |
ctx->clip.x1 = m3gClampInt(ctx->clip.x1, xMin, xMax);
|
sl@0
|
316 |
ctx->clip.y1 = m3gClampInt(ctx->clip.y1, yMin, yMax);
|
sl@0
|
317 |
}
|
sl@0
|
318 |
|
sl@0
|
319 |
/*!
|
sl@0
|
320 |
* \internal
|
sl@0
|
321 |
* \brief Computes the GL scissor rectangle
|
sl@0
|
322 |
*
|
sl@0
|
323 |
* The scissor rectangle is the intersection of the viewport and the
|
sl@0
|
324 |
* clipping rectangle.
|
sl@0
|
325 |
*/
|
sl@0
|
326 |
static void m3gUpdateScissor(RenderContext *ctx)
|
sl@0
|
327 |
{
|
sl@0
|
328 |
int sx0 = ctx->viewport.x;
|
sl@0
|
329 |
int sy0 = ctx->viewport.y;
|
sl@0
|
330 |
int sx1 = sx0 + ctx->viewport.width;
|
sl@0
|
331 |
int sy1 = sy0 + ctx->viewport.height;
|
sl@0
|
332 |
|
sl@0
|
333 |
sx0 = M3G_MAX(sx0, ctx->clip.x0);
|
sl@0
|
334 |
sy0 = M3G_MAX(sy0, ctx->clip.y0);
|
sl@0
|
335 |
sx1 = M3G_MIN(sx1, ctx->clip.x1);
|
sl@0
|
336 |
sy1 = M3G_MIN(sy1, ctx->clip.y1);
|
sl@0
|
337 |
|
sl@0
|
338 |
ctx->scissor.x = sx0;
|
sl@0
|
339 |
ctx->scissor.y = sy0;
|
sl@0
|
340 |
|
sl@0
|
341 |
if (sx0 < sx1 && sy0 < sy1) {
|
sl@0
|
342 |
ctx->scissor.width = sx1 - sx0;
|
sl@0
|
343 |
ctx->scissor.height = sy1 - sy0;
|
sl@0
|
344 |
}
|
sl@0
|
345 |
else {
|
sl@0
|
346 |
ctx->scissor.width = ctx->scissor.height = 0;
|
sl@0
|
347 |
}
|
sl@0
|
348 |
}
|
sl@0
|
349 |
|
sl@0
|
350 |
/*!
|
sl@0
|
351 |
* \internal
|
sl@0
|
352 |
* \brief Checks whether we can render in a given format
|
sl@0
|
353 |
*/
|
sl@0
|
354 |
static M3Gbool m3gValidTargetFormat(M3GPixelFormat format)
|
sl@0
|
355 |
{
|
sl@0
|
356 |
return m3gInRange(format, M3G_RGB8, M3G_RGBA4);
|
sl@0
|
357 |
}
|
sl@0
|
358 |
|
sl@0
|
359 |
/*!
|
sl@0
|
360 |
* \internal
|
sl@0
|
361 |
* \brief Checks whether a given format has alpha
|
sl@0
|
362 |
*/
|
sl@0
|
363 |
static M3Gbool m3gFormatHasAlpha(M3GPixelFormat format)
|
sl@0
|
364 |
{
|
sl@0
|
365 |
switch (format) {
|
sl@0
|
366 |
case M3G_A8:
|
sl@0
|
367 |
case M3G_LA8:
|
sl@0
|
368 |
case M3G_LA4:
|
sl@0
|
369 |
case M3G_RGBA8:
|
sl@0
|
370 |
case M3G_BGRA8:
|
sl@0
|
371 |
case M3G_RGBA4:
|
sl@0
|
372 |
case M3G_RGB5A1:
|
sl@0
|
373 |
case M3G_PALETTE8_RGBA8:
|
sl@0
|
374 |
return M3G_TRUE;
|
sl@0
|
375 |
default:
|
sl@0
|
376 |
return M3G_FALSE;
|
sl@0
|
377 |
}
|
sl@0
|
378 |
}
|
sl@0
|
379 |
|
sl@0
|
380 |
/*!
|
sl@0
|
381 |
* \internal
|
sl@0
|
382 |
* \brief Sets the global alpha write enable flag.
|
sl@0
|
383 |
*
|
sl@0
|
384 |
* Used for disabling the alpha channel writes when the rendering
|
sl@0
|
385 |
* target is a Java MIDP Image that has an alpha channel.
|
sl@0
|
386 |
*
|
sl@0
|
387 |
* \param ctx the rendering context
|
sl@0
|
388 |
* \param enable alpha write enable flag
|
sl@0
|
389 |
*/
|
sl@0
|
390 |
M3G_API void m3gSetAlphaWrite(M3GRenderContext ctx, M3Gbool enable)
|
sl@0
|
391 |
{
|
sl@0
|
392 |
ctx->alphaWrite = enable;
|
sl@0
|
393 |
}
|
sl@0
|
394 |
|
sl@0
|
395 |
/*!
|
sl@0
|
396 |
* \internal
|
sl@0
|
397 |
* \brief Reads the global alpha write enable flag.
|
sl@0
|
398 |
*
|
sl@0
|
399 |
* \param ctx the rendering context
|
sl@0
|
400 |
*/
|
sl@0
|
401 |
M3G_API M3Gbool m3gGetAlphaWrite(M3GRenderContext ctx)
|
sl@0
|
402 |
{
|
sl@0
|
403 |
return ctx->alphaWrite;
|
sl@0
|
404 |
}
|
sl@0
|
405 |
|
sl@0
|
406 |
/*!
|
sl@0
|
407 |
* \brief Frees all GLES resources allocated by the M3G API
|
sl@0
|
408 |
* (EGL surfaces, contexts and texture objects).
|
sl@0
|
409 |
*
|
sl@0
|
410 |
* \note M3G must not be bound to any target when calling this.
|
sl@0
|
411 |
*
|
sl@0
|
412 |
*/
|
sl@0
|
413 |
M3G_API void m3gFreeGLESResources(M3GRenderContext ctx)
|
sl@0
|
414 |
{
|
sl@0
|
415 |
#ifdef M3G_ENABLE_GLES_RESOURCE_HANDLING
|
sl@0
|
416 |
|
sl@0
|
417 |
PointerArray image2DObjects;
|
sl@0
|
418 |
M3Gint i;
|
sl@0
|
419 |
|
sl@0
|
420 |
/* M3G must not be bound to a rendering target at this point. */
|
sl@0
|
421 |
if (ctx->target.type != SURFACE_NONE) {
|
sl@0
|
422 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OPERATION);
|
sl@0
|
423 |
}
|
sl@0
|
424 |
|
sl@0
|
425 |
/* EGL might not be initialized yet, so do it here just in case. */
|
sl@0
|
426 |
eglInitialize(eglGetDisplay(EGL_DEFAULT_DISPLAY), NULL, NULL);
|
sl@0
|
427 |
eglBindAPI(EGL_OPENGL_ES_API);
|
sl@0
|
428 |
eglMakeCurrent(eglGetDisplay(EGL_DEFAULT_DISPLAY), NULL, NULL, NULL);
|
sl@0
|
429 |
|
sl@0
|
430 |
/* Delete EGL surfaces */
|
sl@0
|
431 |
for (i = 0; i < M3G_MAX_GL_SURFACES; ++i) {
|
sl@0
|
432 |
GLSurfaceRecord *surf = &ctx->glSurface[i];
|
sl@0
|
433 |
if (surf->handle) {
|
sl@0
|
434 |
m3gDeleteGLSurface(surf->handle);
|
sl@0
|
435 |
}
|
sl@0
|
436 |
m3gZero(surf, sizeof(GLSurfaceRecord));
|
sl@0
|
437 |
}
|
sl@0
|
438 |
if (ctx->backBuffer.glSurface != NULL) {
|
sl@0
|
439 |
m3gDeleteGLSurface(ctx->backBuffer.glSurface);
|
sl@0
|
440 |
m3gZero(&ctx->backBuffer, sizeof(BackBuffer));
|
sl@0
|
441 |
}
|
sl@0
|
442 |
|
sl@0
|
443 |
/* Delete EGL contexts */
|
sl@0
|
444 |
for (i = 0; i < M3G_MAX_GL_CONTEXTS; ++i) {
|
sl@0
|
445 |
GLContextRecord *context = &ctx->glContext[i];
|
sl@0
|
446 |
if (context->handle) {
|
sl@0
|
447 |
m3gDeleteGLContext(context->handle);
|
sl@0
|
448 |
}
|
sl@0
|
449 |
m3gZero(context, sizeof(GLContextRecord));
|
sl@0
|
450 |
}
|
sl@0
|
451 |
|
sl@0
|
452 |
/* Delete references to GLES texture objects from all live Image2D objects.
|
sl@0
|
453 |
Texture objects themselves have already been destroyed with the last GL context. */
|
sl@0
|
454 |
|
sl@0
|
455 |
m3gInitArray(&image2DObjects);
|
sl@0
|
456 |
m3gGetObjectsWithClassID(M3G_INTERFACE(ctx), M3G_CLASS_IMAGE, &image2DObjects);
|
sl@0
|
457 |
|
sl@0
|
458 |
i = m3gArraySize(&image2DObjects);
|
sl@0
|
459 |
|
sl@0
|
460 |
while (i > 0) {
|
sl@0
|
461 |
Image *image = (Image*)m3gGetArrayElement(&image2DObjects, --i);
|
sl@0
|
462 |
|
sl@0
|
463 |
m3gInvalidateImage(image);
|
sl@0
|
464 |
image->texObject = 0;
|
sl@0
|
465 |
}
|
sl@0
|
466 |
m3gDestroyArray(&image2DObjects, M3G_INTERFACE(ctx));
|
sl@0
|
467 |
#endif
|
sl@0
|
468 |
}
|
sl@0
|
469 |
|
sl@0
|
470 |
|
sl@0
|
471 |
/*!
|
sl@0
|
472 |
* \internal
|
sl@0
|
473 |
* \brief Sets up a new rendering target
|
sl@0
|
474 |
*
|
sl@0
|
475 |
* \param ctx the rendering context
|
sl@0
|
476 |
* \param targetType rendering target type
|
sl@0
|
477 |
* \param width width of the target
|
sl@0
|
478 |
* \param height height of the target
|
sl@0
|
479 |
* \param format target pixel format
|
sl@0
|
480 |
* \param handle user object handle
|
sl@0
|
481 |
*/
|
sl@0
|
482 |
static M3Gbool m3gBindRenderTarget(RenderContext *ctx,
|
sl@0
|
483 |
M3Genum targetType,
|
sl@0
|
484 |
M3Gint width, M3Gint height,
|
sl@0
|
485 |
M3GPixelFormat format,
|
sl@0
|
486 |
M3Guint handle)
|
sl@0
|
487 |
{
|
sl@0
|
488 |
/* Check for generic errors */
|
sl@0
|
489 |
|
sl@0
|
490 |
if (ctx->target.type != SURFACE_NONE) {
|
sl@0
|
491 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OPERATION);
|
sl@0
|
492 |
return M3G_FALSE;
|
sl@0
|
493 |
}
|
sl@0
|
494 |
if (!m3gValidTargetFormat(format)) {
|
sl@0
|
495 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_ENUM);
|
sl@0
|
496 |
return M3G_FALSE;
|
sl@0
|
497 |
}
|
sl@0
|
498 |
|
sl@0
|
499 |
/* If target width or height exceeds maximum viewport width or height
|
sl@0
|
500 |
an exception is thrown. */
|
sl@0
|
501 |
|
sl@0
|
502 |
if (width > M3G_MAX_VIEWPORT_WIDTH ||
|
sl@0
|
503 |
height > M3G_MAX_VIEWPORT_HEIGHT) {
|
sl@0
|
504 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_ENUM);
|
sl@0
|
505 |
return M3G_FALSE;
|
sl@0
|
506 |
}
|
sl@0
|
507 |
|
sl@0
|
508 |
/* Everything checks out; set up the target parameters */
|
sl@0
|
509 |
|
sl@0
|
510 |
ctx->target.type = targetType;
|
sl@0
|
511 |
ctx->target.width = width;
|
sl@0
|
512 |
ctx->target.height = height;
|
sl@0
|
513 |
ctx->display.width = width;
|
sl@0
|
514 |
ctx->display.height = height;
|
sl@0
|
515 |
ctx->target.format = format;
|
sl@0
|
516 |
ctx->target.handle = handle;
|
sl@0
|
517 |
m3gResetRectangles(ctx);
|
sl@0
|
518 |
m3gUpdateScissor(ctx);
|
sl@0
|
519 |
m3gValidateBuffers(ctx);
|
sl@0
|
520 |
|
sl@0
|
521 |
/* Invalidate lights in case we're using a different OpenGL
|
sl@0
|
522 |
* rendering context this time around */
|
sl@0
|
523 |
|
sl@0
|
524 |
ctx->lastScope = 0;
|
sl@0
|
525 |
|
sl@0
|
526 |
return M3G_TRUE;
|
sl@0
|
527 |
}
|
sl@0
|
528 |
|
sl@0
|
529 |
/*!
|
sl@0
|
530 |
* \internal
|
sl@0
|
531 |
* \brief Initializes the current GL context to default settings.
|
sl@0
|
532 |
*/
|
sl@0
|
533 |
static void m3gSetGLDefaults(void)
|
sl@0
|
534 |
{
|
sl@0
|
535 |
static const GLfloat black[] = {0.f, 0.f, 0.f, 0.f};
|
sl@0
|
536 |
glEnable(GL_NORMALIZE);
|
sl@0
|
537 |
glEnable(GL_SCISSOR_TEST);
|
sl@0
|
538 |
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
|
sl@0
|
539 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
sl@0
|
540 |
}
|
sl@0
|
541 |
|
sl@0
|
542 |
/*!
|
sl@0
|
543 |
* \internal
|
sl@0
|
544 |
* \brief Validates the buffers required for a rendering context
|
sl@0
|
545 |
*
|
sl@0
|
546 |
* Allocates or reallocates buffers as necessary, according to the
|
sl@0
|
547 |
* currently set flags of the context.
|
sl@0
|
548 |
*/
|
sl@0
|
549 |
static void m3gValidateBuffers(RenderContext *ctx)
|
sl@0
|
550 |
{
|
sl@0
|
551 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
552 |
|
sl@0
|
553 |
/* Initialize OpenGL if not already done */
|
sl@0
|
554 |
|
sl@0
|
555 |
if (!ctx->glInitialized) {
|
sl@0
|
556 |
m3gInitializeGL(M3G_INTERFACE(ctx));
|
sl@0
|
557 |
ctx->glInitialized = M3G_TRUE;
|
sl@0
|
558 |
}
|
sl@0
|
559 |
|
sl@0
|
560 |
/* Check whether we can render directly to the target or need to
|
sl@0
|
561 |
* use a back buffer */
|
sl@0
|
562 |
|
sl@0
|
563 |
ctx->target.buffered = !m3gCanDirectRender(ctx);
|
sl@0
|
564 |
# if defined(M3G_FORCE_BUFFERED_RENDERING)
|
sl@0
|
565 |
ctx->target.buffered = M3G_TRUE;
|
sl@0
|
566 |
# endif
|
sl@0
|
567 |
|
sl@0
|
568 |
/* If direct rendering wasn't possible, check that the back buffer
|
sl@0
|
569 |
* for buffered rendering exists. */
|
sl@0
|
570 |
|
sl@0
|
571 |
if (ctx->target.buffered) {
|
sl@0
|
572 |
if (!m3gValidateBackBuffer(ctx)) {
|
sl@0
|
573 |
return; /* out of memory */
|
sl@0
|
574 |
}
|
sl@0
|
575 |
}
|
sl@0
|
576 |
|
sl@0
|
577 |
/* With the legacy NGL API, we also manage the depth buffer */
|
sl@0
|
578 |
|
sl@0
|
579 |
# if defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
580 |
if (!m3gValidateDepthBuffer(ctx)) {
|
sl@0
|
581 |
return; /* out of memory */
|
sl@0
|
582 |
}
|
sl@0
|
583 |
# endif
|
sl@0
|
584 |
|
sl@0
|
585 |
/* Delay blitting from the front buffer until we know it's
|
sl@0
|
586 |
* necessary; let's raise a flag to check that later on */
|
sl@0
|
587 |
|
sl@0
|
588 |
if (ctx->target.buffered) {
|
sl@0
|
589 |
if (ctx->modeBits & M3G_OVERWRITE_BIT) {
|
sl@0
|
590 |
ctx->backBuffer.contentsValid = M3G_TRUE;
|
sl@0
|
591 |
}
|
sl@0
|
592 |
else {
|
sl@0
|
593 |
ctx->backBuffer.contentsValid = M3G_FALSE;
|
sl@0
|
594 |
}
|
sl@0
|
595 |
}
|
sl@0
|
596 |
}
|
sl@0
|
597 |
|
sl@0
|
598 |
/*!
|
sl@0
|
599 |
* \internal
|
sl@0
|
600 |
* \brief Makes a GL context current to this thread and the currently
|
sl@0
|
601 |
* set rendering target buffer
|
sl@0
|
602 |
*/
|
sl@0
|
603 |
static void m3gMakeCurrent(RenderContext *ctx)
|
sl@0
|
604 |
{
|
sl@0
|
605 |
m3gMakeGLCurrent(ctx);
|
sl@0
|
606 |
|
sl@0
|
607 |
/* Note that the depth buffer may in some cases exist even if not
|
sl@0
|
608 |
* explicitly requested, so we need to disable the depth test just
|
sl@0
|
609 |
* in case */
|
sl@0
|
610 |
|
sl@0
|
611 |
if ((ctx->bufferBits & M3G_DEPTH_BUFFER_BIT) == 0) {
|
sl@0
|
612 |
glDisable(GL_DEPTH_TEST);
|
sl@0
|
613 |
}
|
sl@0
|
614 |
else {
|
sl@0
|
615 |
glEnable(GL_DEPTH_TEST);
|
sl@0
|
616 |
}
|
sl@0
|
617 |
|
sl@0
|
618 |
/* Enable multisampling if required */
|
sl@0
|
619 |
|
sl@0
|
620 |
if (ctx->modeBits & M3G_ANTIALIAS_BIT) {
|
sl@0
|
621 |
glEnable(GL_MULTISAMPLE);
|
sl@0
|
622 |
}
|
sl@0
|
623 |
else {
|
sl@0
|
624 |
glDisable(GL_MULTISAMPLE);
|
sl@0
|
625 |
}
|
sl@0
|
626 |
|
sl@0
|
627 |
M3G_ASSERT_GL;
|
sl@0
|
628 |
}
|
sl@0
|
629 |
|
sl@0
|
630 |
/*!
|
sl@0
|
631 |
* \internal
|
sl@0
|
632 |
* \brief Returns the HW acceleration status of the current context
|
sl@0
|
633 |
*/
|
sl@0
|
634 |
static M3Gbool m3gIsAccelerated(const RenderContext *ctx)
|
sl@0
|
635 |
{
|
sl@0
|
636 |
return ctx->accelerated;
|
sl@0
|
637 |
}
|
sl@0
|
638 |
|
sl@0
|
639 |
/*!
|
sl@0
|
640 |
* \internal
|
sl@0
|
641 |
* \brief Sets the currently enabled lights to the GL state
|
sl@0
|
642 |
*
|
sl@0
|
643 |
* \note the correct viewing matrix *must* be set prior to calling
|
sl@0
|
644 |
* this for the lights to be transformed into eye space correctly
|
sl@0
|
645 |
*/
|
sl@0
|
646 |
static M3G_INLINE void m3gApplyLights(RenderContext *ctx, M3Gint scope)
|
sl@0
|
647 |
{
|
sl@0
|
648 |
if (ctx->lastScope != scope) {
|
sl@0
|
649 |
|
sl@0
|
650 |
/* If coming from RenderNode, we have the geometry in camera
|
sl@0
|
651 |
* space but the lights in world space, so we need to apply
|
sl@0
|
652 |
* the viewing matrix to the lights only */
|
sl@0
|
653 |
|
sl@0
|
654 |
if (ctx->renderMode == RENDER_NODES) {
|
sl@0
|
655 |
glPushMatrix();
|
sl@0
|
656 |
glLoadMatrixf(ctx->viewTransform);
|
sl@0
|
657 |
}
|
sl@0
|
658 |
|
sl@0
|
659 |
m3gSelectGLLights(&ctx->lightManager, 8, scope, 0, 0, 0);
|
sl@0
|
660 |
ctx->lastScope = scope;
|
sl@0
|
661 |
|
sl@0
|
662 |
if (ctx->renderMode == RENDER_NODES) {
|
sl@0
|
663 |
glPopMatrix();
|
sl@0
|
664 |
}
|
sl@0
|
665 |
}
|
sl@0
|
666 |
M3G_ASSERT_GL;
|
sl@0
|
667 |
}
|
sl@0
|
668 |
|
sl@0
|
669 |
/*!
|
sl@0
|
670 |
* \internal
|
sl@0
|
671 |
* \brief Gets the current camera
|
sl@0
|
672 |
*/
|
sl@0
|
673 |
static const Camera *m3gGetCurrentCamera(const RenderContext *ctx) {
|
sl@0
|
674 |
return ctx->camera;
|
sl@0
|
675 |
}
|
sl@0
|
676 |
|
sl@0
|
677 |
/*!
|
sl@0
|
678 |
* \internal
|
sl@0
|
679 |
* \brief Sets up some rendering parameters that
|
sl@0
|
680 |
* do not change during scene renders.
|
sl@0
|
681 |
*/
|
sl@0
|
682 |
static void m3gInitRender(M3GRenderContext context, M3Genum renderMode)
|
sl@0
|
683 |
{
|
sl@0
|
684 |
RenderContext *ctx = (RenderContext *) context;
|
sl@0
|
685 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
686 |
|
sl@0
|
687 |
m3gIncrementRenderTimeStamp(ctx);
|
sl@0
|
688 |
m3gMakeCurrent(ctx);
|
sl@0
|
689 |
m3gCollectGLObjects(M3G_INTERFACE(ctx));
|
sl@0
|
690 |
|
sl@0
|
691 |
/* If buffered rendering, blit the image to the back buffer at
|
sl@0
|
692 |
* this point */
|
sl@0
|
693 |
|
sl@0
|
694 |
if (ctx->target.buffered && !ctx->backBuffer.contentsValid) {
|
sl@0
|
695 |
m3gUpdateBackBuffer(ctx);
|
sl@0
|
696 |
}
|
sl@0
|
697 |
|
sl@0
|
698 |
/* Set up viewport and scissoring */
|
sl@0
|
699 |
|
sl@0
|
700 |
glViewport(ctx->viewport.x, ctx->viewport.y,
|
sl@0
|
701 |
ctx->viewport.width, ctx->viewport.height);
|
sl@0
|
702 |
glDepthRangef(ctx->depthNear, ctx->depthFar);
|
sl@0
|
703 |
glScissor(ctx->scissor.x, ctx->scissor.y,
|
sl@0
|
704 |
ctx->scissor.width, ctx->scissor.height);
|
sl@0
|
705 |
M3G_ASSERT_GL;
|
sl@0
|
706 |
|
sl@0
|
707 |
/* Set up the projection and viewing transformations (static
|
sl@0
|
708 |
* during rendering) */
|
sl@0
|
709 |
|
sl@0
|
710 |
m3gApplyProjection(ctx->camera);
|
sl@0
|
711 |
if (renderMode == RENDER_NODES) {
|
sl@0
|
712 |
glLoadIdentity();
|
sl@0
|
713 |
}
|
sl@0
|
714 |
else {
|
sl@0
|
715 |
glLoadMatrixf(ctx->viewTransform);
|
sl@0
|
716 |
}
|
sl@0
|
717 |
M3G_ASSERT_GL;
|
sl@0
|
718 |
|
sl@0
|
719 |
/* Invalidate any already set GL lights if rendering mode changed */
|
sl@0
|
720 |
|
sl@0
|
721 |
if (renderMode != ctx->renderMode) {
|
sl@0
|
722 |
ctx->lastScope = 0;
|
sl@0
|
723 |
}
|
sl@0
|
724 |
ctx->renderMode = renderMode;
|
sl@0
|
725 |
}
|
sl@0
|
726 |
|
sl@0
|
727 |
/*!
|
sl@0
|
728 |
* \internal
|
sl@0
|
729 |
* \brief A workaround for a broken implementation of glColorMask
|
sl@0
|
730 |
*
|
sl@0
|
731 |
* Saves the framebuffer in the OpenGL default texture each time the
|
sl@0
|
732 |
* color mask changes, for restoring later. Not very pretty, but
|
sl@0
|
733 |
* works as long as the default texture is not touched in between --
|
sl@0
|
734 |
* currently, we only touch that when copying to and from the back
|
sl@0
|
735 |
* buffer.
|
sl@0
|
736 |
*
|
sl@0
|
737 |
* \param newColorWrite the color mask state we're about to change to
|
sl@0
|
738 |
* \param newAlphaWrite the alpha write state we're about to change to
|
sl@0
|
739 |
*/
|
sl@0
|
740 |
static void m3gUpdateColorMaskStatus(RenderContext *ctx,
|
sl@0
|
741 |
M3Gbool newColorWrite,
|
sl@0
|
742 |
M3Gbool newAlphaWrite)
|
sl@0
|
743 |
{
|
sl@0
|
744 |
GLint pow2Width, pow2Height;
|
sl@0
|
745 |
|
sl@0
|
746 |
/* Get the global alpha write value */
|
sl@0
|
747 |
newAlphaWrite &= m3gGetAlphaWrite(ctx);
|
sl@0
|
748 |
|
sl@0
|
749 |
/* Check that the ColorMask state is actually about to change */
|
sl@0
|
750 |
|
sl@0
|
751 |
if (ctx->currentColorWrite == newColorWrite
|
sl@0
|
752 |
&& (ctx->currentAlphaWrite == newAlphaWrite || !m3gFormatHasAlpha(ctx->target.format))) {
|
sl@0
|
753 |
return; /* no change, quick exit */
|
sl@0
|
754 |
}
|
sl@0
|
755 |
|
sl@0
|
756 |
pow2Width = m3gNextPowerOfTwo(ctx->clip.x1 - ctx->clip.x0);
|
sl@0
|
757 |
pow2Height = m3gNextPowerOfTwo(ctx->clip.y1 - ctx->clip.y0);
|
sl@0
|
758 |
|
sl@0
|
759 |
/* If we previously had stored something, restore it now */
|
sl@0
|
760 |
|
sl@0
|
761 |
if (ctx->currentColorWrite != ctx->currentAlphaWrite) {
|
sl@0
|
762 |
|
sl@0
|
763 |
/* Disable any stray state we don't want */
|
sl@0
|
764 |
|
sl@0
|
765 |
glDisable(GL_CULL_FACE);
|
sl@0
|
766 |
glDisable(GL_ALPHA_TEST);
|
sl@0
|
767 |
glDisableClientState(GL_NORMAL_ARRAY);
|
sl@0
|
768 |
glDisableClientState(GL_COLOR_ARRAY);
|
sl@0
|
769 |
glDisable(GL_LIGHTING);
|
sl@0
|
770 |
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
sl@0
|
771 |
glDepthMask(GL_FALSE);
|
sl@0
|
772 |
glDepthFunc(GL_ALWAYS);
|
sl@0
|
773 |
m3gDisableTextures();
|
sl@0
|
774 |
M3G_ASSERT_GL;
|
sl@0
|
775 |
|
sl@0
|
776 |
/* Bind the default texture and set up screen space rendering */
|
sl@0
|
777 |
|
sl@0
|
778 |
glActiveTexture(GL_TEXTURE0);
|
sl@0
|
779 |
glBindTexture(GL_TEXTURE_2D, 0);
|
sl@0
|
780 |
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
sl@0
|
781 |
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
sl@0
|
782 |
M3G_ASSERT_GL;
|
sl@0
|
783 |
|
sl@0
|
784 |
glScissor(ctx->clip.x0, ctx->clip.y0,
|
sl@0
|
785 |
ctx->clip.x1 - ctx->clip.x0, ctx->clip.y1 - ctx->clip.y0);
|
sl@0
|
786 |
m3gPushScreenSpace(ctx, M3G_FALSE);
|
sl@0
|
787 |
glViewport(0, 0, ctx->target.width, ctx->target.height);
|
sl@0
|
788 |
glMatrixMode(GL_PROJECTION);
|
sl@0
|
789 |
glOrthox(0, ctx->target.width << 16,
|
sl@0
|
790 |
0, ctx->target.height << 16,
|
sl@0
|
791 |
-1 << 16, 1 << 16);
|
sl@0
|
792 |
glMatrixMode(GL_MODELVIEW);
|
sl@0
|
793 |
|
sl@0
|
794 |
/* Set up texture and vertex coordinate arrays */
|
sl@0
|
795 |
|
sl@0
|
796 |
glClientActiveTexture(GL_TEXTURE0);
|
sl@0
|
797 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
sl@0
|
798 |
glEnableClientState(GL_VERTEX_ARRAY);
|
sl@0
|
799 |
glMatrixMode(GL_TEXTURE);
|
sl@0
|
800 |
glLoadIdentity();
|
sl@0
|
801 |
glMatrixMode(GL_MODELVIEW);
|
sl@0
|
802 |
M3G_ASSERT_GL;
|
sl@0
|
803 |
|
sl@0
|
804 |
/* Blend the texture with the frame buffer */
|
sl@0
|
805 |
{
|
sl@0
|
806 |
static const M3Gbyte tc[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
|
sl@0
|
807 |
GLshort pos[8];
|
sl@0
|
808 |
|
sl@0
|
809 |
GLfixed cm = (GLfixed)(ctx->currentColorWrite ? 0 : 1 << 16);
|
sl@0
|
810 |
GLfixed am = (GLfixed)(ctx->currentAlphaWrite ? 0 : 1 << 16);
|
sl@0
|
811 |
|
sl@0
|
812 |
glVertexPointer(2, GL_SHORT, 0, pos);
|
sl@0
|
813 |
glTexCoordPointer(2, GL_BYTE, 0, tc);
|
sl@0
|
814 |
|
sl@0
|
815 |
pos[0] = (GLshort) ctx->clip.x0;
|
sl@0
|
816 |
pos[1] = (GLshort) ctx->clip.y0;
|
sl@0
|
817 |
pos[2] = pos[0];
|
sl@0
|
818 |
pos[3] = (GLshort) (pos[1] + pow2Height);
|
sl@0
|
819 |
pos[4] = (GLshort) (pos[0] + pow2Width);
|
sl@0
|
820 |
pos[5] = pos[1];
|
sl@0
|
821 |
pos[6] = pos[4];
|
sl@0
|
822 |
pos[7] = pos[3];
|
sl@0
|
823 |
|
sl@0
|
824 |
glEnable(GL_BLEND);
|
sl@0
|
825 |
glColor4x(cm, cm, cm, am);
|
sl@0
|
826 |
|
sl@0
|
827 |
/* Zero the masked channels */
|
sl@0
|
828 |
|
sl@0
|
829 |
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
|
sl@0
|
830 |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
sl@0
|
831 |
|
sl@0
|
832 |
/* Add the masked channels from the stored texture */
|
sl@0
|
833 |
|
sl@0
|
834 |
glEnable(GL_TEXTURE_2D);
|
sl@0
|
835 |
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
sl@0
|
836 |
glBlendFunc(GL_ONE, GL_ONE);
|
sl@0
|
837 |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
sl@0
|
838 |
}
|
sl@0
|
839 |
|
sl@0
|
840 |
/* Restore the mandatory state */
|
sl@0
|
841 |
|
sl@0
|
842 |
glScissor(ctx->scissor.x, ctx->scissor.y,
|
sl@0
|
843 |
ctx->scissor.width, ctx->scissor.height);
|
sl@0
|
844 |
glViewport(ctx->viewport.x, ctx->viewport.y,
|
sl@0
|
845 |
ctx->viewport.width, ctx->viewport.height);
|
sl@0
|
846 |
m3gPopSpace(ctx);
|
sl@0
|
847 |
}
|
sl@0
|
848 |
|
sl@0
|
849 |
/* Copy the current clip rectangle into the default texture if
|
sl@0
|
850 |
* we're going to be rendering with unsupported masks in effect */
|
sl@0
|
851 |
|
sl@0
|
852 |
if (newColorWrite != newAlphaWrite) {
|
sl@0
|
853 |
GLenum err;
|
sl@0
|
854 |
|
sl@0
|
855 |
glBindTexture(GL_TEXTURE_2D, 0);
|
sl@0
|
856 |
M3G_ASSERT_GL;
|
sl@0
|
857 |
|
sl@0
|
858 |
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
|
sl@0
|
859 |
ctx->clip.x0, ctx->clip.y0,
|
sl@0
|
860 |
pow2Width, pow2Height,
|
sl@0
|
861 |
0);
|
sl@0
|
862 |
err = glGetError();
|
sl@0
|
863 |
if (err == GL_INVALID_OPERATION) {
|
sl@0
|
864 |
/* Incompatible FB format -- must be GL_RGB then */
|
sl@0
|
865 |
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
|
sl@0
|
866 |
ctx->clip.x0, ctx->clip.y0,
|
sl@0
|
867 |
pow2Width, pow2Height,
|
sl@0
|
868 |
0);
|
sl@0
|
869 |
err = glGetError();
|
sl@0
|
870 |
}
|
sl@0
|
871 |
if (err == GL_OUT_OF_MEMORY) {
|
sl@0
|
872 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_OUT_OF_MEMORY);
|
sl@0
|
873 |
}
|
sl@0
|
874 |
M3G_ASSERT(!err);
|
sl@0
|
875 |
}
|
sl@0
|
876 |
else {
|
sl@0
|
877 |
|
sl@0
|
878 |
/* Texture not needed for now, so allow GL to free some
|
sl@0
|
879 |
* resources */
|
sl@0
|
880 |
|
sl@0
|
881 |
glTexImage2D(GL_TEXTURE_2D, 0,
|
sl@0
|
882 |
GL_RGBA,
|
sl@0
|
883 |
1, 1,
|
sl@0
|
884 |
0,
|
sl@0
|
885 |
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
sl@0
|
886 |
}
|
sl@0
|
887 |
|
sl@0
|
888 |
ctx->currentColorWrite = newColorWrite;
|
sl@0
|
889 |
ctx->currentAlphaWrite = newAlphaWrite;
|
sl@0
|
890 |
}
|
sl@0
|
891 |
|
sl@0
|
892 |
/*!
|
sl@0
|
893 |
* \internal
|
sl@0
|
894 |
* \brief Sets the GL to input screen space coordinates
|
sl@0
|
895 |
*
|
sl@0
|
896 |
* This pushes the current modelview and projection matrices into the
|
sl@0
|
897 |
* matrix stack, then sets up an orthogonal projection and an identity
|
sl@0
|
898 |
* modelview matrix.
|
sl@0
|
899 |
*
|
sl@0
|
900 |
* \param ctx the rendering context
|
sl@0
|
901 |
* \param realPixels M3G_TRUE to use actual pixel coordinates,
|
sl@0
|
902 |
* M3G_FALSE to use normalized device coordinates
|
sl@0
|
903 |
*/
|
sl@0
|
904 |
static void m3gPushScreenSpace(RenderContext *ctx, M3Gbool realPixels)
|
sl@0
|
905 |
{
|
sl@0
|
906 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
907 |
|
sl@0
|
908 |
glMatrixMode(GL_PROJECTION);
|
sl@0
|
909 |
glPushMatrix();
|
sl@0
|
910 |
glLoadIdentity();
|
sl@0
|
911 |
if (realPixels) {
|
sl@0
|
912 |
int w = ctx->viewport.width;
|
sl@0
|
913 |
int h = ctx->viewport.height;
|
sl@0
|
914 |
glOrthox(0, w << 16, 0, h << 16, -1 << 16, 1 << 16);
|
sl@0
|
915 |
}
|
sl@0
|
916 |
glMatrixMode(GL_MODELVIEW);
|
sl@0
|
917 |
glPushMatrix();
|
sl@0
|
918 |
glLoadIdentity();
|
sl@0
|
919 |
}
|
sl@0
|
920 |
|
sl@0
|
921 |
/*!
|
sl@0
|
922 |
* \internal
|
sl@0
|
923 |
* \brief Restores the projection and modelview matrix modified by
|
sl@0
|
924 |
* m3gPushScreenSpace
|
sl@0
|
925 |
*/
|
sl@0
|
926 |
static void m3gPopSpace(RenderContext *ctx)
|
sl@0
|
927 |
{
|
sl@0
|
928 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
929 |
|
sl@0
|
930 |
M3G_UNREF(ctx);
|
sl@0
|
931 |
glMatrixMode(GL_PROJECTION);
|
sl@0
|
932 |
glPopMatrix();
|
sl@0
|
933 |
glMatrixMode(GL_MODELVIEW);
|
sl@0
|
934 |
glPopMatrix();
|
sl@0
|
935 |
}
|
sl@0
|
936 |
|
sl@0
|
937 |
/*!
|
sl@0
|
938 |
* \internal
|
sl@0
|
939 |
* \brief Clears the current buffer(s)
|
sl@0
|
940 |
*/
|
sl@0
|
941 |
static void m3gClearInternal(RenderContext *ctx, Background *bg)
|
sl@0
|
942 |
{
|
sl@0
|
943 |
m3gMakeCurrent(ctx);
|
sl@0
|
944 |
|
sl@0
|
945 |
/* If buffered rendering, copy data to the back buffer at this
|
sl@0
|
946 |
* point if we're not clearing the whole clip rectangle */
|
sl@0
|
947 |
|
sl@0
|
948 |
if (ctx->target.buffered && !ctx->backBuffer.contentsValid) {
|
sl@0
|
949 |
if (ctx->scissor.x > ctx->clip.x0 || ctx->scissor.y > ctx->clip.y0 ||
|
sl@0
|
950 |
ctx->scissor.x + ctx->scissor.width < ctx->clip.x1 ||
|
sl@0
|
951 |
ctx->scissor.y + ctx->scissor.height < ctx->clip.y1) {
|
sl@0
|
952 |
m3gUpdateBackBuffer(ctx);
|
sl@0
|
953 |
}
|
sl@0
|
954 |
}
|
sl@0
|
955 |
|
sl@0
|
956 |
if (m3gGetColorMaskWorkaround(M3G_INTERFACE(ctx))) {
|
sl@0
|
957 |
m3gUpdateColorMaskStatus(ctx, M3G_TRUE, M3G_TRUE);
|
sl@0
|
958 |
}
|
sl@0
|
959 |
|
sl@0
|
960 |
glDepthMask(GL_TRUE);
|
sl@0
|
961 |
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, m3gGetAlphaWrite(ctx));
|
sl@0
|
962 |
glDepthRangef(ctx->depthNear, ctx->depthFar);
|
sl@0
|
963 |
glViewport(ctx->viewport.x, ctx->viewport.y,
|
sl@0
|
964 |
ctx->viewport.width, ctx->viewport.height);
|
sl@0
|
965 |
glScissor(ctx->scissor.x, ctx->scissor.y,
|
sl@0
|
966 |
ctx->scissor.width, ctx->scissor.height);
|
sl@0
|
967 |
|
sl@0
|
968 |
/* Touch the background image to make sure it's created prior to
|
sl@0
|
969 |
* locking memory for rendering */
|
sl@0
|
970 |
|
sl@0
|
971 |
if (bg != NULL && bg->image != NULL) {
|
sl@0
|
972 |
if (!m3gGetPowerOfTwoImage(bg->image)) {
|
sl@0
|
973 |
return; /* out of memory */
|
sl@0
|
974 |
}
|
sl@0
|
975 |
}
|
sl@0
|
976 |
|
sl@0
|
977 |
/* All clear for clearing... */
|
sl@0
|
978 |
|
sl@0
|
979 |
m3gLockFrameBuffer(ctx);
|
sl@0
|
980 |
|
sl@0
|
981 |
if (bg != NULL) {
|
sl@0
|
982 |
m3gApplyBackground(ctx, bg);
|
sl@0
|
983 |
if (ctx->target.buffered && bg->colorClearEnable) {
|
sl@0
|
984 |
ctx->backBuffer.contentsValid = M3G_TRUE;
|
sl@0
|
985 |
}
|
sl@0
|
986 |
}
|
sl@0
|
987 |
else {
|
sl@0
|
988 |
glClearColorx(0, 0, 0, 0);
|
sl@0
|
989 |
glClearDepthx(1 << 16);
|
sl@0
|
990 |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
sl@0
|
991 |
if (ctx->target.buffered) {
|
sl@0
|
992 |
ctx->backBuffer.contentsValid = M3G_TRUE;
|
sl@0
|
993 |
}
|
sl@0
|
994 |
}
|
sl@0
|
995 |
|
sl@0
|
996 |
m3gReleaseFrameBuffer(ctx);
|
sl@0
|
997 |
}
|
sl@0
|
998 |
|
sl@0
|
999 |
/*!
|
sl@0
|
1000 |
* \internal
|
sl@0
|
1001 |
* \brief Draws a batch of primitives
|
sl@0
|
1002 |
*
|
sl@0
|
1003 |
* This is the place most rendering commands are eventually routed to;
|
sl@0
|
1004 |
* sprites and backgrounds are the only exception to this. We assume
|
sl@0
|
1005 |
* that all eror checking has been performed at this point.
|
sl@0
|
1006 |
*/
|
sl@0
|
1007 |
static void m3gDrawMesh(RenderContext *ctx,
|
sl@0
|
1008 |
const VertexBuffer *vb,
|
sl@0
|
1009 |
const IndexBuffer *ib,
|
sl@0
|
1010 |
const Appearance *app,
|
sl@0
|
1011 |
const M3GMatrix *modelTransform,
|
sl@0
|
1012 |
M3Gint alphaFactor,
|
sl@0
|
1013 |
M3Gint scope)
|
sl@0
|
1014 |
{
|
sl@0
|
1015 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1016 |
M3G_VALIDATE_OBJECT(vb);
|
sl@0
|
1017 |
M3G_VALIDATE_OBJECT(ib);
|
sl@0
|
1018 |
M3G_VALIDATE_OBJECT(app);
|
sl@0
|
1019 |
|
sl@0
|
1020 |
/* Check whether we need to use alternate rendering to get
|
sl@0
|
1021 |
* two-sided lighting */
|
sl@0
|
1022 |
if (m3gGetTwoSidedLightingWorkaround(M3G_INTERFACE(ctx))) {
|
sl@0
|
1023 |
if (m3gSplitDrawMesh(ctx, vb, ib, app, modelTransform, alphaFactor, scope)) {
|
sl@0
|
1024 |
return;
|
sl@0
|
1025 |
}
|
sl@0
|
1026 |
}
|
sl@0
|
1027 |
|
sl@0
|
1028 |
M3G_ASSERT(m3gInRange(alphaFactor, 0, 0x10000));
|
sl@0
|
1029 |
|
sl@0
|
1030 |
if (m3gGetColorMaskWorkaround(M3G_INTERFACE(ctx))) {
|
sl@0
|
1031 |
m3gUpdateColorMaskStatus(ctx, m3gColorMask(app), m3gAlphaMask(app));
|
sl@0
|
1032 |
}
|
sl@0
|
1033 |
|
sl@0
|
1034 |
/* Load lights */
|
sl@0
|
1035 |
|
sl@0
|
1036 |
m3gApplyLights(ctx, scope);
|
sl@0
|
1037 |
|
sl@0
|
1038 |
/* Apply the extra modeling transformation if present */
|
sl@0
|
1039 |
|
sl@0
|
1040 |
if (modelTransform != NULL) {
|
sl@0
|
1041 |
float transform[16];
|
sl@0
|
1042 |
m3gGetMatrixColumns(modelTransform, transform);
|
sl@0
|
1043 |
|
sl@0
|
1044 |
glPushMatrix();
|
sl@0
|
1045 |
glMultMatrixf(transform);
|
sl@0
|
1046 |
}
|
sl@0
|
1047 |
|
sl@0
|
1048 |
/* Check whether we need to create an alpha-factored color cache
|
sl@0
|
1049 |
* for the vertex buffer; this requires unlocking the frame buffer
|
sl@0
|
1050 |
* for a while, and we may even run out of memory in the process,
|
sl@0
|
1051 |
* but we still need to exit with the frame buffer lock and the
|
sl@0
|
1052 |
* matrix stack in the expected state */
|
sl@0
|
1053 |
|
sl@0
|
1054 |
if (alphaFactor < 0x10000 && !m3gValidateAlphaCache(vb)) {
|
sl@0
|
1055 |
M3Gbool ok;
|
sl@0
|
1056 |
m3gReleaseFrameBuffer(ctx);
|
sl@0
|
1057 |
ok = m3gCreateAlphaColorCache(vb->colors);
|
sl@0
|
1058 |
m3gLockFrameBuffer(ctx);
|
sl@0
|
1059 |
if (!ok) {
|
sl@0
|
1060 |
goto RestoreModelview; /* let's just skip the drawing part */
|
sl@0
|
1061 |
}
|
sl@0
|
1062 |
}
|
sl@0
|
1063 |
|
sl@0
|
1064 |
# if defined(M3G_NGL_TEXTURE_API)
|
sl@0
|
1065 |
/* Similarly to the alpha cache above, also check whether any
|
sl@0
|
1066 |
* textures may need to allocate mipmaps at this point */
|
sl@0
|
1067 |
{
|
sl@0
|
1068 |
M3Gint i;
|
sl@0
|
1069 |
for (i = 0; i < M3G_NUM_TEXTURE_UNITS; ++i) {
|
sl@0
|
1070 |
Texture *tex = app->texture[i];
|
sl@0
|
1071 |
if (tex && !m3gValidateTextureMipmapping(tex)) {
|
sl@0
|
1072 |
M3Gbool ok;
|
sl@0
|
1073 |
m3gReleaseFrameBuffer(ctx);
|
sl@0
|
1074 |
ok = m3gValidateMipmapMemory(m3gGetTextureImage(tex));
|
sl@0
|
1075 |
m3gLockFrameBuffer(ctx);
|
sl@0
|
1076 |
if (!ok) {
|
sl@0
|
1077 |
goto RestoreModelview;
|
sl@0
|
1078 |
}
|
sl@0
|
1079 |
}
|
sl@0
|
1080 |
}
|
sl@0
|
1081 |
}
|
sl@0
|
1082 |
# endif
|
sl@0
|
1083 |
|
sl@0
|
1084 |
/* Load up the rest of the stuff we need for rendering; note that
|
sl@0
|
1085 |
* the vertex buffer scale and bias apply to the texture matrix
|
sl@0
|
1086 |
* from the appearance object, so they need to be applied last */
|
sl@0
|
1087 |
|
sl@0
|
1088 |
m3gApplyAppearance(app, ctx, alphaFactor);
|
sl@0
|
1089 |
m3gLockVertexBuffer(vb, alphaFactor);
|
sl@0
|
1090 |
m3gApplyScaleAndBias(vb);
|
sl@0
|
1091 |
|
sl@0
|
1092 |
/* All ready, render and then release the stuff we bound above */
|
sl@0
|
1093 |
|
sl@0
|
1094 |
m3gSendIndexBuffer(ib);
|
sl@0
|
1095 |
m3gReleaseVertexBuffer(vb);
|
sl@0
|
1096 |
m3gReleaseTextures(app);
|
sl@0
|
1097 |
|
sl@0
|
1098 |
/* Restore viewing-only modelview if changed */
|
sl@0
|
1099 |
|
sl@0
|
1100 |
RestoreModelview:
|
sl@0
|
1101 |
if (modelTransform != NULL) {
|
sl@0
|
1102 |
glPopMatrix();
|
sl@0
|
1103 |
}
|
sl@0
|
1104 |
}
|
sl@0
|
1105 |
|
sl@0
|
1106 |
/*!
|
sl@0
|
1107 |
* \internal
|
sl@0
|
1108 |
* \brief Validates background format against current target
|
sl@0
|
1109 |
*
|
sl@0
|
1110 |
* \retval M3G_TRUE valid format
|
sl@0
|
1111 |
* \retval M3G_FALSE invalid format
|
sl@0
|
1112 |
*/
|
sl@0
|
1113 |
static M3Gbool m3gValidateBackground(RenderContext *ctx, Background *bg)
|
sl@0
|
1114 |
{
|
sl@0
|
1115 |
/* Check that source image and target formats match */
|
sl@0
|
1116 |
if (bg != NULL && bg->image != NULL) {
|
sl@0
|
1117 |
M3GPixelFormat boundFormat =
|
sl@0
|
1118 |
(ctx->target.type == SURFACE_IMAGE)
|
sl@0
|
1119 |
? m3gPixelFormat(((const Image *)ctx->target.handle)->format)
|
sl@0
|
1120 |
: ctx->target.format;
|
sl@0
|
1121 |
if (ctx->target.type == SURFACE_IMAGE && boundFormat == M3G_RGBA8) {
|
sl@0
|
1122 |
return (m3gGetFormat(bg->image) == M3G_RGBA);
|
sl@0
|
1123 |
}
|
sl@0
|
1124 |
else {
|
sl@0
|
1125 |
return (m3gGetFormat(bg->image) == M3G_RGB);
|
sl@0
|
1126 |
}
|
sl@0
|
1127 |
}
|
sl@0
|
1128 |
|
sl@0
|
1129 |
return M3G_TRUE;
|
sl@0
|
1130 |
}
|
sl@0
|
1131 |
|
sl@0
|
1132 |
/*----------------------------------------------------------------------
|
sl@0
|
1133 |
* Virtual function table
|
sl@0
|
1134 |
*--------------------------------------------------------------------*/
|
sl@0
|
1135 |
|
sl@0
|
1136 |
static const ObjectVFTable m3gvf_RenderContext = {
|
sl@0
|
1137 |
NULL, /* ApplyAnimation */
|
sl@0
|
1138 |
NULL, /* IsCompatible */
|
sl@0
|
1139 |
NULL, /* UpdateProperty */
|
sl@0
|
1140 |
NULL, /* GetReference */
|
sl@0
|
1141 |
NULL, /* find */
|
sl@0
|
1142 |
NULL, /* CreateClone */
|
sl@0
|
1143 |
m3gDestroyContext
|
sl@0
|
1144 |
};
|
sl@0
|
1145 |
|
sl@0
|
1146 |
|
sl@0
|
1147 |
/*----------------------------------------------------------------------
|
sl@0
|
1148 |
* Public API
|
sl@0
|
1149 |
*--------------------------------------------------------------------*/
|
sl@0
|
1150 |
|
sl@0
|
1151 |
/*!
|
sl@0
|
1152 |
* \brief Creates and initializes a new rendering context
|
sl@0
|
1153 |
*
|
sl@0
|
1154 |
* \param bufferBits buffer bitmask
|
sl@0
|
1155 |
* \param width maximum width of context
|
sl@0
|
1156 |
* \param height maximum height of context
|
sl@0
|
1157 |
* \param modeBits hint bitmask
|
sl@0
|
1158 |
* \param mem pointer to memory block to allocate from
|
sl@0
|
1159 |
*/
|
sl@0
|
1160 |
/*@access M3GInterface@*/
|
sl@0
|
1161 |
/*@access M3GRenderContext@*/
|
sl@0
|
1162 |
/*@only@*/
|
sl@0
|
1163 |
M3G_API M3GRenderContext m3gCreateContext(M3GInterface interface)/*@*/
|
sl@0
|
1164 |
{
|
sl@0
|
1165 |
Interface *m3g = (Interface*) interface;
|
sl@0
|
1166 |
M3G_VALIDATE_INTERFACE(m3g);
|
sl@0
|
1167 |
|
sl@0
|
1168 |
{
|
sl@0
|
1169 |
RenderContext *ctx =
|
sl@0
|
1170 |
(RenderContext*) m3gAllocZ(m3g, (int) sizeof(RenderContext));
|
sl@0
|
1171 |
if (ctx == NULL) {
|
sl@0
|
1172 |
return NULL; /* m3gAlloc automatically raises out-of-mem */
|
sl@0
|
1173 |
}
|
sl@0
|
1174 |
|
sl@0
|
1175 |
ctx->renderQueue = m3gCreateRenderQueue(m3g);
|
sl@0
|
1176 |
if (ctx->renderQueue == NULL) {
|
sl@0
|
1177 |
m3gFree(m3g, ctx);
|
sl@0
|
1178 |
return NULL;
|
sl@0
|
1179 |
}
|
sl@0
|
1180 |
ctx->bufferBits = M3G_COLOR_BUFFER_BIT|M3G_DEPTH_BUFFER_BIT;
|
sl@0
|
1181 |
ctx->depthNear = 0.0f;
|
sl@0
|
1182 |
ctx->depthFar = 1.0f;
|
sl@0
|
1183 |
|
sl@0
|
1184 |
m3gInitObject(&ctx->object, m3g, M3G_CLASS_RENDER_CONTEXT);
|
sl@0
|
1185 |
|
sl@0
|
1186 |
m3gSetAlphaWrite(ctx, M3G_TRUE);
|
sl@0
|
1187 |
|
sl@0
|
1188 |
if (m3gGetColorMaskWorkaround(M3G_INTERFACE(ctx))) {
|
sl@0
|
1189 |
ctx->currentColorWrite = M3G_TRUE;
|
sl@0
|
1190 |
ctx->currentAlphaWrite = m3gGetAlphaWrite(ctx);
|
sl@0
|
1191 |
}
|
sl@0
|
1192 |
|
sl@0
|
1193 |
return (M3GRenderContext)ctx;
|
sl@0
|
1194 |
}
|
sl@0
|
1195 |
}
|
sl@0
|
1196 |
|
sl@0
|
1197 |
/*!
|
sl@0
|
1198 |
* \brief Sets the buffers to use for subsequent rendering
|
sl@0
|
1199 |
*/
|
sl@0
|
1200 |
M3G_API M3Gbool m3gSetRenderBuffers(M3GRenderContext hCtx,
|
sl@0
|
1201 |
M3Gbitmask bufferBits)
|
sl@0
|
1202 |
{
|
sl@0
|
1203 |
RenderContext *ctx = (RenderContext *) hCtx;
|
sl@0
|
1204 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1205 |
|
sl@0
|
1206 |
if ((bufferBits & ~(M3G_COLOR_BUFFER_BIT|M3G_DEPTH_BUFFER_BIT|M3G_STENCIL_BUFFER_BIT|M3G_MULTISAMPLE_BUFFER_BIT)) != 0) {
|
sl@0
|
1207 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_VALUE);
|
sl@0
|
1208 |
return M3G_FALSE;
|
sl@0
|
1209 |
}
|
sl@0
|
1210 |
ctx->bufferBits = bufferBits;
|
sl@0
|
1211 |
return M3G_TRUE;
|
sl@0
|
1212 |
}
|
sl@0
|
1213 |
|
sl@0
|
1214 |
/*!
|
sl@0
|
1215 |
* \brief Sets the rendering quality hints to use for subsequent
|
sl@0
|
1216 |
* rendering
|
sl@0
|
1217 |
*
|
sl@0
|
1218 |
* \note This may not take effect before the target is released and
|
sl@0
|
1219 |
* rebound
|
sl@0
|
1220 |
*/
|
sl@0
|
1221 |
M3G_API M3Gbool m3gSetRenderHints(M3GRenderContext hCtx, M3Gbitmask modeBits)
|
sl@0
|
1222 |
{
|
sl@0
|
1223 |
RenderContext *ctx = (RenderContext *) hCtx;
|
sl@0
|
1224 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1225 |
|
sl@0
|
1226 |
if ((modeBits & ~(M3G_OVERWRITE_BIT|M3G_ANTIALIAS_BIT|M3G_DITHER_BIT|M3G_TRUECOLOR_BIT)) != 0) {
|
sl@0
|
1227 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_VALUE);
|
sl@0
|
1228 |
return M3G_FALSE;
|
sl@0
|
1229 |
}
|
sl@0
|
1230 |
|
sl@0
|
1231 |
/* Disable features not supported in the current configuration */
|
sl@0
|
1232 |
|
sl@0
|
1233 |
if (M3G_SUPPORT_ANTIALIASING == M3G_FALSE ||
|
sl@0
|
1234 |
!m3gIsAntialiasingSupported(M3G_INTERFACE(ctx))) {
|
sl@0
|
1235 |
modeBits &= ~M3G_ANTIALIAS_BIT;
|
sl@0
|
1236 |
}
|
sl@0
|
1237 |
if (M3G_SUPPORT_DITHERING == M3G_FALSE) {
|
sl@0
|
1238 |
modeBits &= ~M3G_DITHER_BIT;
|
sl@0
|
1239 |
}
|
sl@0
|
1240 |
if (M3G_SUPPORT_TRUE_COLOR == M3G_FALSE) {
|
sl@0
|
1241 |
modeBits &= ~M3G_TRUECOLOR_BIT;
|
sl@0
|
1242 |
}
|
sl@0
|
1243 |
|
sl@0
|
1244 |
ctx->modeBits = modeBits;
|
sl@0
|
1245 |
return M3G_TRUE;
|
sl@0
|
1246 |
}
|
sl@0
|
1247 |
|
sl@0
|
1248 |
M3G_API void m3gBindImageTarget(M3GRenderContext hCtx, M3GImage hImage)
|
sl@0
|
1249 |
{
|
sl@0
|
1250 |
RenderContext *ctx = (RenderContext *) hCtx;
|
sl@0
|
1251 |
Image *img = (Image *) hImage;
|
sl@0
|
1252 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1253 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1254 |
|
sl@0
|
1255 |
M3G_LOG1(M3G_LOG_RENDERING, "Binding image target 0x%08X\n",
|
sl@0
|
1256 |
(unsigned) img);
|
sl@0
|
1257 |
|
sl@0
|
1258 |
/* Check for image-specific errors */
|
sl@0
|
1259 |
|
sl@0
|
1260 |
if ((img->flags & M3G_DYNAMIC) == 0
|
sl@0
|
1261 |
|| !m3gValidTargetFormat(img->internalFormat)) {
|
sl@0
|
1262 |
|
sl@0
|
1263 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_ENUM);
|
sl@0
|
1264 |
return;
|
sl@0
|
1265 |
}
|
sl@0
|
1266 |
|
sl@0
|
1267 |
/* Do the generic checking and set-up */
|
sl@0
|
1268 |
|
sl@0
|
1269 |
if (!m3gBindRenderTarget(ctx,
|
sl@0
|
1270 |
SURFACE_IMAGE,
|
sl@0
|
1271 |
img->width, img->height,
|
sl@0
|
1272 |
img->internalFormat,
|
sl@0
|
1273 |
(M3Guint) hImage)) {
|
sl@0
|
1274 |
return; /* appropriate error raised automatically */
|
sl@0
|
1275 |
}
|
sl@0
|
1276 |
|
sl@0
|
1277 |
/* Set up image-specific parameters */
|
sl@0
|
1278 |
|
sl@0
|
1279 |
# if defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
1280 |
ctx->target.stride = m3gGetImageStride(img);
|
sl@0
|
1281 |
ctx->target.pixels = NULL;
|
sl@0
|
1282 |
# endif
|
sl@0
|
1283 |
|
sl@0
|
1284 |
m3gAddRef((Object*) img);
|
sl@0
|
1285 |
}
|
sl@0
|
1286 |
|
sl@0
|
1287 |
/*!
|
sl@0
|
1288 |
*/
|
sl@0
|
1289 |
M3G_API M3Guint m3gGetUserHandle(M3GRenderContext hCtx)
|
sl@0
|
1290 |
{
|
sl@0
|
1291 |
RenderContext *ctx = (RenderContext *) hCtx;
|
sl@0
|
1292 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1293 |
|
sl@0
|
1294 |
if (ctx->target.type == SURFACE_MEMORY) {
|
sl@0
|
1295 |
return ctx->target.handle;
|
sl@0
|
1296 |
}
|
sl@0
|
1297 |
return 0;
|
sl@0
|
1298 |
}
|
sl@0
|
1299 |
|
sl@0
|
1300 |
/*!
|
sl@0
|
1301 |
*/
|
sl@0
|
1302 |
M3G_API void m3gSetUserData(M3GRenderContext hCtx, M3Guint hData)
|
sl@0
|
1303 |
{
|
sl@0
|
1304 |
RenderContext *ctx = (RenderContext *) hCtx;
|
sl@0
|
1305 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1306 |
ctx->target.userData = hData;
|
sl@0
|
1307 |
}
|
sl@0
|
1308 |
|
sl@0
|
1309 |
/*!
|
sl@0
|
1310 |
*/
|
sl@0
|
1311 |
M3G_API M3Guint m3gGetUserData(M3GRenderContext hCtx)
|
sl@0
|
1312 |
{
|
sl@0
|
1313 |
RenderContext *ctx = (RenderContext *) hCtx;
|
sl@0
|
1314 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1315 |
return ctx->target.userData;
|
sl@0
|
1316 |
}
|
sl@0
|
1317 |
|
sl@0
|
1318 |
/*!
|
sl@0
|
1319 |
* \brief Clears the current buffer(s)
|
sl@0
|
1320 |
*/
|
sl@0
|
1321 |
M3G_API void m3gClear(M3GRenderContext context, M3GBackground hBackground)
|
sl@0
|
1322 |
{
|
sl@0
|
1323 |
RenderContext *ctx = (RenderContext*) context;
|
sl@0
|
1324 |
Background *bg = (Background *) hBackground;
|
sl@0
|
1325 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1326 |
|
sl@0
|
1327 |
M3G_LOG(M3G_LOG_STAGES, "Clearing frame buffer\n");
|
sl@0
|
1328 |
|
sl@0
|
1329 |
/* Check errors */
|
sl@0
|
1330 |
|
sl@0
|
1331 |
if (ctx->target.type == SURFACE_NONE) {
|
sl@0
|
1332 |
m3gRaiseError(M3G_INTERFACE(context), M3G_INVALID_OPERATION);
|
sl@0
|
1333 |
return;
|
sl@0
|
1334 |
}
|
sl@0
|
1335 |
|
sl@0
|
1336 |
if(m3gValidateBackground(ctx, bg)) {
|
sl@0
|
1337 |
m3gClearInternal(ctx, bg);
|
sl@0
|
1338 |
}
|
sl@0
|
1339 |
else {
|
sl@0
|
1340 |
m3gRaiseError(M3G_INTERFACE(bg), M3G_INVALID_VALUE);
|
sl@0
|
1341 |
}
|
sl@0
|
1342 |
}
|
sl@0
|
1343 |
|
sl@0
|
1344 |
/*!
|
sl@0
|
1345 |
* \brief Release the currently bound color buffer
|
sl@0
|
1346 |
*
|
sl@0
|
1347 |
* Flushes all rendering and commits the final result to the currently
|
sl@0
|
1348 |
* bound target color buffer. Any changes to the target buffer since
|
sl@0
|
1349 |
* it was bound may be overwritten.
|
sl@0
|
1350 |
*/
|
sl@0
|
1351 |
M3G_API void m3gReleaseTarget(M3GRenderContext context)
|
sl@0
|
1352 |
{
|
sl@0
|
1353 |
RenderContext *ctx = (RenderContext*) context;
|
sl@0
|
1354 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1355 |
|
sl@0
|
1356 |
M3G_LOG(M3G_LOG_RENDERING, "Releasing target\n");
|
sl@0
|
1357 |
|
sl@0
|
1358 |
if (ctx->target.type == SURFACE_NONE) {
|
sl@0
|
1359 |
return;
|
sl@0
|
1360 |
}
|
sl@0
|
1361 |
|
sl@0
|
1362 |
m3gMakeCurrent(ctx);
|
sl@0
|
1363 |
|
sl@0
|
1364 |
if (m3gGetColorMaskWorkaround(M3G_INTERFACE(ctx))) {
|
sl@0
|
1365 |
m3gUpdateColorMaskStatus(ctx, M3G_TRUE, M3G_TRUE);
|
sl@0
|
1366 |
}
|
sl@0
|
1367 |
|
sl@0
|
1368 |
glFinish();
|
sl@0
|
1369 |
|
sl@0
|
1370 |
/* Update the real target if we rendered into the back buffer */
|
sl@0
|
1371 |
|
sl@0
|
1372 |
if (ctx->target.buffered) {
|
sl@0
|
1373 |
m3gUpdateTargetBuffer(ctx);
|
sl@0
|
1374 |
}
|
sl@0
|
1375 |
|
sl@0
|
1376 |
/* Invalidate Image targets so that mipmap levels and/or OpenGL
|
sl@0
|
1377 |
* texture objects are updated accordingly */
|
sl@0
|
1378 |
|
sl@0
|
1379 |
if (ctx->target.type == SURFACE_IMAGE) {
|
sl@0
|
1380 |
Image *img = (Image *) ctx->target.handle;
|
sl@0
|
1381 |
M3G_VALIDATE_OBJECT(img);
|
sl@0
|
1382 |
m3gInvalidateImage(img);
|
sl@0
|
1383 |
m3gDeleteRef((Object*) img);
|
sl@0
|
1384 |
}
|
sl@0
|
1385 |
|
sl@0
|
1386 |
/* Swap in case we rendered onto a double-buffered surface,
|
sl@0
|
1387 |
* release any GL resources that might have been release since the
|
sl@0
|
1388 |
* last time we rendered, then release the GL context so we don't
|
sl@0
|
1389 |
* hog resources */
|
sl@0
|
1390 |
# if !defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
1391 |
if (ctx->target.type == SURFACE_WINDOW) {
|
sl@0
|
1392 |
m3gSwapBuffers(ctx->target.surface);
|
sl@0
|
1393 |
}
|
sl@0
|
1394 |
# endif
|
sl@0
|
1395 |
m3gCollectGLObjects(M3G_INTERFACE(ctx));
|
sl@0
|
1396 |
# if !defined(M3G_NGL_CONTEXT_API)
|
sl@0
|
1397 |
m3gMakeGLCurrent(NULL);
|
sl@0
|
1398 |
ctx->target.surface = NULL;
|
sl@0
|
1399 |
# else
|
sl@0
|
1400 |
if (ctx->target.type == SURFACE_MEMORY && ctx->target.pixels == NULL) {
|
sl@0
|
1401 |
m3gSignalTargetRelease(M3G_INTERFACE(ctx), ctx->target.handle);
|
sl@0
|
1402 |
}
|
sl@0
|
1403 |
# endif
|
sl@0
|
1404 |
|
sl@0
|
1405 |
ctx->target.type = SURFACE_NONE;
|
sl@0
|
1406 |
ctx->renderQueue->root = NULL;
|
sl@0
|
1407 |
|
sl@0
|
1408 |
# if (M3G_PROFILE_LOG_INTERVAL > 0)
|
sl@0
|
1409 |
m3gLogProfileCounters(M3G_INTERFACE(ctx));
|
sl@0
|
1410 |
# endif
|
sl@0
|
1411 |
}
|
sl@0
|
1412 |
|
sl@0
|
1413 |
/*!
|
sl@0
|
1414 |
* \brief Sets a camera for this context
|
sl@0
|
1415 |
*/
|
sl@0
|
1416 |
M3G_API void m3gSetCamera(M3GRenderContext context,
|
sl@0
|
1417 |
M3GCamera hCamera,
|
sl@0
|
1418 |
M3GMatrix *transform)
|
sl@0
|
1419 |
{
|
sl@0
|
1420 |
Matrix m;
|
sl@0
|
1421 |
RenderContext *ctx = (RenderContext*) context;
|
sl@0
|
1422 |
const Camera *camera = (Camera *)hCamera;
|
sl@0
|
1423 |
|
sl@0
|
1424 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1425 |
|
sl@0
|
1426 |
M3G_ASSIGN_REF(ctx->camera, camera);
|
sl@0
|
1427 |
|
sl@0
|
1428 |
if (transform != NULL) {
|
sl@0
|
1429 |
if (!m3gMatrixInverse(&m, transform)) {
|
sl@0
|
1430 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_ARITHMETIC_ERROR);
|
sl@0
|
1431 |
return;
|
sl@0
|
1432 |
}
|
sl@0
|
1433 |
}
|
sl@0
|
1434 |
else {
|
sl@0
|
1435 |
m3gIdentityMatrix(&m);
|
sl@0
|
1436 |
}
|
sl@0
|
1437 |
|
sl@0
|
1438 |
m3gGetMatrixColumns(&m, ctx->viewTransform);
|
sl@0
|
1439 |
|
sl@0
|
1440 |
ctx->lastScope = 0;
|
sl@0
|
1441 |
}
|
sl@0
|
1442 |
|
sl@0
|
1443 |
/*!
|
sl@0
|
1444 |
* \brief Adds a light to the light array for this context
|
sl@0
|
1445 |
*/
|
sl@0
|
1446 |
M3G_API M3Gint m3gAddLight(M3GRenderContext hCtx,
|
sl@0
|
1447 |
M3GLight hLight,
|
sl@0
|
1448 |
const M3GMatrix *transform)
|
sl@0
|
1449 |
{
|
sl@0
|
1450 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1451 |
Light *light = (Light *)hLight;
|
sl@0
|
1452 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1453 |
|
sl@0
|
1454 |
if (light == NULL) {
|
sl@0
|
1455 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_VALUE);
|
sl@0
|
1456 |
return -1;
|
sl@0
|
1457 |
}
|
sl@0
|
1458 |
else {
|
sl@0
|
1459 |
LightManager *mgr = &ctx->lightManager;
|
sl@0
|
1460 |
M3G_VALIDATE_OBJECT(light);
|
sl@0
|
1461 |
ctx->lastScope = 0;
|
sl@0
|
1462 |
return m3gInsertLight(mgr, light, transform, M3G_INTERFACE(ctx));
|
sl@0
|
1463 |
}
|
sl@0
|
1464 |
}
|
sl@0
|
1465 |
|
sl@0
|
1466 |
/**
|
sl@0
|
1467 |
* \brief Sets a light for this context
|
sl@0
|
1468 |
*/
|
sl@0
|
1469 |
M3G_API void m3gSetLight(M3GRenderContext context,
|
sl@0
|
1470 |
M3Gint lightIndex,
|
sl@0
|
1471 |
M3GLight hLight,
|
sl@0
|
1472 |
const M3GMatrix *transform)
|
sl@0
|
1473 |
{
|
sl@0
|
1474 |
RenderContext *ctx = (RenderContext*) context;
|
sl@0
|
1475 |
Light *light = (Light *)hLight;
|
sl@0
|
1476 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1477 |
|
sl@0
|
1478 |
/* Check for invalid arguments */
|
sl@0
|
1479 |
if (lightIndex < 0 || lightIndex >= m3gLightArraySize(&ctx->lightManager)) {
|
sl@0
|
1480 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_INDEX);
|
sl@0
|
1481 |
return;
|
sl@0
|
1482 |
}
|
sl@0
|
1483 |
|
sl@0
|
1484 |
ctx->lastScope = 0;
|
sl@0
|
1485 |
m3gReplaceLight(&ctx->lightManager, lightIndex, light, transform);
|
sl@0
|
1486 |
}
|
sl@0
|
1487 |
|
sl@0
|
1488 |
/*!
|
sl@0
|
1489 |
* \brief Removes all lights from this context
|
sl@0
|
1490 |
*/
|
sl@0
|
1491 |
M3G_API void m3gClearLights(M3GRenderContext context)
|
sl@0
|
1492 |
{
|
sl@0
|
1493 |
RenderContext *ctx = (RenderContext *)context;
|
sl@0
|
1494 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1495 |
ctx->lastScope = 0;
|
sl@0
|
1496 |
m3gClearLights2(&ctx->lightManager);
|
sl@0
|
1497 |
}
|
sl@0
|
1498 |
|
sl@0
|
1499 |
/*!
|
sl@0
|
1500 |
* \brief Sets the viewport
|
sl@0
|
1501 |
*
|
sl@0
|
1502 |
*/
|
sl@0
|
1503 |
M3G_API void m3gSetViewport(M3GRenderContext hCtx,
|
sl@0
|
1504 |
M3Gint x, M3Gint y,
|
sl@0
|
1505 |
M3Gint width, M3Gint height)
|
sl@0
|
1506 |
{
|
sl@0
|
1507 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1508 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1509 |
|
sl@0
|
1510 |
/* Note that the error checking here differs from that specified
|
sl@0
|
1511 |
* for the Java API; this is to avoid complications when setting
|
sl@0
|
1512 |
* from BindTarget where the clip rectangle may be zero.
|
sl@0
|
1513 |
* Additional checks are performed in the Java glue code. */
|
sl@0
|
1514 |
|
sl@0
|
1515 |
if (width < 0 || height < 0) {
|
sl@0
|
1516 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_VALUE);
|
sl@0
|
1517 |
return;
|
sl@0
|
1518 |
}
|
sl@0
|
1519 |
|
sl@0
|
1520 |
width = M3G_MIN(width, M3G_MAX_VIEWPORT_DIMENSION);
|
sl@0
|
1521 |
height = M3G_MIN(height, M3G_MAX_VIEWPORT_DIMENSION);
|
sl@0
|
1522 |
|
sl@0
|
1523 |
ctx->viewport.x = x;
|
sl@0
|
1524 |
ctx->viewport.y = ctx->target.height - (y + height);
|
sl@0
|
1525 |
ctx->viewport.width = width;
|
sl@0
|
1526 |
ctx->viewport.height = height;
|
sl@0
|
1527 |
m3gUpdateScissor(ctx);
|
sl@0
|
1528 |
}
|
sl@0
|
1529 |
|
sl@0
|
1530 |
|
sl@0
|
1531 |
/*!
|
sl@0
|
1532 |
* \brief Gets the viewport
|
sl@0
|
1533 |
*/
|
sl@0
|
1534 |
M3G_API void m3gGetViewport(M3GRenderContext hCtx,
|
sl@0
|
1535 |
M3Gint *x, M3Gint *y,
|
sl@0
|
1536 |
M3Gint *width, M3Gint *height)
|
sl@0
|
1537 |
{
|
sl@0
|
1538 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1539 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1540 |
|
sl@0
|
1541 |
*x = ctx->viewport.x;
|
sl@0
|
1542 |
*y = ctx->target.height - (ctx->viewport.y + ctx->viewport.height);
|
sl@0
|
1543 |
*width = ctx->viewport.width;
|
sl@0
|
1544 |
*height = ctx->viewport.height;
|
sl@0
|
1545 |
}
|
sl@0
|
1546 |
|
sl@0
|
1547 |
/*!
|
sl@0
|
1548 |
* \brief Sets the scissor rectangle
|
sl@0
|
1549 |
*/
|
sl@0
|
1550 |
M3G_API void m3gSetClipRect(M3GRenderContext hCtx,
|
sl@0
|
1551 |
M3Gint x, M3Gint y,
|
sl@0
|
1552 |
M3Gint width, M3Gint height)
|
sl@0
|
1553 |
{
|
sl@0
|
1554 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1555 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1556 |
|
sl@0
|
1557 |
if (width < 0 || height < 0) {
|
sl@0
|
1558 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_VALUE);
|
sl@0
|
1559 |
return;
|
sl@0
|
1560 |
}
|
sl@0
|
1561 |
ctx->clip.x0 = x;
|
sl@0
|
1562 |
ctx->clip.y0 = ctx->target.height - (y + height);
|
sl@0
|
1563 |
ctx->clip.x1 = x + width;
|
sl@0
|
1564 |
ctx->clip.y1 = ctx->clip.y0 + height;
|
sl@0
|
1565 |
m3gValidateClipRect(ctx);
|
sl@0
|
1566 |
m3gUpdateScissor(ctx);
|
sl@0
|
1567 |
}
|
sl@0
|
1568 |
|
sl@0
|
1569 |
/*!
|
sl@0
|
1570 |
* \brief Sets the physical display area
|
sl@0
|
1571 |
*
|
sl@0
|
1572 |
* The display are is normally set to the full rendering targte size
|
sl@0
|
1573 |
* in m3gBindTarget, but this function allows overriding the default
|
sl@0
|
1574 |
* setting.
|
sl@0
|
1575 |
*
|
sl@0
|
1576 |
* Any pixels outside of the display area can be discarded for
|
sl@0
|
1577 |
* performance. The origin is assumed to be in the top-left corner of
|
sl@0
|
1578 |
* the rendering target.
|
sl@0
|
1579 |
*/
|
sl@0
|
1580 |
M3G_API void m3gSetDisplayArea(M3GRenderContext hCtx,
|
sl@0
|
1581 |
M3Gint width, M3Gint height)
|
sl@0
|
1582 |
{
|
sl@0
|
1583 |
RenderContext *ctx = (RenderContext*) hCtx;
|
sl@0
|
1584 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1585 |
|
sl@0
|
1586 |
ctx->display.width = M3G_MIN(width, ctx->target.width);
|
sl@0
|
1587 |
ctx->display.height = M3G_MIN(height, ctx->target.height);
|
sl@0
|
1588 |
}
|
sl@0
|
1589 |
|
sl@0
|
1590 |
/*!
|
sl@0
|
1591 |
* \brief Sets depth range
|
sl@0
|
1592 |
*
|
sl@0
|
1593 |
*/
|
sl@0
|
1594 |
M3G_API void m3gSetDepthRange(M3GRenderContext hCtx,
|
sl@0
|
1595 |
M3Gfloat depthNear, M3Gfloat depthFar)
|
sl@0
|
1596 |
{
|
sl@0
|
1597 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1598 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1599 |
|
sl@0
|
1600 |
if (depthNear < 0 || depthNear > 1.0f || depthFar < 0 || depthFar > 1.0f) {
|
sl@0
|
1601 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_VALUE);
|
sl@0
|
1602 |
return;
|
sl@0
|
1603 |
}
|
sl@0
|
1604 |
|
sl@0
|
1605 |
ctx->depthNear = depthNear;
|
sl@0
|
1606 |
ctx->depthFar = depthFar;
|
sl@0
|
1607 |
}
|
sl@0
|
1608 |
|
sl@0
|
1609 |
/*!
|
sl@0
|
1610 |
* \brief Gets depth range
|
sl@0
|
1611 |
*
|
sl@0
|
1612 |
*/
|
sl@0
|
1613 |
M3G_API void m3gGetDepthRange(M3GRenderContext hCtx,
|
sl@0
|
1614 |
M3Gfloat *depthNear, M3Gfloat *depthFar)
|
sl@0
|
1615 |
{
|
sl@0
|
1616 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1617 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1618 |
|
sl@0
|
1619 |
*depthNear = ctx->depthNear;
|
sl@0
|
1620 |
*depthFar= ctx->depthFar;
|
sl@0
|
1621 |
}
|
sl@0
|
1622 |
|
sl@0
|
1623 |
/*!
|
sl@0
|
1624 |
* \brief Gets current view transform
|
sl@0
|
1625 |
*
|
sl@0
|
1626 |
*/
|
sl@0
|
1627 |
|
sl@0
|
1628 |
M3G_API void m3gGetViewTransform(M3GRenderContext hCtx,
|
sl@0
|
1629 |
M3GMatrix *transform)
|
sl@0
|
1630 |
{
|
sl@0
|
1631 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1632 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1633 |
m3gSetMatrixColumns(transform, ctx->viewTransform);
|
sl@0
|
1634 |
m3gInvertMatrix(transform); /*lint !e534 always invertible */
|
sl@0
|
1635 |
}
|
sl@0
|
1636 |
|
sl@0
|
1637 |
/*!
|
sl@0
|
1638 |
* \brief Gets current Camera
|
sl@0
|
1639 |
*
|
sl@0
|
1640 |
*/
|
sl@0
|
1641 |
|
sl@0
|
1642 |
M3G_API M3GCamera m3gGetCamera(M3GRenderContext hCtx)
|
sl@0
|
1643 |
{
|
sl@0
|
1644 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1645 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1646 |
return (M3GCamera) ctx->camera;
|
sl@0
|
1647 |
}
|
sl@0
|
1648 |
|
sl@0
|
1649 |
/*!
|
sl@0
|
1650 |
* \brief Gets light transform of given light
|
sl@0
|
1651 |
*
|
sl@0
|
1652 |
*/
|
sl@0
|
1653 |
|
sl@0
|
1654 |
M3G_API M3GLight m3gGetLightTransform (M3GRenderContext hCtx,
|
sl@0
|
1655 |
M3Gint lightIndex, M3GMatrix *transform)
|
sl@0
|
1656 |
{
|
sl@0
|
1657 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1658 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1659 |
return m3gGetLightTransformInternal(&ctx->lightManager, lightIndex, transform);
|
sl@0
|
1660 |
}
|
sl@0
|
1661 |
|
sl@0
|
1662 |
/*!
|
sl@0
|
1663 |
* \brief Gets light count
|
sl@0
|
1664 |
*
|
sl@0
|
1665 |
*/
|
sl@0
|
1666 |
|
sl@0
|
1667 |
M3G_API M3Gsizei m3gGetLightCount (M3GRenderContext hCtx)
|
sl@0
|
1668 |
{
|
sl@0
|
1669 |
RenderContext *ctx = (RenderContext *)hCtx;
|
sl@0
|
1670 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1671 |
return m3gLightArraySize(&ctx->lightManager);
|
sl@0
|
1672 |
}
|
sl@0
|
1673 |
|
sl@0
|
1674 |
/*!
|
sl@0
|
1675 |
* \brief Renders a world
|
sl@0
|
1676 |
*
|
sl@0
|
1677 |
*/
|
sl@0
|
1678 |
M3G_API void m3gRenderWorld(M3GRenderContext context, M3GWorld hWorld)
|
sl@0
|
1679 |
{
|
sl@0
|
1680 |
Camera *camera;
|
sl@0
|
1681 |
RenderContext *ctx = (RenderContext*) context;
|
sl@0
|
1682 |
World *world = (World *) hWorld;
|
sl@0
|
1683 |
|
sl@0
|
1684 |
M3G_LOG1(M3G_LOG_STAGES, "Rendering World 0x%08X\n", (unsigned) world);
|
sl@0
|
1685 |
|
sl@0
|
1686 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1687 |
M3G_VALIDATE_OBJECT(world);
|
sl@0
|
1688 |
|
sl@0
|
1689 |
camera = m3gGetActiveCamera(world);
|
sl@0
|
1690 |
|
sl@0
|
1691 |
/* Check for errors */
|
sl@0
|
1692 |
|
sl@0
|
1693 |
if (ctx->target.type == SURFACE_NONE) {
|
sl@0
|
1694 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OPERATION);
|
sl@0
|
1695 |
return;
|
sl@0
|
1696 |
}
|
sl@0
|
1697 |
|
sl@0
|
1698 |
if (camera == NULL ||
|
sl@0
|
1699 |
!m3gIsChildOf((Node *)world, (Node *)camera)) {
|
sl@0
|
1700 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OPERATION);
|
sl@0
|
1701 |
return;
|
sl@0
|
1702 |
}
|
sl@0
|
1703 |
|
sl@0
|
1704 |
/* Exit if the camera will show nothing (zero view volume) */
|
sl@0
|
1705 |
|
sl@0
|
1706 |
if (!m3gValidProjection(camera)) {
|
sl@0
|
1707 |
return;
|
sl@0
|
1708 |
}
|
sl@0
|
1709 |
|
sl@0
|
1710 |
/* Override the currently set viewing transformation with identity
|
sl@0
|
1711 |
* (will fix this before we return) */
|
sl@0
|
1712 |
|
sl@0
|
1713 |
m3gSetCamera(ctx, camera, NULL);
|
sl@0
|
1714 |
|
sl@0
|
1715 |
if (m3gValidateBackground(ctx, world->background)) {
|
sl@0
|
1716 |
m3gClearInternal(ctx, world->background);
|
sl@0
|
1717 |
}
|
sl@0
|
1718 |
else {
|
sl@0
|
1719 |
m3gRaiseError(M3G_INTERFACE(world), M3G_INVALID_OPERATION);
|
sl@0
|
1720 |
return;
|
sl@0
|
1721 |
}
|
sl@0
|
1722 |
|
sl@0
|
1723 |
/* All clear for rendering */
|
sl@0
|
1724 |
|
sl@0
|
1725 |
M3G_LOG(M3G_LOG_RENDERING, "Rendering: start\n");
|
sl@0
|
1726 |
M3G_ASSERT(ctx->renderQueue->root == NULL);
|
sl@0
|
1727 |
M3G_BEGIN_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_VALIDATE);
|
sl@0
|
1728 |
|
sl@0
|
1729 |
if (m3gValidateNode((Node*) world, NODE_RENDER_BIT, camera->node.scope)) {
|
sl@0
|
1730 |
M3Gbool setup;
|
sl@0
|
1731 |
SetupRenderState s;
|
sl@0
|
1732 |
M3G_END_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_VALIDATE);
|
sl@0
|
1733 |
|
sl@0
|
1734 |
/* We start the traversal from the camera, so set the initial
|
sl@0
|
1735 |
* camera-space transformation to identity */
|
sl@0
|
1736 |
|
sl@0
|
1737 |
m3gIdentityMatrix(&s.toCamera);
|
sl@0
|
1738 |
s.cullMask = CULLMASK_ALL;
|
sl@0
|
1739 |
|
sl@0
|
1740 |
m3gClearLights2(&ctx->lightManager);
|
sl@0
|
1741 |
|
sl@0
|
1742 |
ctx->renderQueue->root = (Node *)world;
|
sl@0
|
1743 |
ctx->renderQueue->scope = camera->node.scope;
|
sl@0
|
1744 |
ctx->renderQueue->lightManager = &ctx->lightManager;
|
sl@0
|
1745 |
ctx->renderQueue->camera = camera;
|
sl@0
|
1746 |
|
sl@0
|
1747 |
M3G_BEGIN_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_SETUP);
|
sl@0
|
1748 |
|
sl@0
|
1749 |
setup = M3G_VFUNC(Node, camera, setupRender)((Node *) camera,
|
sl@0
|
1750 |
NULL,
|
sl@0
|
1751 |
&s,
|
sl@0
|
1752 |
ctx->renderQueue);
|
sl@0
|
1753 |
M3G_END_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_SETUP);
|
sl@0
|
1754 |
M3G_LOG(M3G_LOG_RENDERING, "Rendering: commit\n");
|
sl@0
|
1755 |
M3G_BEGIN_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_COMMIT);
|
sl@0
|
1756 |
|
sl@0
|
1757 |
if (setup) {
|
sl@0
|
1758 |
m3gInitRender(ctx, RENDER_WORLD);
|
sl@0
|
1759 |
m3gLockFrameBuffer(ctx);
|
sl@0
|
1760 |
m3gCommit(ctx->renderQueue, ctx);
|
sl@0
|
1761 |
m3gReleaseFrameBuffer(ctx);
|
sl@0
|
1762 |
}
|
sl@0
|
1763 |
|
sl@0
|
1764 |
M3G_END_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_COMMIT);
|
sl@0
|
1765 |
|
sl@0
|
1766 |
/* Fix light and camera transformations to be relative to world
|
sl@0
|
1767 |
* space on exit */
|
sl@0
|
1768 |
|
sl@0
|
1769 |
if (setup) {
|
sl@0
|
1770 |
Matrix m;
|
sl@0
|
1771 |
if (m3gGetTransformTo((Node*) world, (Node*) camera, &m)) {
|
sl@0
|
1772 |
m3gGetMatrixColumns(&m, ctx->viewTransform);
|
sl@0
|
1773 |
if (m3gInvertMatrix(&m)) {
|
sl@0
|
1774 |
m3gTransformLights(&ctx->lightManager, &m);
|
sl@0
|
1775 |
}
|
sl@0
|
1776 |
else {
|
sl@0
|
1777 |
M3G_ASSERT(M3G_FALSE);
|
sl@0
|
1778 |
}
|
sl@0
|
1779 |
}
|
sl@0
|
1780 |
else {
|
sl@0
|
1781 |
M3G_ASSERT(M3G_FALSE);
|
sl@0
|
1782 |
}
|
sl@0
|
1783 |
}
|
sl@0
|
1784 |
}
|
sl@0
|
1785 |
|
sl@0
|
1786 |
m3gClearRenderQueue(ctx->renderQueue);
|
sl@0
|
1787 |
M3G_LOG(M3G_LOG_RENDERING, "Rendering: end\n");
|
sl@0
|
1788 |
}
|
sl@0
|
1789 |
|
sl@0
|
1790 |
/*!
|
sl@0
|
1791 |
* \brief Renders a node or subtree
|
sl@0
|
1792 |
*/
|
sl@0
|
1793 |
M3G_API void m3gRenderNode(M3GRenderContext context,
|
sl@0
|
1794 |
M3GNode hNode,
|
sl@0
|
1795 |
const M3GMatrix *transform)
|
sl@0
|
1796 |
{
|
sl@0
|
1797 |
RenderContext *ctx = (RenderContext*) context;
|
sl@0
|
1798 |
Node *node = (Node *) hNode;
|
sl@0
|
1799 |
|
sl@0
|
1800 |
M3G_LOG1(M3G_LOG_STAGES, "Rendering Node 0x%08X\n", (unsigned) node);
|
sl@0
|
1801 |
|
sl@0
|
1802 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1803 |
M3G_VALIDATE_OBJECT(node);
|
sl@0
|
1804 |
|
sl@0
|
1805 |
/* Check for errors */
|
sl@0
|
1806 |
|
sl@0
|
1807 |
if (node == NULL) {
|
sl@0
|
1808 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_NULL_POINTER);
|
sl@0
|
1809 |
return;
|
sl@0
|
1810 |
}
|
sl@0
|
1811 |
|
sl@0
|
1812 |
if (ctx->target.type == SURFACE_NONE || ctx->camera == NULL) {
|
sl@0
|
1813 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OPERATION);
|
sl@0
|
1814 |
return;
|
sl@0
|
1815 |
}
|
sl@0
|
1816 |
|
sl@0
|
1817 |
/* Exit if the camera will show nothing (zero view volume) */
|
sl@0
|
1818 |
|
sl@0
|
1819 |
if (!m3gValidProjection(ctx->camera)) {
|
sl@0
|
1820 |
return;
|
sl@0
|
1821 |
}
|
sl@0
|
1822 |
|
sl@0
|
1823 |
/* All clear, draw away */
|
sl@0
|
1824 |
|
sl@0
|
1825 |
M3G_LOG(M3G_LOG_RENDERING, "Rendering: start\n");
|
sl@0
|
1826 |
M3G_ASSERT(ctx->renderQueue->root == NULL);
|
sl@0
|
1827 |
M3G_BEGIN_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_VALIDATE);
|
sl@0
|
1828 |
|
sl@0
|
1829 |
if (m3gValidateNode(node, NODE_RENDER_BIT, ctx->camera->node.scope)) {
|
sl@0
|
1830 |
M3Gbool setup;
|
sl@0
|
1831 |
SetupRenderState s;
|
sl@0
|
1832 |
M3G_END_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_VALIDATE);
|
sl@0
|
1833 |
|
sl@0
|
1834 |
s.cullMask = CULLMASK_ALL;
|
sl@0
|
1835 |
|
sl@0
|
1836 |
/* We start the traversal from world space, so preload the
|
sl@0
|
1837 |
* current camera-space transformation to get camera-space
|
sl@0
|
1838 |
* meshes and correct view frustum culling */
|
sl@0
|
1839 |
|
sl@0
|
1840 |
m3gSetMatrixColumns(&s.toCamera, ctx->viewTransform);
|
sl@0
|
1841 |
if (transform) {
|
sl@0
|
1842 |
m3gMulMatrix(&s.toCamera, transform);
|
sl@0
|
1843 |
}
|
sl@0
|
1844 |
ctx->renderQueue->root = (Node *) node;
|
sl@0
|
1845 |
ctx->renderQueue->scope = ctx->camera->node.scope;
|
sl@0
|
1846 |
ctx->renderQueue->lightManager = NULL;
|
sl@0
|
1847 |
ctx->renderQueue->camera = ctx->camera;
|
sl@0
|
1848 |
|
sl@0
|
1849 |
M3G_BEGIN_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_SETUP);
|
sl@0
|
1850 |
|
sl@0
|
1851 |
setup = M3G_VFUNC(Node, node, setupRender)(node,
|
sl@0
|
1852 |
NULL,
|
sl@0
|
1853 |
&s,
|
sl@0
|
1854 |
ctx->renderQueue);
|
sl@0
|
1855 |
M3G_END_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_SETUP);
|
sl@0
|
1856 |
M3G_LOG(M3G_LOG_RENDERING, "Rendering: commit\n");
|
sl@0
|
1857 |
M3G_BEGIN_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_COMMIT);
|
sl@0
|
1858 |
|
sl@0
|
1859 |
if (setup) {
|
sl@0
|
1860 |
m3gInitRender(ctx, RENDER_NODES);
|
sl@0
|
1861 |
m3gLockFrameBuffer(ctx);
|
sl@0
|
1862 |
m3gCommit(ctx->renderQueue, ctx);
|
sl@0
|
1863 |
m3gReleaseFrameBuffer(ctx);
|
sl@0
|
1864 |
}
|
sl@0
|
1865 |
|
sl@0
|
1866 |
M3G_END_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_COMMIT);
|
sl@0
|
1867 |
}
|
sl@0
|
1868 |
|
sl@0
|
1869 |
m3gClearRenderQueue(ctx->renderQueue);
|
sl@0
|
1870 |
|
sl@0
|
1871 |
M3G_LOG(M3G_LOG_RENDERING, "Rendering: end\n");
|
sl@0
|
1872 |
}
|
sl@0
|
1873 |
|
sl@0
|
1874 |
/*!
|
sl@0
|
1875 |
* \brief Render a set of primitives
|
sl@0
|
1876 |
*
|
sl@0
|
1877 |
*/
|
sl@0
|
1878 |
M3G_API void m3gRender(M3GRenderContext context,
|
sl@0
|
1879 |
M3GVertexBuffer hVertices,
|
sl@0
|
1880 |
M3GIndexBuffer hIndices,
|
sl@0
|
1881 |
M3GAppearance hAppearance,
|
sl@0
|
1882 |
const M3GMatrix *transformMatrix,
|
sl@0
|
1883 |
M3Gfloat alphaFactor,
|
sl@0
|
1884 |
M3Gint scope)
|
sl@0
|
1885 |
{
|
sl@0
|
1886 |
RenderContext *ctx = (RenderContext *) context;
|
sl@0
|
1887 |
const VertexBuffer *vb = (const VertexBuffer *) hVertices;
|
sl@0
|
1888 |
const IndexBuffer *ib = (const IndexBuffer *) hIndices;
|
sl@0
|
1889 |
const Appearance *app = (const Appearance *) hAppearance;
|
sl@0
|
1890 |
M3G_VALIDATE_OBJECT(ctx);
|
sl@0
|
1891 |
|
sl@0
|
1892 |
M3G_LOG1(M3G_LOG_STAGES, "Rendering vertex buffer 0x%08X\n",
|
sl@0
|
1893 |
(unsigned) vb);
|
sl@0
|
1894 |
|
sl@0
|
1895 |
/* Check validity of input */
|
sl@0
|
1896 |
|
sl@0
|
1897 |
if (ctx->target.type == SURFACE_NONE || ctx->camera == NULL) {
|
sl@0
|
1898 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OPERATION);
|
sl@0
|
1899 |
return;
|
sl@0
|
1900 |
}
|
sl@0
|
1901 |
|
sl@0
|
1902 |
/* Quick exit if out of scope or zero view volume */
|
sl@0
|
1903 |
|
sl@0
|
1904 |
if ((scope & ctx->camera->node.scope) == 0
|
sl@0
|
1905 |
|| !m3gValidProjection(ctx->camera)) {
|
sl@0
|
1906 |
return;
|
sl@0
|
1907 |
}
|
sl@0
|
1908 |
|
sl@0
|
1909 |
if (vb == NULL || ib == NULL || app == NULL) {
|
sl@0
|
1910 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OBJECT);
|
sl@0
|
1911 |
return;
|
sl@0
|
1912 |
}
|
sl@0
|
1913 |
|
sl@0
|
1914 |
if (!m3gValidateVertexBuffer(vb, app, m3gGetMaxIndex(ib))) {
|
sl@0
|
1915 |
m3gRaiseError(M3G_INTERFACE(ctx), M3G_INVALID_OPERATION);
|
sl@0
|
1916 |
return;
|
sl@0
|
1917 |
}
|
sl@0
|
1918 |
|
sl@0
|
1919 |
/* Everything checks out, so draw */
|
sl@0
|
1920 |
|
sl@0
|
1921 |
M3G_LOG(M3G_LOG_RENDERING, "Rendering: start immediate\n");
|
sl@0
|
1922 |
|
sl@0
|
1923 |
m3gInitRender(ctx, RENDER_IMMEDIATE);
|
sl@0
|
1924 |
m3gLockFrameBuffer(ctx);
|
sl@0
|
1925 |
m3gDrawMesh(ctx,
|
sl@0
|
1926 |
vb, ib, app,
|
sl@0
|
1927 |
transformMatrix,
|
sl@0
|
1928 |
m3gRoundToInt(
|
sl@0
|
1929 |
m3gMul(alphaFactor,
|
sl@0
|
1930 |
(M3Gfloat)(1 << NODE_ALPHA_FACTOR_BITS))),
|
sl@0
|
1931 |
scope);
|
sl@0
|
1932 |
m3gReleaseFrameBuffer(ctx);
|
sl@0
|
1933 |
|
sl@0
|
1934 |
M3G_LOG(M3G_LOG_RENDERING, "Rendering: end immediate\n");
|
sl@0
|
1935 |
}
|
sl@0
|
1936 |
|