本文主要是介绍计算机图形学 -- 基本图形函数练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;
}
这篇关于计算机图形学 -- 基本图形函数练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!