void TinyGLRenderer::setupCameraPerspective(float pitch, float heading, float fov) { // TODO: Find a correct and exact formula for the FOV TGLfloat glFOV = 0.63 * fov; // Approximative and experimental formula if (fov > 79.0 && fov < 81.0) glFOV = 50.5; // Somewhat good value for fov == 80 else if (fov > 59.0 && fov < 61.0) glFOV = 36.0; // Somewhat good value for fov == 60 // NOTE: tinyGL viewport implementation needs to be checked as it doesn't behave the same as openGL tglViewport(0, kTopBorderHeight, kOriginalWidth, kFrameHeight); tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); Math::Matrix4 m = Math::makePerspectiveMatrix(glFOV, (TGLfloat)kOriginalWidth / (TGLfloat)kFrameHeight, 1.0, 10000.0); tglMultMatrixf(m.getData()); // Rotate the model to simulate the rotation of the camera tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); tglRotatef(pitch, -1.0f, 0.0f, 0.0f); tglRotatef(heading - 180.0f, 0.0f, 1.0f, 0.0f); tglGetFloatv(TGL_MODELVIEW_MATRIX, _cubeModelViewMatrix); tglGetFloatv(TGL_PROJECTION_MATRIX, _cubeProjectionMatrix); tglGetIntegerv(TGL_VIEWPORT, (TGLint *)_cubeViewport); }
void GfxTinyGL::drawHierachyNode(const ModelNode *node, int *x1, int *y1, int *x2, int *y2) { Graphics::Vector3d animPos = node->_pos + node->_animPos; float animPitch = node->_pitch + node->_animPitch; float animYaw = node->_yaw + node->_animYaw; float animRoll = node->_roll + node->_animRoll; translateViewpointStart(animPos, animPitch, animYaw, animRoll); if (node->_hierVisible) { tglPushMatrix(); tglTranslatef(node->_pivot.x(), node->_pivot.y(), node->_pivot.z()); if (!_currentShadowArray) { Sprite* sprite = node->_sprite; while (sprite) { sprite->draw(); sprite = sprite->_next; } } if (node->_mesh && node->_meshVisible) { node->_mesh->draw(x1, y1, x2, y2); } tglMatrixMode(TGL_MODELVIEW); tglPopMatrix(); if (node->_child) { node->_child->draw(x1, y1, x2, y2); tglMatrixMode(TGL_MODELVIEW); } } translateViewpointFinish(); if (node->_sibling) node->_sibling->draw(x1, y1, x2, y2); }
void TinyGLRenderer::setupCameraOrtho2D() { tglViewport(0, 0, kOriginalWidth, kOriginalHeight); tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); tglOrtho(0.0, kOriginalWidth, kOriginalHeight, 0.0, -1.0, 1.0); tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); }
void TinyGLRenderer::setupCameraPerspective(float pitch, float heading, float fov) { Renderer::setupCameraPerspective(pitch, heading, fov); // NOTE: tinyGL viewport implementation needs to be checked as it doesn't behave the same as openGL tglViewport(0, kTopBorderHeight, kOriginalWidth, kFrameHeight); tglMatrixMode(TGL_PROJECTION); tglLoadMatrixf(_projectionMatrix.getData()); tglMatrixMode(TGL_MODELVIEW); tglLoadMatrixf(_modelViewMatrix.getData()); }
void GfxTinyGL::setupCamera(float fov, float nclip, float fclip, float roll) { tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); float right = nclip * tan(fov / 2 * (LOCAL_PI / 180)); tglFrustum(-right, right, -right * 0.75, right * 0.75, nclip, fclip); tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); tglRotatef(roll, 0, 0, -1); }
// Zoom into the cube inline void scene1(float scene_time) { tglClear(TGL_COLOR_BUFFER_BIT); float min_zoom = -10; float max_zoom = -2.5; float zoom = min_zoom + (max_zoom-min_zoom) * (float)scene_time/timeline[scene]; tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); tglTranslatef(0, 0, zoom); tglBegin(TGL_LINES); for(int p=0; p<8; ++p) { tglVertex3fv(cube[cubestrip1[p]]); } tglEnd(); tglBegin(TGL_LINES); for(int p=0; p<8; ++p) { tglVertex3fv(cube[cubestrip2[p]]); } tglEnd(); tglSwap(); delay(50); }
// Spinning Cube! inline void scene2(float scene_time) { int i = scene_time / 25; tglClear(TGL_COLOR_BUFFER_BIT); float zoom = 0; if(scene_time > 5000) zoom = 0.5*sin((float)(i-20)/10.0); float rot_speed = 1.0; if(scene_time > 12000) rot_speed += (float)(scene_time - 12000) / 1500.0; tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); tglTranslatef(0, 0, -2.5+zoom); tglRotatef((float)(i % 360) * rot_speed, 0, 1, 0); tglRotatef((float)(i % 360) * rot_speed, 1, 1, 0); tglBegin(TGL_LINES); for(int p=0; p<8; ++p) { tglVertex3fv(cube[cubestrip1[p]]); } tglEnd(); tglBegin(TGL_LINES); for(int p=0; p<8; ++p) { tglVertex3fv(cube[cubestrip2[p]]); } tglEnd(); tglSwap(); }
void GfxTinyGL::startActorDraw(Graphics::Vector3d pos, float scale, float yaw, float pitch, float roll) { tglEnable(TGL_TEXTURE_2D); tglMatrixMode(TGL_MODELVIEW); tglPushMatrix(); if (_currentShadowArray) { // TODO find out why shadowMask at device in woods is null if (!_currentShadowArray->shadowMask) { _currentShadowArray->shadowMask = new byte[_screenWidth * _screenHeight]; _currentShadowArray->shadowMaskSize = _screenWidth * _screenHeight; } assert(_currentShadowArray->shadowMask); //tglSetShadowColor(255, 255, 255); tglSetShadowColor(_shadowColorR, _shadowColorG, _shadowColorB); tglSetShadowMaskBuf(_currentShadowArray->shadowMask); SectorListType::iterator i = _currentShadowArray->planeList.begin(); Sector *shadowSector = i->sector; tglShadowProjection(_currentShadowArray->pos, shadowSector->getVertices()[0], shadowSector->getNormal(), _currentShadowArray->dontNegate); } tglTranslatef(pos.x(), pos.y(), pos.z()); tglScalef(scale, scale, scale); tglRotatef(yaw, 0, 0, 1); tglRotatef(pitch, 1, 0, 0); tglRotatef(roll, 0, 1, 0); }
void TinyGLRenderer::init(Graphics::PixelBuffer &screenBuffer) { debug("Initializing Software 3D Renderer"); _nonPowerOfTwoTexSupport = true; _fb = new TinyGL::FrameBuffer(kOriginalWidth, kOriginalHeight, screenBuffer); TinyGL::glInit(_fb, 512); tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); tglDisable(TGL_LIGHTING); tglEnable(TGL_TEXTURE_2D); tglEnable(TGL_DEPTH_TEST); }
void initGL() { tglInit(); tglViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); tglMatrixMode(TGL_PROJECTION); float aspect = (float)SCREEN_HEIGHT / (float)SCREEN_WIDTH; tglFrustum(-1.0, 1.0, -aspect, aspect, 1.0, 5.0); }
void TinyGLRenderer::init() { debug("Initializing Software 3D Renderer"); computeScreenViewport(); Graphics::PixelBuffer screenBuffer = _system->getScreenPixelBuffer(); _fb = new TinyGL::FrameBuffer(kOriginalWidth, kOriginalHeight, screenBuffer); TinyGL::glInit(_fb, 512); tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); tglDisable(TGL_LIGHTING); tglEnable(TGL_TEXTURE_2D); tglEnable(TGL_DEPTH_TEST); }
void GfxTinyGL::drawSprite(const Sprite *sprite) { tglMatrixMode(TGL_TEXTURE); tglLoadIdentity(); tglMatrixMode(TGL_MODELVIEW); tglPushMatrix(); tglTranslatef(sprite->_pos.x(), sprite->_pos.y(), sprite->_pos.z()); TGLfloat modelview[16]; tglGetFloatv(TGL_MODELVIEW_MATRIX, modelview); // We want screen-aligned sprites so reset the rotation part of the matrix. for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) { modelview[i * 4 + j] = 1.0f; } else { modelview[i * 4 + j] = 0.0f; } } } tglLoadMatrixf(modelview); tglDisable(TGL_LIGHTING); tglBegin(TGL_POLYGON); tglTexCoord2f(0.0f, 0.0f); tglVertex3f(sprite->_width / 2, sprite->_height, 0.0f); tglTexCoord2f(0.0f, 1.0f); tglVertex3f(sprite->_width / 2, 0.0f, 0.0f); tglTexCoord2f(1.0f, 1.0f); tglVertex3f(-sprite->_width / 2, 0.0f, 0.0f); tglTexCoord2f(1.0f, 0.0f); tglVertex3f(-sprite->_width / 2, sprite->_height, 0.0f); tglEnd(); tglEnable(TGL_LIGHTING); tglPopMatrix(); }
void GfxTinyGL::finishActorDraw() { tglMatrixMode(TGL_MODELVIEW); tglPopMatrix(); tglDisable(TGL_TEXTURE_2D); if (_currentShadowArray) { tglSetShadowMaskBuf(NULL); }/* else { uint16 *dst = (uint16 *)_zb->pbuf; uint16 c = 0xffff; for (int x = g_winX1; x <= g_winX2; x++) { WRITE_LE_UINT16(dst + 640 * g_winY1 + x, c); } for (int x = g_winX1; x <= g_winX2; x++) { WRITE_LE_UINT16(dst + 640 * g_winY2 + x, c); } for (int y = g_winY1; y <= g_winY2; y++) { WRITE_LE_UINT16(dst + 640 * y + g_winX1, c); } for (int y = g_winY1; y <= g_winY2; y++) { WRITE_LE_UINT16(dst + 640 * y + g_winX2, c); } }*/ }
void glInit(void *zbuffer1, int textureSize) { FrameBuffer *zbuffer = (FrameBuffer *)zbuffer1; GLContext *c; GLViewport *v; 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); c = new GLContext(); gl_ctx = c; c->fb = zbuffer; c->fb->_textureSize = c->_textureSize = textureSize; c->fb->_textureSizeMask = (textureSize - 1) << ZB_POINT_ST_FRAC_BITS; // allocate GLVertex array c->vertex_max = POLYGON_MAX_VERTEX; c->vertex = (GLVertex *)gl_malloc(POLYGON_MAX_VERTEX * sizeof(GLVertex)); // 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; c->in_begin = 0; // lights for (int i = 0; i < T_MAX_LIGHTS; i++) { GLLight *l = &c->lights[i]; l->ambient = Vector4(0, 0, 0, 1); if (i == 0) { l->diffuse = Vector4(1, 1, 1, 1); l->specular = Vector4(1, 1, 1, 1); l->has_specular = true; } else { l->diffuse = Vector4(0, 0, 0, 1); l->specular = Vector4(0, 0, 0, 1); l->has_specular = false; } l->position = Vector4(0, 0, 1, 0); l->spot_direction = Vector3(0, 0, -1); l->spot_exponent = 0; l->spot_cutoff = 180; l->attenuation[0] = 1; l->attenuation[1] = 0; l->attenuation[2] = 0; l->cos_spot_cutoff = -1.0f; l->norm_spot_direction = Vector3(0, 0, -1); l->norm_position = Vector3(0, 0, 1); l->enabled = 0; l->next = NULL; l->prev = NULL; } c->first_light = NULL; c->ambient_light_model = Vector4(0.2f, 0.2f, 0.2f, 1); c->local_light_model = 0; c->lighting_enabled = 0; c->light_model_two_side = 0; // default materials */ for (int i = 0; i < 2; i++) { GLMaterial *m = &c->materials[i]; 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); m->has_specular = false; 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 c->current_color = Vector4(1.0f, 1.0f, 1.0f, 1.0f); c->longcurrent_color[0] = 65535; c->longcurrent_color[1] = 65535; c->longcurrent_color[2] = 65535; c->longcurrent_color[3] = 65535; c->current_normal = Vector4(1.0f, 0.0f, 0.0f, 0.0f); c->current_edge_flag = 1; c->current_tex_coord = Vector4(0.0f, 0.0f, 0.0f, 1.0f); 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; // clear c->clear_color = Vector4(0.0f, 0.0f, 0.0f, 0.0f); c->clear_depth = 0; // selection c->render_mode = TGL_RENDER; c->select_buffer = NULL; c->name_stack_size = 0; // blending c->fb->enableBlending(false); // alpha test c->fb->enableAlphaTest(false); // matrix c->matrix_mode = 0; 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; for (int i = 0; i < 3; i++) { c->matrix_stack[i] = (Matrix4 *)gl_zalloc(c->matrix_stack_depth_max[i] * sizeof(Matrix4)); c->matrix_stack_ptr[i] = c->matrix_stack[i]; } tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); tglMatrixMode(TGL_TEXTURE); tglLoadIdentity(); tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); tglBlendFunc(TGL_SRC_ALPHA, TGL_ONE_MINUS_SRC_ALPHA); tglAlphaFunc(TGL_ALWAYS, 0.f); tglDepthFunc(TGL_LESS); c->matrix_model_projection_updated = 1; // opengl 1.1 arrays c->client_states = 0; // opengl 1.1 polygon offset c->offset_states = 0; // shadow mode c->shadow_mode = 0; // 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; // depth test c->depth_test = 0; c->color_mask = (1 << 24) | (1 << 16) | (1 << 8) | (1 << 0); const int kDrawCallMemory = 5 * 1024 * 1024; c->_currentAllocatorIndex = 0; c->_drawCallAllocator[0].initialize(kDrawCallMemory); c->_drawCallAllocator[1].initialize(kDrawCallMemory); c->_enableDirtyRectangles = false; Graphics::Internal::tglBlitSetScissorRect(0, 0, c->fb->xsize, c->fb->ysize); }
void glInit(void *zbuffer1) { ZBuffer *zbuffer = (ZBuffer *)zbuffer1; GLContext *c; GLViewport *v; c = (GLContext *)gl_zalloc(sizeof(GLContext)); gl_ctx = c; c->zb = zbuffer; // allocate GLVertex array c->vertex_max = POLYGON_MAX_VERTEX; c->vertex = (GLVertex *)gl_malloc(POLYGON_MAX_VERTEX * sizeof(GLVertex)); // 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; c->in_begin = 0; // lights for (int i = 0; i < T_MAX_LIGHTS; i++) { GLLight *l = &c->lights[i]; l->ambient = gl_V4_New(0, 0, 0, 1); l->diffuse = gl_V4_New(1, 1, 1, 1); l->specular = gl_V4_New(1, 1, 1, 1); l->position = gl_V4_New(0, 0, 1, 0); l->norm_position = gl_V3_New(0, 0, 1); l->spot_direction = gl_V3_New(0, 0, -1); l->norm_spot_direction = gl_V3_New(0, 0, - 1); l->spot_exponent = 0; l->spot_cutoff = 180; l->attenuation[0] = 1; l->attenuation[1] = 0; l->attenuation[2] = 0; l->enabled = 0; } c->first_light = NULL; c->ambient_light_model = gl_V4_New(0.2f, 0.2f, 0.2f, 1); c->local_light_model = 0; c->lighting_enabled = 0; c->light_model_two_side = 0; // default materials */ for (int i = 0; i < 2; i++) { GLMaterial *m = &c->materials[i]; m->emission = gl_V4_New(0, 0, 0, 1); m->ambient = gl_V4_New(0.2f, 0.2f, 0.2f, 1); m->diffuse = (gl_V4_New(0.8f, 0.8f, 0.8f, 1)); m->specular = gl_V4_New(0, 0, 0, 1); 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 c->current_color.X = 1.0; c->current_color.Y = 1.0; c->current_color.Z = 1.0; c->current_color.W = 1.0; c->longcurrent_color[0] = 65535; c->longcurrent_color[1] = 65535; c->longcurrent_color[2] = 65535; c->current_normal.X = 1.0; c->current_normal.Y = 0.0; c->current_normal.Z = 0.0; c->current_normal.W = 0.0; c->current_edge_flag = 1; c->current_tex_coord.X = 0; c->current_tex_coord.Y = 0; c->current_tex_coord.Z = 0; c->current_tex_coord.W = 1; 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; // clear c->clear_color.v[0] = 0; c->clear_color.v[1] = 0; c->clear_color.v[2] = 0; c->clear_color.v[3] = 0; c->clear_depth = 0; // selection c->render_mode = TGL_RENDER; c->select_buffer = NULL; c->name_stack_size = 0; // matrix c->matrix_mode = 0; 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; for (int i = 0; i < 3; i++) { c->matrix_stack[i] = (M4 *)gl_zalloc(c->matrix_stack_depth_max[i] * sizeof(M4)); c->matrix_stack_ptr[i] = c->matrix_stack[i]; } tglMatrixMode(TGL_PROJECTION); tglLoadIdentity(); tglMatrixMode(TGL_TEXTURE); tglLoadIdentity(); tglMatrixMode(TGL_MODELVIEW); tglLoadIdentity(); c->matrix_model_projection_updated = 1; // opengl 1.1 arrays c->client_states = 0; // opengl 1.1 polygon offset c->offset_states = 0; // shadow mode c->shadow_mode = 0; // 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; // depth test c->depth_test = 0; c->color_mask = (1 << 24) | (1 << 16) | (1 << 8) | (1 << 0); }
void GfxTinyGL::set3DMode() { tglMatrixMode(TGL_MODELVIEW); tglEnable(TGL_DEPTH_TEST); }