计算机图形学 -- 基本图形函数练习

2024-06-10 13:18

本文主要是介绍计算机图形学 -- 基本图形函数练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



1. 基本




#include<glut.h>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")void mydisplay(void)
{//glClearColor : 通过glClear使用红,绿,蓝以及AFA值来清除颜色缓冲区,并且缺省值全是0. 即(0,0,0,0)glClearColor(1.0,1.0,0.0,0.0);	//设置背景为绿色		glClear(GL_COLOR_BUFFER_BIT);//在左上角画一个黄色的矩形glColor3f(0.0f,1.0f,1.0f);glRectf(-0.9f,0.0f,0.0f,0.9f);//分别是左下角和右上角两点坐标//在右上角画一个花花的三角形glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);	glVertex2f(0.9f,0.0f);glColor3f(0.0f,1.0f,0.0f);	glVertex2f(0.5f,0.6f);glColor3f(0.0f,0.0f,1.0f);	glVertex2f(0.1f,0.0f);glEnd();//画两个点glPointSize(5);//表示点的像素glBegin(GL_POINTS);glColor3f(0.0f,0.0f,0.0f);	glVertex2f(0.05f,0.0f);glColor3f(1.0f,0.0f,0.0f);	glVertex2f(0.05f,-0.5f);glEnd();glFlush();
}int main(int argc,char *argv[])
{glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowPosition(300,100);glutInitWindowSize(500,500);glutCreateWindow("Basic Shape");glutDisplayFunc(&mydisplay);glutMainLoop();return 0;
}



2. 升级1




#include<glut.h>
#include<math.h>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
const GLfloat R = 0.8f;
const GLfloat PI = 3.1415926536f;
const int n = 600;void basicshape2(void)
{int i;glClearColor(1.0,1.0,0.0,0.8);glClear(GL_COLOR_BUFFER_BIT);/* ---------------------- ----------------------*/glLineWidth(3);glBegin(GL_POLYGON);	//以闭合折线方式画圆glColor3f(1.0f,0.0f,0.0f);for(i=0; i<n; ++i)glVertex2f(R*cos(2*PI/n*i), R*sin(2*PI/n*i));glEnd();/* ---------------------- ----------------------*/glBegin(GL_LINE_LOOP);	//以闭合折线方式画五角星glColor3f(0.0f,1.0f,0.0f);GLfloat bx=R*cos(18*PI/180);GLfloat by=R*sin(18*PI/180);GLfloat cx=R*sin(36*PI/180);GLfloat cy=R*cos(36*PI/180);glVertex2f(-bx,by);glVertex2f(bx,by);glVertex2f(-cx,-cy);glVertex2f(0.0f,R);glVertex2f(cx,-cy);glEnd();/* ---------------------- ----------------------*/glBegin(GL_POLYGON);	//以填充方式画菱形,多边形,五角星也是可以的啦~glVertex2f(-0.8f,0.95f);glVertex2f(-0.7f,0.85f);glVertex2f(-0.8f,0.75f);glVertex2f(-0.9f,0.85f);glEnd();glBegin(GL_POLYGON);	//对称一下glVertex2f(0.8f,0.95f);glVertex2f(0.7f,0.85f);glVertex2f(0.8f,0.75f);glVertex2f(0.9f,0.85f);glEnd();//这里有两个菱形,可以用GL_QUADS一次搞定,独立的~/* ---------------------- ----------------------*/glColor3f(0.0f,0.0f,1.0f);glBegin(GL_TRIANGLES);	//两个三角形glVertex2f(-1.0f,-1.0f);glVertex2f(-0.95f,-0.7f);glVertex2f(-0.8f,-0.7f);glVertex2f(-1.0f,-1.0f);glVertex2f(-0.8f,-0.75f);glVertex2f(-0.8f,-0.95f);glEnd();glFlush();
}int main(int argc,char *argv[])
{glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);glutInitWindowPosition(150,150);glutInitWindowSize(500,500);glutCreateWindow("Baisc Shape2");glutDisplayFunc(basicshape2);glutMainLoop();return 0;
}


3. 升级2








