初学OpenGL

今天在公司开始了第一天的实习。首先就让学习ray casting算法。

关于ray casting算法的心得我会在学习透彻之后再写一篇文章分析。今天首先做的事情就是配置了OpenGL然后学习了OpenGL配合VS的一些基本使用。

先上代码吧

// openGL_test_d.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "GL/glut.h"
#include "math.h"
using namespace std;
void  GL_display()
{
const  GLfloat pi = 3.1415926f;
GLfloat a = cos(54 * pi / 180);
GLfloat b = sin(54 * pi / 180);
GLfloat c = cos(18 * pi / 180);
GLfloat d = sin(18 * pi / 180);
GLfloat pointA[2] = {0,1};
GLfloat pointB[2] = { -a, -b };
GLfloat pointC[2] = { c, d };
GLfloat pointD[2] = { -c, d };
GLfloat pointE[2] = { a, -b };
glClear(GL_COLOR_BUFFER_BIT);     //用于清除当前的颜色缓冲
	glBegin(GL_LINE_LOOP);   //设置连线方式位闭曲线
	glVertex2fv(pointA);
	glVertex2fv(pointB);
	glVertex2fv(pointC);
	glVertex2fv(pointD);
	glVertex2fv(pointE);
	glEnd();
	glFlush();           //强制刷新缓冲区,让画图程序进行
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);    //初始化
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);   //设置显示方式RGB单缓冲
	glutInitWindowPosition(500, 500);             //设置窗口位置
	glutInitWindowSize(500, 500);                 //设置窗口大小
	glutCreateWindow("这真的是我的第一个openGL程序啦!!!");   //窗口名称
	glutDisplayFunc(&GL_display);               //函数显示
	glutMainLoop();                            //用循环让画图程序一直进行
	return 0;
}

关于OpenGL的配置网上有详细教程,就不再说明了。这段代码的功能就是画一个五角星,用函数GL_display画出,这里用GLfloat而不用float的原因是不同的编译器可能float的内存大小会是32位或64位,而GLfloat不会出现这样的问题。

画图时首先定义了一些点,然后glVertex2fv用来定义这些点,并按照顺序连接起来。

总的来说,用openGL进行基础的画图工作的步骤就是这样,画图函数中先清除缓冲,然后定义点的顺序。在主函数中先进行初始化,并设置好一系列的参数,如显示方式,窗口大小,窗口位置等等,然后调用函数进行图像的显示,最后用循环让图像显示程序一直进行下去。

下面是另一个基础的图像显示代码,显示了一个六角星,图像初始化过程与画图过程与以上大致相同

// GL_test_e.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<GL/glut.h>
#include "math.h"

const GLfloat pi = 3.1415926f;
const GLfloat a = cos(pi / 6);
const GLfloat b = sin(pi / 6);

void GL_display()
{
	GLfloat pointA[2] = { 0, 1.0 };
	GLfloat pointB[2] = { a, b };
	GLfloat pointC[2] = { a, -b };
	GLfloat pointD[2] = { 0, -1.0 };
	GLfloat pointE[2] = { -a, -b };
	GLfloat pointF[2] = { -a, b };
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_LINE_LOOP);
	glVertex2fv(pointA);
	glVertex2fv(pointE);
	glVertex2fv(pointC);
	glEnd();
	glFlush();
	glBegin(GL_LINE_LOOP);
	glVertex2fv(pointF);
	glVertex2fv(pointD);
	glVertex2fv(pointB);
	glEnd();
	glFlush();
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(500, 500);
	glutInitWindowSize(600, 600);
	glutCreateWindow("六芒星的咒符!!!");
	glutDisplayFunc(&GL_display);
	glutMainLoop();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值