Bài giải bài tập Đồ họa máy tính
Trang 1Solution Tut 4
Câu 1
Gọi
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
D
là ma trận của phép biến đối xứng qua trục Oy
T(0,1)Rz(450)DRz(-450)T(0,-1)
0 1 0 1
1 0 0 1
0 0 1 0
0 0 0 1
Câu 2
Tịnh tiến ABCD sao cho điểm A tiến về gốc toạ độ O
1
1 0 0 6
0 1 0 5 ( )
0 0 1 0
0 0 0 1
Biến đổi kích thước hình chữ nhật từ 6x3 về hình chữ nhật kích thước 3x4
2
2 4
3 4
( 6 , 3 ,1) 3
Thực hiện phép trượt đối với hình chữ nhật theo chiều Oy để nó trở thành hình bình hành
3
1 0 0 0
1 1 0 0
3
0 0 1 0
0 0 0 1
Tịnh tiến hình bình hành sao cho đỉnh A từ O tiến về vị trí đỉnh A‘ như kết quả
Trang 21 0 0 0
0 1 0 1 (0, 1, 0)
0 0 1 0
0 0 0 1
Kết quả như sau:
1 0 0 0
3
2
20
Câu 3
Chương trình hoàn chỉnh như sau
Sử dụng hàm OnDisplay_cau3a và OnDisplay_cau3b để hiển thị ra kết quả của câu 3a, 3b
#include <Windows.h>
#include <gl/GL.h>
#include <gl/glut.h>
#include <math.h>
#define SPACE 0.3
int moving, startx, starty;
GLfloat angle = 0; /* in degrees */
GLfloat angle2 = 0; /* in degrees */
void drawCube() {
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0.1f, 0.1f, 0.1f);
glVertex3f(0.1f, -0.1f, 0.1f);
glVertex3f(0.1f, 0.1f, -0.1f);
glVertex3f(0.1f, -0.1f, -0.1f);
glVertex3f(-0.1f, 0.1f, -0.1f);
glVertex3f(-0.1f, -0.1f, -0.1f);
glVertex3f(-0.1f, 0.1f, 0.1f);
glVertex3f(-0.1f, -0.1f, 0.1f);
glVertex3f(0.1f, 0.1f, 0.1f);
glVertex3f(0.1f, -0.1f, 0.1f);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0.1f, 0.1f, 0.1f);
glVertex3f(0.1f, 0.1f, -0.1f);
Trang 3glVertex3f(-0.1f, 0.1f, 0.1f);
glVertex3f(-0.1f, 0.1f, -0.1f);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0.1f, -0.1f, 0.1f);
glVertex3f(0.1f, -0.1f, -0.1f);
glVertex3f(-0.1f, -0.1f, 0.1f);
glVertex3f(-0.1f, -0.1f, -0.1f);
glEnd();
}
void OnDisplay_cau3a(){
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
//glDisable(GL_COLOR_LOGIC_OP);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4, 4, -4, 4, -100, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle2, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
float fScale;
for(int i = 0; i<9; i++)
{
glPushMatrix();
glColor3f(1.0f, 0.0f, 0.0f);
//Ve hinh lap phuong tren truc Ox glTranslated(SPACE*i, 0, 0);
drawCube();
//Ve canh chu V trong mat phang xOy fScale = 1;
glColor3f(0.0f, 1.0f, 0.0f);
for(int j = 0; j< 8 -i ; j++) {
glPushMatrix();
glTranslated(0, SPACE*(j+1), 0); drawCube();
glPopMatrix();
} //Ve canh chu V trong mat phang xOz fScale = 1;
glColor3f(0.0f, 0.0f, 1.0f);
for(int j = 0; j< 8 -i ; j++) {
glPushMatrix();
glTranslated(0, 0, SPACE*(j+1)); drawCube();
glPopMatrix();
} glPopMatrix();
}
glutSwapBuffers();
}
Trang 4void OnDisplay_cau3b(){
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
//glDisable(GL_COLOR_LOGIC_OP);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4, 4, -4, 4, -100, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle2, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
float fScale = 1.0f;
float fRotate = 0.0f;
glColor3f(0.0f, 0.0f, 0.0f);
drawCube();
for(int i = 1; i<10; i++)
{
fScale *= 0.9f;
fRotate += 22.5f;
glPushMatrix();
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glTranslatef(-SPACE*i, 0.0f, 0.0f);
glRotatef(fRotate, 1.0f, 0.0f, 0.0f);
glScalef(fScale, fScale, fScale);
drawCube();
glPopMatrix();
glPushMatrix();
glTranslatef(SPACE*i, 0.0f, 0.0f);
glRotatef(fRotate, 1.0f, 0.0f, 0.0f);
glScalef(fScale, fScale, fScale);
drawCube();
glPopMatrix();
glColor3f(0.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(0.0f, -SPACE*i, 0.0f);
glRotatef(fRotate, 0.0f, 1.0f, 0.0f);
glScalef(fScale, fScale, fScale);
drawCube();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, SPACE*i, 0.0f);
glRotatef(fRotate, 0.0f, 1.0f, 0.0f);
glScalef(fScale, fScale, fScale);
drawCube();
glPopMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -SPACE*i);
glRotatef(fRotate, 0.0f, 0.0f, 1.0f);
glScalef(fScale, fScale, fScale);
drawCube();
glPopMatrix();
Trang 5glPushMatrix();
glTranslatef(0.0f, 0.0f, SPACE*i);
glRotatef(fRotate, 0.0f, 0.0f, 1.0f);
glScalef(fScale, fScale, fScale);
drawCube();
glPopMatrix();
glPopMatrix();
}
glutSwapBuffers();
}
void OnMouseMove(int , int )
{
if (moving) {
angle = angle + (x - startx)*0.1;
angle2 = angle2 + (y - starty)*0.1;
startx = x; starty = y; glutPostRedisplay();
}
}
void OnMouse(int button, int state, int , int ) {
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
moving = 1;
startx = x; starty = y; }
if (state == GLUT_UP) {
moving = 0;
} }
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(800, 800);
glutCreateWindow("simple");
glutDisplayFunc(OnDisplay_cau3b);
glutMouseFunc(OnMouse);
glutMotionFunc(OnMouseMove);
glutMainLoop();
}
Câu 4
5 glTranslatef(3f, 2f, 1f);
6 glRotatef(60f, 0f, 0f, 1f);
7 glScalef(2.5f, 1f, 1.5f);
8 glRotatef(90f, 0f, 0f, 1f);
Rz(900)S(2.5, 1, 1.5)Rz(600)T(3,2,1)
Trang 60 0
2
0.5 1.25 3 0 2
Câu 5
#include <Windows.h>
#include <gl/GL.h>
#include <gl/glut.h>
#include <math.h>
int iWnd;
float sqrt2 = sqrt(2.0);
struct SPOSITION {
float x, y;
SPOSITION(float , float ):x(x), y(y) {}
};
struct SCOLOR {
float r, g, b;
SCOLOR(int , int , int ): r(r/255.0f), g(g/255.0f), b(b/255.0f) {} };
void drawSquare(float , float , float ) {
glColor3f(r, g, b);
glBegin(GL_POLYGON);
glVertex2f(-.5f, 5f);
glVertex2f(-.5f, -.5f);
glVertex2f(.5f, -.5f);
glVertex2f(.5f, 5f);
glEnd();
}
void drawSquareAt(SPOSITION origin, SCOLOR color, float edge) {
glPushMatrix();
glTranslatef(origin.x, origin.y, 0);
glRotatef(45, 0, 0, 1);
glScalef(edge, edge, edge);
glPolygonMode(GL_FRONT, GL_FILL);
drawSquare(color.r, color.g, color.b);
glPolygonMode(GL_FRONT, GL_LINE);
drawSquare(0, 0, 0);
glPopMatrix();
}
#define ORANGE SCOLOR(230, 130, 0)
#define SKY SCOLOR(160, 215, 225)
#define VIOLET SCOLOR(190, 180, 210)
#define PINK SCOLOR(225, 170, 160)
#define YELLOW SCOLOR(255, 255, 0)
Trang 7#define BROWN SCOLOR(170, 75, 65)
void drawTessellation(int row=6, int col=8) {
for (int x=0; x<col*2; x++)
for (int y=0; y<row*2; y++) {
if (x==0 || y==0) continue;
//ORANGE
if (x%4 == 2 && y %2 == 1)
drawSquareAt(SPOSITION(x, y), ORANGE, sqrt2); //SKY
if (x%4 == 1 && y %4 == 2)
drawSquareAt(SPOSITION(x, y), SKY, sqrt2); //VIOLET
if (x%4 == 3 && y %4 == 2)
drawSquareAt(SPOSITION(x, y), VIOLET, sqrt2); //PINK
if (x%4 == 0 && y %2 == 1 && y!=1 && y!= row*2-1)
drawSquareAt(SPOSITION(x, y), PINK, sqrt2); //YELLOW
if (x%4 == 3 && y %4 == 0 && x!= col*2-1)
drawSquareAt(SPOSITION(x, y), YELLOW, sqrt2); //BROWN
if (x%4 == 1 && y %4 == 0 && x!=1)
drawSquareAt(SPOSITION(x, y), BROWN, sqrt2); }
}
void drawGrid() {
for (int i=0; i< 50; i++) {
glBegin(GL_QUAD_STRIP);
for (int j=0; j< 50; j++) {
glVertex2i(i, j);
glVertex2i(i+1, j);
} glEnd();
}
}
void OnDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 20, -5, 20, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawTessellation();
//drawGrid();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(400, 400);
iWnd = glutCreateWindow("simple");
glutDisplayFunc(OnDisplay);
glutMainLoop();
}