Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

THỰC HÀNH ĐỒ HỌA MÁY TÍNH

BIẾN ĐỔI HỆ QUAN SÁT (VIEW TRANSFORMATION)

Họ và tên Sinh viên: Hoàng Trung Mã Sinh viên: 102220214


Họ và tên Sinh viên: Nguyễn Ngọc Hải Mã Sinh viên: 102220144
Họ và tên Sinh viên: Hồ Văn Khải Mã Sinh viên: 102220193

Nhóm: 22NH13 (STT: 25)

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>

using namespace std;

double camera_angle = 0.0;


#define PI 3.1415926535897932

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();

double eyex = 25.0*cos(camera_angle*PI/180.0);

KhoaCNTT – Trường ĐHBK 1


double eyez = 25.0*sin(camera_angle*PI/180.0);
/*gluLookAt(
eyex,25.0,-eyez,
0.0,0.0,0.0,
0.0,1.0,0.0
);
*/
gluLookAt(
25.0,25.0,-25.0,
0.0,0.0,0.0,
0.0,1.0,0.0
);
//camera_angle = camera_angle + 1.0; // advance camera by 5 degree;
//if(camera_angle > 360.0) camera_angle = 0.0;

//write display codes


glMatrixMode(GL_MODELVIEW);
glColor3f(0.3,0.3,0.3);
#define EXTX 14
#define EXTZ 14
glBegin(GL_LINES);
double x = -(double)EXTX;
for(int i = 1; i<=2*EXTX+1; i++)
{
glVertex3f(x,0.0,-15.0);
glVertex3f(x,0.0,15.0);
x += 1.0;
}
glEnd();
glBegin(GL_LINES);
double z = -(double)EXTZ;
for(int i = 1; i<=2*EXTZ+1; i++)
{
glVertex3f(-15.0,0.0,z);
glVertex3f(15.0,0.0,z);
z += 1.0;
}
glEnd();
glColor3f(1.0,1.0,1.0); // white
glBegin(GL_LINE_LOOP);
glVertex3f(-15.0,0.0,15.0);
glVertex3f(15.0,0.0,15.0);
glVertex3f(15.0,0.0,-15.0);
glVertex3f(-15.0,0.0,-15.0);
glEnd();

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);

KhoaCNTT – Trường ĐHBK 2


glRotatef(camera_angle,0.0,0.0,1.0);
highTriangle();
glPopMatrix();

glutSwapBuffers();
}

void spinDisplay(void)
{
camera_angle = camera_angle + 1.0;
if(camera_angle > 360.0) camera_angle = camera_angle - 360.0;
glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)


{
switch(button)
{
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN) glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
if(state==GLUT_DOWN) glutIdleFunc(NULL);
break;
default:
break;
}
}//mouse

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

int main(int argc, char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500,500);
glutInitWindowPosition (0, 0);
glutCreateWindow("Hello 3D");
init(); // init display modes
glutDisplayFunc(display); // display update
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}//end

2. labflyCamera
 File camera.h
#include "GL/glut.h"
#include "math.h"
#include <iostream>

KhoaCNTT – Trường ĐHBK 3


// << Support Classes for Camera >>
//////////// Point3 class
class Point3{
public:
float x,y,z;
void set(float dx, float dy, float dz){x = dx; y = dy; z = dz;}
void set(Point3& p){x = p.x; y = p.y; z = p.z;}
Point3(float xx, float yy, float zz){x = xx; y = yy; z = zz;}
Point3(){x = y = z = 0;}
void build4tuple(float v[])
{// load 4-tuple with this point: v[3] = 1 for homogeneous
v[0] = x; v[1] = y; v[2] = z; v[3] = 1.0f;
}
};

//////////// Vector3 class


class Vector3{
public:
float x,y,z;
void set(float dx, float dy, float dz){ x = dx; y = dy; z = dz;}
void set(Vector3& v){ x = v.x; y = v.y; z = v.z;}
void flip(){x = -x; y = -y; z = -z;} // reverse this vector
void setDiff(Point3& a, Point3& b)//set to difference a - b
{ x = a.x - b.x; y = a.y - b.y; z = a.z - b.z;}
void normalize()//adjust this vector to unit length
{
double sizeSq = x * x + y * y + z * z;
if(sizeSq < 0.0000001)
{
std::cerr << "\nnormalize() sees vector (0,0,0)!";
return; // does nothing to zero vectors;
}
float scaleFactor = 1.0/(float)sqrt(sizeSq);
x *= scaleFactor; y *= scaleFactor; z *= scaleFactor;
}
Vector3(float xx, float yy, float zz){x = xx; y = yy; z = zz;}
Vector3(Vector3& v){x = v.x; y = v.y; z = v.z;}
Vector3(){x = y = z = 0;} //default constructor
Vector3 cross(Vector3 b) //return this cross b
{
Vector3 c(y*b.z - z*b.y, z*b.x - x*b.z, x*b.y - y*b.x);
return c;
}
float dot(Vector3 b) // return this dotted with b
{return x * b.x + y * b.y + z * b.z;}
};

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

KhoaCNTT – Trường ĐHBK 4


void yaw(float angle); // yaw it
void slide(float delU, float delV, float delN); // slide it
void setShape(float vAng, float asp, float nearD, float farD);
};

void Camera :: setModelViewMatrix(void)


