Mac OpenGL

//
//  main.c
//  OpenGL
//
//  Created by gang.zhou on 2020/1/13.
//  Copyright © 2020 gang.zhou. All rights reserved.
//

#include <stdio.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

int main(const int argc, const char * argv[]) {
    if(GLFW_TRUE == glfwInit()){
        GLFWwindow* window = glfwCreateWindow(160 * 4 * 1.5, 90 * 4 * 1.5, "Hello", NULL, NULL);
        if(NULL != window){
            glfwMakeContextCurrent(window);
            gladLoadGL();
            glViewport(0, 0, 640 * 1.5, 360 * 1.5);
            
            //顶点Sharder
            const GLuint vertexShader =  glCreateShader(GL_VERTEX_SHADER);
            const GLchar* vertexSource = "attribute vec3 aPos;\n varying vec3 aColor;\n void main(){\n gl_Position = vec4(aPos,1.0);\n aColor = vec3(aPos.x < 0.0?aPos.x * -1.0:aPos.x,aPos.x < 0.0?aPos.y * -1.0:aPos.y,aPos.z < 0.0?aPos.z * -1.0:aPos.z);\n}\n";
            glShaderSource(vertexShader, 1, &vertexSource, NULL);
            glCompileShader(vertexShader);
            GLint success;
            glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
            if(!success){
                GLchar infoLog[1024] = {0};
                glGetShaderInfoLog(vertexShader, 1024, NULL, infoLog);
                printf("vertexShader %s \n",infoLog);
            }
            
            
            const GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
            const GLchar* fragmentSource = "varying vec3 aColor;\n void main(){\n gl_FragColor = vec4(aColor,1.0);\n }\n";
            glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
            glCompileShader(fragmentShader);
            glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
            if(!success){
                GLchar infoLog[1024] = {0};
                glGetShaderInfoLog(fragmentShader, 1024, NULL, infoLog);
                printf("fragmentShader %s \n",infoLog);
            }
            GLuint shaderProgram =  glCreateProgram();
            glAttachShader(shaderProgram, vertexShader);
            glAttachShader(shaderProgram, fragmentShader);
            glLinkProgram(shaderProgram);
            glDeleteShader(vertexShader);
            glDeleteShader(fragmentShader);
            glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
            if(!success){
                GLchar infoLog[1024] = {0};
                glGetProgramInfoLog(shaderProgram, 1024, NULL, infoLog);
                printf("%s \n",infoLog);
            }
            
            const GLint aPosLocation =  glGetAttribLocation(shaderProgram, "aPos");
            const float vertices[] = {
                -0.5f,-0.5f,0,
                0.5f,-0.5f,0,
                0.5f,0.5f,0,
                -0.5f,0.5f,0
            };
            
            const GLint indices[] = {
                0, 1, 3,
                1, 2, 3
            };
            
            GLuint VAO;
            glGenVertexArraysAPPLE(1, &VAO);
            glBindVertexArrayAPPLE(VAO);
            
            GLuint VBO;
            glGenBuffers(1, &VBO);
            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
            
            GLuint EBO;
            glGenBuffers(1, &EBO);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
            
            glVertexAttribPointer(aPosLocation, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
            glEnableVertexAttribArray(aPosLocation);
            
            
            const float vertices2[] = {
                -1.0f,-1.0f,0,
                1.0f,-1.0f,0,
                1.0f,1.0f,0,
            };
            
            GLuint VAO2;
            glGenVertexArraysAPPLE(1, &VAO2);
            glBindVertexArrayAPPLE(VAO2);
            GLuint VBO2;
            glGenBuffers(1, &VBO2);
            glBindBuffer(GL_ARRAY_BUFFER, VBO2);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
            glVertexAttribPointer(aPosLocation, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
            glEnableVertexAttribArray(aPosLocation);
            
            while(!glfwWindowShouldClose(window)){
                glClearColor(0.5, 0, 0.5, 1);
                glClear(GL_COLOR_BUFFER_BIT);
                glUseProgram(shaderProgram);
                glBindVertexArrayAPPLE(VAO);
                glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);
                
                glBindVertexArrayAPPLE(VAO2);
                glDrawArrays(GL_TRIANGLES, 0, 3);
                glfwSwapBuffers(window);
                glfwPollEvents();
            }
            glDeleteVertexArraysAPPLE(1, &VAO);
            glDeleteBuffers(1, &VBO);
            glDeleteBuffers(1, &EBO);

            glDeleteVertexArraysAPPLE(1, &VAO2);
            glDeleteBuffers(1, &VBO2);
        }
    }
    glfwTerminate();
    return 0;
}

屏幕快照 2020-01-13 下午6.46.58.png

推荐阅读更多精彩内容