首页 > 其他分享 >OpenGL学习01-环境配置-实测好用

OpenGL学习01-环境配置-实测好用

时间:2024-10-15 16:20:47浏览次数:8  
标签:01 OpenGL int void 0.5 window GLFW GL 好用

首先下载Visual Studio 2022,配置环境,安装库等

开发环境:Visual Studio 2022

语言:C++

freeglut 库
glfw 库

以上两个库用于窗口管理

glew 库
glad 库

以上两个库帮助我们链接到openGL比较新的实现方法

相同功能库可以二选一

Visual Studio 2022安装教程

参考可以这个

Visual Studio 2022安装和使用(适合新手) - 知乎 (zhihu.com)

配置openGL的库+调试代码

教程参考可以这个

00-配置 Visual Studio Comminity 2022 OpenGL 开发环境_哔哩哔哩_bilibili

1 freeglut 库调试代码

#include <GL/freeglut.h>
#include <stdlib.h>
#include <stdio.h>
void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //glRectf(-0.5f,-0.5f,0.5f,.5f);
    glutSolidTeapot(0.5f);
    //glFlush();
    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("OpenGL with FreeGlut mini");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

2 glfw 库调试代码

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while(!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClearColor(1.0, 0, 0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}
 

3 glew k库调试代码

#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>

int main() {
    // start GL context and O/S window using the GLFW helper library
    if (!glfwInit()) {
        fprintf(stderr, "ERROR: could not start GLFW3\n");
        return 1;
    }

    // uncomment these lines if on Apple OS X
    /*glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/

    GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
    if (!window) {
        fprintf(stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent(window);

    // start GLEW extension handler
    glewExperimental = GL_TRUE;
    glewInit();

    // get version info
    const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString(GL_VERSION); // version as a string
    printf("Renderer: %s\n", renderer);
    printf("OpenGL version supported %s\n", version);

    // tell GL to only draw onto a pixel if the shape is closer to the viewer
    glEnable(GL_DEPTH_TEST); // enable depth-testing
    glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"

    /* OTHER STUFF GOES HERE NEXT */
    float points[] = {
   0.0f,  0.5f,  0.0f,
   0.5f, -0.5f,  0.0f,
  -0.5f, -0.5f,  0.0f
    };
    GLuint vbo = 0;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
    GLuint vao = 0;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    const char* vertex_shader =
        "#version 400\n"
        "in vec3 vp;"
        "void main() {"
        "  gl_Position = vec4(vp, 1.0);"
        "}";
    const char* fragment_shader =
        "#version 400\n"
        "out vec4 frag_colour;"
        "void main() {"
        "  frag_colour = vec4(0.5, 0.0, 0.5, 1.0);"
        "}";
    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs, 1, &vertex_shader, NULL);
    glCompileShader(vs);
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, 1, &fragment_shader, NULL);
    glCompileShader(fs);
    GLuint shader_programme = glCreateProgram();
    glAttachShader(shader_programme, fs);
    glAttachShader(shader_programme, vs);
    glLinkProgram(shader_programme);

    while (!glfwWindowShouldClose(window)) {
        // wipe the drawing surface clear
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram(shader_programme);
        glBindVertexArray(vao);
        // draw points 0-3 from the currently bound VAO with current in-use shader
        glDrawArrays(GL_TRIANGLES, 0, 3);
        // update other events like input handling 
        glfwPollEvents();
        // put the stuff we've been drawing onto the display
        glfwSwapBuffers(window);
    }

    // close GL context and any other GLFW resources
    glfwTerminate();
    return 0;
}

4 glad 库调试代码

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

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    //------------------------ ---- - ~
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation

    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGl - Explained", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create:GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    // glad; load al1 OpenGl function pointers

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


    // -- 步骤 1: 准备着色器程序
    unsigned int shaderProgram = glad_glCreateProgram();

    // 着色器程序: vertexShader 子程序
    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";
    unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    // 着色器程序: fragmentshader 子程序
    const char* frapmentShaderSource = "#version 330 core\n"
        "out vec4 FragColor;\n"
        "void main()\n"
        "{\n"
        "    FragColor = vec4(1.0f,0.5f,0.2f,1.0f);\n"
        "}";
    unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &frapmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    // END --------

