Professional Documents
Culture Documents
Huong Dan Code Va Bai Tap
Huong Dan Code Va Bai Tap
*********************
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;
}
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);
}
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);
}
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;
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);
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>
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();
}