OpenGL - 纯C语言开发

Xcode环境: Version 8.3 (8E162)
glut已经被放弃使用了,在Xcode中无法调用glut的方法,所以需要配置GLEW个GLFW.
配置GLEW+GLFW并使用Xcode写OpenGL应用

第一个OpenGL程序 :


#include     //高级扩展
#include  //代替glut

#include 

#pragma mark - 图形渲染
void Render(void) {
    //设置背影颜色
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    //清除一个或一组特定的缓冲区
    glClear(GL_COLOR_BUFFER_BIT);
    //开始渲染,点,线,片
    glBegin(GL_LINE_LOOP);
    {
        //红
        glColor3f(1.0,0.0,0.0);
        //顶点二维坐标
        glVertex2f(0, .5);
        
        //绿
        glColor3f(1.0,0.0,0.0);
        //顶点二维坐标
        glVertex2f(-.5,-.5);
        
        //蓝
        glColor3f(1.0, 0.0, 0.0);
        //顶点二维坐标
        glVertex2f(.5, -.5);
        
    }
    //结束
    glEnd();
}

#pragma mark - 回调
static void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}
#pragma mark - 创建窗口
int glCreateWindow() {
    
    glfwSetErrorCallback(error_callback);
    
    if(!glfwInit()) return -1;
    //创建一个视窗
    GLFWwindow* win;
    win = glfwCreateWindow(960, 640, "模板", NULL, NULL);
    
    if(!win) {
        //程序终止
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    
    //驱动程序的初始化中没有出现任何问题
    if(!glewInit()) return -1;
    
    //设置当前窗口上下文
    glfwMakeContextCurrent(win);
    glfwSetKeyCallback(win, key_callback);
    
    //这里是一只持续地做渲染工作
    while(!glfwWindowShouldClose(win)){
        //图形渲染工作
        Render();
        glfwSwapBuffers(win);
        glfwPollEvents();
    }
    
    //程序终止
    glfwDestroyWindow(win);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}


#pragma mark - 主程序入口
int main(int argc, const char * argv[]) {
    glCreateWindow();
    return 0;
}

你可能感兴趣的:(OpenGL - 纯C语言开发)