Rubic - Tam giac - Ngoi nha - Lục lăng xoay
1. Rubic
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
// ve 8 canh rubic co pha 3 mau
#include <GL/glut.h>
/*
* định nghĩa các thông số */
#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 500
#define WINDOW_X 100
#define WINDOW_Y 100
#define WINDOW_TITLE "RGB-ColorCube"
#define FOV_ANGLE 30
#define CENTER_X 0.0
#define CENTER_Y 0.0
#define CENTER_Z 0.0
#define VIEWER_X 0.0
#define VIEWER_Y 0.0
#define VIEWER_Z -2.1
#define UP_X 0.0
#define UP_Y 1.0
#define UP_Z 0.0
#define CLIPPLANE_NEAR 1.0
#define CLIPPLANE_FAR 20.0
#define ROTATION_SPEED 2.0
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
GLfloat rot_x = 0.0, rot_y = 0.0;
GLfloat saved_x, saved_y;
/* * các màu sắc sử dụng */
GLfloat red[4] = {.8, 0.0, 0.0, 1.0};
GLfloat white[4] = {.8, .8, .8, 1.0};
/*Các phương thức */
void usage(void);
void draw_scene(void);
void draw_object(void);
void init(int argc, char **argv, void (*draw)(void));
void save_position(int button, int state, int x, int y);
struct point get_coords(double a, double b);
void vertex(double a, double b);
void rotate(int x, int y);
void set_color(int angle);
int main(int argc, char **argv) {
init(argc, argv, draw_scene);
return 0;
}
void draw_scene(void) {
static GLfloat old_rot_matrix[16];
static int initialized = 0;
GLfloat new_rot_matrix[16];
/* tính toán vị trí mới của ma trận */
glPushMatrix();
glLoadIdentity();
glRotatef(rot_x, 1.0, 0.0, 0.0);
glRotatef(rot_y, 0.0, 1.0, 0.0);
glGetFloatv(GL_MODELVIEW_MATRIX, new_rot_matrix);
glPopMatrix();
glPushMatrix();
glLoadIdentity();
glMultMatrixf(new_rot_matrix);
if (initialized) {
glMultMatrixf(old_rot_matrix);
}
glGetFloatv(GL_MODELVIEW_MATRIX, old_rot_matrix);
initialized = 1;
glPopMatrix();
glPushMatrix();
glMultMatrixf(old_rot_matrix);
draw_object();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
/* * nhận thư viện GL */
void init(int argc, char **argv, void (*draw)(void)) {
GLfloat light0_pos[] = {100.0, 100.0, 100.0, 1.0};
GLfloat light0_color[] = {1.0, 1.0, 1.0, 1.0};
GLfloat ambient_light[] = {0.8, 0.8, 0.8, 1.0};
glutInit(&argc, argv);
/* Create a window */
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(WINDOW_X, WINDOW_Y);
glutCreateWindow(WINDOW_TITLE);
glClearColor(0.0, 0.0, 0.0, 0.0);
/* Set up some light sources */
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_light);
glEnable(GL_LIGHT0);
/* glEnable(GL_LIGHTING); */
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
/* Create a viewing frustum */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(FOV_ANGLE, WINDOW_WIDTH/WINDOW_HEIGHT, CLIPPLANE_NEAR,
CLIPPLANE_FAR);
gluLookAt(VIEWER_X, VIEWER_Y, -VIEWER_Z, CENTER_X, CENTER_Y, CENTER_Z,
UP_X, UP_Y, UP_Z);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(VIEWER_X, VIEWER_Y, VIEWER_Z);
glutDisplayFunc(draw);
glutMouseFunc(save_position);
glutMotionFunc(rotate);
glutMainLoop();
}
/* * lưu vị trí của điểm chuột */
void save_position(int button, int state, int x, int y) {
if (state == GLUT_DOWN) {
saved_x = x;
saved_y = y;
}}
/* * thực hiện vòng quay */
void rotate(int x, int y) {
rot_y = (GLfloat)(x - saved_x) * ROTATION_SPEED;
rot_x = (GLfloat)(y - saved_y) * ROTATION_SPEED;
saved_x = x;
saved_y = y;
glutPostRedisplay();
}
/* Draw a object */
void draw_object(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUAD_STRIP);
glNormal3f(0,0,1);
glColor3f(0,0,0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3f(0,1,0);
glVertex3f(-0.5,0.5,-0.5);
glColor3f(1,0,0);
glVertex3f(0.5,-0.5,-0.5);
glColor3f(1,1,0);
glVertex3f(0.5,0.5,-0.5);
glNormal3f(1,0,0);
glColor3f(1,0,1);
glVertex3f(0.5,-0.5,0.5);
glColor3f(1,1,1);
glVertex3f(0.5,0.5,0.5);
glNormal3f(0,0,1);
glColor3f(0,0,1);
glVertex3f(-0.5,-0.5,0.5);
glColor3f(0,1,1);
glVertex3f(-0.5,0.5,0.5);
glNormal3f(-1,0,0);
glColor3f(0,0,0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3f(0,1,0);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glColor3f(0,1,0);
glVertex3f(-0.5,0.5,-0.5);
glColor3f(0,1,1);
glVertex3f(-0.5,0.5,0.5);
glColor3f(1,1,1);
glVertex3f(0.5,0.5,0.5);
glColor3f(1,1,0);
glVertex3f(0.5,0.5,-0.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0,-1,0);
glColor3f(0,0,0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3f(0,0,1);
glVertex3f(-0.5,-0.5,0.5);
glColor3f(1,0,1);
glVertex3f(0.5,-0.5,0.5);
glColor3f(1,0,0);
glVertex3f(0.5,-0.5,-0.5);
glEnd();
2. Tam giac
#include "stdafx.h"
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
#include <iostream>
float angle = 0.0;
void renderScene(void)
{
// thiet lap bit cho che do color depth de su dung double buffering
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// day camera vao chup
glPushMatrix();
// xoay goc nhin
glRotatef(angle, 0.0, 1.0, 0.0);
// bat dau ve tam giac
glBegin(GL_TRIANGLES);
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
// dua goc nhin cua camera ra ngoai man hinh
glPopMatrix();
// dich bit man hinh bang double buffering
glutSwapBuffers();
// thay doi goc quay
angle++;
}
void changeSize(int w, int h)
{
// neu chieu cao h = 0 thi coi nhu ko hop le
if(h == 0)
h = 1; // thiet lap lai it nhat la 1 don vi pixel
// tinh ti le chieu rong va chieu cao
float ratio = 1.0 * w / h;
// cai dat lai toa do diem truoc khi chinh sua
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// dat viewport (vi tri sua) vao toan bo cua so
glViewport(0, 0, w, h);
// chinh sua lai diem dat toa do va khung hinh
gluPerspective(45, ratio, 1, 1000);
// xac dinh che do nhin cua camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// cai dat goc nhin cho camera
gluLookAt(0.0, 0.0, 5.0,
0.0, 0.0, -1.0,
0.0f, 1.0f, 0.0f);
}
int main(int argc, char *argv[])
{
// khoi tao GLUT
glutInit(&argc, argv);
// khoi tao vi tri Window
glutInitWindowPosition(100, 100);
// khoi tao kich thuoc Window
glutInitWindowSize(640, 480);
// khoi tao che do hien thi
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
// tao tieu de cho Window
glutCreateWindow("Tam giac xoay");
// trieu hoi ham xu ly render
glutDisplayFunc(renderScene);
// goi ham Idle de tao animation
glutIdleFunc(renderScene);
// ve lai Window neu kich thuoc Window dc thay doi
glutReshapeFunc(changeSize);
// kich hoat depth testing de dung double buffering
glEnable(GL_DEPTH_TEST);
// giu chuong trinh luon luon hien thi
glutMainLoop();
return 0;
}
3. Ngôi nhà
#include "stdafx.h"
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
...
void myInit()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0f, 1.0f, 1.0f);
glPointSize(5.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myHouse()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);//khung ngôi nhà
glVertex2i(40, 40);
glVertex2i(40, 120);
glVertex2i(100, 180);
glVertex2i(160, 120);
glVertex2i(160, 40);
glEnd();
glBegin(GL_LINE_LOOP);//cua so
glVertex2i(110, 60);
glVertex2i(110, 100);
glVertex2i(150, 100);
glVertex2i(150, 60);
glEnd();
glBegin(GL_LINE_STRIP);//cua chinh
glVertex2i(50, 40);
glVertex2i(50, 110);
glVertex2i(100, 110);
glVertex2i(100, 40);
glEnd();
glBegin(GL_LINE_STRIP);//ông khói
glVertex2i(50, 130);
glVertex2i(50, 180);
glVertex2i(70, 180);
glVertex2i(70, 150);
glEnd();
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv); //initialize the tool kit
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB); //set the display mode
glutInitWindowSize(640, 480); //set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("My House");//open the screen window
glutDisplayFunc(myHouse);
myInit();
glutMainLoop();
return 0;
}
4. Lục lăng xoay
#include "stdafx.h"
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
void resize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(width <= height)
glOrtho(-1.0, 1.0,
-1.0 * (GLdouble)height
1.0 * (GLdouble)height
-1.0, 1.0);
else
glOrtho(-1.0 * (GLfloat)width
1.0 * (GLfloat)width
-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
void display(void)
{
static const double x[6] = {0.75, 0.375, -0.375, -0.75, -0.375, 0.375};
static const double y[6] = { 0.0, 0.65, 0.65, 0.0, -0.65, -0.65};
static double angle = 0.0;
int i, j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3d(0.0, 0.0, 0.0);
glPushMatrix();
glRotated(angle, 0, 0, 1.0);
glBegin(GL_LINE_LOOP);
for(i = 0; i < 6; i++) glVertex2d(x[i], y[i]);
glEnd();
glBegin(GL_LINES);
for(i = 0; i < 6; i++)
for(j = i + 2; j < i + 5; j++) {
glVertex2d(x[i], y[i]);
glVertex2d(x[j % 6], y[j % 6]);
}
glEnd();
glPopMatrix();
glutSwapBuffers();
angle += 1.0;
}
void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(320, 320);
glutInitWindowPosition(80, 80);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Truong Thanh Tuyen - Nguyen Ngoc Tu");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
Bạn đang đọc truyện trên: AzTruyen.Top