Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

BÀI THỰC HÀNH OPENGL

*********************

Sinh viên thực hiện các bài tập dưới đây bằng cách gõ các đoạn code trong phần
CÁC VÍ DỤ THAM KHẢO và xem kết quả. Sau đó sinh viên sẽ tự thực hành các bài
tập sau
Bài tập 1: Sinh viên vẽ và tô màu ngôi sao năm cánh.
Bài tập 2: Sinh viên vẽ ngôi nhà có ống khói và cửa ra vào, cửa sổ.
Bài tập 3: SInh viên vẽ hình số 8 là hai đường tròn giao nhau, mỗi đường là 1 màu
Bài tập 4: Sinh viên vẽ hình Hangman, có cử động 2 tay khi chọn phím tương ứng:
S  tay trái đưa lên; X tay trái đưa xuống;
K  tay phải đưa lên và M  tay phải đưa xuống.
CÁC VÍ DỤ THAM KHẢO

#include <GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex3f (200.0, 120.0, 0.0);
glVertex3f (440.0, 120.0, 0.0);
glVertex3f (440.0, 360.0, 0.0);
glVertex3f (200.0, 360.0, 0.0);
glEnd();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

Sau đó, tô màu đỏ cho tam giác


#include <GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(5.0);
glBegin(GL_TRIANGLES);
glVertex3f(160.0, 120.0, 0.0);
glVertex3f(480.0, 160.0, 0.0);
glVertex3f(320.0, 320.0, 0.0);
glEnd();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Bài tập 3: Tô màu cho tam giác như hình sau:
#include <GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}

// draw a square at (x, y) having the size of edges is 'a'


void glSquare(GLint x, GLint y, GLint a)
{
glBegin(GL_POLYGON);
glVertex2i(x, y);
glVertex2i(x + a, y);
glVertex2i(x + a, y + a);
glVertex2i(x, y + a);
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
GLint size = 200; // edge size at 200 pixels
GLint x = (screenWidth-size)/2; // horizontal center
GLint y = (screenHeight-size)/2; // vertical center
glSquare(x, y, size);

glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
void glRect(GLint x, GLint y, GLint width, GLint height)
{
glBegin(GL_POLYGON);
glVertex2i(x, y);
glVertex2i(x + width, y);
glVertex2i(x + width, y + height);
glVertex2i(x, y + height);
glEnd();
}
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
GLint w = 400, h = 200; // width = 400, height = 200 pixels
GLint x = (screenWidth-w)/2; // horizontal center
GLint y = (screenHeight-h)/2; // vertical center
glRect(x, y, w, h);
glFlush();
}
#include <GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;

void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glShadeModel(GL_SMOOTH);
}
void DoubleTriangles()
{
glBegin(GL_TRIANGLES);
glVertex2i(50, 50);
glVertex2i(200, 50);
glVertex2i(100, 150);

glVertex2i(300,100);
glVertex2i(450,150);
glVertex2i(350,250);
glEnd();
}

void display()
{

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
DoubleTriangles();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
#include <GL/glut.h>
#include <math.h>
const int screenWidth = 640;
const int screenHeight = 480;
const double R = 150;
const double pi = 3.141592654;

typedef struct GLStarPoint {


GLfloat x, y;
} GLStarPoint;

void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glShadeModel(GL_SMOOTH);
}

void display()
{
GLStarPoint V1, V2, V3, V4, V5, V0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(6.0);
glBegin(GL_POINTS);
V0.x = screenWidth/2; V0.y = screenHeight/2;
V1.x = V0.x; V1.y =V0.y + R;
V2.x = V0.x + R*sin(2*pi/5);
V2.y = V0.y + R*cos(2*pi/5);
V3.x = V0.x + R*sin(pi/5);
V3.y = V0.y - R*cos(pi/5);
V4.x = V0.x - R*sin(pi/5);
V4.y = V0.y - R*cos(pi/5);
V5.x = V0.x - R*sin(2*pi/5);
V5.y = V0.y + R*cos(2*pi/5);

// Ve cac dinh cua ngoi sao


glVertex2i(V1.x, V1.y);
glVertex2i(V2.x, V2.y);
glVertex2i(V3.x, V3.y);
glVertex2i(V4.x, V4.y);
glVertex2i(V5.x, V5.y);
glEnd();
glFlush();
}

void reshape(int w, int h)


{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
}
#include <GL/glut.h>
#include <math.h>
const int screenWidth = 640;
const int screenHeight = 480;
const double pi = 3.141592654;

static GLfloat spin = 0.0;


void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glShadeModel(GL_SMOOTH);
}

void spinDisplay(void)
{
spin = spin +2.0;
if (spin >360.0) spin = spin - 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;
}
}
void display()
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 1.0);
glPushMatrix();
glRotatef(spin,0.0,0.0,1.0);
glRectf(-25.0,-25.0,25.0,25.0);
glPopMatrix();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

BÀI TẬP: Chúng ta xét ví dụ về xây dựng mô hình Trái Đất quay xung quanh Mặt Trời

#include <GL/glut.h>
#include <math.h>

static int year = 0, day = 0;

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f (1.0, 0, 0);
glutWireSphere(1.0, 20, 16);
glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
glTranslatef (2.0, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glColor3f (0, 0, 1.0);
glutWireSphere(0.2, 10, 8);
glPopMatrix();
glutSwapBuffers();
}

void reshape (int w, int h)


{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

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


{
switch (key)
{
case 'd':
day = (day + 10) % 360;
glutPostRedisplay();
break;
case 'D':
day = (day - 10) % 360;
glutPostRedisplay();
break;
case 'y':
year = (year + 5) % 360;
glutPostRedisplay();
break;
case 'Y':
year = (year - 5) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

You might also like