实验一:OpenGL基础实验
(实验环境的熟悉、简单图形的绘制和输出)
1、实验目的和要求
学习基本的OpenGL图形绘制和输出函数,掌握使用基于C++ OpenGL开发图形程序的流程。
2、实验设备
PC机、CodeBlocks\VS系列\OpenGL安装包
3、实验内容及原理
搭建VS\CODEBLOCK开发环境中利用OpenGL开发图形学程序的程序框架。
实验1: 习题5-27
实验2: 习题5-28
实验3: 习题5-29
实验4: 习题5-30
实验原理(基本知识)
- glLineStipple(GLint factor, GLushort pattern)函数:定义点画模式,factor是重复因子;pattern是由1和0组成的16位序列,如果模型中对应的位是1,则绘制这个像素,否则不绘制。使用前必须使用glEnable(GL_LINE_STIPPLE);使用结束后,调用glDisable(GL_LINE_STIPPLE)来结束状态。
- glPolygonStipple(const GLubyte *mask)函数:用于填充32*32的模板位图,该位图由0、1组成掩码,0表示不绘制,1表示绘制。创建模板位图时,mask通常按照从左到右从上到下的顺序使用单个字节(每字节从最高位开始使用)。使用前必须使用glEnable(GL_POLYGON_STIPPLE);使用结束后,调用glDisable(GL_POLYGON_STIPPLE)来结束状态。
- glTranslatef(GLdouble x, GLdouble y, GLdouble z); 位移(x,y,z)向量。glScalef(GLfloat x, GLfloat y, GLfloat z); 参数x,y,z分别为模型在x,y,z轴方向的缩放。
- glutStrokeCharacter(void *font, int chracter):绘制字符的函数。
4、实验源程序代码、运行结果
4.1习题5.27 绘制实线、虚线和点划线
4.1.1源代码:
#include <GL/glut.h>
using namespace std;
void DrawLine(int x0, int y0, int x1, int y1, double color_r, double color_g, double color_b) {
// 绘制实线
glColor3f(color_r, color_g, color_b);
glBegin(GL_LINES);
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glEnd();
// 绘制虚线
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0b1111000011110000);
glBegin(GL_LINES);
glVertex2f(x0, y0 + 50);
glVertex2f(x1, y1 + 50);
glEnd();
glDisable(GL_LINE_STIPPLE);
// 绘制点划线
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0b1111111000010000);
glBegin(GL_LINES);
glVertex2f(x0, y0 - 50);
glVertex2f(x1, y1 - 50);
glEnd();
glDisable(GL_LINE_STIPPLE);
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
DrawLine(50, 150, 200, 150, 0.9921875f, 0.26171875f, 0.39453125f);
glFlush();
}
void ChangeSize(GLsizei w, GLsizei h) {
if (h == 0) {
h = 1;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h) {
glOrtho(0.0f, 250.f, 0.0f, 250.0f * h / w, 1.0f, -1.0f);
} else {
glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0f,-1.0f);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Setup() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
int main() {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("实线、虚线、点划线");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize);
Setup();
glutMainLoop();
return 0;
}
4.1.2运行结果:
4.2习题5.28
4.2.1源代码:
#include <GL/glut.h>
#include <vector>
using namespace std;
GLubyte mask[] = {
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
};
void DrawPolygon(vector< vector<double> > points, double color_r, double color_g, double color_b) {
// 绘制多边形
glColor3f(color_r, color_g, color_b);
glEnable(GL_POLYGON_STIPPLE);
glPolygonStipple(mask);
glBegin(GL_POLYGON);
for (int i = 0; i < points.size(); i++) {
glVertex2f(points[i][0], points[i][1]);
}
glDisable(GL_POLYGON_STIPPLE);
glEnd();
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
vector< vector<double> > points(5, vector<double>(2));
points[3][1] = 100.0f; points[3][0] = 0.0f;
points[4][1] = 10.0f; points[4][0] = 50.0f;
points[0][1] = 10.0f; points[0][0] = 150.0f;
points[1][1] = 100.0f; points[1][0] = 200.0f;
points[2][1] = 170.0f; points[2][0] = 100.0f;
DrawPolygon(points, 0.0f, 0.0f, 0.0f);
glFlush();
}
void ChangeSize(GLsizei w, GLsizei h) {
if (h == 0) {
h = 1;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h) {
glOrtho(0.0f, 250.f, 0.0f, 250.0f * h / w, 1.0f, -1.0f);
} else {
glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0f,-1.0f);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Setup() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
int main() {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("黑白相间");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize);
Setup();
glutMainLoop();
return 0;
}
4.2.2运行结果:
4.3习题5.29
4.3.1源代码:
#include <GL/glut.h>
#include <vector>
#include <cmath>
using namespace std;
const float pi = 3.1415926f;
void DrawSinAndCos() {
// sin
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
for (float x = 0; x < 40; x += 0.1f) {
glVertex2f(x * 10, sin(x) * 20 + 150);
}
glEnd();
// cos
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINE_STRIP);
for (float x = 0; x < 40; x += 0.1f) {
glVertex2f(x * 10, cos(x) * 20 + 150);
}
glEnd();
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
DrawSinAndCos();
glFlush();
}
void ChangeSize(GLsizei w, GLsizei h) {
if (h == 0) {
h = 1;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h) {
glOrtho(0.0f, 250.f, 0.0f, 250.0f * h / w, 1.0f, -1.0f);
} else {
glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0f,-1.0f);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Setup() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
int main() {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("sin && cos");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize);
Setup();
glutMainLoop();
return 0;
}
4.3.2运行结果:
4.4习题5.30
4.4.1源代码:
#include <GL/glut.h>
#include <vector>
#include <string>
using namespace std;
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
string str = "OpenGL";
glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(30, 100, 0);
glScalef(0.4, 0.4, 0.4);
for (int i = 0; i < str.length(); i++) {
glutStrokeCharacter(GLUT_STROKE_ROMAN, str[i]);
}
glFlush();
}
void ChangeSize(GLsizei w, GLsizei h) {
if (h == 0) {
h = 1;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h) {
glOrtho(0.0f, 250.f, 0.0f, 250.0f * h / w, 1.0f, -1.0f);
} else {
glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0f,-1.0f);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Setup() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
int main() {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("OpenGL");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize);
Setup();
glutMainLoop();
return 0;
}
4.4.2运行结果: