准备工作
- CLTools
- glew
- libGLTools.a
百度云盘地址:http://pan.baidu.com/s/1i4PTEb7 密码 :nbgc
配置
- 打开Xcode -> macOS -> Cocoa Application 创建Mac项目;
- 添加OpenGl.framework GLUT.framework 两个系统库;
- 添加CLTools.h,glew.h,在Bulid Settings中 Header Search path 选项添加CLTool.h 和glew.h 的路径;
- 拖动libGLTools.a静态库到工程的frameworks文件夹中.
- 删除AppDelegate.h /AppDelegate.m /main.m/ViewController.h/ViewController.m,创建main.cpp文件
测试
复制代码到main.cpp文件中
#include "GLShaderManager.h"
#include "GLTools.h"
#include <GLUT/GLUT.h>
GLShaderManager shaderManager;
GLBatch triangleBatch;
void changeSize(int w,int h)
{
glViewport(0, 0, w, h);
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
GLfloat vRed[] = {1.0,0.0,0.0,1.0f};
shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);
triangleBatch.Draw();
glutSwapBuffers();
}
void setupRC()
{
glClearColor(0.98f, 0.40f, 0.7f, 1);
shaderManager.InitializeStockShaders();
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();
}
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 status = glewInit();
if (GLEW_OK != status) {
printf("GLEW Error:%s\n",glewGetErrorString(status));
return 1;
}
setupRC();
glutMainLoop();
return 0;
}
运行,出现以下结果则证明成功
网友评论