Professional Documents
Culture Documents
LabDHMT ViewTransformation
LabDHMT ViewTransformation
1. labViewCamera.cpp...................................................................................................1
2. labflyCamera..............................................................................................................1
1. labViewCamera.cpp
Minh họa cách thay đổi vị trí quan sát (eye/camera) một đối tượng sử dụng hàm
gluLookAt()
#include<windows.h>
#include<cstdio>
#include<cmath>
#include<GL/glut.h>
void highTriangle()
{
glBegin(GL_LINE_LOOP);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0,0.0,0.0);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,1.0,0.0);
glVertex3f(0.0,0.0,1.0);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glColor3f(1.0,1.0,0.0);
glTranslated(5.0,1.0,5.0);
glRotatef(camera_angle,1.0,0.0,0.0);
glutWireCube(5.0);
glPopMatrix();
glPushMatrix();
glColor3f(0.0,1.0,1.0);
glTranslated(-8.0,0.0,8.0);
glScalef(5.0,5.0,5.0);
glutSwapBuffers();
}
void spinDisplay(void)
{
camera_angle = camera_angle + 1.0;
if(camera_angle > 360.0) camera_angle = camera_angle - 360.0;
glutPostRedisplay();
}
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); // black color
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70,1,0.1,100.0);
}//init
2. labflyCamera
File camera.h
#include "GL/glut.h"
#include "math.h"
#include <iostream>
class Camera{
private:
Point3 eye;
Vector3 u,v,n;
double viewAngle, aspect, nearDist, farDist; // view volume shape
void setModelViewMatrix(); // tell OpenGL where the Camera is
public:
Camera(){};
//Camera(){u.set(1, 0, 0); v.set(0, 1, 0); n.set(0, 0, 1);};
// default constructor
void set(Point3 eye, Point3 look, Vector3 up); // like gluLookAt()
void roll(float angle); // roll it
void pitch(float angle); // increase pitch
void Camera :: setShape(float vAng, float asp, float nearD, float farD)
{ // set shape of the view volume and set up the projection matrix
viewAngle = vAng; aspect = asp; nearDist = nearD; farDist = farD;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(viewAngle, aspect, nearDist, farDist);
}
File flyCamera.cpp
/* flyCamera.cpp
* This program demonstrates the application of "flying" a camera
around a teapot.
*/
#include <fstream>
#include <cmath>
#include <gl/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "Camera.h"