标签:
1.下载OpenGL超级宝典5(蓝宝书)的资源,链接如下:http://pan.baidu.com/s/1c02PtYC,解压得到SB5文件夹,里面有书籍的课程源码以及GLTools库。
2.依次点击Xcode-->Preferences-->Locations-->Sources Trees,再点“+”号按钮添加Name: GLTOOLS_INCLUDE, Display Name: GLTools include Folder, Path: ~/Desktop/SB5/src/GLTools/include(本人解压的SB5文件夹在桌面,读者可以根据自己情况添加)。
3.Xcode下新建一个Cocoa工程,命名Triangle,删除项目下得AppDelegate.*两个文件和main.m文件。
4.点击项目-->TARGETS-->Build Settings-->Header Search Paths,添加 $(GLTOOLS_INCLUDE);然后同样的点击项目-->TARGETS-->Build Phases-->Link Binary With Libraries,点击“+”号添加,GLUT.framework,OpenGL.framework,点击Add Other添加桌面/SB5/Src/GLTools/libGLTools.a文件
5.右击项目-->New File-->C++ File,新增一个Triangle.cpp(不需要头文件),将下面代码拷贝:
// // Triangle.cpp // Triangle // // Created by Tank on 15/8/11. // Copyright (c) 2015年 jinx. All rights reserved. // // Triangle.cpp // Our first OpenGL program that will just draw a triangle on the screen. #include <GLTools.h> // OpenGL toolkit #include <GLShaderManager.h> // Shader Manager Class #ifdef __APPLE__ #include <glut/glut.h> // OS X version of GLUT #else #define FREEGLUT_STATIC #include <GL/glut.h> // Windows FreeGlut equivalent #endif GLBatch triangleBatch; GLShaderManager shaderManager; /////////////////////////////////////////////////////////////////////////////// // Window has changed size, or has just been created. In either case, we need // to use the window dimensions to set the viewport and the projection matrix. void ChangeSize(int w, int h) { glViewport(0, 0, w, h); } /////////////////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering context. // This is the first opportunity to do any OpenGL related tasks. void SetupRC() { // Blue background glClearColor(0.0f, 0.0f, 1.0f, 1.0f ); shaderManager.InitializeStockShaders(); // Load up a triangle GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f }; triangleBatch.Begin(GL_TRIANGLES, 3); triangleBatch.CopyVertexData3f(vVerts); triangleBatch.End(); } /////////////////////////////////////////////////////////////////////////////// // Called to draw scene void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f }; shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed); triangleBatch.Draw(); // Perform the buffer swap to display back buffer glutSwapBuffers(); } /////////////////////////////////////////////////////////////////////////////// // Main entry point for GLUT based programs int main(int argc, char* argv[]) { gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); glutInitWindowSize(800, 600); glutCreateWindow("Triangle"); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); return 1; } SetupRC(); glutMainLoop(); return 0; }
6.cmd + R运行,就可以看到:
OK,第一个三角形成功出现(PS:本人第一次写博客,咳咳,很多不完善,见谅哦)。
=======================================================
原创文章,转载请注明 http://www.cnblogs.com/cheergo/,邮件avril376495267@qq.com,欢迎各位与我展开交流!
=======================================================
Mac OSX下搭建OpenGL超级宝典5(蓝宝书)开发环境(运行第一个三角形程序)
标签:
原文地址:http://www.cnblogs.com/cheergo/p/4721393.html