1.opengl基本窗口设置
原教程:https://github.com/cybercser/OpenGL_3_3_Tutorial_Translation/blob/master/Tutorial%201%20Opening%20a%20window.md
这程序大概顺序为:
声明所需库->初始化glfw库->利用glfw库创建窗口->初始化glew(?????)->设置键盘检测和主要代码
开工啦。从处理依赖库开始:我们要用一些基本库在控制台显示消息:
// Include standard headers #include <stdio.h> #include <stdlib.h>
然后是GLEW库。其原理我们以后再说。
// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic. #include <GL/glew.h>
我们使用GLFW库处理窗口和键盘消息,把它也包含进来:
// Include GLFW #include <GL/glfw.h>
下文中的GLM是个很有用3D数学库,我们暂时用不到,但很快就会派上用场。GLM库很好用,但也没什么神奇的,您不妨自己试着写一个。添加“using namespace”,这样就可以不用写“glm::vec3”,直接写“vec3”。
// Include GLM #include <glm/glm.hpp> using namespace glm;
把这些#include都粘贴到playground.cpp。编译时编译器报错,说缺少main函数,那就创建一个 呗:
int main(){
首先初始化GLFW :
if( !glfwInit() ) // Initialise GLFW 初始化glfw { fprintf( stderr, "Failed to initialize GLFW\n" ); return -1; } glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing 4x抗锯齿用处不大 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); //设置opengl版本上限 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3); //设置opengl版本下限 glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )// Open a window and create its OpenGL context 打开窗口并且创建它的opengl上下文??
{ fprintf( stderr, "Failed to open GLFW window\n" ); glfwTerminate(); return -1; } // Initialize GLEW glewExperimental=true; // Needed in core profile ?? if (glewInit() != GLEW_OK) { fprintf(stderr, "Failed to initialize GLEW\n"); // return -1; } glfwSetWindowTitle( "Tutorial 01" );
生成并运行。一个窗口弹出后立即关闭了。可不是嘛,还没设置等待用户按Esc键再关闭呢:
glfwEnable( GLFW_STICKY_KEYS );// Ensure we can capture the escape key being pressed below 确认我们能够捕获到esc键的输入在其按下之前 do{ // Draw nothing, see you in tutorial 2 ! // Swap buffers 交换缓冲区?? glfwSwapBuffers(); } while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS && glfwGetWindowParam( GLFW_OPENED ) ); /*Check if the ESC key was pressed or the window was closed 检查是否键盘输入为esc和窗口是否关闭*/
别忘了补充return 0;

浙公网安备 33010602011771号