#include
#include
#include
#include
using namespace std;
void myDisplay(void){
glClearColor(0.0, 0, 0,0); // 设置背景默认颜色
glClear(GL_COLOR_BUFFER_BIT); // 清空当前可写的颜色缓冲,并设为默认值(和上一个函数一起起作用)
// 一个矩形
glColor3f( 1.0, 1.0, 0.0);
glRectf (-0.5, -0.5, 0.5, 0.5);
// 一个三角形
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); glVertex2f( 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0); glVertex2f( 0.8, -0.5);
glColor3f(1.0, 0.0, 1.0); glVertex2f(-0.8, -0.5);
glEnd();
// 三个点
glPointSize(3); // 设置点 的大小
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.4, -0.4);
glColor3f(1.0, 0.0, 0.0); glVertex2f( 0.0, 0.0);
glColor3f(1.0, 0.0, 0.0); glVertex2f( 0.4, 0.4);
glEnd();
glFlush(); // 用于强制刷新缓冲,保证绘图命令立即被执行
}
int main(int argc, char *argv[]){
glutInit(&argc, argv); // 来识别要用glut来控制
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); // 设置显示的模式
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("图形学实验1.1");
glutDisplayFunc(&myDisplay); // 调用显示函数
glutMainLoop();
return 0;
}
几点需要主要的
#include
#include
#include
#include
using namespace std;
const double PI = acos(-1.0);
struct Point{
double x, y;
Point(){}
Point(double _x, double _y){
x = _x; y = _y;
}
};
void myDisplay(void){
glClearColor(0.0, 0.0, 0,0);
glClear(GL_COLOR_BUFFER_BIT);
// 矩形
glColor3f( 1.0, 1.0, 1.0);
glRectf (-0.8, -0.8, 0.8, 0.8);
double rate = 0.8;
// 大三角形
glBegin(GL_TRIANGLES);
glColor3f(0.0, 1.0, 0.0); glVertex2f(-1 * rate, 1 * rate);
glColor3f(1.0, 1.0, 0.0); glVertex2f( 1 * rate, 1 * rate);
glColor3f(1.0, 0.0, 0.0); glVertex2f( 0 * rate, -1 * rate);
glEnd();
// 圆
int N = 100; // 精度
double R = 0.62 * rate;
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_POLYGON);
for(int i = 0; i < N; i++) {
glVertex2f(R * cos(2 * PI/ N * i), R * sin(2 * PI / N * i));
}
glEnd();
// 五角星
Point arr[5];
arr[0] = Point(-0.5, -0.65);
arr[1] = Point( 0.5, -0.65);
arr[2] = Point( 0.7, 0.2);
arr[3] = Point( 0.0, 0.7);
arr[4] = Point(-0.7, 0.2);
glColor3f(0, 0, 1);
glLineWidth(1 ); // 设置线宽
for(int i = 0; i < 5; i++){
glBegin(GL_LINES);
glVertex2f(arr[i].x, arr[i].y);
int j = (i + 2) % 5;
glVertex2f(arr[j].x, arr[j].y);
glEnd();
}
// 小三角形
Point a[3];
a[0] = Point(0.7 , -0.7);
a[1] = Point(0.6 , -0.7);
a[2] = Point(0.65, -0.6);
glColor3f(1, 0, 1);
for(int i = -1; i <= 1; i += 2){
glBegin(GL_POLYGON);
for(int j = 0; j < 3; j++){
glVertex2f(i * a[j].x, a[j].y);
}
glEnd();
glColor3f(1.0, 0.7, 0.0);
}
glFlush();
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("图形学实验1.2");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
几点需要注意的:
#include
#include
#include
#include
using namespace std;
struct Point{
double x, y;
Point(){}
Point(double _x, double _y){
x = _x; y = _y;
}
Point operator + (const Point &a) const{
return Point(this->x + a.x, this->y + a.y);
}
Point operator / (const int &a) const{
return Point(this->x / a, this->y / a);
}
};
const double PI = acos(-1.0);
void setColor(int id){
double r, g, b;
if(id == 1) {r = 0.0; g = 0.0; b = 0.0;}
if(id == 2) {r = 1.0; g = 1.0; b = 1.0;}
if(id == 3) {r = 1.0; g = 0.0; b = 0.0;}
if(id == 4) {r = 1.0, g = 1.0; b = 0.0;}
glColor3f(r, g, b);
}
void dfs(Point a[], int id){
if(id > 4) return ;
setColor(id);
glBegin(GL_POLYGON);
for(int i = 0; i < 4; i++) glVertex2f(a[i].x, a[i].y);
glEnd();
Point t[4]; for(int i = 0; i < 4; i++) t[i] = (a[i] + a[(i + 1 ) % 4]) / 2;
dfs(t, id + 1);
}
void setcolor(int id){
double r, g, b;
if(id == 0) {r = 1.0, g = 0.0, b = 0.0;}
if(id == 1) {r = 0.0, g = 1.0, b = 0.0;}
if(id == 2) {r = 0.0, g = 0.0, b = 1.0;}
if(id == 3) {r = 1.0, g = 1.0, b = 0.0;}
glColor3f(r, g, b);
}
void myDisplay(void){
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// 递归画矩形嵌套
Point a[4];
a[0] = Point(-0.5, -0.5);
a[1] = Point( 0.5, -0.5);
a[2] = Point( 0.5, 0.5);
a[3] = Point(-0.5, 0.5);
dfs(a, 1);
// 通过旋转得到 四个三角形
double r[4], t[4];
r[1] = sqrt(2.0) / 2.0; t[1] = PI / 4.0;
r[3] = 1.0; t[3] = 0.0;
r[2] = sqrt(2.0) / 2.0; t[2] = -PI / 4.0;
for(int i = 0; i < 4; i++){
setcolor(i);
glBegin(GL_POLYGON);
for(int j = 1; j <= 3; j++){
glVertex2f(cos(t[j]) * r[j], sin(t[j]) * r[j]);
t[j] += PI / 2;
}
glEnd();
}
glFlush();
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("图形学实验1.3");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
注意:
总结 :第一次的实验,主要是对opengl的熟悉,还有简单图形的处理。