一、安装
我的系统是win10 64位,用的是visual studio2013,vs装在了E盘下面。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
visual studio下载地址如下:
链接:https://pan.baidu.com/s/1K74efwL5rSyONF7VS1IRMQ
提取码:pb7k
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
安装过程我就不多说了,首先解压,然后按照说明文档做就行
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
然后下载OpenGL配置文件glut,下载链接如下:
链接:https://pan.baidu.com/s/1lkFYBpdrc0Ry3cYW_0h2Xw
提取码:i1b9
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面是安装过程,打开glut文件夹,发现有5个文件:
1.把glut.h 复制到vs安装的目录下,如果你是默认安装,那么应该去C盘找,如果没有没有GL文件夹,自己创建
2.把glut.lib和glut32.lib复制到:
3.把glut.dll 和glut32.dll复制到 C:\Windows\SysWOW64下:
这时候已经ok啦。我们用vs创建项目吧!
1.在开始栏里找到visual studio 2013
2.打开visual stdio2013,新建--->项目
3.选择空项目
4.写测试文件
5.测试文件代码如下:
#include <GL\glut.h>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f); //画矩形
glFlush();
}
int main(int argc, char *argv[])
{
//初始化
glutInit(&argc, argv);
//显示模式
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
//窗口位置
glutInitWindowPosition(100, 100);
//窗口尺寸
glutInitWindowSize(400, 400);
//创建窗口,窗口标题为“FirstOpenGLProgram”
glutCreateWindow("FirstOpenGLProgram");
//绘制回掉函数
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
二、简单代码分析
好了,基本步骤已经完成,接下来我们要阅读简单的代码并对OpenGL进行理解了:
我们要用opengl画一个cube,创建cube.cpp,代码如下
#include <windows.h>
#include <GL/glut.h>
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Anything you want to display on the screen, draw it here. * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Clear the screen. Set the current color to white.
* Draw the wire frame cube.
*/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity(); /* clear the matrix */
//--------------- you can define your own transformation-----------
glTranslatef(0.0, 0.0, -5.0); /* viewing transformation */
glScalef(1.0, 2.0, 1.0); /* modeling transformation */
//---------------- you can draw something else here-----------
glutWireCube(1.0); /* draw the cube */
glFlush();
}
/* Called when the window is first opened and whenever
* the window is reconfigured (moved or resized).
*/
void reshape(int w, int h)
{
/* define the projection */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW); /* back to modelview matrix */
glViewport(0, 0, w, h); /* define the viewport */
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//define size and the relative positon of the applicaiton window on the display
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
//init the defined window with "argv[1]" as topic showed on the top the window
glutCreateWindow(argv[0]);
// opengl setup
init();
//define callbacks
glutDisplayFunc(display);
glutReshapeFunc(reshape);
//enter the loop for display
glutMainLoop();
return 0;
}
运行结果如下:
下面说一下我对这个程序的理解,首先理解GLUT是个啥玩意儿,官网英文解释如下:
GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL. GLUT makes it considerably easier to learn about and explore OpenGL programming. GLUT provides a portable API so you can write a single OpenGL program that works across all PC and workstation OS platforms.
GLUT is designed for constructing small to medium sized OpenGL programs. While GLUT is well-suited to learning OpenGL and developing simple OpenGL applications, GLUT is not a full-featured toolkit so large applications requiring sophisticated user interfaces are better off using native window system toolkits. GLUT is simple, easy, and small.
意思就是GLUT简单、轻便、快捷,能够在跨平台工作,比较适合初学者使用。但是GLUT并不会全能的,如果需要开发更加精致的OpenGL程序,实现更加完善的图形学画面,需要其他的工具包。
The toolkit supports:
- Multiple windows for OpenGL rendering
- Callback driven event processing
- Sophisticated input devices
- An 'idle' routine and timers
- A simple, cascading pop-up menu facility
- Utility routines to generate various solid and wire frame objects
- Support for bitmap and stroke fonts
- Miscellaneous window management functions
那么这个程序中调用的函数都有什么作用呢:
>>>>>>初始化函数
void glutInit(int* argc,char** argv)-------初始化GLUT库
void glutInitDisplayMode(unsigned int mode)--------设置图形显示模式
- GLUT_RGBA:当未指明GLUT-RGBA或GLUT-INDEX时,是默认使用的模式。表明欲建立RGBA模式的窗口。
- GLUT_RGB:与GLUT-RGBA作用相同。
- GLUT_INDEX:指明为颜色索引模式。
- GLUT_SINGLE:只使用单缓存
- GLUT_DOUBLE:使用双缓存。以避免把计算机作图的过程都表现出来,或者为了平滑地实现动画。
- GLUT_ACCUM:让窗口使用累加的缓存。
- GLUT_ALPHA:让颜色缓冲区使用alpha组件。
- GLUT_DEPTH:使用深度缓存。
- GLUT_STENCIL:使用模板缓存。
- GLUT_MULTISAMPLE:让窗口支持多例程。
- GLUT_STEREO:使窗口支持立体。
- GLUT_LUMINACE:luminance是亮度的意思。但是很遗憾,在多数OpenGL平台上,不被支持。
glutInitWindowSize(500, 500)--------设置程序要产生的窗口大小,以左上角为原点,以像素为单位
glutInitWindowPosition(100, 100)----------设置程序要产生的窗口位置,以左上角为原点,以像素为单位
>>>>>>窗口管理函数
int glutCreateWindow(char* name)---------产生一个顶层的窗口,name作为窗口的名字,也就是窗口标题栏显示的内容,返回值是生成窗口的标记符,可用函数glutGetWindow()加以引用。
>>>>>>>注册回调函数
void glutDisplayFunc(void (*func)(void) )--------为当前窗口设置显示回调函数
void glutReshapeFunc(void (*Func)(int width, int height) )----------指定当窗口的大小改变时调用的函数
>>>>>>>事件处理函数
void glutMainLoop(void)----------让 glut 程序进入事件循环。在一个glut程序中最多只能调用一次。一旦调用,会直到程序结束才返回
>>>>>>>自定义函数
init():
glClearColor()-------设置清除颜色
void glShadeModel(GLenum mode)--------指明使用哪种着色技术,可以取值GL_FLAT和GL_SMOOTH。默认取值是GL_SMOOTH, 在使用顶点数据绘制几何图形时,如果为每个顶点指定了顶点颜色,此时若使用GL_SMOOTH,每个顶点使用对应的顶点颜色来着色,而顶点之间的片元颜色则使用差值的方式来计算获得,结果就是渐变色;而若使用GL_FLAT,假设几何图形由n个三角形构成,则只会使用顶点颜色数组中最后n个颜色进行着色。 比如,左图是GL_SMOOTH着色,右图是GL_FLAT着色:
void display(void):
glClear(GL_COLOR_BUFFER_BIT)-----https://blog.csdn.net/shuaihj/article/details/7230138
glColor3f(1.0, 1.0, 1.0)------设置渲染颜色
glLoadIdentity()-------https://blog.csdn.net/shuaihj/article/details/7228265
glTranslatef(0.0, 0.0, -5.0)--------https://www.cnblogs.com/lihuiyy/archive/2011/11/10/2244317.html
glutWireCube()--------模型绘制函数
https://blog.csdn.net/augusdi/article/details/20546919
https://blog.csdn.net/yearafteryear/article/details/9174465
glflush()------https://blog.csdn.net/meegomeego/article/details/8594373
https://blog.csdn.net/xb554790401/article/details/38704493
void reshape(int w, int h):
glMatrixMode(GL_PROJECTION)------------https://blog.csdn.net/jiangdf/article/details/8460012
glFrustum()-----------https://blog.csdn.net/peng6662001/article/details/7082436
深深地觉得想要学好图形学,真的需要把计算机的原理等基础打的非常扎实。。。。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
以上链接都是讲的比较清楚的blog,后续还会继续更新~