#include<GL/glut.h>
#include<math.h>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
const float R1 = 0.8f;
const float R2 = 0.4f;
const float PI = 3.1415926;
void myDisplay_1()
{glClearColor(1.0f,1.0f,1.0f,0.0f);glClear(GL_COLOR_BUFFER_BIT);//*****下面开始画矩形和三角形glColor3f(1.0f,0.0f,0.0f);glBegin(GL_POLYGON);glVertex2f(0.4f,0);glVertex2f(0,0.4f);glVertex2f(-0.4f,0);glVertex2f(0,-0.4f);glEnd();glBegin(GL_TRIANGLES);	//四个三角形glColor3f(1.0f,1.0f,0.0f);glVertex2f(0.4f,-0.4f);glVertex2f(0.8f,0);glVertex2f(0.4f,0.4f);glColor3f(0.0f,0.0f,1.0f);glVertex2f(0.4f,0.4f);glVertex2f(-0.4f,0.4f);glVertex2f(0,0.8f);glColor3f(1.0f,0.0f,1.0f);glVertex2f(-0.4f,0.4f);glVertex2f(-0.4f,-0.4f);glVertex2f(-0.8f,0);glColor3f(0.0f,1.0f,0.0f);glVertex2f(-0.4f,-0.4f);glVertex2f(0.4f,-0.4f);glVertex2f(0,-0.8f);glEnd();//******描边glColor3f(0.0f,0.0f,0.0f);glBegin(GL_LINE_LOOP);glVertex2f(0.4f,0);//矩形的边glVertex2f(0,0.4f);glVertex2f(-0.4f,0);glVertex2f(0,-0.4f);glEnd();glBegin(GL_LINE_LOOP);//三角形的边,可当作两个矩形glVertex2f(0.8f,0);glVertex2f(0.4f,-0.4f);glVertex2f(0,-0.8f);glVertex2f(-0.4f,-0.4f);glVertex2f(-0.8f,0);glVertex2f(-0.4f,0.4f);glVertex2f(0,0.8f);glVertex2f(0.4f,0.4f);glVertex2f(0.4f,-0.4f);glVertex2f(-0.4f,-0.4f);glVertex2f(-0.4f,0.4f);glVertex2f(0.4f,0.4f);glEnd();glFlush();
}void myDisplay_2()
{glClearColor(1.0f,1.0f,1.0f,0.0f);glClear(GL_COLOR_BUFFER_BIT);for(int i=0;i<=270;i+=90){glColor3f(1.0f,0.0f,0.0f);glBegin(GL_POLYGON);glVertex2f(R1*cos(i*PI/180),R1*sin(i*PI/180));glVertex2f(R2*cos((i+25)*PI/180),R2*sin((i+25)*PI/180));glVertex2f(0,0);glVertex2f(R2*cos((i-25)*PI/180),R2*sin((i-25)*PI/180));glEnd();glColor3f(0.0f,0.0f,0.0f);glBegin(GL_LINE_LOOP);glVertex2f(R1*cos(i*PI/180),R1*sin(i*PI/180));glVertex2f(R2*cos((i+25)*PI/180),R2*sin((i+25)*PI/180));glVertex2f(0,0);glVertex2f(R2*cos((i-25)*PI/180),R2*sin((i-25)*PI/180));glEnd();	}for(i=45;i<=315;i+=90){glColor3f(0.0f,1.0f,0.0f);glBegin(GL_POLYGON);glVertex2f(R1*cos(i*PI/180),R1*sin(i*PI/180));glVertex2f(R2*cos((i+20)*PI/180),R2*sin((i+20)*PI/180));glVertex2f(0,0);glVertex2f(R2*cos((i-20)*PI/180),R2*sin((i-20)*PI/180));glEnd();glColor3f(0.0f,0.0f,0.0f);glBegin(GL_LINE_LOOP);glVertex2f(R1*cos(i*PI/180),R1*sin(i*PI/180));glVertex2f(R2*cos((i+20)*PI/180),R2*sin((i+20)*PI/180));glVertex2f(0,0);glVertex2f(R2*cos((i-20)*PI/180),R2*sin((i-20)*PI/180));glEnd();}glFlush();
}int main(int argc,char * argv[])
{glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);glutInitWindowPosition(100,100);glutInitWindowSize(500,500);glutCreateWindow("sheng2_1");glutDisplayFunc(myDisplay_1);glutInitWindowPosition(700,100);glutInitWindowSize(500,500);glutCreateWindow("sheng2_2");glutDisplayFunc(myDisplay_2);glutMainLoop();return 0;
}



这篇关于计算机图形学 -- 基本图形函数练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048268

相关文章

使用Python进行文件读写操作的基本方法

《使用Python进行文件读写操作的基本方法》今天的内容来介绍Python中进行文件读写操作的方法,这在学习Python时是必不可少的技术点,希望可以帮助到正在学习python的小伙伴,以下是Pyth... 目录一、文件读取:二、文件写入:三、文件追加:四、文件读写的二进制模式:五、使用 json 模块读写

Oracle的to_date()函数详解

《Oracle的to_date()函数详解》Oracle的to_date()函数用于日期格式转换,需要注意Oracle中不区分大小写的MM和mm格式代码,应使用mi代替分钟,此外,Oracle还支持毫... 目录oracle的to_date()函数一.在使用Oracle的to_date函数来做日期转换二.日

如何测试计算机的内存是否存在问题? 判断电脑内存故障的多种方法

《如何测试计算机的内存是否存在问题?判断电脑内存故障的多种方法》内存是电脑中非常重要的组件之一,如果内存出现故障,可能会导致电脑出现各种问题,如蓝屏、死机、程序崩溃等,如何判断内存是否出现故障呢?下... 如果你的电脑是崩溃、冻结还是不稳定,那么它的内存可能有问题。要进行检查,你可以使用Windows 11

C++11的函数包装器std::function使用示例

《C++11的函数包装器std::function使用示例》C++11引入的std::function是最常用的函数包装器,它可以存储任何可调用对象并提供统一的调用接口,以下是关于函数包装器的详细讲解... 目录一、std::function 的基本用法1. 基本语法二、如何使用 std::function

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

RabbitMQ练习(AMQP 0-9-1 Overview)

1、What is AMQP 0-9-1 AMQP 0-9-1(高级消息队列协议)是一种网络协议,它允许遵从该协议的客户端(Publisher或者Consumer)应用程序与遵从该协议的消息中间件代理(Broker,如RabbitMQ)进行通信。 AMQP 0-9-1模型的核心概念包括消息发布者(producers/publisher)、消息(messages)、交换机(exchanges)、