一. 例子
按照下面的步驟完成后就可以得倒如下的例子?? Github
二. 準備資源庫
資源庫可以在這下載
三. 配置 OpenGL 環(huán)境
1.創(chuàng)建項目 Xcode -> macOS -> App
2.AppDelegate.h、AppDelegate.m豪嗽、main.m、ViewController.h、 ViewController.m 刪除
3.創(chuàng)建 main.cpp文件
4.添加OpenGl.framework 和 GLUT.framework
5.添加前面準備的庫
5.1將庫圖入其中如下圖選擇
5.2完成后如下圖
6.配置路徑GLTools.h,glew.h Bulid Settings Header Search path GLTool.h glew.h
四.添加代碼測試
//
// main.cpp
// OpenGL
//
// Created by Jonathan on 2020/7/2.
// Copyright ? 2020. All rights reserved.
//
#include <stdio.h>
#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,1.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;
}