Opengl入门实例1

主函数不变:

int _tmain(int argc, _TCHAR* argv[])
{
	glutInit(&argc, (char**)argv); //初始化glut,必须调用,复制黏贴这句话即可
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); //设置显示方式,RGB、单缓冲。当然还有GLUT_INDEX索引颜色 GLUT_DOUBLE双缓冲(Qt中看到过双缓冲)
	glutInitWindowPosition(100, 100); //位置
	glutInitWindowSize(400, 400);//窗口大小
	glutCreateWindow("第一个OpenGL程序"); //创建窗口,设置标题
	glutDisplayFunc(&myDisplay); // 当绘制窗口时调用myDisplay,像Cocos2d-x刷帧Draw中的操作
	glutMainLoop(); //消息循环
	return 0;
}

画圆⚪

n设置为20时

#include "pch.h"
#include <iostream>
#include<gl/glut.h>
#include <tchar.h>
#include <math.h>
//#pragma comment(lib, "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\lib\\glut32.lib")
const int n = 20;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;
void myDisplay(void)
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT); //清除颜色
	//glRectf(-0.5f, -0.5f, 0.5f, 0.5f); //画一个矩形
	glBegin(GL_POLYGON);
	for (i = 0; i < n; i++)
		glVertex2f(R*cos(2 * Pi / n * i), R*sin(2 * Pi / n * i));
	glEnd();
	glFlush(); //让前面的命令立即执行而不是在缓冲区,与fflush(stdout)作用类似
}
  • n设置为200后,圆更加圆滑
    n设置为200时更圆滑

画五角星

在这里插入图片描述
出了小小的状况 (=. =!)

const GLfloat Pi = 3.1415926536f;
void myDisplay(void)
{
	GLfloat a = 1 / (2 - 2 * cos(72 * Pi / 180));
	GLfloat bx = a*cos(18*Pi/180);
	GLfloat by = a * sin(18 * Pi / 180);
	GLfloat cy = -a * cos(18 * Pi / 180);
	GLfloat PointA[2] = { 0,a };
	GLfloat PointB[2] = { bx,by };
	GLfloat PointC[2] = { 0.5,cy };
	GLfloat PointD[2] = { -0.5,cy };
	GLfloat PointE[2] = { -bx,by };
	glClear(GL_COLOR_BUFFER_BIT); //清除颜色
	glBegin(GL_POLYGON);
		glVertex2fv(PointA);
		glVertex2fv(PointC);
		glVertex2fv(PointE);
		glVertex2fv(PointB);
		glVertex2fv(PointD);
	glEnd();
	glFlush(); //让前面的命令立即执行而不是在缓冲区,与fflush(stdout)作用类似
}

正弦函数

在这里插入图片描述

void myDisplay(void)
{
	GLfloat x;
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_LINES);
	glVertex2f(-1.0f, 0.0f);
	glVertex2f(1.0f, 0.0f);
	glVertex2f(0.0f, -1.0f);
	glVertex2f(0.0f, 1.0f);
	glEnd();
	glBegin(GL_LINE_STRIP);
	for (x = -1.0f / factor; x < 1.0f / factor; x += 0.01f)
	{
		glVertex2f(x*factor, sin(x)*factor);
	}
	glEnd();
	glFlush(); //让前面的命令立即执行而不是在缓冲区,与fflush(stdout)作用类似
}

猜你喜欢

转载自blog.csdn.net/weixin_43118073/article/details/104714338