计算机图形学:实验二——OpenGL绘制基本图形

1. 实验目的
练习OpenGL基础编程,实现OpenGL的颜色混合和渐变效果。
2. 实验内容和要求
按要求完成以下两个绘图,提交纸质实验报告,同时提交实验报告和代码的电子版。
I). 利用OpenGL对下面的图形进行七种颜色的渐变填充,实现彩虹效果。
II). 利用OpenGL绘制如下图形,颜色利用随机函数随机设定,实现颜色混合的效果。
3.实验结果
第一题:
源代码:

#include"pch.h"  //因为我下载的是visual stdio 2017所以必须加这个头文件
#include<GL/glut.h>
#include<math.h>
#define PI 3.1415926

void init(void) {
    
    
	glClearColor(1.0, 1.0, 1.0, 1.0);

	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
}

void lineSegment(void) {
    
    
	glClear(GL_COLOR_BUFFER_BIT);
	int i;
	float t, t1;

	for (i = 0; i < 100; i++) {
    
    
		t = PI / 100 * i;
		t1 = PI / 100 * (i + 1);
		glShadeModel(GL_SMOOTH);
		glBegin(GL_POLYGON);
			glColor3f(0, 0, 0); glVertex2f(32 * cos(t), 32 * sin(t));
			glColor3f(0, 0, 0); glVertex2f(32 * cos(t1), 32 * sin(t1));
			glColor3f(0, 0, 0); glVertex2f(35 * cos(t1), 35 * sin(t1));
			glColor3f(0, 0, 0); glVertex2f(35 * cos(t), 35 * sin(t));
		glEnd();
		glBegin(GL_POLYGON);
			glColor3f(0.6, 0.125, 0.94); glVertex2f(35 * cos(t), 35 * sin(t));
			glColor3f(0.6, 0.125, 0.94); glVertex2f(35 * cos(t1), 35 * sin(t1));
			glColor3f(0, 0, 1); glVertex2f(45 * cos(t1), 45 * sin(t1));
			glColor3f(0, 0, 1); glVertex2f(45 * cos(t), 45 * sin(t));
		glEnd();
		glBegin(GL_POLYGON);
			glColor3f(0, 0, 1); glVertex2f(45 * cos(t1), 45 * sin(t1));
			glColor3f(0, 0, 1); glVertex2f(45 * cos(t), 45 * sin(t));
			glColor3f(0, 1, 1); glVertex2f(55 * cos(t), 55 * sin(t));
			glColor3f(0, 1, 1); glVertex2f(55 * cos(t1), 55 * sin(t1));
		glEnd();
		glBegin(GL_POLYGON);
			glColor3f(0, 1, 1); glVertex2f(55 * cos(t), 55 * sin(t));
			glColor3f(0, 1, 1); glVertex2f(55 * cos(t1), 55 * sin(t1));
			glColor3f(0, 1, 0); glVertex2f(65 * cos(t1), 65 * sin(t1));
			glColor3f(0, 1, 0); glVertex2f(65 * cos(t), 65 * sin(t));
		glEnd();
		glBegin(GL_POLYGON);
			glColor3f(0, 1, 0); glVertex2f(65 * cos(t1), 65 * sin(t1));
			glColor3f(0, 1, 0); glVertex2f(65 * cos(t), 65 * sin(t));
			glColor3f(1, 1, 0); glVertex2f(75 * cos(t), 75 * sin(t));
			glColor3f(1, 1, 0); glVertex2f(75 * cos(t1), 75 * sin(t1));
		glEnd();
		glBegin(GL_POLYGON);
			glColor3f(1, 1, 0); glVertex2f(75 * cos(t), 75 * sin(t));
			glColor3f(1, 1, 0); glVertex2f(75 * cos(t1), 75 * sin(t1));
			glColor3f(1, 0.38, 0); glVertex2f(85 * cos(t1), 85 * sin(t1));
			glColor3f(1, 0.38, 0); glVertex2f(85 * cos(t), 85 * sin(t));
		glEnd();
		glBegin(GL_POLYGON);
			glColor3f(1, 0.38, 0); glVertex2f(85 * cos(t1), 85 * sin(t1));
			glColor3f(1, 0.38, 0); glVertex2f(85 * cos(t), 85 * sin(t));
			glColor3f(1, 0, 0); glVertex2f(95 * cos(t), 95 * sin(t));
			glColor3f(1, 0, 0); glVertex2f(95 * cos(t1), 95 * sin(t1));
		glEnd();
		glBegin(GL_POLYGON);
			glColor3f(0, 0, 0); glVertex2f(95 * cos(t1), 95 * sin(t1));
			glColor3f(0, 0, 0); glVertex2f(95 * cos(t), 95 * sin(t));
			glColor3f(0, 0, 0); glVertex2f(98 * cos(t), 98 * sin(t));
			glColor3f(0, 0, 0); glVertex2f(98 * cos(t1), 98 * sin(t1));
		glEnd();
	}
	glColor3f(0, 0, 0);
	glLineWidth(3);
	glBegin(GL_LINES);
		glVertex2i(32 * cos(0), 32 * sin(0));
		glVertex2i(98 * cos(0), 98 * sin(0));
		glVertex2i(32 * cos(PI), 32 * sin(PI));
		glVertex2i(98 * cos(PI), 98 * sin(PI));
	glEnd();
	glFlush();

}
void main(int argc, char** argv) {
    
    
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(600, 600);
	glutCreateWindow("彩虹");

	init();
	glutDisplayFunc(lineSegment);

	glutMainLoop();
}

运行结果
在这里插入图片描述
第二题
源代码

#include"pch.h"  //因为我下载的是visual stdio 2017所以必须加这个头文件
#include<GL/glut.h>
#include<math.h>
#include<stdlib.h>

#define PI 3.1415926

void init(void) {
    
    
	glClearColor(1.0, 1.0, 1.0, 1.0);

	glEnable(GL_BLEND);
	//glBlendFunc(GL_ONE, GL_ZERO);// sFactor, dFactor
	//glBlendFunc(GL_ZERO, GL_ONE);// sFactor, dFactor
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	//glBlendFunc(GL_ONE, GL_ONE);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
}

void lineSegment(void) {
    
    
	float a,b,c;
	float t,t1;
	glClear(GL_COLOR_BUFFER_BIT);
	for (int i = 0; i < 10; i++) {
    
    
		t = 2 * PI / 10 * i;
		a = rand() % 256 / 255.0;
		b = rand() % 256 / 255.0;
		c = rand() % 256 / 255.0;
		glColor4f(a, b, c , 0.5);
		glBegin(GL_POLYGON);
			for (int j = 0; j < 2000; j++) {
    
    
				t1 = 2 * PI / 2000 * j;
				glVertex2f(35 * cos(t) + 35 * cos(t1), 35 * sin(t)+ 35 * sin(t1));
			}
		glEnd();
	}
	glFlush();

}
void main(int argc, char** argv) {
    
    
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(600, 600);
	glutCreateWindow("扇形图");

	init();
	glutDisplayFunc(lineSegment);

	glutMainLoop();
}

运行结果
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42534724/article/details/109923684