Professional Documents
Culture Documents
Lab 16
Lab 16
Lab 16
Sources:
#include <cmath>
#include <vector>
#include <windows.h>
#include <gl/glut.h>
class Point {
public:
double x, y, z;
Point() {}
~Point() {}
double dot(Point v) {
return x * v.x + y * v.y + z * v.z;
}
Point operator*(double v) {
return Point(x * v, y * v, z * v);
}
Point operator*(Point v) {
return Point(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
Point normalize() {
return *this / sqrt(x * x + y * y + z * z);
}
};
Point pos(0, 0, -150);
Point u(0, 1, 0);
Point r(1, 0, 0);
Point l(0, 0, 1);
if (it > 0) {
drawKoch(x1, y1, x3, y3, it - 1);
drawKoch(x3, y3, x, y, it - 1);
drawKoch(x, y, x4, y4, it - 1);
drawKoch(x4, y4, x2, y2, it - 1);
} else {
glBegin(GL_LINES); {
glVertex3f(x1, y1, 0);
glVertex3f(x3, y3, 0);
glVertex3f(x3, y3, 0);
glVertex3f(x, y, 0);
glVertex3f(x, y, 0);
glVertex3f(x4, y4, 0);
glVertex3f(x4, y4, 0);
glVertex3f(x2, y2, 0);
}
glEnd();
}
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(pos.x, pos.y, pos.z, pos.x + l.x, pos.y + l.y, pos.z + l.z, u.x, u.y,
u.z);
glMatrixMode(GL_MODELVIEW);
glColor3f(0, 0, 1.0);
drawKoch(-50, 0, 50, 0, iterations);
glutSwapBuffers();
}
void init() {
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80, 1, 1, 1000.0);
}
init();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutMainLoop();
return 0;
Bài tập:
- Thay đổi các cấp hiển thị đường cong
Sources:
#include <GL/glut.h>
#include <complex>
using std::complex;
// The number of times to iterate before we assume that a point isn't in the
// Mandelbrot set.
// (You may need to turn this up if you zoom further into the set.)
int iterations = 0;
while( abs( z ) < 2.0 && iterations < MAX_ITERATIONS )
z = ( z * z ) + c;
++iterations;
glVertex2i( x, y );
}
else
glVertex2i( x, y );
glEnd();
}
void display()
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glutCreateWindow( "Mandelbrot" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
Bài 3:
Sources:
#include <GL/glut.h>
#include <stdio.h>
/*
*/
struct Type_rgb{
float r;
float g;
float b;
};
/*
* value.
*/
/*
*/
void mandelbrotset()
/*
* of iteration
printf("\nstart");
x = 0;
y = 0;
iteration = 0;
max_iteration = 1000;
/*
*/
y = (2*x*y) + y0;
x = xtemp;
iteration = iteration + 1;
/*
*to black.
*/
pixels[loc].r = 0;
pixels[loc].g = 0;
pixels[loc].b = 0;
}else{
/*
*/
pixels[loc].r = pattern[iteration].r;
pixels[loc].g = pattern[iteration].g;
pixels[loc].b = pattern[iteration].b;
loc = loc + 1;
void Init( )
/*
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
int i;
float r, g, b;
/*
*/
pixels[i].r = 1;
pixels[i].g = 1;
pixels[i].b = 1;
i = 0;
/*
*/
pattern[i].g = r;
pattern[i].b = g;
i++;
/*
* condition is 1000.
*/
pattern[i].r = 1;
pattern[i].g = 1;
pattern[i].b = 1;
mandelbrotset();
void onDisplay()
/*
*/
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
/*
*/
glutSwapBuffers();
/*
*/
glutInit(&argc, argv);
Init ();
/*
*/
glutDisplayFunc(onDisplay);
/*
*/
glutMainLoop();
return 0;
#include <cmath>
#include <vector>
#include <windows.h>
#include <gl/glut.h>
#include <iostream>
Point() {}
~Point() {}
double dot(Point v) {
return x * v.x + y * v.y + z * v.z;
}
Point operator*(double v) {
return Point(x * v, y * v, z * v);
}
Point operator*(Point v) {
return Point(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
Point normalize() {
return *this / sqrt(x * x + y * y + z * z);
}
};
if (it > 0) {
drawKoch(x1, y1, x3, y3, it - 1);
drawKoch(x3, y3, x, y, it - 1);
drawKoch(x, y, x4, y4, it - 1);
drawKoch(x4, y4, x2, y2, it - 1);
} else {
glBegin(GL_LINES); {
glVertex3f(x1, y1, 0);
glVertex3f(x3, y3, 0);
glVertex3f(x3, y3, 0);
glVertex3f(x, y, 0);
glVertex3f(x, y, 0);
glVertex3f(x4, y4, 0);
glVertex3f(x4, y4, 0);
glVertex3f(x2, y2, 0);
}
glEnd();
}
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(pos.x, pos.y, pos.z, pos.x + l.x, pos.y + l.y, pos.z + l.z, u.x,
u.y, u.z);
glMatrixMode(GL_MODELVIEW);
// Tô màu và v? đư?ng cong Koch v?i h?nh d?ng khác
glColor3f(1.0, 0, 0);
drawKoch(-100, 0, 100, 0, iterations);
glColor3f(0, 0, 1.0);
drawKoch(0, 150, -100, 0, iterations);
glutSwapBuffers();
}
void init() {
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80, 1, 1, 1000.0);
}
glutInit(&argc, argv);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("My OpenGL Program");
init();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
2) Viết chương trình nhập n và vẽ đường cong C cấp n.
Source:
#include <GL/glut.h>
#include <stdio.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
int iter;
zx = new_zx;
zy = new_zy;
if (iter == MAX_ITER) {
} else {
glVertex2f(x, y);
glEnd();
glFlush();
void init() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Mandelbrot Set");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
5) Viết chương trình sinh đường Pythagoras.
Source:
#include <GL/glut.h>
#include <math.h>
glBegin(GL_POLYGON);
glVertex2f(x, y);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glEnd();
if (depth == 0) return;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
void init() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Pythagoras Tree");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
6) Viết chương trình sinh tập Julia.
Source:
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
int iter;
glVertex2f(x, y);
glEnd();
glFlush();
void init() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Julia Set");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
7) Viết chương trình vẽ các đường cong tô vùng: Phoenix, Hilbert, Sierpinxki.
Source:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
if (iter <= 0) {
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
return;
}
void drawHilbert(int x, int y, int size, int iter, int direction) {
if (iter <= 0) {
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
return;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
drawPhoenix(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2,
WINDOW_WIDTH / 4, 8);
glPointSize(1.0);
drawHilbert(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2,
WINDOW_WIDTH / 4, 5, 1);
glPointSize(1.0);
drawSierpinski(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2,
WINDOW_WIDTH / 4, 8);
glFlush();
void init() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Tong hop");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;