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

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

相关文章

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

SpringBoot整合MybatisPlus的基本应用指南

《SpringBoot整合MybatisPlus的基本应用指南》MyBatis-Plus,简称MP,是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,下面小编就来和大家介绍一下... 目录一、MyBATisPlus简介二、SpringBoot整合MybatisPlus1、创建数据库和

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

kotlin的函数forEach示例详解

《kotlin的函数forEach示例详解》在Kotlin中,forEach是一个高阶函数,用于遍历集合中的每个元素并对其执行指定的操作,它的核心特点是简洁、函数式,适用于需要遍历集合且无需返回值的场... 目录一、基本用法1️⃣ 遍历集合2️⃣ 遍历数组3️⃣ 遍历 Map二、与 for 循环的区别三、高

C语言字符函数和字符串函数示例详解

《C语言字符函数和字符串函数示例详解》本文详细介绍了C语言中字符分类函数、字符转换函数及字符串操作函数的使用方法,并通过示例代码展示了如何实现这些功能,通过这些内容,读者可以深入理解并掌握C语言中的字... 目录一、字符分类函数二、字符转换函数三、strlen的使用和模拟实现3.1strlen函数3.2st

MySQL中COALESCE函数示例详解

《MySQL中COALESCE函数示例详解》COALESCE是一个功能强大且常用的SQL函数,主要用来处理NULL值和实现灵活的值选择策略,能够使查询逻辑更清晰、简洁,:本文主要介绍MySQL中C... 目录语法示例1. 替换 NULL 值2. 用于字段默认值3. 多列优先级4. 结合聚合函数注意事项总结C

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链