OpenGL 初探 (Mac OS 下 OpenGL环境搭建)
2020-07-02 本文已影响0人
蚂蚁安然
一. 例子
按照下面的步骤完成后就可以得倒如下的例子🌰 Github
二. 准备资源库
资源库图片.png资源库可以在这下载
三. 配置 OpenGL 环境
1.创建项目 Xcode -> macOS -> App
项目创建.png
2.AppDelegate.h、AppDelegate.m、main.m、ViewController.h、 ViewController.m 删除
删除.png
3.创建 main.cpp文件
创建.png
4.添加OpenGl.framework 和 GLUT.framework
添加.png
5.添加前面准备的库
5.1将库图入其中如下图选择
截屏2020-07-02下午2.52.25.png
5.2完成后如下图
截屏2020-07-02下午2.54.57.png
6.配置路径GLTools.h,glew.h Bulid Settings Header Search path GLTool.h glew.h
截屏2020-07-02下午3.00.45.png
四.添加代码测试
//
// 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;
}