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::getBoundingBoxPos(const Mesh *model, int *x1, int *y1, int *x2, int *y2) { if (_currentShadowArray) { *x1 = -1; *y1 = -1; *x2 = -1; *y2 = -1; return; } TGLfloat top = 1000; TGLfloat right = -1000; TGLfloat left = 1000; TGLfloat bottom = -1000; TGLfloat winX, winY, winZ; for (int i = 0; i < model->_numFaces; i++) { Graphics::Vector3d v; float* pVertices; for (int j = 0; j < model->_faces[i]._numVertices; j++) { TGLfloat modelView[16], projection[16]; TGLint viewPort[4]; tglGetFloatv(TGL_MODELVIEW_MATRIX, modelView); tglGetFloatv(TGL_PROJECTION_MATRIX, projection); tglGetIntegerv(TGL_VIEWPORT, viewPort); pVertices = model->_vertices + 3 * model->_faces[i]._vertices[j]; v.set(*(pVertices), *(pVertices + 1), *(pVertices + 2)); tgluProject(v.x(), v.y(), v.z(), modelView, projection, viewPort, &winX, &winY, &winZ); if (winX > right) right = winX; if (winX < left) left = winX; if (winY < top) top = winY; if (winY > bottom) bottom = winY; } } float t = bottom; bottom = 480 - top; top = 480 - t; if (left < 0) left = 0; if (right > 639) right = 639; if (top < 0) top = 0; if (bottom > 479) bottom = 479; if (top > 479 || left > 639 || bottom < 0 || right < 0) { *x1 = -1; *y1 = -1; *x2 = -1; *y2 = -1; return; } *x1 = (int)left; *y1 = (int)top; *x2 = (int)right; *y2 = (int)bottom; /* uint16 *dst = (uint16 *)_zb->pbuf; uint16 c = 0xffff; for (int x = left; x <= right; x++) { WRITE_LE_UINT16(dst + 640 * (int)top + x, c); } for (int x = left; x <= right; x++) { WRITE_LE_UINT16(dst + 640 * (int)bottom + x, c); } for (int y = top; y <= bottom; y++) { WRITE_LE_UINT16(dst + 640 * y + (int)left, c); } for (int y = top; y <= bottom; y++) { WRITE_LE_UINT16(dst + 640 * y + (int)right, c); }*/ }