// 步 2:准备数据这是一维数组需要 vao 来声明格式,shader program 根据 vao 来解析)
    float vertices[] = {
        -0.5f, -0.5f,0.0f,
        0.5f,-0.5f,0.0f,
        0.0f,0.5f,0.0f
    };

    // 步骤 3: 声明数据结构 : 至少一个 VA
    unsigned int vao;
    glGenVertexArrays(1, &vao);
    //[HOWO] glVertexAttribpointer(index, size, type, normalized, stride, pointer) :
    glBindVertexArray(vao); // 必须绑定到 OPENGL 上下文之后才能设置,否则不会输出图像,设置 VAO
    unsigned int vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo); // 当前上下文 GL ARRAY BUFFER 设置为 vbo r
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // 填充 VertexArrays 的第-项
    glEnableVertexAttribArray(0); // VertexArrays 的第一项准备就绪
    // END---- -


    while (!glfwWindowShouldClose(window))
    {
        processInput(window);
        // -- 渲港流程 -----------
        //物体 1
        glBindVertexArray(vao); // 绑定(设置) v为当前上下文 VAO,若前面已经过一次,没有变化的话,不需要
        glUseProgram(shaderProgram);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        // 物体 2...n (重复物体 1 的流程)
        //.-- end ----------
        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // ----
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    // glfw: terminate, clearing all previously allocated GLFW resources
    glfwTerminate();
    return 0;
}
// process all input : query GLFW whether relevant keys are pressed / released this frame and react accordingly
void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window,true);
}
//glfw: whenever the window size changed(by 0s or user resize) this callback function executes
void framebuffer_size_callback(GLFWwindow * window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and
    // height will be significantly larger than specified on retina displays.
    glViewport(0,0,width,height);
}

标签:01,OpenGL,int,void,0.5,window,GLFW,GL,好用
From: https://blog.csdn.net/weixin_45628169/article/details/142952585

相关文章

  • Exchange2016服务器使用到的9个端口(重要)
    Exchange2016服务器使用到的9个端口(重要)端口名称及用途:443:客户端连接CAS服务器(web,Outlook)80:有时候通过80端口访问owa,会进行重定向到443IMAP协议端口:     143:非加密端口     993:加密端口pop3协议端口:     110:非加密端口     995:加......
  • 单元格之间创建循环超链接01
    我们可以使用openpyxl库来操作Excel文件。以下是代码,展示了如何在指定的工作表中为具有相同值的多个单元格之间创建循环超链接安装openpyxl首先,确保你已经安装了openpyxl库。如果没有安装,可以使用以下命令进行安装:pipinstallopenpyxlimportopenpyxldefcrea......
  • cvpr注意事项和注册流程(2025版)(20241015更新还未开放注册)
    本文章基于现有网上没有cvpr详细版本的一步一步的注册流程进行编写,用于指导自己和方便他人进行注册。接下来将从CVPR2025的重要节点、变更事项、注册流程进行说明重要节点CVPR2025变更的重要事项Duetothedramaticincreaseinthenumberofsubmissionsandthedeterio......
  • 1014 CW 模拟赛 D.进化
    题面挂个pdf题面下载算法分析题目发现,一次进化等效于:在\(a\)两端加\(0\)对于\(i\in[1,n],a_i\leftarrowa_{i-1}\oplusa_{i+1}\)于是猜测在\(k\)次操作之后有\(a_i\leftarrowa_{i+k}\oplusa_{i-k}\)代入计算后发现这个式子显然错误,原因......
  • 错误:1001 SQLSTATE: HY000 (ER_NISAMCHK)
    遇到 SQLSTATE:HY000(ER_NISAMCHK) 错误通常表示MySQL在处理某些操作时出现了内部错误。这个错误代码并不常见,但通常与数据文件或表结构的完整性有关。以下是可能的原因及解决步骤:数据文件损坏数据文件可能已经损坏,导致MySQL无法正确读取或处理数据。表结构问题......
  • NFLS 241014 比赛总结
    T1JZOI5246TripProblem有一串长为\(n\)的序列\(a\),有\(m\)组询问,每组询问给出一个区间,求区间内有多少个数满足以下条件之一:在区间内,它的左侧不存在大于它的数。在区间内,它的右侧不存在大于它的数。Solution离散化,用权值线段树求出序列上每个数左边和右边第一个比它......
  • [TJOI2019] 甲苯先生的字符串 题解
    T2[TJOI2019]甲苯先生的字符串矩阵乘法优化DP,所以一般来说这种DP都不怎么难。30pts的DP是裸的:设\(f_{i,j}\)为前\(i\)位、最后一位是\(j\)的方案数,则有转移方程:\[f_{i,j}=\sum_{k=0}^{25}f_{i-1,k}\landk\nej\]想要矩阵优化,我们想到构造答案矩阵:\[\mathit{an......
  • 每日总结001
    一.今天中午看了一本小说,看得着迷,然后很急切的想知道后续剧情,如果之前遇到这种情况,我会毫不犹豫的继续追,但是今天我做出了改变,我努力说服自己沉迷于小说会浪费很多宝贵的时间,我应该适可而止,并且下午还有课,最终我克服了自己的欲望,选择了休息。二.下午两点上课,中午1.15的闹钟响了......
  • [NOI2014] 动物园——KMP 倍增
    [NOI2014]动物园题目描述近日,园长发现动物园中好吃懒做的动物越来越多了。例如企鹅,只会卖萌向游客要吃的。为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习算法。某天,园长给动物们讲解KMP算法。园长:“对于一个字符串\(S\),它......
  • Leetcode_exercise_01
    题目两数之和枚举所有可能的两个不同的数字之和,与target做比较。哈希表查询//方法一:classSolution{public:vector<int>twoSum(vector<int>&nums,inttarget){intn=nums.size();for(inti=0;i<n;++i){......