1.准备资源
- GLTools
- glew
- libGLTools.a
2.创建工程
2.1 打开XCode -> macOS ->App
2.2 添加OpenGL.framework和GLUT.framework2个系统库
2.3 将准备的资源文件拖入到工程中
2.4 删除原工程中的AppDelegate.h 、AppDelegate.m、ViewController.h、ViewController.m、main.m
2.5 创建main.cpp或者main.mm.创建main.cpp文件取消掉勾选头文件项
2.6 添加头文件路径
3.测试环境是否搭建成功
main.cpp代码如下:
#include "GLShaderManager.h"
#include "GLTools.h"
#include <GLUT/GLUT.h>
/ /定义一个,着色管理器
GLShaderManager shaderManager;
/ /简单的批次容器,是GLTools的一个简单的容器类。
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,1.00,1.0,0.5f};
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[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|
GLUT_STENCIL);
//GLUT窗口大小、窗口标题
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;
}
网友评论