1.创建Mac程序: Xcode-> (File -> New ->Progect) Cocoa APP -> 填写程序名,选择工程存储地址创建
2.删除文件 : AppDelegate.h,AppDelegate.m,ViewController.h,ViewController.m,main.m
再导入文件OpenGL 工程文件文件 “include”文件夹和“libGLTools.a”文件
3. 添加系统文件库“GLUT.framework”和“OpenGL.framework”库文件 “libGLTools.a”文件是(拖入工程时)系统默认导入的, 如果没有也要添加进去.
4.创建“main.cpp”文件:
粘贴代码 //
// main.cpp
// CreatOpenGL
//
// Created by Aiken on 2018/12/16.
// Copyright © 2018 Aiken. All rights reserved.
//
#include<stdio.h>
#include "GLTools.h"
#include
GLBatchtriangleBatch;
GLShaderManager shaderManager;
voidChangeSize(intw,inth)
{
glViewport(0,0, w, h);
}
voidSetupRC()
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
shaderManager.InitializeStockShaders();
GLfloatvVerts[] = {
-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();
}
voidRenderScene(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
GLfloatvRed[] = {1.0f,0.0f,0.0f,1.0f};
shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);
triangleBatch.Draw();
glutSwapBuffers();
}
intmain(intargc,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));
return1;
}
SetupRC();
glutMainLoop();
return 0;
}
5.运行工程: 会出现错误提示,将对应位置的的<> 系统引入, 根据提示全部更改为""直接引入就可以了, 只有错误提示的地方需要更改,其他地方引用照旧. 运行效果如下