{ // load modelview matrix with existing Camera values
float m[16];
Vector3 eVec(eye.x, eye.y, eye.z); // a vector version of eye
m[0] = u.x; m[4] = u.y; m[8] = u.z; m[12] = -eVec.dot(u);
m[1] = v.x; m[5] = v.y; m[9] = v.z; m[13] = -eVec.dot(v);
m[2] = n.x; m[6] = n.y; m[10] = n.z; m[14] = -eVec.dot(n);
m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1.0;
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(m); // load OpenGL's modelview matrix
}

void Camera :: set(Point3 Eye, Point3 look, Vector3 up)


{ // create a modelview matrix and send it to OpenGL
Vector3 t;
eye.set(Eye); // store the given eye position
n.set(eye.x - look.x, eye.y - look.y, eye.z - look.z); // make n
//t.set(up.y*n.z - up.z*n.y, up.z*n.x - up.x*n.z, up.x*n.y -
up.y*n.x);
u.set(up.y*n.z - up.z*n.y, up.z*n.x - up.x*n.z, up.x*n.y -
up.y*n.x);
//Vector3 t(up.cross(n));
//u.set(t); // make u = up X n
//u.set(up.cross(n)); // make u = up X n
n.normalize(); u.normalize(); // make them unit length
v.set(n.y*u.z - n.z*u.y, n.z*u.x - n.x*u.z, n.x*u.y - n.y*u.x);
//t = up.cross(n);
//v.set(t); // make v = n X u
//v.set(n.cross(u)); // make v = n X u
setModelViewMatrix(); // tell OpenGL
}

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);
}

void Camera :: slide(float delU, float delV, float delN)


{
eye.x += delU * u.x + delV * v.x + delN * n.x;
eye.y += delU * u.y + delV * v.y + delN * n.y;
eye.z += delU * u.z + delV * v.z + delN * n.z;
setModelViewMatrix();
}

void Camera :: roll(float angle)


{ // roll the camera through angle degrees
float cs = cos( angle / 180 * 3.14159265 );
float sn = sin( angle / 180 * 3.14159265 );
Vector3 t = u; // remember old u
u.set(cs*t.x - sn*v.x, cs*t.y - sn*v.y, cs*t.z - sn*v.z );
v.set(sn*t.x + cs*v.x, sn*t.y + cs*v.y, sn*t.z + cs*v.z );
setModelViewMatrix();
}

KhoaCNTT – Trường ĐHBK 5


void Camera :: pitch(float angle)
{ // pitch the camera through angle degrees
float cs = cos( angle / 180 * 3.14159265 );
float sn = sin( angle / 180 * 3.14159265 );
Vector3 t = n; // remember old n
n.set(cs*t.x + sn*v.x, cs*t.y + sn*v.y, cs*t.z + sn*v.z );
v.set(-sn*t.x + cs*v.x, -sn*t.y + cs*v.y, -sn*t.z + cs*v.z );
setModelViewMatrix();
}

void Camera :: yaw(float angle)


{ // yaw the camera through angle degrees
float cs = cos( angle / 180 * 3.14159265 );
float sn = sin( angle / 180 * 3.14159265 );
Vector3 t = u; // remember old n
u.set(cs*t.x + sn*n.x, cs*t.y + sn*n.y, cs*t.z + sn*n.z );
n.set(-sn*t.x + cs*n.x, -sn*t.y + cs*n.y, -sn*t.z + cs*n.z );
setModelViewMatrix();
}

 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"

Camera cam; // global camera object

void myKeyboard(unsigned char key, int x, int y)


{ //use 'F', 'ctrl-F', 'P' and 'ctrl-P' to control the camera
switch(key)
{
// controls for camera
case 'F': cam.slide(0, 0, 0.2);
break; // slide camera forward
case 'F'-64: cam.slide(0,0,-0.2);
break; //slide camera back
// add up/down control
case 'P': cam.pitch(-1.0);
break;
case 'P' - 64: cam.pitch( 1.0);
break;
// add roll control
case 'A': cam.roll(-1.0);
break;
case 'A' - 64: cam.roll( 1.0);
break;
// add left/right control
case 'M': cam.yaw(-1.0);
break;
case 'M' - 64: cam.yaw( 1.0);

KhoaCNTT – Trường ĐHBK 6


break;
}
glutPostRedisplay(); // draw it again
}
//<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>>>>>>>>>>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutWireTeapot(1.0); // draw the teapot
glFlush();
glutSwapBuffers(); // display the screen just made
}

void myMouse(int button, int state, int x, int y)


{
switch (button) {
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
exit (-1);
break;
default:
break;
}
}

//<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


int main(int argc, char **argv)
{
Point3 eye, look;
Vector3 up;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // double buffering
glutInitWindowSize(640,480);
glutInitWindowPosition(50, 50);
glutCreateWindow("Fly a camera around a teapot");
//
glClearColor(1.0f,1.0f,1.0f,1.0f); // background is white
glColor3f(0.0f,0.0f,0.0f); // set color of stuff
glViewport(0, 0, 640, 480);
eye.set(4, 4, 4);
//eye.set(4, 0, 4);
look.set(0, 0, 0);
up.set(0, 1, 0);
cam.set(eye, look, up); // make the initial
camera
cam.setShape(30.0f, 64.0f/48.0f, 0.5f, 50.0f);
//
glutKeyboardFunc(myKeyboard);
glutMouseFunc(myMouse); // register myMouse
function
glutDisplayFunc(myDisplay);
glutMainLoop();
}//main

 Kết quả thực hiện


Nhấn các phím P, Ctrl+P, F, Ctrl+F, A, Ctrl+A, M, Ctrl+M.

KhoaCNTT – Trường ĐHBK 7


-----------------------------------------------

KhoaCNTT – Trường ĐHBK 8

You might also like