2020-05-31 08:30:28 登录注册 RSS

当前位置: 公理网 >> 反腐行动 >> 利用GLFW和GLEW搭建OpenGL开发环境

利用GLFW和GLEW搭建OpenGL开发环境
发布时间:2018-11-03| 来源:公理网 | 点击发表评论
版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/dejingqianshen/article/details/52717258
(如需交流,请关注公众号:神马观止)

1.下载相应的头文件和库文件

GLEW:http://glew.sourceforge.net/index.html

GLFW:http://www.glfw.org/download.html

得到glew-2.0.0-win32.zip和glfw-3.2.1.bin.WIN32.zip,这里我们采用静态链接库的方式,IDE采用的是VisualStudio2013,用32位库文件。

(1)头文件:在本地创建文件夹OpenGL,在OpenGL下创建inlcude文件夹,然后将\glfw-3.2.1.bin.WIN32\include下的GLFW拷贝到inlcude里,并将\glew-2.0.0\include中的GL也拷贝到include文件夹中。

(2)LIB文件:将\glfw-3.2.1.bin.WIN32\lib-vc2013下的所有文件拷贝到C:\ProgramFiles(x86)\WindowsKits\8.1\Lib\winv6.3\um\x86,同时将\glew-2.0.0\lib\Release\Win32下的所有文件也拷贝到C:\ProgramFiles(x86)\WindowsKits\8.1\Lib\winv6.3\um\x86。

2.打开VS2013,新建工程

20161001133944389?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


建立一个Win32控制台程序,同时勾选空项目的选项

20161001133952705?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


然后添加一个.cpp文件编写主程序

20161001133958702?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


3.添加GLFW和GLEW的头文件包含目录,右击工程“HelloTriangle”,选择“属性”,如下图,选择“VC++目录”。

20161001134005174?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


20161001134010343?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


选择OpenGL\include文件夹

20161001134013924?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


4.将LIB文件名添加到工程

20161001134018280?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


20161001134023534?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


5.编写OpenGL应用程序

20161001134027765?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


6.运行

20161001134032534?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"alt=""/>


7.代码



