1.窗口界面的显示,400*300像素大小的窗口,四个角有四个红色的点。
#include "stdafx.h"
#include <gl/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(0, 0);
glVertex2i(0, 299);
glVertex2i(399, 0);
glVertex2i(399, 299);
glEnd();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
//对GLUT初始化,随后调用三个函数指定即将建立的推向视窗的特征
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//设置图形显示模式,要求系统为图像提供一个单独的RGB帧缓冲器
glutInitWindowSize(400, 300);
//设置图形大小400*300个像素点
glutInitWindowPosition(200, 100);
glutCreateWindow("sample");
glClearColor(0.0, 0.0, 0.0,0.0);
gluOrtho2D(0.0, 400.0, 0.0, 300.0);
//次函数为照相机取景框。
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
2.画一个心形线,其方程为r=1-cos0。
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <gl/glut.h>
#include <math.h>
GLfloat r(double angle)
{
return 1 - cos(angle);
}
void display(void)
{
double t, p = 3.141593 / 180;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
for (int i = 0; i < 360; i += 4) {
t = i*p;
glVertex2f(r(t)*cos(t), r(t)*sin(t));
}
glEnd();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
//对GLUT初始化,随后调用三个函数指定即将建立的推向视窗的特征
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//设置图形显示模式,要求系统为图像提供一个单独的RGB帧缓冲器
glutInitWindowSize(400, 300);
//设置图形大小400*300个像素点
glutInitWindowPosition(200, 100);
glutCreateWindow("sample");
glClearColor(0.0, 0.0, 0.0,0.0);
gluOrtho2D(-2.0, 2.0, -1.5, 1.5);
//次函数为照相机取景框。
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
并可以同在在glBegin函数前添加glPointSize(5.0);改变画笔像素宽度
通过添加reshape函数保持窗口比例变化时,图形比例不变。glViewport(0,0,width,height)是保持视区与调整后图像一致。
并在glutDisplayFunc(display)后面添加glutReshapeFunc(reshape);reshape函数还是比较好理解的,当比例不对时,重新设置其视区。
void reshape(GLsizei width, GLsizei height) {
float w_aspect = 4.0 / 3.0, aspect = ((float)width) / height;
if (aspect <= w_aspect)
glViewport(0, (height - width / w_aspect) / 2, width, width / w_aspect);
else glViewport((width - height*w_aspect) / 2, 0, height*w_aspect, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -1.5, 1.5);
}
4.Mandelbrot虫图像的显示:
原理呢,根据变换公式xn+1=xn^2+z,Mandelbrot集是指x从0起点利用公式变换不导致发散的全体负数z的集合。
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <gl/glut.h>
#include <math.h>
int width = 400;
int height = 400;
int Mandelbrot(double a, double b, int n) {
double x = 0, y = 0, xx = 0, yy = 0;
int i = 0;
while (i++ < n)
{
y = 2 * x*y + b;
x = xx - yy + a;
xx = x*x;
yy = y*y;
if ((xx + yy) > 4) return i;
}
return 0;
}
void display(void)
{
double min_a, max_a, min_b, max_b, step_b, step_a, a, b;
int c, n = 64;
float scale = 255.0 / n;
min_a = -2.0;
max_a = 0.5;
min_b = -1.25;
max_b = 1.25;
step_a = (max_a - min_a) / width;
step_b = (max_b - min_b) / height;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
a = min_a;
for (int x = 0; x < width; x++) {
b = min_b;
for (int y = 0; y < height; y++) {
c = 255- scale*Mandelbrot(a, b, n);
glColor3ub(c, c, c);
glVertex2i(x, y);
b += step_b;
}
a += step_a;
}
glEnd();
glFlush();
}
/*void reshape(GLsizei width, GLsizei height) {
float w_aspect = 4.0 / 3.0, aspect = ((float)width) / height;
if (aspect <= w_aspect)
glViewport(0, (height - width / w_aspect) / 2, width, width / w_aspect);
else glViewport((width - height*w_aspect) / 2, 0, height*w_aspect, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -1.5, 1.5);
}*/
int main(int argc,char**argv)
{
glutInit(&argc, argv);
//对GLUT初始化,随后调用三个函数指定即将建立的推向视窗的特征
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//设置图形显示模式,要求系统为图像提供一个单独的RGB帧缓冲器
glutInitWindowSize(400, 400);
//设置图形大小400*300个像素点
glutInitWindowPosition(200, 100);
glutCreateWindow("sample");
glClearColor(0.0, 0.0, 0.0,0.0);
gluOrtho2D(0.0, 400.0, 0.0, 400.0);
//次函数为照相机取景框。
glutDisplayFunc(display);
//glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
将z设为固定值,得到一个Julia集的不发散数。
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <gl/glut.h>
#include <math.h>
int width = 400;
int height = 300;
int Julia(double x, double y, int n)
{
double xx, yy, p = -0.7454, q = 0.1131;
int i = 0;
xx = x*x;
yy = y*y;
while (i++ < n) {
y = 2 * x*y + q;
x = xx - yy + p;
xx = x*x;
yy = y*y;
if ((xx + yy) > 4) return i;
}
return 0;
}
void display(void)
{
double min_a, max_a, min_b, max_b, step_b, step_a, a, b;
int c, n = 180;
float scale = 255.0 / n;
min_a = -1.6;
max_a = 1.6;
min_b = -1.2;
max_b = 1.2;
step_a = (max_a - min_a) / width;
step_b = (max_b - min_b) / height;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
a = min_a;
for (int x = 0; x < width; x++) {
b = min_b;
for (int y = 0; y < height; y++) {
c = 255- scale*Julia(a, b, n);
glColor3ub(c, c, c);
glVertex2i(x, y);
b += step_b;
}
a += step_a;
}
glEnd();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
//对GLUT初始化,随后调用三个函数指定即将建立的推向视窗的特征
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//设置图形显示模式,要求系统为图像提供一个单独的RGB帧缓冲器
glutInitWindowSize(width, height);
//设置图形大小400*300个像素点
glutInitWindowPosition(200, 100);
glutCreateWindow("sample");
glClearColor(0.0, 0.0, 0.0,0.0);
gluOrtho2D(0.0, 400.0, 0.0, 300.0);
//次函数为照相机取景框。
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
当然可以添加颜色:将函数改为glColor3ub(red(c), green(c), blue(c));
int red(int c) {
if (c <= 160)return 0;
else if (c < 224) return (c - 160) * 4;
else return 255;
}
int green(int c)
{
if (c <= 64) return 0;
else if (c < 128) return (c - 64) * 4;
else if (c < 192) return 255;
else return (255 - c) * 4;
}
int blue(int c)
{
if (c < 64) return c * 4;
else if (c <= 96) return 255;
else if (c <= 160) return(160 - c) * 4;
else return 0;
}