0

我正在尝试为我的一个小项目同时构建一个 opengl 脚本和一个 qt GUI 窗口。即使在将glad、glfw 头文件和glfw3.lib 文件链接到我的编译器设置并将glad.c 导入我的项目之后。这是发生错误的代码

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

#include <QtWidgets/QApplication>
#include "login.h"

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(int argc, char* argv[]) {
    // initialize GLFW
    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_FOWARD_COMPAT, GL_TRUE);
    #endif

    // creating the glfw window
        GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "aprende opengl", NULL, NULL);
        
        // if the window does not initialize
        if (window == NULL) {
            std::cout << "Failed to create GLFW window" << std::endl;
            glfwTerminate();
            return -1;
        }
        glfwMakeContextCurrent(window);
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

        // glad: load all openGL pointers
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
            std::cout << "Failed to initialize GLAD" << std::endl;
            return -1;
        }

        //builds the shader with our header file
        // you can name your shader files however you like
        shader ourShader("3.3.shader.vs", "3.3.shader.fs");

        // set up vertex data (and buffer(s)) and configure vertex attributes
        // ------------------------------------------------------------------
        float vertices[] = {
            // positions         // colors
             0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,  // bottom right
            -0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,  // bottom left
             0.0f,  0.5f, 0.0f,  0.0f, 0.0f, 1.0f   // top 
        };

        unsigned int VBO, VAO;
        // here are the identifiers that fail to be recognized
        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);

        // bind the Vertex Array Object first, then bind and set vertex
        // buffer(s), and then configure vertex attributes(s).
        glBindVertexArray(VAO);

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

        // position attributes
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);

        // color attributes
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
        glEnableVertexAttribArray(1);

        // You can unbind the VAO afterwards so other VAO calls won't accidentally 
        // modify this VAO, but this rarely happens. Modifying other VAOs 
        // requires a call to glBindVertexArray anyways so we generally 
        // don't unbind VAOs (nor VBOs) when it's not directly necessary.
        // glBindVertexArray(0);

        // render loop
        while (!glfwWindowShouldClose(window)) {
            // input
            processInput(window);

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

            // renderiza el triangulo
            ourShader.use();
            glBindVertexArray(VAO);
            glDrawArrays(GL_TRIANGLES, 0, 3);

            // GLFW: swap buffers and poll IO events (mouse pressed, etc)
            glfwSwapBuffers(window);
            glfwPollEvents();
        }

        glDeleteVertexArrays(1, &VAO);
        glDeleteBuffers(1, &VBO);
        // glfw: terminate, clearing all previously allocated GLFW resources.
        // ------------------------------------------------------------------
        glfwTerminate();

        // QT constructor stuff
        QApplication a(argc, argv);
        login w;
        w.show();
        return a.exec();

}

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

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);
}

这是输出(我的项目称为“simulador_7”):

>------ Build started: Project: simulador_7, Configuration: Debug x64 ------
1>shader_class.cpp
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader.h(42,35): warning C4101: 'e': unreferenced local variable
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\minwindef.h(130,1): warning C4005: 'APIENTRY': macro redefinition
1>C:\Users\Jose Miguel\Documents\simulador\librerias\include\glad\glad.h(32): message : see previous definition of 'APIENTRY'
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(60,3): error C3861: 'glGenVertexArrays': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(61,3): error C3861: 'glGenBuffers': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(65,3): error C3861: 'glBindVertexArray': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(67,3): error C3861: 'glBindBuffer': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(68,3): error C3861: 'glBufferData': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(71,3): error C3861: 'glVertexAttribPointer': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(72,3): error C3861: 'glEnableVertexAttribArray': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(75,3): error C3861: 'glVertexAttribPointer': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(76,3): error C3861: 'glEnableVertexAttribArray': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(90,4): error C3861: 'glClearColor': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(91,4): error C3861: 'glClear': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(95,4): error C3861: 'glBindVertexArray': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(96,4): error C3861: 'glDrawArrays': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(104,3): error C3861: 'glDeleteVertexArrays': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(105,3): error C3861: 'glDeleteBuffers': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(126,2): error C3861: 'glViewport': identifier not found
1>Done building project "simulador_7.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

为什么会这样?我一次又一次地链接相同的库但无济于事,清理了项目。我的其他 openGL 实践项目可以毫无问题地链接到其相应的库/包含。我已经包含了我的两个库的照片并在此处包含文件夹[1] 在此处输入图像描述。如您所见,我已经包含了glad/glfw3 目录以及QT 对应的目录。这些文件夹之间可能有重叠吗?在我添加 include/QTWidgets 路径之前,高兴的标题工作得很好。

链接到此处(库)1和此处(包括)2的图像

https://imgur.com/a/8GG995y[![这里][1]][1]

4

0 回答 0