OpenGL环境搭建

2020-07-03  本文已影响0人  卡布奇诺_95d2

准备资源

CLTools
glew
libGLTools.a

开始配置

  1. 打开Xcode->File->New->Project->App:


    image.png
  2. 将资源文件拖入工程中,libGLTools.a 可直接拖入Frameworks文件夹下,注意配置头文件搜索路径。


    image.png
  3. 将工程中不需要的文件删除,如AppDelegate.h AppDelegate.m main.m ViewController.h ViewController.m 等。
  4. 新建main.cpp文件
  5. 写入代码:
//
//  main.cpp
//  test_opengl
//
//  Created by macbook pro on 2020/7/3.
//  Copyright © 2020 hq. All rights reserved.
//

#include <stdio.h>
#include "GLTools.h"
#include <glut/glut.h>

GLBatch triangleBatch;
GLShaderManager shaderManager;

void ChangeSize(int w,int h)
{
    glViewport(0,0, w, h);
}
void SetupRC()
{
    glClearColor(0.0f,0.0f,1.0f,1.0f);
    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();
}

void RenderScene(void) {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    
    GLfloat vRed[] = {1.0f,0.0f,0.0f,1.0f};

    shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);
 

    triangleBatch.Draw();
    glutSwapBuffers();
}
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 err = glewInit();
    if(GLEW_OK != err) {
        fprintf(stderr,"glew error:%s\n",glewGetErrorString(err));
        return 1;
    }
    SetupRC();
    glutMainLoop();
    return 0;
}
  1. 运行结果


    image.png

    至止,环境搭建完成。

上一篇下一篇

猜你喜欢

热点阅读