void Lua_V2::WorldToScreen() { lua_Object xObj = lua_getparam(1); lua_Object yObj = lua_getparam(2); lua_Object zObj = lua_getparam(3); if (!lua_isnumber(xObj) || !lua_isnumber(yObj) || !lua_isnumber(zObj)) { lua_pushnumber(0.0); lua_pushnumber(0.0); return; } float x = lua_getnumber(xObj); float y = lua_getnumber(yObj); float z = lua_getnumber(zObj); Math::Vector3d pos = Math::Vector3d(x, y, z); const Set::Setup *setup = g_emi->getCurrSet()->getCurrSetup(); const Math::Vector3d interest = setup->_interest; const float roll = setup->_roll; const Math::Quaternion quat = Math::Quaternion(interest.x(), interest.y(), interest.z(), roll); Math::Matrix4 view = quat.toMatrix(); view.transpose(); pos -= setup->_pos; pos = view.getRotation() * pos; pos.z() = -pos.z(); Math::Matrix4 proj = GfxBase::makeProjMatrix(setup->_fov, setup->_nclip, setup->_fclip); proj.transpose(); Math::Vector4d screen = proj * Math::Vector4d(pos.x(), pos.y(), pos.z(), 1.0); screen /= screen.w(); lua_pushnumber((screen.x() + 1) * 320); lua_pushnumber((1 - screen.y()) * 240); }
void BaseRenderer::setupCameraPerspective(float pitch, float heading, float fov) { _projectionMatrix = makeProjectionMatrix(fov); _modelViewMatrix = Math::Matrix4(180.0f - heading, pitch, 0.0f, Math::EO_YXZ); Math::Matrix4 proj = _projectionMatrix; Math::Matrix4 model = _modelViewMatrix; proj.transpose(); model.transpose(); _mvpMatrix = proj * model; _frustum.setup(_mvpMatrix); _mvpMatrix.transpose(); }
void ShaderRenderer::setupCameraPerspective(float pitch, float heading, float fov) { // TODO: Find a correct and exact formula for the FOV GLfloat 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 glViewport(0, kBottomBorderHeight, kOriginalWidth, kFrameHeight); const Math::Vector2d topLeft = Math::Vector2d(0, kBottomBorderHeight + kFrameHeight); const Math::Vector2d bottomRight = Math::Vector2d(kOriginalWidth, kBottomBorderHeight); _viewport = Math::Rect2d(topLeft, bottomRight); float nclip = 1.0, fclip = 10000.0; float aspect = _viewport.getWidth() / _viewport.getHeight(); // taken from glm float range = nclip * tan(glFOV / 2 * (LOCAL_PI / 180)); float left = -range * aspect; float right = range * aspect; float bottom = -range; float top = range; Math::Matrix4 proj; proj(0,0) = (2.0f * nclip) / (right - left); proj(1,1) = (2.0f * nclip) / (top - bottom); proj(2,0) = (right + left) / (right - left); proj(2,1) = 0.0f; // (top + bottom) / (top - bottom); proj(2,2) = -(fclip + nclip) / (fclip - nclip); proj(2,3) = -1.0f; proj(3,2) = -(2.0f * fclip * nclip) / (fclip - nclip); proj(3,3) = 0.0f; proj.transpose(); Math::Matrix4 model = Math::Quaternion::fromEuler(180.0f - heading, pitch, 0.0f).toMatrix(); model.transpose(); _mvpMatrix = proj * model; _mvpMatrix.transpose(); }
void OpenGLSPropRenderer::render(const Math::Vector3d position, float direction) { if (_faceVBO == -1) { // Update the OpenGL Buffer Objects if required clearVertices(); uploadVertices(); } _gfx->set3DMode(); Math::Matrix4 model = getModelMatrix(position, direction); Math::Matrix4 view = StarkScene->getViewMatrix(); Math::Matrix4 projection = StarkScene->getProjectionMatrix(); Math::Matrix4 mvp = projection * view * model; mvp.transpose(); _shader->use(true); _shader->setUniform("mvp", mvp); const Common::Array<Formats::BiffMesh::Face> &faces = _model->getFaces(); const Common::Array<Formats::BiffMesh::Material> &materials = _model->getMaterials(); for (Common::Array<Formats::BiffMesh::Face>::const_iterator face = faces.begin(); face != faces.end(); ++face) { const Formats::BiffMesh::Material &material = materials[face->materialId]; // For each face draw its vertices from the VBO, indexed by the EBO const Gfx::Texture *tex = _texture->getTexture(material.texture); if (tex) { tex->bind(); } else { glBindTexture(GL_TEXTURE_2D, 0); } GLuint ebo = _faceEBO[face]; _shader->enableVertexAttribute("position", _faceVBO, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 0); _shader->enableVertexAttribute("normal", _faceVBO, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 12); _shader->enableVertexAttribute("texcoord", _faceVBO, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 24); _shader->use(true); _shader->setUniform("textured", tex != nullptr); _shader->setUniform("color", Math::Vector3d(material.r, material.g, material.b)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glDrawElements(GL_TRIANGLES, face->vertexIndices.size(), GL_UNSIGNED_INT, 0); glUseProgram(0); } }