openGL 学习笔记 2

    xiaoxiao2021-03-26  33

    蓝宝书 第二章

    1 windows平台使用头文件(根据环境不同会有变化)

    #include<windows.h> #include<gl/gl.h> #include<gl/glu.h> 

    2 openGL变量类型与C语言变量类型对应表

    表2.1 第81页

    3 API函数命名规则

    库前缀+根命令+参数数量+参数类型

    glColor3f(……)

    4 常见RGB复合颜色表

    表2.2 89页

    5 单缓冲和双缓冲

    glutInitDisplayMode(GLUT_SIMPLE)     ->使用glFlush()

    glutInitDisplayMode(GLUT_DOUBLE)   ->使用glutSwapBuffers()

    6 OpenGL状态机

    使用void glEnable(GLenum capability) 启用某一功能/状态

    使用void glDisable(GLenum capability) 禁用某一功能/状态

    使用Glboolean glIsEnabled(GLenum capability) 查询某一功能/状态是否启用

    使用下列函数获得功能/状态的参数

    void glGetBooleanv(GLenum pname, GLboolean *params); void glGetDoublev(GLenum pname, GLdouble *params); void glGetFloatv(GLenum pname, GLfloat *params); void glGetIntegerv(GLenum pname, GLint *params); 

    7 保存和恢复状态 保存状态 void glPushAttrib(GLbitfield mask)

    恢复状态 void glPopAttrib(GLbitfield mask)

    例如:glPushAtrrib(GL_TEXTURE_BIT|GL_LIGHTING_BIT)

    8 错误信息

    返回错误 Glenum glGetError(void)

    返回错误详细信息 const GLubyte* gluErrorString(GLenum errorCode)

    表2.3 错误信息列举   105页

    9 获取字符串

    const GLubyte *glGetString(GLenum name)

    const char* version = (const char*)glGetString(GL_VERSION); 获取版本号

    glGetString(GL_VENDOR); 返回OpenGL的提供厂商。

    glGetString(GL_RENDERER); 返回执行OpenGL渲染的设备,通常就是显卡的名字。

    glGetString(GL_EXTENSIONS); 返回所支持的所有扩展,每两个扩展之间用空格隔开。

    10 控制行为

    void glHint(GLenum target, GLenum mode)

    参考http://blog.csdn.net/shuaihj/article/details/7230867

    11 OpenGL扩展(对其他设备/平台的支持)

    略……

    代码:

    例2.1

    #include <Windows.h> #include <GL\GL.h> #include <GL\GLU.h> #include <GL\glut.h> void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT);//清除缓冲 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);//设置模式 单缓冲 RGB模式 glutCreateWindow("Simple");//创建窗口 glutDisplayFunc(RenderScene);//设置回调函数 循环调用 SetupRC();//初始化openGL glutMainLoop();//启动程序 --无返回 return 0; } 例2.2 #include <Windows.h> #include <GL\GL.h> #include <GL\GLU.h> #include <GL\glut.h> void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); glRectf(-50.0f, 50.0f, 50.0f, -50.0f);//设置矩形 glFlush(); } void SetupRC() { glClearColor(0.0f, 0.0f, 1.0f, 0.5f); } void ChangeSize(GLsizei w, GLsizei h)//窗口大小改变函数---2D { GLfloat aspectRatio; if (h == 0) h = 1; glViewport(0, 0, w, h);//设置新窗口大小 glMatrixMode(GL_PROJECTION); glLoadIdentity(); aspectRatio = (GLfloat)w / (GLfloat)h; if (w <= h) glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); else glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc,char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutCreateWindow("GLRect"); glutDisplayFunc(RenderScene);//显示回调函数 glutReshapeFunc(ChangeSize);//窗口大小变形回调函数 SetupRC(); glutMainLoop(); return 0; }

    例2.3 跳动方块

    #include <Windows.h> #include <GL\GL.h> #include <GL\GLU.h> #include <GL\glut.h> //初始化方块位置和大小 GLfloat x1 = 0.0f; GLfloat y1 = 0.0f; GLfloat rsize = 25; //每一步x y移动的方向 GLfloat xstep = 5.0f; GLfloat ystep = 5.0f; GLfloat windowWidth; GLfloat windowHight; void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); glRectf(x1, y1, x1 + rsize, y1 - rsize); glutSwapBuffers(); //glFlush(); } void TimerFunction(int value) { if (x1 > windowWidth - rsize || x1 < -windowWidth) xstep = -xstep; if (y1>windowHight || y1 < -windowHight + rsize) ystep = -ystep; x1 += xstep; y1 += ystep; if (x1>(windowWidth - rsize + xstep)) x1 = windowWidth - rsize - 1; else if (x1 < -(windowWidth + xstep)) x1 = -windowWidth - 1; if (y1>(windowHight + ystep)) y1 = windowHight - 1; else if (y1 < -(windowHight - rsize + ystep)) y1 = -windowHight + rsize - 1; glutPostRedisplay();//当前窗口需要重新绘制 glutTimerFunc(100, TimerFunction, 1);//确保函数循环调用 } void SetupRC() { glClearColor(0.0f, 0.0f, 1.0f, 0.5f); } void ChangeSize(GLsizei w, GLsizei h) { GLfloat aspectRatio; if (h == 0) h = 1; glViewport(0, 0, w, h);//设置新窗口大小 glMatrixMode(GL_PROJECTION); glLoadIdentity(); aspectRatio = (GLfloat)w / (GLfloat)h; if (w <= h) glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); else glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(800, 600); glutCreateWindow("Bounce"); glutDisplayFunc(RenderScene);//显示回调函数 glutReshapeFunc(ChangeSize);//窗口大小变形回调函数 glutTimerFunc(33, TimerFunction, 1);//定时调用函数 时间 函数 区别值 SetupRC(); glutMainLoop(); return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-664152.html

    最新回复(0)