#includeiostream//GLEW#defineGLEW_STATIC#includeGL/glew.h//GLFW#includeGLFW/glfw3.h//Functionprototypesvoidkey_callback(GLFWwindow*window,intkey,intscancode,intaction,intmode);//WindowdimensionsconstGLuintWIDTH=800,HEIGHT=600;//ShadersconstGLchar*vertexShaderSource="#version330core\n""layout(location=0)invec3position;\n""voidmain()\n""{\n""gl_Position=vec4(position.x,position.y,position.z,1.0);\n""}\0";constGLchar*fragmentShaderSource="#version330core\n""outvec4color;\n""voidmain()\n""{\n""color=vec4(1.0f,0.5f,0.2f,1.0f);\n""}\n\0";//TheMAINfunction,fromherewestarttheapplicationandrunthegameloopintmain(){std::cout"StartingGLFWcontext,OpenGL3.3"std::endl;//InitGLFWglfwInit();//SetalltherequiredoptionsforGLFWglfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);//CreateaGLFWwindowobjectthatwecanuseforGLFW'sfunctionsGLFWwindow*window=glfwCreateWindow(WIDTH,HEIGHT,"LearnOpenGL",nullptr,nullptr);glfwMakeContextCurrent(window);//SettherequiredcallbackfunctionsglfwSetKeyCallback(window,key_callback);//SetthistotruesoGLEWknowstouseamodernapproachtoretrievingfunctionpointersandextensionsglewExperimental=GL_TRUE;//InitializeGLEWtosetuptheOpenGLFunctionpointersglewInit();//Definetheviewportdimensionsintwidth,height;glfwGetFramebufferSize(window,width,height);glViewport(0,0,width,height);//Buildandcompileourshaderprogram//VertexshaderGLuintvertexShader=glCreateShader(GL_VERTEX_SHADER);glShaderSource(vertexShader,1,vertexShaderSource,NULL);glCompileShader(vertexShader);//CheckforcompiletimeerrorsGLintsuccess;GLcharinfoLog[512];glGetShaderiv(vertexShader,GL_COMPILE_STATUS,success);if(!success){glGetShaderInfoLog(vertexShader,512,NULL,infoLog);std::cout"ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"infoLogstd::endl;}//FragmentshaderGLuintfragmentShader=glCreateShader(GL_FRAGMENT_SHADER);glShaderSource(fragmentShader,1,fragmentShaderSource,NULL);glCompileShader(fragmentShader);//CheckforcompiletimeerrorsglGetShaderiv(fragmentShader,GL_COMPILE_STATUS,success);if(!success){glGetShaderInfoLog(fragmentShader,512,NULL,infoLog);std::cout"ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n"infoLogstd::endl;}//LinkshadersGLuintshaderProgram=glCreateProgram();glAttachShader(shaderProgram,vertexShader);glAttachShader(shaderProgram,fragmentShader);glLinkProgram(shaderProgram);//CheckforlinkingerrorsglGetProgramiv(shaderProgram,GL_LINK_STATUS,success);if(!success){glGetProgramInfoLog(shaderProgram,512,NULL,infoLog);std::cout"ERROR::SHADER::PROGRAM::LINKING_FAILED\n"infoLogstd::endl;}glDeleteShader(vertexShader);glDeleteShader(fragmentShader);//Setupvertexdata(andbuffer(s))andattributepointers//GLfloatvertices[]={////Firsttriangle//0.5f,0.5f,//TopRight//0.5f,-0.5f,//BottomRight//-0.5f,0.5f,//TopLeft////Secondtriangle//0.5f,-0.5f,//BottomRight//-0.5f,-0.5f,//BottomLeft//-0.5f,0.5f//TopLeft//};GLfloatvertices[]={0.5f,0.5f,0.0f,//TopRight0.5f,-0.5f,0.0f,//BottomRight-0.5f,-0.5f,0.0f,//BottomLeft-0.5f,0.5f,0.0f//TopLeft};GLuintindices[]={//Notethatwestartfrom0!0,1,3,//FirstTriangle1,2,3//SecondTriangle};GLuintVBO,VAO,EBO;glGenVertexArrays(1,VAO);glGenBuffers(1,VBO);glGenBuffers(1,EBO);//BindtheVertexArrayObjectfirst,thenbindandsetvertexbuffer(s)andattributepointer(s).glBindVertexArray(VAO);glBindBuffer(GL_ARRAY_BUFFER,VBO);glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(GLfloat),(GLvoid*)0);glEnableVertexAttribArray(0);glBindBuffer(GL_ARRAY_BUFFER,0);//Notethatthisisallowed,thecalltoglVertexAttribPointerregisteredVBOasthecurrentlyboundvertexbufferobjectsoafterwardswecansafelyunbindglBindVertexArray(0);//UnbindVAO(it'salwaysagoodthingtounbindanybuffer/arraytopreventstrangebugs),remember:doNOTunbindtheEBO,keepitboundtothisVAO//Uncommentingthiscallwillresultinwireframepolygons.//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//Gameloopwhile(!glfwWindowShouldClose(window)){//Checkifanyeventshavebeenactiviated(keypressed,mousemovedetc.)andcallcorrespondingresponsefunctionsglfwPollEvents();//Render//ClearthecolorbufferglClearColor(0.2f,0.3f,0.3f,1.0f);glClear(GL_COLOR_BUFFER_BIT);//DrawourfirsttriangleglUseProgram(shaderProgram);glBindVertexArray(VAO);//glDrawArrays(GL_TRIANGLES,0,6);glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);glBindVertexArray(0);//SwapthescreenbuffersglfwSwapBuffers(window);}//Properlyde-allocateallresourcesoncethey'veoutlivedtheirpurposeglDeleteVertexArrays(1,VAO);glDeleteBuffers(1,VBO);glDeleteBuffers(1,EBO);//TerminateGLFW,clearinganyresourcesallocatedbyGLFW.glfwTerminate();return0;}//Iscalledwheneverakeyispressed/releasedviaGLFWvoidkey_callback(GLFWwindow*window,intkey,intscancode,intaction,intmode){if(key==GLFW_KEY_ESCAPEaction==GLFW_PRESS)glfwSetWindowShouldClose(window,GL_TRUE);}



阅读更多

授予每个自然月内发布4篇或4篇以上原创或翻译IT博文的用户。不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

最新新闻

手机浏览

公理网 版权所有

公理网 Total 0.048396(s) query 6, 报料QQ:点击这里

给我发消息