Professional Documents
Culture Documents
MỤC LỤC
1. Chương trình lab02transform.cpp biến đổi các đối tượng...........................................1
2. Chương trình lab02rotation01.cpp xoay hình vuông một góc 20 o. Tâm xoay là gốc
tọa độ.................................................................................................................................3
3. Chương trình lab02rotation02.cpp xoay hình vuông một góc 20o. Tâm xoay là
điểm(x, y)..........................................................................................................................3
4. Chương trình lab02rotation03.cpp vẽ hình chữ nhật quay quanh tâm.......................4
5. Chương trình lab02affine.cpp: Xây dựng các hàm biến đổi affine.............................6
6. Chương trình lab02earth.cpp vẽ hình trái đất quay xung quanh mặt trời.................9
7. Bài tập.............................................................................................................................11
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glMatrixMode(GL_MODELVIEW); // To operate on Model-View matrix
glLoadIdentity(); // Reset the model-view matrix
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(width >= height) {
// aspect >= 1, set the height from -1 to 1, with larger width
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
} else {
// aspect < 1, set the width to -1 to 1, with larger height
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
}
} reshape
void myInit(void){
glClearColor(0.7f,0.7f,0.7f,0.7f); //To nen mau xam
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
glMatrixMode(GL_MODELVIEW);
}
void myDisplay(void){
int x=50, y=40;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(200,100,400,300);
glColor3f(1.0f,1.0f,0.0f);
glPointSize(4.);
glColor3f(0.0f,0.0f,1.0f);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("HO VA TEN - Modelview matrix");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
#include <GL/glut.h>
#include <GL/gl.h>
static GLfloat spin = 0.0; // Goc quay hien tai cua hinh chu nhat
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0); // Xoay mot goc spin quanh truc z
glColor3f(1.0, 1.0, 1.0); // Mau ve cho hcn(mau trang)
glRectf(-25.0, -25.0, 25.0, 25.0); // ve hcn
glPopMatrix();
glutSwapBuffers(); // Hoan doi 2 buffer
}
void spinDisplay(void)
{
spin = spin + 2.0; // Xoay them 2 deg cho moi lan lap
if(spin > 360.0) spin = spin - 360.0;
glutPostRedisplay(); // Thuc hien viec ve lai
}
5. Chương trình lab02affine.cpp: Xây dựng các hàm biến đổi affine
#include <stdlib.h>
//#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <assert.h>
#include <stdio.h>
class Point;
class Vector;
/***********************
* Basic Point and Vector Classes with some standard functions
class Vector {
public:
float dx, dy;
Vector() : dx(0), dy(0) { }
Vector(Point& p, Point& q) {
dx = q.x - p.x;
dy = q.y - p.y;
}
//=====================================================
// Draw a ray from start to end Include arrow and tick marks
void drawRay(Point start, Point end, int ticks) {
// First draw the line segment itself
glBegin(GL_LINES);
{
glVertex2f(start.x, start.y);
glVertex2f(end.x, end.y);
}
glEnd();
}
void printMatrix() {
float m[16];
glGetFloatv(GL_MODELVIEW_MATRIX, m);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
printf("After Identity\n");
printMatrix(); printf("\n");
glPushMatrix();
intensity -= 0.1;
glColor3f(intensity, intensity, intensity);
glTranslated(0, 2, 0.0);
printf("After Now Translating Again...\n");
printMatrix(); printf("\n");
drawCoordinateFrame(); drawHouse();
intensity -= 0.1;
glColor3f(intensity, intensity, intensity);
glRotated(30, 0, 0, 1); // 30 degrees about origin(z-axis)
glTranslated(0, 5, 0);
glScaled(1.2, 0.5, 1);
printf("After Rotate, Translate, Scale\n");
printMatrix(); printf("\n");
drawCoordinateFrame(); drawHouse();
glPopMatrix();
glPushMatrix();
intensity -= 0.1;
glColor3f(intensity, intensity, intensity);
glRotated(-30, 0, 0, 1); // 30 degrees about origin(z-axis)
glTranslated(0, 5, 0);
glScaled(1.2, 0.5, 1);
drawCoordinateFrame(); drawHouse();
glPopMatrix();
intensity -= 0.1;
glColor3f(intensity, intensity, intensity);
shearMatrix(1, 0);
drawCoordinateFrame(); drawHouse();
printf("At the End\n");
printMatrix(); printf("\n");
void display() {
// Set the viewport to the full screen!
glViewport(0, 0, WIN_SIZE, WIN_SIZE);
glFlush();
glutSwapBuffers(); // needed for double buffering!
}
6. Chương trình lab02earth.cpp vẽ hình trái đất quay xung quanh mặt
trời
#include "Gl/glut.h"
static int year = 0, day = 0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
//Loai bo mot phan cua doi tuong bi che boi doi tuong khac
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
}
void display(void)
{
// xoa color buffer va depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); // luu lai ma tran hien hanh
glColor3f(1.0, 0, 0); // thiet lap mau ve la mau do
// quay mot goc tuong ung voi thoi gian trong nam
glRotatef((GLfloat) year, 0.0, 1.0, 0.0);
// tinh tien den vi tri hien tai cua trai dat tren quy dao quanh mat troi
glTranslatef(2.0, 0.0, 0.0);
// quay trai dat tuong ung voi thoi gian trong ngay
glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
glColor3f(0, 0, 1.0); // thiet lap mau ve la mau blue
glutWireSphere(0.2, 10, 8); // ve trai dat
// phuc hoi lai ma tran hien hanh cu: tuong ung voi quay lai vi tri ban dau
glPopMatrix();
glutSwapBuffers();
}
7. Bài tập
Cho đường thẳng PQ có P(1, 2, 0), Q(7, 14). Lập trình biểu diễn các phép biến đổi
đường thẳng PQ thông qua điều khiển bàn phím như sau:
1) Nhấn phím t, T: Dịch chuyển PG một đoạn theo khoảng cách(5, 0, 0)
2) Nhấn phím r, R: Quay PG một góc quanh trục Oy một góc 30o
3) Nhấn phím p, P: Lấy đối xứng của PG qua trục Ox
4) Nhấn phím f, F: Lấy đối xứng của PG qua trục MN
5) Nhấn phím h, H: Quay PG quanh trục MN một góc 30o
------------------------------------------------
// Điểm P và Q
GLfloat P[] = {1.0f, 2.0f, 0.0f};
GLfloat Q[] = {7.0f, 14.0f, 0.0f};
// Điểm PG ban đầu là Q
GLfloat PG[] = {7.0f, 14.0f, 0.0f};
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
KhoaCNTT – Trường ĐHBK Đà Nẵng 14
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glPointSize(5.0);
glutDisplayFunc(draw);
glutKeyboardFunc(keyCallback);
glutMainLoop();
return 0;
}