Tải bản đầy đủ (.pdf) (8 trang)

Bài giải bài tập Đồ họa máy tính 02

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (173.08 KB, 8 trang )

Faculty of Computer Science and Engineering
Department of Computer Science

Bài tập 2
1/
a)
// left top
setViewport(0, 320, 320, 640);
setCamera(0, 1, 0, 1);
drawFigure();

// left bottom
setViewport(0, 320, 0, 320);


setCamera(1, 0, 0, 1);
drawFigure();

// right bottom
setViewport(320, 640, 0, 320);
setCamera(0, 1, 0, 1);
drawFigure();

// right top
setViewport(320, 640, 320, 640);
setCamera(1, 0, 0, 1);
drawFigure();


b)
// left top
setViewport(0, 320, 320, 640);
setCamera(0, 1, 0, 1);
drawFigure();

// left bottom
setViewport(0, 320, 0, 320);
setCamera(1, 0, 0, 1);
drawFigure();


// right bottom
setViewport(320, 640, 0, 320);
setCamera(0, 1, 0, 1);
drawFigure();
Faculty of Computer Science and Engineering
Department of Computer Science


// right top
setViewport(320, 640, 320, 640);
setCamera(1, 0, 0, 1);
drawFigure();



// left top
setViewport(160, 320, 320, 480);
setCamera(0, 1, 0, 1);
drawFigure();

// left bottom
setViewport(160, 320, 160, 320);
setCamera(1, 0, 0, 1);
drawFigure();


// right bottom
setViewport(320, 480, 160, 320);
setCamera(0, 1, 0, 1);
drawFigure();

// right top
setViewport(320, 480, 320, 480);
setCamera(1, 0, 0, 1);
drawFigure();

c)
// left top

setViewport(0, 320, 320, 640);
setCamera(0, 1, 0, 1);
drawFigure();

setViewport(0, 320, 320, 640);
setCamera(1, 0, 0, 1);
drawFigure();


// left bottom
setViewport(0, 320, 0, 320);
setCamera(1, 0, 0, 1);

Faculty of Computer Science and Engineering
Department of Computer Science

drawFigure();

setViewport(0, 320, 0, 320);
setCamera(0, 1, 0, 1);
drawFigure();

// right bottom
setViewport(320, 640, 0, 320);
setCamera(0, 1, 0, 1);

drawFigure();

setViewport(320, 640, 0, 320);
setCamera(1, 0, 0, 1);
drawFigure();


// right top
setViewport(320, 640, 320, 640);
setCamera(1, 0, 0, 1);
drawFigure();


setViewport(320, 640, 320, 640);
setCamera(0, 1, 0, 1);
drawFigure();


// left top
setViewport(160, 320, 320, 480);
setCamera(0, 1, 0, 1);
drawFigure();

// left bottom
setViewport(160, 320, 160, 320);

setCamera(1, 0, 0, 1);
drawFigure();

// right bottom
setViewport(320, 480, 160, 320);
setCamera(0, 1, 0, 1);
drawFigure();

// right top
setViewport(320, 480, 320, 480);
Faculty of Computer Science and Engineering
Department of Computer Science


setCamera(1, 0, 0, 1);
drawFigure();

2/
void drawPlatonic( ){
GLfloat t = 0.618;
GLfloat v0[3] = {0,1,t};
GLfloat v1[3] = {0,1,-t};
GLfloat v2[3] = {1,t,0};
GLfloat v3[3] = {1,-t,0};
GLfloat v4[3] = {0,-1,-t};

GLfloat v5[3] = {0,-1,t};
GLfloat v6[3] = {t,0,1};
GLfloat v7[3] = {-t,0,1};
GLfloat v8[3] = {t,0,-1};
GLfloat v9[3] = {-t,0,-1};
GLfloat v10[3] = {-1,t,0};
GLfloat v11[3] = {-1,-t,0};

glBegin(GL_TRIANGLE_FAN);
glVertex3fv(v0);
glVertex3fv(v1);
glVertex3fv(v2);

glVertex3fv(v6);
glVertex3fv(v7);
glVertex3fv(v10);
glVertex3fv(v1);
glEnd( );

glBegin(GL_TRIANGLE_FAN);
glVertex3fv(v4);
glVertex3fv(v8);
glVertex3fv(v3);
glVertex3fv(v5);
glVertex3fv(v11);

glVertex3fv(v9);
glVertex3fv(v8);
glEnd( );

glBegin(GL_TRIANGLE_STRIP);
glVertex3fv(v1);
Faculty of Computer Science and Engineering
Department of Computer Science

glVertex3fv(v8);
glVertex3fv(v2);
glVertex3fv(v3);

glVertex3fv(v6);
glVertex3fv(v5);
glVertex3fv(v7);
glVertex3fv(v11);
glVertex3fv(v10);
glVertex3fv(v9);
glVertex3fv(v1);
glVertex3fv(v8);
glEnd( );

}




