码迷,mamicode.com
首页 > 其他好文 > 详细

OpenGL 第一部分

时间:2020-07-01 17:29:57      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:oid   cat   视频   att   viewport   cli   unsigned   arrays   ==   

OpenGL 参考资料:

https://learnopengl.com/  原版

https://learnopengl-cn.github.io/  中文版

https://www.bilibili.com/video/BV11W411N7b9?from=search&seid=8920882218134207707(傅老师视频 ,目前 共有三部分)

 

OpenGL 配置:

GLAD,GLFW + CLion 编辑器:(个人觉得 jetbrains 的产品比vs 好用 )

参考文章:https://www.pianshen.com/article/17731169605/

技术图片

 

 

注:glad 网站很容易访问,glfw 网站下载很慢,可以使用下面的现成文件:

https://files.cnblogs.com/files/zach0812/glad_and_glfw_win64.zip

 

测试代码:

技术图片
#include <iostream>
#include "glad/glad.h"
#include "GLFW/glfw3.h"

using namespace std;

int main() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);


    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        glfwTerminate();
        return -1;
    }

    glViewport(0, 0, 800, 600);

    while (!glfwWindowShouldClose(window)) {

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);


        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}
main.cpp

如果都没问题了,测试效果如下:

技术图片

 

 

 

 

Hello Triangle:

技术图片
#include <iostream>
#include "glad/glad.h"
#include "GLFW/glfw3.h"

using namespace std;

float vertices[] = {
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f,
        0.0f, 0.5f, 0.0f
};

const char *vertexShaderSrc = "#version 330 core                                           \n"
                              "layout (location = 0) in vec3 aPos;                         \n"
                              "void main(){                                                \n"
                              "    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);}       \n";

const char *fragmentShaderSrc = "#version 330 core\n"
                                "out vec4 FragColor;\n"
                                "void main(){\n"
                                "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);} \n";

void porcessInput(GLFWwindow *window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, true);
    }
}

int main() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);


    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        glfwTerminate();
        return -1;
    }

    glViewport(0, 0, 800, 600);
    // VAO vertex array obj
    // VBO vertex buffer obj
    // EBO ele buffer obj

    // gpu 中生成一个 VAO
    unsigned int VAO;
    glGenVertexArrays(1, &VAO); // 产生一个 VAO,返回的是 VAO 的 id
    glBindVertexArray(VAO);

    // 把cpu输入的VBO 绑定到 上面的VAO
    unsigned int VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


    // 生成一个vertex shader
    unsigned int vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSrc, NULL); // 一个字符串 加载源码
    glCompileShader(vertexShader);


    // 生称一个fragment shader
    unsigned int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSrc, NULL);
    glCompileShader(fragmentShader);


    // 造一个program
    unsigned int shaderProgram;
    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader); // 将 vertex shader 贴到 program
    glAttachShader(shaderProgram, fragmentShader); // 将 fragment shader 贴到 program
    glLinkProgram(shaderProgram);


    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
    glEnableVertexAttribArray(0);


    while (!glfwWindowShouldClose(window)) {
        porcessInput(window);

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);
        glDrawArrays(GL_TRIANGLES,0,3);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}
main.cpp

 

OpenGL 第一部分

标签:oid   cat   视频   att   viewport   cli   unsigned   arrays   ==   

原文地址:https://www.cnblogs.com/zach0812/p/13220295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!