OpenGL HelloWorld

程序始于HelloWorld

#include<gl/GLUT.H>

void RenderScene(void)
{
	// Clear the window with current clearing color
	glClear(GL_COLOR_BUFFER_BIT);
	// Flush drawing commands
	glFlush();
}
///
// Set up the rendering state
void SetupRC(void)
{
	glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
///
// Main program entry point
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
	glutCreateWindow(“Simple”);
	glutDisplayFunc(RenderScene);
	SetupRC();
	glutMainLoop();
	return 0;
}

这是OpenGL宝典里的第一个OpenGL程序例子。分析如下:

glutInit(&argc, argv);//使用glut库需要进行初始化
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
//GLUT_SINGLE 的意思是使用单个缓冲窗口,单个缓冲窗口的意思是所有的绘制命令都在窗口绘制的时候一次
//性执行.可供选择的还有GLUT_DOUBLE,这个方式实际是有一个后台缓冲缓存了所有渲染命令的结果,在全部
//完成时快速的和屏幕缓冲进行交换,常用于制作动画效果。

//GLUT_RGBA 指定颜色模型为RGBA
glutCreateWindow("Simple");
//创建一个窗口,参数为标题的名称
glutDisplayFunc(RenderScene);
//设定一个渲染时的回调函数RenderScene,GLUT会在窗口需要作出任何绘制的时候调用这个回调函数
//在窗口的尺寸变化或者再次聚焦该窗口的时候也会调用这个回调函数
/*原文
This line establishes the previously defined function RenderScene as the display callback
function. This means that GLUT calls the function pointed to here whenever the window
needs to be drawn. This call occurs when the window is first displayed or when the
window is resized or uncovered, for example. This is the place where we put our OpenGL
rendering function calls.
*/
glutMainLoop();
/*这个函数启动了GLUT框架。在这一步之前做的操作相当于设置属性,并未执行。glutMainLoop只需要被唤醒
一次,然后在窗口关闭之前是会一直循环执行。它会处理所有操作系统提供的信息,键盘输入等等,直到你主动
关闭程序。*/
/*
This function starts the GLUT framework running. After you define callbacks for screen
display and other functions (coming up), you turn GLUT loose. glutMainLoop never
returns after it is called until the main window is closed, and needs to be called only once
from an application. This function processes all the operating system–specific messages,
keystrokes, and so on until you terminate the program.
*/
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
/*
    原型是
    void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
    这句代码是用于清屏的。
    GLclampf 是OpenGL定义的一种float数据类型 代表(0,1)之间的浮点数,使用四个GLclampf的
    浮点数来表示不同的颜色,和windows上的RGBA表示类似,不同的是windows使用(0,255)的整数
    来表示,不同的 windows能表示 255*255*255 大约 16W多种颜色。但是在OpenGL中使用三个小
    数在理论上食用油无穷多个排列的,但是受到计算机的字节数限制,总数也是有限的。如果是设备
    使用24bytes表示颜色,那么也只有255*255*255种颜色。
*/
glClear(GL_COLOR_BUFFER_BIT);
//Clearing the Color Buffer
使用glClearColorz中指定的值设定颜色缓存区的值,即将窗口中的每一个像素设置为背景色
glFlush();

//清空OpenGL命令缓冲区,强制执行命令缓冲区中所有OpenGL函数
/*
    OpenGL内部使用渲染管线来处理命令队列,OpenGL驱动执行这些命令之前,这些命令和申明处在
    挂起状态,也就是在排队。因为渲染需要和硬件打交道,而和硬件进行交互的效率非常低,所以一
    次性处理一个大的数据块远比多次处理一个个非常小的数据块的效率高的多的多。
    屏幕上的各个像素的计算是并行处理的。
*/

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值