首页 > 其他分享 >LearnOpenGL "Hello Triangle"

LearnOpenGL "Hello Triangle"

时间:2022-12-04 16:35:09浏览次数:46  
标签:Triangle int GLFWwindow window LearnOpenGL glfwWindowHint GLFW NULL Hello

放图占位

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

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}
/// <summary>
/// 输入函数
/// </summary>
/// <param name="window">窗口</param>
void processInput(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 the window" << std::endl;
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    //初始化GLAD
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    //视口,前两个参数控制窗口左下角的位置
    glViewport(0, 0, 800, 600);

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

    //顶点着色器
    unsigned int vertexShader;
    //创建的着色器类型以参数形式提供给glCreateShader,此处为顶点着色器
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    //片段着色器
    unsigned int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &FragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    //着色器程序
    unsigned int shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    float vertices[] = {
        -0.5f, -0.5f, 0.0f, // left 
        0.0f,-0.5f,0.0f,//中间
        -0.25f,0.5f,0.0f,
        0.25f,0.5f,0.0f,
        0.5f, -0.5f, 0.0f // right 
    };

    unsigned int indices[] = {
        0,1,2,
        1,3,4
    };

    unsigned int VBO, VAO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),indices,GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    //解绑
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    while (!glfwWindowShouldClose(window)) {
        //输入
        processInput(window);

        //渲染指令
        glClearColor(0.2f, 0.3f, 0.3f, 0.1f);
        glClear(GL_COLOR_BUFFER_BIT);
        
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        //线框模式
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        glDrawElements(GL_TRIANGLES, 6,GL_UNSIGNED_INT,0);
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        //检查并调用事件,交换缓冲
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &EBO);
    glDeleteProgram(shaderProgram);

    glfwTerminate();
    return 0;
}

 

 

标签:Triangle,int,GLFWwindow,window,LearnOpenGL,glfwWindowHint,GLFW,NULL,Hello
From: https://www.cnblogs.com/lofu/p/16950072.html

相关文章

  • Hello world!
    大家好!我是练习时长114514年的......(等下跑题了)我是大爱无疆。我的Minecraft基岩版好友是:BureelSum191826我的网易版名字是:申二二我的bilibili是:密客小村民感谢大家......
  • [ABC251Ex] Fill Triangle 题解
    [ABC251Ex]FillTriangleSolution目录[ABC251Ex]FillTriangleSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面存在序列$a_n$,将......
  • Hello world
    这是我的第一篇技术博客,刚刚开始学习C语言,下面开始我的第一个helloworld//包含stdio.h的文件#include<stdio.h>intmain()//主函数-程序的入口,有且只有一个main函......
  • 极简的HelloWorld程序
    使用汇编代码,不引入libc库。代码://no_main.ccharstr[]="Helloworld!\n";voidmy_print(){asm("movl$13,%%edx\n\t""movq%0,%%rcx\n\t""movl......
  • Java第一课Hello World
    java第一课HelloWorld学习新建文件夹放写的代码新建.txt文件,并写入java输出HelloWorld的代码 publicclassHello{   publicstaticvoidmain(String[......
  • 汇编helloworld程序
    datasegment;定义数据段outputdb'Helloworld!$';定义一个字符串,记得要以$为结束标志dataends;数据段结束codesegment;代码段定义开始assumeds:data,cs......
  • spring boot入门---helloworld
    功能:浏览器发送hello请求,服务器接收请求并处理,响应helloworld字符串。1、创建一个Maven工程(jar)2、导入springboot相关的依赖<parent>  <groupId>org.springframework.......
  • 在Eclipse中用Maven创建Servlet实现输出Hello World
    1、新建servlet2、内容如图3、重新部署运行......
  • 《Spring Boot 实战:从0到1》第1章 快速开始Hello World
    第1章快速开始HelloWorld大约20年前,程序员们使用“企业级JavaBean”(EJB)开发企业应用,需要配置复杂的XML。在二十世纪初期,新兴Java技术——Spring,横空出世。使用极简XML和P......
  • 编写Hello World!(0基础)
    HelloWorld新建一个文件夹,存放代码新建一个Java文件新建一个文本文档,后缀名改为.java【注意】系统可能没有打开后缀名编写代码publicclassHello{ ......