ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

init.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 /*
00024  * This file is based on, or a modified version of code from TinyGL (C) 1997-1998 Fabrice Bellard,
00025  * which is licensed under the zlib-license (see LICENSE).
00026  * It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
00027  */
00028 
00029 #include "graphics/tinygl/zgl.h"
00030 #include "graphics/tinygl/zblit.h"
00031 #include "graphics/tinygl/zdirtyrect.h"
00032 
00033 namespace TinyGL {
00034 
00035 GLContext *gl_ctx;
00036 
00037 void initSharedState(GLContext *c) {
00038     GLSharedState *s = &c->shared_state;
00039     s->lists = (GLList **)gl_zalloc(sizeof(GLList *) * MAX_DISPLAY_LISTS);
00040     s->texture_hash_table = (GLTexture **)gl_zalloc(sizeof(GLTexture *) * TEXTURE_HASH_TABLE_SIZE);
00041 
00042     alloc_texture(c, 0);
00043 }
00044 
00045 void endSharedState(GLContext *c) {
00046     GLSharedState *s = &c->shared_state;
00047 
00048     free_texture(c, 0);
00049     for (int i = 0; i < MAX_DISPLAY_LISTS; i++) {
00050         // TODO
00051     }
00052     gl_free(s->lists);
00053 
00054     gl_free(s->texture_hash_table);
00055 }
00056 
00057 void glInit(void *zbuffer1, int textureSize) {
00058     FrameBuffer *zbuffer = (FrameBuffer *)zbuffer1;
00059     GLContext *c;
00060     GLViewport *v;
00061 
00062     if ((textureSize & (textureSize - 1)))
00063         error("glInit: texture size not power of two: %d", textureSize);
00064 
00065     if (textureSize <= 1 || textureSize > 4096)
00066         error("glInit: texture size not allowed: %d", textureSize);
00067 
00068     c = new GLContext();
00069     gl_ctx = c;
00070 
00071     c->fb = zbuffer;
00072 
00073     c->fb->_textureSize = c->_textureSize = textureSize;
00074     c->fb->_textureSizeMask = (textureSize - 1) << ZB_POINT_ST_FRAC_BITS;
00075     c->renderRect = Common::Rect(0, 0, zbuffer->xsize, zbuffer->ysize);
00076 
00077     // allocate GLVertex array
00078     c->vertex_max = POLYGON_MAX_VERTEX;
00079     c->vertex = (GLVertex *)gl_malloc(POLYGON_MAX_VERTEX * sizeof(GLVertex));
00080 
00081     // viewport
00082     v = &c->viewport;
00083     v->xmin = 0;
00084     v->ymin = 0;
00085     v->xsize = zbuffer->xsize;
00086     v->ysize = zbuffer->ysize;
00087     v->updated = 1;
00088 
00089     // shared state
00090     initSharedState(c);
00091 
00092     // lists
00093 
00094     c->exec_flag = 1;
00095     c->compile_flag = 0;
00096     c->print_flag = 0;
00097 
00098     c->in_begin = 0;
00099 
00100     // lights
00101     for (int i = 0; i < T_MAX_LIGHTS; i++) {
00102         GLLight *l = &c->lights[i];
00103         l->ambient = Vector4(0, 0, 0, 1);
00104         if (i == 0) {
00105             l->diffuse = Vector4(1, 1, 1, 1);
00106             l->specular = Vector4(1, 1, 1, 1);
00107             l->has_specular = true;
00108         } else {
00109             l->diffuse = Vector4(0, 0, 0, 1);
00110             l->specular = Vector4(0, 0, 0, 1);
00111             l->has_specular = false;
00112         }
00113         l->position = Vector4(0, 0, 1, 0);
00114         l->spot_direction = Vector3(0, 0, -1);
00115         l->spot_exponent = 0;
00116         l->spot_cutoff = 180;
00117         l->attenuation[0] = 1;
00118         l->attenuation[1] = 0;
00119         l->attenuation[2] = 0;
00120         l->cos_spot_cutoff = -1.0f;
00121         l->norm_spot_direction = Vector3(0, 0, -1);
00122         l->norm_position = Vector3(0, 0, 1);
00123         l->enabled = 0;
00124         l->next = NULL;
00125         l->prev = NULL;
00126     }
00127     c->first_light = NULL;
00128     c->ambient_light_model = Vector4(0.2f, 0.2f, 0.2f, 1);
00129     c->local_light_model = 0;
00130     c->lighting_enabled = 0;
00131     c->light_model_two_side = 0;
00132 
00133     // default materials */
00134     for (int i = 0; i < 2; i++) {
00135         GLMaterial *m = &c->materials[i];
00136         m->emission = Vector4(0, 0, 0, 1);
00137         m->ambient = Vector4(0.2f, 0.2f, 0.2f, 1);
00138         m->diffuse = Vector4(0.8f, 0.8f, 0.8f, 1);
00139         m->specular = Vector4(0, 0, 0, 1);
00140         m->has_specular = false;
00141         m->shininess = 0;
00142     }
00143     c->current_color_material_mode = TGL_FRONT_AND_BACK;
00144     c->current_color_material_type = TGL_AMBIENT_AND_DIFFUSE;
00145     c->color_material_enabled = 0;
00146 
00147     // textures
00148     glInitTextures(c);
00149 
00150     // default state
00151     c->current_color = Vector4(1.0f, 1.0f, 1.0f, 1.0f);
00152 
00153     c->current_normal = Vector4(1.0f, 0.0f, 0.0f, 0.0f);
00154 
00155     c->current_edge_flag = 1;
00156 
00157     c->current_tex_coord = Vector4(0.0f, 0.0f, 0.0f, 1.0f);
00158 
00159     c->polygon_mode_front = TGL_FILL;
00160     c->polygon_mode_back = TGL_FILL;
00161 
00162     c->current_front_face = 0; // 0 = GL_CCW  1 = GL_CW
00163     c->current_cull_face = TGL_BACK;
00164     c->current_shade_model = TGL_SMOOTH;
00165     c->cull_face_enabled = 0;
00166 
00167     // clear
00168     c->clear_color = Vector4(0.0f, 0.0f, 0.0f, 0.0f);
00169     c->clear_depth = 1.0f;
00170 
00171     // selection
00172     c->render_mode = TGL_RENDER;
00173     c->select_buffer = NULL;
00174     c->name_stack_size = 0;
00175 
00176     // blending
00177     c->fb->enableBlending(false);
00178 
00179     // alpha test
00180     c->fb->enableAlphaTest(false);
00181 
00182     // matrix
00183     c->matrix_mode = 0;
00184 
00185     c->matrix_stack_depth_max[0] = MAX_MODELVIEW_STACK_DEPTH;
00186     c->matrix_stack_depth_max[1] = MAX_PROJECTION_STACK_DEPTH;
00187     c->matrix_stack_depth_max[2] = MAX_TEXTURE_STACK_DEPTH;
00188 
00189     for (int i = 0; i < 3; i++) {
00190         c->matrix_stack[i] = (Matrix4 *)gl_zalloc(c->matrix_stack_depth_max[i] * sizeof(Matrix4));
00191         c->matrix_stack_ptr[i] = c->matrix_stack[i];
00192     }
00193 
00194     tglMatrixMode(TGL_PROJECTION);
00195     tglLoadIdentity();
00196     tglMatrixMode(TGL_TEXTURE);
00197     tglLoadIdentity();
00198     tglMatrixMode(TGL_MODELVIEW);
00199     tglLoadIdentity();
00200 
00201     tglBlendFunc(TGL_SRC_ALPHA, TGL_ONE_MINUS_SRC_ALPHA);
00202 
00203     tglAlphaFunc(TGL_ALWAYS, 0.f);
00204 
00205     tglDepthFunc(TGL_LESS);
00206 
00207     c->matrix_model_projection_updated = 1;
00208 
00209     // opengl 1.1 arrays
00210     c->client_states = 0;
00211 
00212     // opengl 1.1 polygon offset
00213     c->offset_states = 0;
00214 
00215     // shadow mode
00216     c->shadow_mode = 0;
00217 
00218     // clear the resize callback function pointer
00219     c->gl_resize_viewport = NULL;
00220 
00221     // specular buffer
00222     c->specbuf_first = NULL;
00223     c->specbuf_used_counter = 0;
00224     c->specbuf_num_buffers = 0;
00225 
00226     // depth test
00227     c->depth_test = 0;
00228 
00229     c->color_mask = (1 << 24) | (1 << 16) | (1 << 8) | (1 << 0);
00230 
00231     const int kDrawCallMemory = 5 * 1024 * 1024;
00232 
00233     c->_currentAllocatorIndex = 0;
00234     c->_drawCallAllocator[0].initialize(kDrawCallMemory);
00235     c->_drawCallAllocator[1].initialize(kDrawCallMemory);
00236     c->_enableDirtyRectangles = true;
00237 
00238     Graphics::Internal::tglBlitResetScissorRect();
00239 }
00240 
00241 void glClose() {
00242     GLContext *c = gl_get_context();
00243 
00244     tglDisposeDrawCallLists(c);
00245     tglDisposeResources(c);
00246 
00247     specbuf_cleanup(c);
00248     for (int i = 0; i < 3; i++)
00249         gl_free(c->matrix_stack[i]);
00250     endSharedState(c);
00251     gl_free(c->vertex);
00252 
00253     delete c;
00254 }
00255 
00256 } // end of namespace TinyGL


Generated on Sat May 18 2019 05:01:05 for ResidualVM by doxygen 1.7.1
curved edge   curved edge