Professional Documents
Culture Documents
ng V n c
dvduc@ioit.ac.vn
H N I 2007/08
M CL C
Ci t OpenGL .................................................................................... 3
Bi I. H th ng h a my tnh ........................................................... 4
Bi II. Cc thu t ton c s v h a hai chi u.................................. .8
Bi III. Thu c tnh hnh v ................................................................. 13
Bi IV. Bi n i hnh h c hai ba chi u ............................................ 16
Bi V. Quan st trong khng gian ba chi u ........................................ 18
Bi VI. M hnh ha b m t v t th .................................................... 21
Bi VII. Lo i b m t khu t ................................................................. 25
Bi VIII. Chi u sng v t bng ......................................................... 32
dvduc-2007/08 1
2 dvduc-2007/08
Ci t OpenGL
I. Visual C/C++ 6.0
dvduc-2007/08 3
BI I. H th ng h a
Th d 1.1
V i m nh trn mn hnh.
#include <gl/glut.h>
#include <gl/gl.h>
void myDisplay(void) {
glClearColor(1.,1.,1.,1.);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.,0.,0.);
//glPointSize(12.0);
glBegin(GL_POINTS);
glVertex2i(0,0);
glEnd();
glFlush();
}
void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100,150);
glutInitWindowSize(640,480);
glutCreateWindow("Th d 1.1");
glutDisplayFunc(myDisplay);
glutMainLoop();
}
Th d 1.2
L p trnh nh n cc phm chu t v bn phm.
#include <gl/glut.h>
#include <gl/gl.h>
#include <stdio.h>
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
glFlush();
}
Th d 1.3
Hi n th xu k t .
#include <gl/glut.h>
#include <gl/gl.h>
#include "string.h"
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3ub(255, 0, 0);
bitmap_output(0,0,0, "Hello OpenGL!", GLUT_BITMAP_TIMES_ROMAN_24);
glFlush();
}
dvduc-2007/08 5
void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow("Thi du 1.3");
glutDisplayFunc(myDisplay);
glutMainLoop();
}
Th d 1.4
V i m nh t i v tr nh n phm tri chu t.
#include <windows.h>
#include <math.h>
#include <gl/glut.h>
#include <gl/gl.h>
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void myInit(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0f,1.0f,1.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
Bi t p 1.1
V i m nh trn c a s khi di v nh n phm chu t, v i mu C cho tr c.
dvduc-2007/08 7
Bi II. Cc thu t ton v c s
Th d 2.1
Vi t ch ng trnh v o n th ng v hnh trn b ng OpenGL.
#include <windows.h>
#include <math.h>
#include <gl/glut.h>
#include <gl/gl.h>
#define PI 3.14159265
class GLintPoint {
public:
GLint x, y;
};
void myDisplay(void) {
glClearColor(1.,1.,1.,1.);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.,0.,0.);
drawLine(100, 150, 500, 200);
glColor3f(0.,0.,1.);
GLintPoint point;
point.x = 300;
point.y = 200;
8 dvduc-2007/08
drawCircle(point, 100.0);
glFlush();
}
Th d 2.2
V o n th ng c h s gc t 0 n 1 b ng thu t ton trung i m.
#include <gl/glut.h>
#include <gl/gl.h>
class GLintPoint {
public:
GLint x, y;
};
dvduc-2007/08 9
x++;
}
else
{ // select NE
d+=incrNE;
x++; y++;
}
drawPoint(x,y);
}
}
void myDisplay(void) {
glClearColor(1.,1.,1.,1.);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.,0.,0.);
midPointLine(100, 200, 400, 300);
glFlush();
}
Th d 2.3
S d ng chu t v ng g p khc b ng OpenGL
#include <windows.h>
#include <math.h>
#include <gl/glut.h>
#include <gl/gl.h>
class GLintPoint {
public:
GLint x, y;
};
10 dvduc-2007/08
void drawLine(GLint x1, GLint y1, GLint x2, GLint y2) {
glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();
glFlush();
}
if (startflag == 1) {
List[0].x = x;
List[0].y = y;
startflag = 0;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
List[1].x = x;
List[1].y = y;
drawLine(List[0].x, screenHeight - List[0].y, List[1].x, screenHeight - List[1].y);
List[0].x = List[1].x;
List[0].y = List[1].y;
}
}
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
dvduc-2007/08 11
Bi t p 2.1
V ng trn b ng thu t ton trung i m v i bn knh v tm cho tr c.
Bi t p 2.2
Vi t ch ng trnh v ng g p khc b ng thu t ton trung i m d ng chu t ch n t a u
mt o n th ng.
12 dvduc-2007/08
Bi III. Thu c tnh hnh v
Th d 3.1
V hnh v i ng vi n c thu c tnh (mu, r ng v nt t)
#include <gl/glut.h>
#include <gl/gl.h>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(12.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LINE_STIPPLE);
glLineStipple(4,0x5555);
glLineWidth(4.);
glBegin(GL_LINE_LOOP);
glVertex2i(100,50);
glVertex2i(100,130);
glVertex2i(150,130);
glEnd();
glFlush();
}
Th d 3.2
V a gic t theo m u.
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
void myInit(void)
dvduc-2007/08 13
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
GLubyte mask[]={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_POLYGON_STIPPLE);
glPolygonStipple(mask);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(40,40);
glVertex2f(220,40);
glVertex2f(220,200);
glVertex2f(130,270);
glVertex2f(40,220);
glVertex2f(40,40);
glEnd();
glFlush();
}
14 dvduc-2007/08
Bi t p 3.1
V hnh khp kn v i cc m u t nh sau:
dvduc-2007/08 15
Bi IV. Bi n i hnh h c
Th d 4.1
Xoay hnh vung m t gc 20 . Tm xoay l g c t a .
#include <gl/glut.h>
#include <gl/gl.h>
void myInit(void) {
glClearColor(0.7f, 0.7f, 0.7f, 0.0f); // t nen mu xm
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode(GL_MODELVIEW);
}
void myDisplay(void) {
int x=320, y=240;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x-100, y-100, x+100, y+100);
glColor3f(1.0f,1.0f,0.0f);
Th d 4.2
Xoay hnh vung m t gc 20 , tm xoay t i v tr (x, y).
#include <gl/glut.h>
#include <gl/gl.h>
void myInit(void) {
glClearColor(0.7f, 0.7f, 0.7f, 0.0f); //Nen mu xm
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode(GL_MODELVIEW);
16 dvduc-2007/08
}
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();
}
Bi t p 4.1
V hnh vung. Nh n phm tri chu t, hnh vung xoay xung quanh chng theo chi u ng c kim
ng h . Nh n phm ph i chu t d ng xoay.
Bi t p 4.2
Cho tam gic A(3, 1), B(1, 3), C(3,3).
1. Hy xc nh t a m i c a cc nh tam gic sau khi xoay m t gc 900 ng c chi u kim
ng h xung quanh i m P(2, 2).
2. Phng to tam gic ln hai l n, gi nguyn v tr c a i m C. Tnh t a cc nh tam gic
sau khi bi n hnh.
dvduc-2007/08 17
Bi V. Quan st ba chi u
Th d 5.1
Hi n th l p ph ng t i m quan st cho tr c, s d ng chi u tr c giao.
#include <gl/glut.h>
#include <gl/gl.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-3.2, 3.2, -2.4, 2.4, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(8, 10, 7, 0, 0, 0, 1, 0, 0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glutWireCube(1); // Ve lap phuong
glFlush();
}
Th d 5.2
Hi n th l p ph ng t i m quan st cho tr c, s d ng chi u ph i c nh.
#include <gl/glut.h>
#include <gl/gl.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}
18 dvduc-2007/08
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,6,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 10, 0, 0, 0, 0, 1, 0, 0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(45, 1, 1, 1);
glutWireCube(1);
glPopMatrix();
glFlush();
}
Th d 5.3
V l p ph ng trong kh i quan st ph i c nh t ng qut (Frustum).
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
}
void myDisplay(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt(2.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutWireCube (1.0);
dvduc-2007/08 19
glFlush ();
}
Bi t p 5.1
Bi t p 5.2
Hi n th l p ph ng t i m quan st, s d ng chi u ph i c nh. Nh n phm tri chu t thay i
gc quan st fovy.
Bi t p 5.3
V cnh tay Robot b ng hm glutWireCube(). Nh n phm s (shoulder) v e (elbow) xoay cnh
tay robot.
20 dvduc-2007/08
Bi VI. M hnh ha ba chi u
Th d 6.1
V ng cong Bzier v i 4 i m i u khi n nh sau:
{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},{2.0, -4.0, 0.0}, {4.0, 4.0,0.0}
GLfloat ctrlpoints[4][3] = {
{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
{2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
}
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
/* The following code displays the control points as dots. */
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < 4; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
glFlush();
}
dvduc-2007/08 21
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Thi Du 6_1");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Th d 6.2
V m t cong Bzier v i t p i u khi n cho tr c.
#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
GLfloat ctrlpoints[4][4][3] = {
{{-1.5, -1.5, 4.0}, {-0.5, -1.5, 2.0},
{0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}},
{{-1.5, 0.5, 4.0}, {-0.5, 0.5, 0.0},
{0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}},
{{-1.5, -0.5, 1.0}, {-0.5, -0.5, 3.0},
{0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}},
{{-1.5, 1.5, -2.0}, {-0.5, 1.5, -2.0},
{0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}}};
void display(void)
{
int i, j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPushMatrix ();
glRotatef(85.0, 1.0, 1.0, 1.0);
for (j = 0; j <= 8; j++) {
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);
glEnd();
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0);
glEnd();
}
glPopMatrix ();
glFlush();
}
22 dvduc-2007/08
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &
&ctrlpoints[0][0][0]);
ctrlpoints[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
}
int main(in
main(intt argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Thi du 6.2");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Th d 6.3
Gi i:
P v Q l cc ng cong b c 3:
Bi u di n b ng ma tr n ng cong Q nh sau:
V0
V
Q(t ) (1 t ) 3 3t (1 t ) 2 3t 2 (1 t ) t 3 1
V2
V3
Vc t ti p tuy n (l y o hm theo t)
V0
V1
Q ' (t ) 3 (1 t ) 2 3(3t 2 4t 1) 3t (3t 2) 3t 2
V2
V3
Phn o n th nh t t i cu i ng cong, v i u=1, P (u) v phn o n th 2 t i u ng
cong, v i t=0, Q (0)
A D
B E
P ' (1) 0 0 3 3 3( D C ) Q ' (0) 3 3 0 0 3( E D)
C F
D G
Bi t p 6.1
V m t cong Bzier v i t p i u khi n cho tr c nh trong th d 6.2. B sung vi c thay it a
i u khi n b ng bn phm.
Bi t p 6.2
M t ng cong Bzier b c 3 c b n i m i u khi n P0=(2, 2, 0), P1=(4, 2, 2), P2=(8, 6, 4),
P3=(12, 0, 0). Gi s R(t) l ng cong Bzier b c hai lin t c c p C1 t i P3 c a ng cong trn.
ng th i gi s R0, R1 v R2 l cc i m i u khi n c a ng cong ny. Hy xc nh R0 v R1.
24 dvduc-2007/08
Bi VII. Lo i b m t khu t
Th d 7.1
V cc kh i a di n b ng OpenGL.
#include <gl/glut.h>
#include <gl/gl.h>
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0., 0., 1.);
glFlush();
}
dvduc-2007/08 25
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("Thi du 7.1");
glutDisplayFunc(myDisplay);
init();
glutReshapeFunc(reshapeFunc);
glutMainLoop();
}
Th d 7.2
V v t mu l p ph ng v i cc nh cho tr c v b sung cc phm x, y xoay l p ph ng.
float angle_x=0.0;
float angle_y=0.0;
void drawcube(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, .0, 0.0);
glMatrixMode(GL_MODELVIEW);
glRotatef(angle_y, 0.0, 1.0, 0.0);
glRotatef(angle_x, 1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3iv(a);
glVertex3iv(d);
glVertex3iv(c);
glVertex3iv(b);
glEnd();
glColor3f(0.0, 1.0, .0);
glBegin(GL_POLYGON);
glVertex3iv(a);
glVertex3iv(b);
glVertex3iv(f);
glVertex3iv(e);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3iv(d);
glVertex3iv(h);
glVertex3iv(g);
glVertex3iv(c);
glEnd();
26 dvduc-2007/08
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3iv(e);
glVertex3iv(f);
glVertex3iv(g);
glVertex3iv(h);
glEnd();
glColor3f(1.0, .0, 1.0);
glBegin(GL_POLYGON);
glVertex3iv(e);
glVertex3iv(h);
glVertex3iv(d);
glVertex3iv(a);
glEnd();
glColor3f(0.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3iv(b);
glVertex3iv(c);
glVertex3iv(g);
glVertex3iv(f);
glEnd();
glFlush();
}
void myDisplay(void)
{
glClearColor(1., 1., 1., 1.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt(2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
drawcube();
glFlush ();
}
dvduc-2007/08 27
case 'Y':
angle_y+=10.;
glutPostRedisplay();
break;
case 'i':
case 'I':
angle_x=0.;
angle_y=0.;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
}
}
Th d 7.3
V v t mu kh i a di n v i cc nh cho tr c v b sung cc phm x, y xoay kh i a di n.
float angle_x=0.0;
float angle_y=0.0;
void drawcube(void) {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glRotatef(angle_y, 0.0, 1.0, 0.0);
glRotatef(angle_x, 1.0, 0.0, 0.0);
// ----------------------------
glColor3f(1.0, 0.0, 0.0);//red
glBegin(GL_POLYGON);
glVertex3iv(d);
glVertex3iv(h);
28 dvduc-2007/08
glVertex3iv(g);
glVertex3iv(c);
glEnd();
glColor3f(0.0, 1.0, 0.0); // green
glBegin(GL_POLYGON);
glVertex3iv(e);
glVertex3iv(f);
glVertex3iv(g);
glVertex3iv(h);
glEnd();
glColor3f(0.0, 0.0, 1.0);//blue
glBegin(GL_POLYGON);
glVertex3iv(b);
glVertex3iv(c);
glVertex3iv(g);
glVertex3iv(f);
glEnd();
glColor3f(1.0, .0, 1.0); // magenta
glBegin(GL_TRIANGLES);
glVertex3iv(a);
glVertex3iv(d);
glVertex3iv(b);
glEnd();
glColor3f(0.0, 1.0, 1.0);//cyan
glBegin(GL_TRIANGLES);
glVertex3iv(d);
glVertex3iv(c);
glVertex3iv(b);
glEnd();
glColor3f(1.0, 1.0, 0.0);//yellow
glBegin(GL_TRIANGLES);
glVertex3iv(e);
glVertex3iv(h);
glVertex3iv(d);
glEnd();
glColor3f(0.0, .0, 0.0);//black
glBegin(GL_TRIANGLES);
glVertex3iv(e);
glVertex3iv(d);
glVertex3iv(a);
glEnd();
glColor3f(0.7, 0.7, 0.7);//white
glBegin(GL_TRIANGLES);
glVertex3iv(e);
glVertex3iv(a);
glVertex3iv(b);
glEnd();
glColor3f(.0, .50, .50);//
glBegin(GL_TRIANGLES);
glVertex3iv(e);
glVertex3iv(b);
glVertex3iv(f);
glEnd();
glFlush();
}
void myDisplay(void)
dvduc-2007/08 29
{
glClearColor(1., 1., 1., 1.0);
glClear (GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
Bi t p 7.1
V v t mu hnh chp ch nh t v i cc nh nh sau:
a={1.0,0.0,0.0}, b={0.0,0.0,
b={0.0,0.0,--1.0}, c={-1.0,0.0,1.0},
1.0,0.0,1.0}, d={0.0,0.0,1.0}, e={0.0,1.0,0.0};
B sung phm x, y vv z xoay hnh chpchp.
Bi t p 7.2
V v t mu hnh chp ch nh t v i cc nh nh sau:
a={
a={2.0,0.0,0.0},
.0,0.0,0.0}, b={0.0,0.0,-
b={0.0,0.0,-2.0}, c={-2.0,0.0,
.0,0.0,2.0},
.0}, d={0.0,0.0,
d={0.0,0.0,2.0},
.0}, e={0.0,2.0,0.0};
.0,0.0};
V l p ph ng v i kch th c c nh ll 2.
B sung phm x, y vv z xoay hnh chp
chp.
Bi t p 7.3
Cho it ng nh hnh
nh d i y. Hy
H xc nh cc m t nhn
nhn th y t i m P=(3, 3, -3).
3).
Bi VIII. Chi u sng v t bng
Th d 8.1
Vi t ch ng trnh chi u sng hnh c u, s d ng hm glutSolidSphere() t m t ngu n sng.
void init(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
Bi t p 8.1
Vi t ch ng trnh chi u sng hnh xuy n, s d ng hm glutSolidTorus(). B sung chu t thay
i v tr chi u sng.
Bi t p 8.2
M t m t ph ng ch nh t t o b i A(0,0), B(1,0), C(1,1) v D(0,1).
Hy tnh c ng ph n chi u t i i m P(0.5, 0.5) b ng k thu t t bng Gauraud. Bi t tr c
c ng trung bnh c a nh sng ph n chi u t i b n nh l: IA=8, IB=9, IC=2, ID=4.
dvduc-2007/08 33