void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); p = Perspective(60.0, (float)w/(float)h, 1.0, 30.0); glUniformMatrix4fv(projection, 1, GL_TRUE, p); }
Camera::Camera(float radius, float initAngle, vec4 at, GLfloat zNear, GLfloat zFar, GLfloat fovy) { m_angle = 90.0f - initAngle; m_phi = 0.0f; m_theta = m_angle; m_at = at; m_up = vec4(0.0f, 1.0f, 0.0f, 0.0f); m_eye = vec4(0.0f, 0.0f, radius, 1.0); m_initRadius = radius; m_radius = m_initRadius; m_zNear = zNear; m_zFar = zFar; m_fovy = fovy; m_perspective = Perspective(m_fovy, m_aspect, m_zNear, m_zFar); // Used for quaternion analysis #ifdef QUAT_ANALYSIS_ON count = 0; camHorRotAngle = 270.0f; #endif // QUAT_ANALYSIS_ON }
void init() { // Load shaders and use the resulting shader program GLuint drawProg = InitShader("Draw.vertex", "Draw.fragment"); GLuint shadowProg = InitShader("ShadowMap.vertex", "ShadowMap.fragment"); glUseProgram(drawProg); meshSetup(Global::red_opaque, 1.2); const mat4 projection = Perspective(Global::FOV, 1.0f, 0.0078125f, 16.0f) * translate(0.0f, 0.0f, 0.75f) * scale(10.0f); Global::root = new RenderGraph( drawProg, shadowProg, *genIdyll(5, 1.2f), projection ); Global::sun = new RenderGraph( drawProg, shadowProg, *genSun( translate(16.0 * Global::sunVec) * scale(0.4f) * rX(M_PI/2.0) ), projection ); help(); }
//std::vector<textitem> textItems; void drawFinal2DText() { //return; extern CFont *util_font; if (!util_font) return; util_font->setBatch(true); glDisable(GL_FOG); // Goto othographic mode Ortho(); CFont::fontStrings *f = new CFont::fontStrings[textItems.size()]; std::copy(textItems.begin(), textItems.end(), f); util_font->Draw2DStrings(f, textItems.size()); textItems.clear(); delete [] f; util_font->setBatch(false); // Go back to perspective Perspective(); }
bool csBox3::ProjectBoxAndOutline (const csTransform& trans, float fov, float sx, float sy, csBox2& sbox, csPoly2D& poly, float& min_z, float& max_z) const { const csVector3& origin = trans.GetOrigin (); int idx = CalculatePointSegment (origin); const Outline &ol = outlines[idx]; int num_array = MIN (ol.num, 6); poly.SetVertexCount (num_array); min_z = 100000000.0; max_z = 0; sbox.StartBoundingBox (); int i; // We go to 8 so that we can calculate the correct min_z/max_z. // If we only go to num_array we will only calculate min_z/max_z // for the outine vertices. for (i = 0 ; i < 8 ; i++) { csVector3 v = trans * GetCorner (ol.vertices[i]); if (v.z > max_z) max_z = v.z; if (v.z < min_z) min_z = v.z; if (i < num_array) { if (v.z < .1) PerspectiveWrong (v, poly[i], fov, sx, sy); else Perspective (v, poly[i], fov, sx, sy); sbox.AddBoundingVertex (poly[i]); } } return max_z >= .1; }
// ProjectionLight Method Definitions ProjectionLight::ProjectionLight(const Transform &light2world, const Spectrum &intensity, const string &texname, float fov) : Light(light2world) { lightPos = LightToWorld(Point(0,0,0)); Intensity = intensity; // Create _ProjectionLight_ MIP-map int width, height; RGBSpectrum *texels = ReadImage(texname, &width, &height); if (texels) projectionMap = new MIPMap<RGBSpectrum>(width, height, texels); else projectionMap = NULL; delete[] texels; // Initialize _ProjectionLight_ projection matrix float aspect = projectionMap ? float(width) / float(height) : 1.f; if (aspect > 1.f) { screenX0 = -aspect; screenX1 = aspect; screenY0 = -1.f; screenY1 = 1.f; } else { screenX0 = -1.f; screenX1 = 1.f; screenY0 = -1.f / aspect; screenY1 = 1.f / aspect; } hither = 1e-3f; yon = 1e30f; lightProjection = Perspective(fov, hither, yon); // Compute cosine of cone surrounding projection directions float opposite = tanf(Radians(fov) / 2.f); float tanDiag = opposite * sqrtf(1.f + 1.f/(aspect*aspect)); cosTotalWidth = cosf(atanf(tanDiag)); }
Matrix4 Camera::getProjectionMatrix() { if (scene == nullptr) { abortWithMessage("In Camera::getProjectionMatrix(): Scene for camera not set."); } return Perspective(fov, scene->aspect, nearDistance, farDistance); }
//------------------------------------------------------------------------------- // @ Game::Render() //------------------------------------------------------------------------------- // Render stuff //------------------------------------------------------------------------------- void Game::Render() { // set perspective matrix Perspective( IvRenderer::mRenderer->GetFOV(), (float)IvRenderer::mRenderer->GetWidth()/(float)IvRenderer::mRenderer->GetHeight(), IvRenderer::mRenderer->GetNearPlane(), IvRenderer::mRenderer->GetFarPlane() ); // set viewer IvSetDefaultViewer( -10.0f, 0.0f, 5.0f ); // draw axes IvDrawAxes(); // draw floor IvDrawFloor(); // draw some objects IvMatrix44 worldTransform; worldTransform.Translation( IvVector3( 0.0f, 0.0f, 1.0f ) ); IvSetWorldMatrix( worldTransform ); IvDrawCube(); worldTransform.Translation( IvVector3( 0.0f, 3.0f, 0.0f ) ); IvSetWorldMatrix( worldTransform ); IvDrawTeapot(); worldTransform.Translation( IvVector3( 0.0f, -2.5f, 1.0f ) ); IvSetWorldMatrix( worldTransform ); IvDrawSphere( 1.0f ); }
ProjectionLight::ProjectionLight(const Transform& lightToWorld, const string& texname, float fov) : Light(lightToWorld) { lightPos = LightToWorld(Point(0, 0, 0)); cv::Mat image = cv::imread(texname, cv::IMREAD_COLOR); //cv::flip(image, image, 1); texture = new cv::Mat[1]; image.copyTo(*texture); float aspect = float(texture->cols) / float(texture->rows); if (aspect > 1.f) { screenX0 = -aspect; screenX1 = aspect; screenY0 = -1.f; screenY1 = 1.f; } else { screenX0 = -1.f; screenX1 = 1.f; screenY0 = -1.f / aspect; screenY1 = 1.f / aspect; } hither = 1e-3f; yon = 1e30f; lightProjection = Perspective(fov, hither, yon); // compute cosine of cone surrounding projection directions float opposite = tanf(Radians(fov) / 2.f); float tanDiag = opposite * sqrtf(1.f + 1.f / (aspect*aspect)); cosTotalWidth = cosf(atanf(tanDiag)); }
// ProjectionLight Method Definitions ProjectionLight::ProjectionLight(const Transform &LightToWorld, const MediumInterface &mediumInterface, const Spectrum &I, const std::string &texname, Float fov) : Light((int)LightFlags::DeltaPosition, LightToWorld, mediumInterface), pLight(LightToWorld(Point3f(0, 0, 0))), I(I) { // Create _ProjectionLight_ MIP map Point2i resolution; std::unique_ptr<RGBSpectrum[]> texels = ReadImage(texname, &resolution); if (texels) projectionMap.reset(new MIPMap<RGBSpectrum>(resolution, texels.get())); // Initialize _ProjectionLight_ projection matrix Float aspect = projectionMap ? (Float(resolution.x) / Float(resolution.y)) : 1; if (aspect > 1) screenBounds = Bounds2f(Point2f(-aspect, -1), Point2f(aspect, 1)); else screenBounds = Bounds2f(Point2f(-1, -1 / aspect), Point2f(1, 1 / aspect)); hither = 1e-3f; yon = 1e30f; lightProjection = Perspective(fov, hither, yon); // Compute cosine of cone surrounding projection directions Float opposite = std::tan(Radians(fov) / 2.f); Float tanDiag = opposite * std::sqrt(1 + 1 / (aspect * aspect)); cosTotalWidth = std::cos(std::atan(tanDiag)); }
//----------------------------------------------------------------------------- // Class: Context // Method: Context // Description: constructor //----------------------------------------------------------------------------- Context::Context ( GLfloat w, GLfloat h, std::string windowName, Camera cam ) : w_(w), h_(h), windowName_(windowName), cam_(cam) { pers_ = Perspective(w_, h_); ortho_ = Ortho(w_, h_); mainWindow_ = NULL; windowOpened_ = true; } // ----- end of method Context::Context (constructor) -----
void reshape(int width, int height){ ww= width; wh = height; //field of view angle, aspect ratio, closest distance from camera to object, largest distanec from camera to object p = Perspective(45.0, (float)width/(float)height, 1.0, 100.0); glViewport( 0, 0, width, height ); }
void setView() { camera.x = lookAt.x + cameraR * cos( thetaXY ) * cos( thetaZ ); camera.y = lookAt.y + cameraR * sin( thetaXY ) * cos( thetaZ ); camera.z = lookAt.z + cameraR * sin( thetaZ ); projMatrix = Perspective( 45.0, 1.0, 0.1, 2.0 * std::fmax( cameraR, 1.0 ) ); mvMatrix = LookAt( camera, lookAt, up ); }
// PerspectiveCamera Method Definitions PerspectiveCamera:: PerspectiveCamera(const AnimatedTransform &cam2world, const float screenWindow[4], float sopen, float sclose, float lensr, float focald, float fov, Film *f) : ProjectiveCamera(cam2world, Perspective(fov, 1e-2f, 1000.f), screenWindow, sopen, sclose, lensr, focald, f) { // Compute differential changes in origin for perspective camera rays dxCamera = RasterToCamera(Point(1,0,0)) - RasterToCamera(Point(0,0,0)); dyCamera = RasterToCamera(Point(0,1,0)) - RasterToCamera(Point(0,0,0)); }
void reshape( int width, int height ) { glViewport( 0, 0, width, height ); GLfloat aspect = GLfloat(width)/height; mat4 projection = Perspective( 45.0, aspect, 0.5, 3.0 ); glUniformMatrix4fv( Projection, 1, GL_TRUE, projection ); }
mat4 Scene::getView() { /*switch(camView) { case VIEW_PERSPECTIVE: return Perspective(fovy, aspect, zNear, zFar); case VIEW_ORTHOGRAPHIC: return Ortho(-1.5, 1.5, -1.5, 1.5, .5, 10.); }*/ printf("getting perspective\n"); return Perspective(fovy, aspect, zNear, zFar); }
mat4 SpelchkCamera::getProjectionMatrix() { switch ( _projectionType ) { case 1: return Ortho( _left, _right, _bottom, _top, _zNear, _zFar ); case 2: return Frustum( _left, _right, _bottom, _top, _zNear, _zFar ); default: return Perspective( _fovy, _aspect, _zNear, _zFar ); } }
mat4 Camera::getProjectionMatrix() { switch (projectionType) { case 1: return Ortho(left, right, bottom, top, zNear, zFar); case 2: return Frustum(left, right, bottom, top, zNear, zFar); default: return Perspective(fovy, aspect, zNear, zFar); } }
Matrix4x4 Matrix4x4::Perspective(float verticalFov, float aspect, float near, float far) { float width; float height; height = Math::TanDeg(verticalFov / 2) * near; width = height * aspect; return Perspective(-width, width, -height, height, near, far); }
/********************************************** PROC: myReshape() DOES: handles the window being resized -- don't change **********************************************************/ void myReshape(int w, int h) { Width = w; Height = h; glViewport(0, 0, w, h); mat4 projection = Perspective(50.0f, (float)w/(float)h, 1.0f, 1000.0f); glUniformMatrix4fv( uProjection, 1, GL_TRUE, projection ); }
void init( void ) { mat4 mxT; vec4 vT, vColor; // 產生所需之 Model View 與 Projection Matrix g_mxModelView = LookAt( g_vEye, g_vAt, g_vUp ); g_mxProjection = Perspective(60.0, (GLfloat)SCREEN_SIZE/(GLfloat)SCREEN_SIZE, 1.0, 1000.0); // 產生物件的實體 g_pFloor = new CQuad; g_pFloor->SetShader(g_mxModelView, g_mxProjection); g_pFloor->SetTRSMatrix(Scale(30,1,30)); g_pFloor->SetShadingMode(GOURAUD_SHADING); g_pFloor->SetTiling(30,30); // g_uiFTexID[0] = g_pFloor->LoadPngImageAndSetTextureObject("texture/checker.png"); // g_uiFTexID[0] = png_load_LIBPNG("texture/checker.png",&g_iTexWidth, &g_iTexHeight, true); g_uiFTexID[0] = png_load_SOIL("texture/checker.png",&g_iTexWidth, &g_iTexHeight, true); // 設定貼圖 g_pFloor->SetMaterials(vec4(0), vec4(0.85f, 0.85f, 0.85f, 1), vec4(1.0f, 1.0f, 1.0f, 1.0f)); g_pFloor->SetKaKdKsShini(0, 0.8f, 0.5f, 1); g_pCube = new CSolidCube; g_pCube->SetShader(g_mxModelView, g_mxProjection); // 設定 Cube vT.x = 1.5f; vT.y = 1.01f; vT.z = -1.5f; mxT = Translate(vT); mxT._m[0][0] = 2.0f; mxT._m[1][1] = 2.0f; mxT._m[2][2] = 2.0f; g_pCube->SetTRSMatrix(mxT); g_pCube->SetShadingMode(GOURAUD_SHADING); // materials g_pCube->SetMaterials(vec4(0.35f, 0.35f, 0.35f, 1), vec4(0.85f, 0.85f, 0.85f, 1), vec4(1.0f, 1.0f, 1.0f, 1.0f)); g_pCube->SetKaKdKsShini(0.25f, 0.8f, 0.2f, 2); g_pCube->LoadPngImageAndSetTextureObject("texture/Masonry.Brick.png"); g_pSphere = new CSolidSphere(1, 16, 16); g_pSphere->SetShader(g_mxModelView, g_mxProjection); // 設定 Sphere vT.x = -3.5; vT.y = 2.0; vT.z = 1.5; mxT = Translate(vT)* RotateX(-90) * Scale(2,2,2); g_pSphere->SetTRSMatrix(mxT); g_pSphere->SetShadingMode(GOURAUD_SHADING); // materials g_pSphere->SetMaterials(vec4(0.15f, 0.15f, 0.15f, 1), vec4(0.85f, 0.85f, 0.85f, 1), vec4(1.0f, 1.0f, 1.0f, 1.0f)); g_pSphere->SetKaKdKsShini(0.15f, 0.5f, 0.2f, 2); g_pSphere->LoadPngImageAndSetTextureObject("texture/earth.png"); // 設定 代表 Light 的 WireSphere g_pLight = new CWireSphere(0.25f, 6, 3); g_pLight->SetShader(g_mxModelView, g_mxProjection); mxT = Translate(g_Light1.position); g_pLight->SetTRSMatrix(mxT); g_pLight->SetColor(g_Light1.diffuse); g_pLight->SetLightingDisable(); }
void init( void ) { vec4 vT, vColor; // 讀進貼圖 g_uiDifTexName = png_load_SOIL("texture/Masonry.Brick.png",&g_iTexWidth, &g_iTexHeight, false); g_uiLghTexName = png_load_SOIL("texture/lightmap1.png",&g_iTexWidth, &g_iTexHeight, false); // 產生所需之 Model View 與 Projection Matrix g_mxModelView = LookAt( g_vEye, g_vAt, g_vUp ); g_mxProjection = Perspective(60.0, (GLfloat)SCREEN_SIZE/(GLfloat)SCREEN_SIZE, 1.0, 1000.0); // 產生物件的實體 g_pFloor = new CQuadMT; g_pFloor->SetShader(g_mxModelView, g_mxProjection); g_pFloor->SetTRSMatrix(Scale(8,1,8)); g_pFloor->SetShadingMode(GOURAUD_SHADING); g_pFloor->SetTiling(1,1); g_pFloor->SetMaterials(vec4(0), vec4(0.85f, 0.85f, 0.85f, 1), vec4(1.0f, 1.0f, 1.0f, 1.0f)); g_pFloor->SetKaKdKsShini(0, 0.8f, 0.5f, 1); g_pFloor->SetColor(vec4(0.95f,0.95f,0.95f,1.0f)); g_pFloor->SetLightingDisable(); g_uiShaderNo = g_pFloor->GetShaderHandle(); // For g_pAimal1 g_pAimal[0] = new CQuad; g_pAimal[0]->SetShader(g_mxModelView, g_mxProjection); mxT = Translate(5, 2.0f, 3.0f) * RotateX(90) *Scale(2,2,2); g_pAimal[0]->SetTRSMatrix(mxT); g_pAimal[0]->SetShadingMode(GOURAUD_SHADING); // 設定貼圖 g_pAimal[0]->SetMaterials(vec4(0), vec4(0.85f, 0.85f, 0.85f, 1), vec4(1.0f, 1.0f, 1.0f, 1.0f)); g_pAimal[0]->SetKaKdKsShini(0, 0.8f, 0.5f, 1); g_pAimal[0]->SetColor(vec4(0.9f, 0.9f, 0.9f, 1.0f)); g_uiAimalTexID[0] = png_load_SOIL("texture/wi_tata.png",&g_iTexWidth, &g_iTexHeight, false); g_pAimal[0]->SetLightingDisable(); // 設定 代表 Light 的 WireSphere ,在這個範例中 燈光是沒有作用的 g_pLight = new CWireSphere(0.25f, 6, 3); g_pLight->SetShader(g_mxModelView, g_mxProjection); mxT = Translate(g_Light1.position); g_pLight->SetTRSMatrix(mxT); g_pLight->SetColor(g_Light1.diffuse); g_pLight->SetLightingDisable(); }
void DrawCube(GLuint program) { float w = (float)glutGet(GLUT_WINDOW_WIDTH); float h = (float)glutGet(GLUT_WINDOW_HEIGHT); // passage des attributs de sommet au shader #ifdef VAO glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); GLint positionLocation = glGetAttribLocation(program, "a_position"); glEnableVertexAttribArray(positionLocation); glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, 0); #else glBindVertexArray(cubeVAO); #endif // variables uniformes (constantes) durant le rendu de la primitive float projection[16]; //Orthographic(projection, 0, w, h, 0, -1.f, 1.f); Perspective(projection, 45.f, w, h, 0.1f, 1000.f); //Identity(projection); GLint projLocation = glGetUniformLocation(program, "u_projectionMatrix"); glUniformMatrix4fv(projLocation, 1, GL_FALSE, projection); int currentTime = glutGet(GLUT_ELAPSED_TIME); int delta = currentTime - previousTime; previousTime = currentTime; static float time = 1.f; time += delta/1000.f; GLint timeLocation = glGetUniformLocation(program, "u_time"); glUniform1f(timeLocation, time); float viewTransform[16]; Identity(viewTransform); viewTransform[14] = -7.0f; GLint viewLocation = glGetUniformLocation(program, "u_viewMatrix"); glUniformMatrix4fv(viewLocation, 1, GL_FALSE, viewTransform); float worldTransform[16]; //Identity(worldTransform); //Translate(worldTransform, 5.f * sin(time), 0.0f, 0.0F); float A[16], B[16]; //RotateX(A, time); Translate(A, 5.f * sin(time), 0.0f, 0.0F); RotateZ(B, time); MatrixProduct(worldTransform, A, B); GLint worldLocation = glGetUniformLocation(program, "u_worldMatrix"); glUniformMatrix4fv(worldLocation, 1, GL_FALSE, worldTransform); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBO); glDrawElements(GL_TRIANGLES, 6 * 2 * 3, GL_UNSIGNED_SHORT, 0); }
void calculateView() { cameraPos.z = getZ( cameraPos.x, cameraPos.y ) + tripodHeight; vec4 lookAtPos( cameraPos.x + cos( cameraRotZ ) * cos( cameraRotXY ), cameraPos.y + cos( cameraRotZ ) * sin( cameraRotXY ), cameraPos.z + sin( cameraRotZ ), 1.0 ); projStack.push( Perspective( 45.0, 1.0, 0.01, viewLimitOn ? viewLimit : 3.0 * std::fmax( gridSize, tripodHeight ) ) ); mvStack.push( LookAt( cameraPos, lookAtPos, up ) ); }
float csHazeMeshObject::GetScreenBoundingBox (long cameranr, long movablenr, float fov, float sx, float sy, const csReversibleTransform& trans, csBox2& sbox, csBox3& cbox) { csVector2 oneCorner; GetTransformedBoundingBox (cameranr, movablenr, trans, cbox); // if the entire bounding box is behind the camera, we're done if ((cbox.MinZ () < 0) && (cbox.MaxZ () < 0)) { return -1; } // Transform from camera to screen space. if (cbox.MinZ () <= 0) { // Sprite is very close to camera. // Just return a maximum bounding box. sbox.Set (-10000, -10000, 10000, 10000); } else { Perspective (cbox.Max (), oneCorner, fov, sx, sy); sbox.StartBoundingBox (oneCorner); csVector3 v (cbox.MinX (), cbox.MinY (), cbox.MaxZ ()); Perspective (v, oneCorner, fov, sx, sy); sbox.AddBoundingVertexSmart (oneCorner); Perspective (cbox.Min (), oneCorner, fov, sx, sy); sbox.AddBoundingVertexSmart (oneCorner); v.Set (cbox.MaxX (), cbox.MaxY (), cbox.MinZ ()); Perspective (v, oneCorner, fov, sx, sy); sbox.AddBoundingVertexSmart (oneCorner); } return cbox.MaxZ (); }
//---------------------------------------------------------------------------- void GL_Reshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); // 產生 projection 矩陣,此處為產生正投影矩陣 g_mxProjection = Perspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 1000.0); // 設定棋盤的 Projection Matrix g_pFloor->SetProjectionMatrix(g_mxProjection); g_pLight->SetProjectionMatrix(g_mxProjection); g_pAimal[0]->SetProjectionMatrix(g_mxProjection); glClearColor( 0.0, 0.0, 0.0, 1.0 ); // black background glEnable(GL_DEPTH_TEST); }
void Camera::reset() { m_theta = m_angle; m_phi = 0.0f; m_radius = m_initRadius; m_at = vec4(0.0f, 0.0f, 0.0f, 1.0f); m_up = vec4(0.0f, 1.0f, 0.0f, 0.0f); m_moveHor = 0.0f; m_moveVert = 0.0f; m_aspect = m_initAspect; m_perspective = Perspective(m_fovy, m_aspect, m_zNear, m_zFar); }
int main() { int Error(0); glm::mat4 Matrix(1); glm::vec3 Scale; glm::quat Orientation; glm::vec3 Translation; glm::vec3 Skew(1); glm::vec4 Perspective(1); glm::decompose(Matrix, Scale, Orientation, Translation, Skew, Perspective); return Error; }
void reshape(int width, int height) { WINDOW_HEIGHT = height; WINDOW_WIDTH = width; // mat4 projection = Ortho(proj_left, proj_right, proj_bottom, proj_top, // proj_znear, proj_zfar); mat4 projection = Perspective(45.0f, 1.0f * WINDOW_WIDTH / WINDOW_HEIGHT, proj_znear, proj_zfar); glUniformMatrix4fv(projection_loc, 1, GL_TRUE, projection); glViewport(0, 0, width, height); glutPostRedisplay(); }
void Camera::UpdateProjection() const { if (isOrtho_) { if (!hasUserOrthoProjection_) orthoProjection_ = CalculateOrthoProjection(zNear_, zFar_); matProjection_ = Ortho(orthoProjection_.left_, orthoProjection_.right_, orthoProjection_.bottom_, orthoProjection_.top_, orthoProjection_.near_, orthoProjection_.far_); } else { CHECK_ASSERT(zNear_ > 0); matProjection_ = Perspective(fovy_, aspectRatio_, zNear_, zFar_); } SetUniformsNeedUpdate(); }