2014-08-13 18:52:52 +02:00
|
|
|
/* ResidualVM - A 3D game interpreter
|
|
|
|
*
|
|
|
|
* ResidualVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the AUTHORS
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is based on, or a modified version of code from TinyGL (C) 1997-1998 Fabrice Bellard,
|
|
|
|
* which is licensed under the zlib-license (see LICENSE).
|
|
|
|
* It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
|
|
|
|
*/
|
2006-05-16 14:52:36 +00:00
|
|
|
|
2009-05-08 07:32:33 +00:00
|
|
|
#include "graphics/tinygl/zgl.h"
|
2014-08-05 20:00:13 +02:00
|
|
|
#include "graphics/tinygl/zblit.h"
|
2005-01-12 15:20:02 +00:00
|
|
|
|
2009-05-25 13:19:29 +00:00
|
|
|
namespace TinyGL {
|
|
|
|
|
2005-01-12 15:20:02 +00:00
|
|
|
GLContext *gl_ctx;
|
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
void initSharedState(GLContext *c) {
|
|
|
|
GLSharedState *s = &c->shared_state;
|
|
|
|
s->lists = (GLList **)gl_zalloc(sizeof(GLList *) * MAX_DISPLAY_LISTS);
|
|
|
|
s->texture_hash_table = (GLTexture **)gl_zalloc(sizeof(GLTexture *) * TEXTURE_HASH_TABLE_SIZE);
|
2005-01-12 15:20:02 +00:00
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
alloc_texture(c, 0);
|
2005-01-12 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
void endSharedState(GLContext *c) {
|
|
|
|
GLSharedState *s = &c->shared_state;
|
2005-01-12 15:20:02 +00:00
|
|
|
|
2012-04-21 12:18:42 +02:00
|
|
|
free_texture(c, 0);
|
2014-02-20 18:46:49 +01:00
|
|
|
for (int i = 0; i < MAX_DISPLAY_LISTS; i++) {
|
2006-05-16 14:52:36 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
gl_free(s->lists);
|
2005-01-12 15:20:02 +00:00
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
gl_free(s->texture_hash_table);
|
2005-01-12 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 13:08:34 +02:00
|
|
|
void glInit(void *zbuffer1, int textureSize) {
|
2014-06-13 14:58:07 +02:00
|
|
|
FrameBuffer *zbuffer = (FrameBuffer *)zbuffer1;
|
2006-05-16 14:52:36 +00:00
|
|
|
GLContext *c;
|
|
|
|
GLViewport *v;
|
2005-01-12 15:20:02 +00:00
|
|
|
|
2014-07-05 13:08:34 +02:00
|
|
|
if ((textureSize & (textureSize - 1)))
|
|
|
|
error("glInit: texture size not power of two: %d", textureSize);
|
|
|
|
|
|
|
|
if (textureSize <= 1 || textureSize > 4096)
|
|
|
|
error("glInit: texture size not allowed: %d", textureSize);
|
|
|
|
|
2014-07-26 17:56:44 +02:00
|
|
|
c = new GLContext();
|
2006-05-16 14:52:36 +00:00
|
|
|
gl_ctx = c;
|
2005-01-12 15:20:02 +00:00
|
|
|
|
2014-06-13 16:30:25 +02:00
|
|
|
c->fb = zbuffer;
|
2005-01-12 15:20:02 +00:00
|
|
|
|
2014-07-05 13:08:34 +02:00
|
|
|
c->fb->_textureSize = c->_textureSize = textureSize;
|
|
|
|
c->fb->_textureSizeMask = (textureSize - 1) << ZB_POINT_ST_FRAC_BITS;
|
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
// allocate GLVertex array
|
|
|
|
c->vertex_max = POLYGON_MAX_VERTEX;
|
|
|
|
c->vertex = (GLVertex *)gl_malloc(POLYGON_MAX_VERTEX * sizeof(GLVertex));
|
2014-02-20 18:46:49 +01:00
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
// viewport
|
|
|
|
v = &c->viewport;
|
|
|
|
v->xmin = 0;
|
|
|
|
v->ymin = 0;
|
|
|
|
v->xsize = zbuffer->xsize;
|
|
|
|
v->ysize = zbuffer->ysize;
|
|
|
|
v->updated = 1;
|
|
|
|
|
|
|
|
// shared state
|
|
|
|
initSharedState(c);
|
|
|
|
|
|
|
|
// lists
|
|
|
|
|
|
|
|
c->exec_flag = 1;
|
|
|
|
c->compile_flag = 0;
|
|
|
|
c->print_flag = 0;
|
|
|
|
|
2014-02-20 18:46:49 +01:00
|
|
|
c->in_begin = 0;
|
2006-05-16 14:52:36 +00:00
|
|
|
|
|
|
|
// lights
|
2014-02-20 17:56:55 +01:00
|
|
|
for (int i = 0; i < T_MAX_LIGHTS; i++) {
|
2006-05-16 14:52:36 +00:00
|
|
|
GLLight *l = &c->lights[i];
|
2014-05-28 19:27:00 +01:00
|
|
|
l->ambient = Vector4(0, 0, 0, 1);
|
2015-03-15 22:14:39 +01:00
|
|
|
if (i == 0) {
|
|
|
|
l->diffuse = Vector4(1, 1, 1, 1);
|
|
|
|
l->specular = Vector4(1, 1, 1, 1);
|
|
|
|
} else {
|
|
|
|
l->diffuse = Vector4(0, 0, 0, 0);
|
|
|
|
l->specular = Vector4(0, 0, 0, 0);
|
|
|
|
}
|
2014-05-28 19:27:00 +01:00
|
|
|
l->position = Vector4(0, 0, 1, 0);
|
|
|
|
l->norm_position = Vector3(0, 0, 1);
|
|
|
|
l->spot_direction = Vector3(0, 0, -1);
|
2014-06-10 17:14:54 +02:00
|
|
|
l->norm_spot_direction = Vector3(0, 0, -1);
|
2006-05-16 14:52:36 +00:00
|
|
|
l->spot_exponent = 0;
|
|
|
|
l->spot_cutoff = 180;
|
|
|
|
l->attenuation[0] = 1;
|
|
|
|
l->attenuation[1] = 0;
|
|
|
|
l->attenuation[2] = 0;
|
|
|
|
l->enabled = 0;
|
|
|
|
}
|
2014-02-20 18:46:49 +01:00
|
|
|
c->first_light = NULL;
|
2014-05-28 19:27:00 +01:00
|
|
|
c->ambient_light_model = Vector4(0.2f, 0.2f, 0.2f, 1);
|
2014-02-20 18:46:49 +01:00
|
|
|
c->local_light_model = 0;
|
|
|
|
c->lighting_enabled = 0;
|
2006-05-16 14:52:36 +00:00
|
|
|
c->light_model_two_side = 0;
|
|
|
|
|
|
|
|
// default materials */
|
2014-02-20 17:56:55 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2006-05-16 14:52:36 +00:00
|
|
|
GLMaterial *m = &c->materials[i];
|
2014-05-28 19:27:00 +01:00
|
|
|
m->emission = Vector4(0, 0, 0, 1);
|
|
|
|
m->ambient = Vector4(0.2f, 0.2f, 0.2f, 1);
|
|
|
|
m->diffuse = Vector4(0.8f, 0.8f, 0.8f, 1);
|
|
|
|
m->specular = Vector4(0, 0, 0, 1);
|
2006-05-16 14:52:36 +00:00
|
|
|
m->shininess = 0;
|
|
|
|
}
|
|
|
|
c->current_color_material_mode = TGL_FRONT_AND_BACK;
|
|
|
|
c->current_color_material_type = TGL_AMBIENT_AND_DIFFUSE;
|
|
|
|
c->color_material_enabled = 0;
|
|
|
|
|
|
|
|
// textures
|
|
|
|
glInitTextures(c);
|
|
|
|
|
|
|
|
// default state
|
2014-06-04 20:33:47 +02:00
|
|
|
c->current_color = Vector4(1.0f, 1.0f, 1.0f, 1.0f);
|
2006-05-16 14:52:36 +00:00
|
|
|
c->longcurrent_color[0] = 65535;
|
|
|
|
c->longcurrent_color[1] = 65535;
|
|
|
|
c->longcurrent_color[2] = 65535;
|
2014-06-19 17:30:28 +02:00
|
|
|
c->longcurrent_color[3] = 65535;
|
2006-05-16 14:52:36 +00:00
|
|
|
|
2014-06-04 20:33:47 +02:00
|
|
|
c->current_normal = Vector4(1.0f, 0.0f, 0.0f, 0.0f);
|
2006-05-16 14:52:36 +00:00
|
|
|
|
|
|
|
c->current_edge_flag = 1;
|
|
|
|
|
2014-06-04 20:33:47 +02:00
|
|
|
c->current_tex_coord = Vector4(0.0f, 0.0f, 0.0f, 1.0f);
|
2006-05-16 14:52:36 +00:00
|
|
|
|
|
|
|
c->polygon_mode_front = TGL_FILL;
|
|
|
|
c->polygon_mode_back = TGL_FILL;
|
|
|
|
|
|
|
|
c->current_front_face = 0; // 0 = GL_CCW 1 = GL_CW
|
|
|
|
c->current_cull_face = TGL_BACK;
|
|
|
|
c->current_shade_model = TGL_SMOOTH;
|
|
|
|
c->cull_face_enabled = 0;
|
2014-02-20 18:46:49 +01:00
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
// clear
|
2014-06-04 20:33:47 +02:00
|
|
|
c->clear_color = Vector4(0.0f, 0.0f, 0.0f, 0.0f);
|
2006-05-16 14:52:36 +00:00
|
|
|
c->clear_depth = 0;
|
|
|
|
|
|
|
|
// selection
|
|
|
|
c->render_mode = TGL_RENDER;
|
|
|
|
c->select_buffer = NULL;
|
|
|
|
c->name_stack_size = 0;
|
2014-07-04 08:43:19 +02:00
|
|
|
|
|
|
|
// blending
|
2014-07-25 15:23:50 +02:00
|
|
|
c->fb->enableBlending(false);
|
2014-07-04 08:43:19 +02:00
|
|
|
|
|
|
|
// alpha test
|
2014-07-25 15:23:50 +02:00
|
|
|
c->fb->enableAlphaTest(false);
|
2014-07-04 08:43:19 +02:00
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
// matrix
|
|
|
|
c->matrix_mode = 0;
|
2014-02-20 18:46:49 +01:00
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
c->matrix_stack_depth_max[0] = MAX_MODELVIEW_STACK_DEPTH;
|
|
|
|
c->matrix_stack_depth_max[1] = MAX_PROJECTION_STACK_DEPTH;
|
|
|
|
c->matrix_stack_depth_max[2] = MAX_TEXTURE_STACK_DEPTH;
|
|
|
|
|
2014-02-20 17:56:55 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2014-06-08 19:54:58 +02:00
|
|
|
c->matrix_stack[i] = (Matrix4 *)gl_zalloc(c->matrix_stack_depth_max[i] * sizeof(Matrix4));
|
2006-05-16 14:52:36 +00:00
|
|
|
c->matrix_stack_ptr[i] = c->matrix_stack[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
tglMatrixMode(TGL_PROJECTION);
|
|
|
|
tglLoadIdentity();
|
|
|
|
tglMatrixMode(TGL_TEXTURE);
|
|
|
|
tglLoadIdentity();
|
|
|
|
tglMatrixMode(TGL_MODELVIEW);
|
|
|
|
tglLoadIdentity();
|
2014-07-04 08:43:19 +02:00
|
|
|
|
2014-06-20 14:13:52 +02:00
|
|
|
tglBlendFunc(TGL_SRC_ALPHA, TGL_ONE_MINUS_SRC_ALPHA);
|
2006-05-16 14:52:36 +00:00
|
|
|
|
2014-07-04 08:43:19 +02:00
|
|
|
tglAlphaFunc(TGL_ALWAYS, 0.f);
|
|
|
|
|
2014-07-04 23:14:44 +02:00
|
|
|
tglDepthFunc(TGL_LESS);
|
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
c->matrix_model_projection_updated = 1;
|
|
|
|
|
|
|
|
// opengl 1.1 arrays
|
|
|
|
c->client_states = 0;
|
|
|
|
|
|
|
|
// opengl 1.1 polygon offset
|
|
|
|
c->offset_states = 0;
|
|
|
|
|
2008-07-05 08:48:39 +00:00
|
|
|
// shadow mode
|
|
|
|
c->shadow_mode = 0;
|
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
// clear the resize callback function pointer
|
|
|
|
c->gl_resize_viewport = NULL;
|
|
|
|
|
|
|
|
// specular buffer
|
|
|
|
c->specbuf_first = NULL;
|
|
|
|
c->specbuf_used_counter = 0;
|
|
|
|
c->specbuf_num_buffers = 0;
|
2005-01-12 15:20:02 +00:00
|
|
|
|
2006-05-16 14:52:36 +00:00
|
|
|
// depth test
|
|
|
|
c->depth_test = 0;
|
2013-07-02 00:11:26 +02:00
|
|
|
|
|
|
|
c->color_mask = (1 << 24) | (1 << 16) | (1 << 8) | (1 << 0);
|
2014-08-05 20:00:13 +02:00
|
|
|
|
2014-08-07 21:47:54 +02:00
|
|
|
const int kDrawCallMemory = 5 * 1024 * 1024;
|
|
|
|
|
2014-08-06 21:50:49 +02:00
|
|
|
c->_currentAllocatorIndex = 0;
|
2014-08-07 21:47:54 +02:00
|
|
|
c->_drawCallAllocator[0].initialize(kDrawCallMemory);
|
|
|
|
c->_drawCallAllocator[1].initialize(kDrawCallMemory);
|
2014-08-15 23:53:57 +02:00
|
|
|
c->_enableDirtyRectangles = false;
|
2014-08-06 21:50:49 +02:00
|
|
|
|
2014-08-10 19:38:06 +02:00
|
|
|
Graphics::Internal::tglBlitSetScissorRect(0, 0, c->fb->xsize, c->fb->ysize);
|
2005-01-12 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
2009-05-25 13:19:29 +00:00
|
|
|
void glClose() {
|
2006-05-16 14:52:36 +00:00
|
|
|
GLContext *c = gl_get_context();
|
2012-04-21 12:18:42 +02:00
|
|
|
|
|
|
|
specbuf_cleanup(c);
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
gl_free(c->matrix_stack[i]);
|
2006-05-16 14:52:36 +00:00
|
|
|
endSharedState(c);
|
2012-04-21 12:18:42 +02:00
|
|
|
gl_free(c->vertex);
|
|
|
|
|
2014-12-27 23:27:27 +01:00
|
|
|
delete c;
|
2005-01-12 15:20:02 +00:00
|
|
|
}
|
2009-05-25 13:19:29 +00:00
|
|
|
|
|
|
|
} // end of namespace TinyGL
|