3/
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#define M_PI 3.1415926
int nSlice, nStack;
float deltaPhi, deltaTheta;
void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

float c = M_PI/180.0;
float phir, phir20, thetar;
float x, y, z;
for(float phi = -90+deltaPhi; phi<90 - deltaPhi; phi+=deltaPhi){
phir = c*phi;
phir20 = c*(phi+deltaPhi);
glBegin(GL_QUAD_STRIP);
for(float theta = -180; theta<=180; theta+=deltaTheta){
thetar = c*theta;
x = sin(thetar)*cos(phir);
z = cos(thetar)*cos(phir);
y = sin(phir);

glVertex3d(x, y, z);
Faculty of Computer Science and Engineering
Department of Computer Science

x = sin(thetar)*cos(phir20);
z = cos(thetar)*cos(phir20);
y = sin(phir20);
glVertex3d(x, y, z);
}
glEnd();
}


float c80;
glBegin(GL_TRIANGLE_FAN);
glVertex3d(0, 1, 0);
c80 = c*(90-deltaPhi);
y = sin(c80);
for(float theta = 180; theta>=-180; theta-=deltaTheta){
thetar = c*theta;
x = sin(thetar)*cos(c80);
z = cos(thetar)*cos(c80);
glVertex3d(x, y, z);
}
glEnd();

glBegin(GL_TRIANGLE_FAN);
glVertex3d(0, -1, 0);
c80 = c*(90-deltaPhi);
y = -sin(c80);
for(theta = -180; theta<=180; theta+=deltaTheta){
thetar = c*theta;
x = sin(thetar)*cos(c80);
z = cos(thetar)*cos(c80);
glVertex3d(x, y, z);
}
glEnd();
glFlush();

}

void myinit(){
glClearColor (1.0, 1.0, 1.0, 1.0);
glColor3f (0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
}

void main(int argc, char **argv){
Faculty of Computer Science and Engineering

Department of Computer Science

cout << "Input number of Stack: ";
cin >> nStack;
cout << "Input number of Slice: ";
cin >> nSlice;
deltaPhi = 180.0/nStack;
deltaTheta = 360.0/nSlice;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("sphere");

myinit();
glutDisplayFunc(display);
glutMainLoop();
}



4/
/*
* nSlice: số lượng các tam giác tạo ra 2 mặt đáy hình tròn
* r : bán kính hình tròn đáy
* height: chiều cao hình trụ

* */
void DrawCylinder(int nSlice,GLfloat r,GLfloat height){
GLfloat a = PI*2/nSlice;

glBegin(GL_TRIANGLE_FAN);
glVertex3f(0,0,0);
for(int i = 0;i < nSlice;i++){
glVertex3f(r*cos(i*a),0,r*sin(i*a));
glVertex3f(r*cos((i+1)*a),0,r*sin((i+1)*a));
}
glEnd( );


glBegin(GL_TRIANGLE_FAN);
glVertex3f(0,height,0);
for(int i = 0;i < nSlice;i++){
glVertex3f(r*cos(i*a),height,r*sin(i*a));
glVertex3f(r*cos((i+1)*a),height,r*sin((i+1)*a));
}
glEnd( );

Faculty of Computer Science and Engineering
Department of Computer Science

glBegin(GL_QUAD_STRIP);

for(int i = 0;i < nSlice;i++){
glVertex3f(r*cos(i*a),0,r*sin(i*a));
glVertex3f(r*cos(i*a),height,r*sin(i*a));
}
glEnd( );

}

×