-3

我有一个使用 opengl 的 C++ 项目。我有 main.cpp 使用 openGL 初始化,创建窗口和类似的东西。我想创建一个类,我可以在其中将一些纹理外包到着色器中。但是,当我尝试使用 shader.h 或glad.h 标头作为包含在任何其他类标头中时,出现错误:

致命错误 C1189:#error:OpenGL 标头已包含,删除此包含,很高兴已提供它

如果我在 main.cpp 中执行所有逻辑,一切都很好,只有在除 main.cpp 之外的任何地方尝试使用 openGL 函数时才会出现问题

主.cpp:

#include <glad/glad.h> // generated from https://glad.dav1d.de
#include "shader.h"

int main()
{
     ... //Do OpenGL Staff
}

着色器.h:

#ifndef SHADER_H
#define SHADER_H

#include <glad/glad.h>
#include <glm/glm.hpp>


class Shader
{
public:
    unsigned int ID;
...//some Shader definition staff
}
#endif

现在我想要外部类“Maze.h”在opengl纹理中加载它的地图,就像这样:

class Maze
{
public:
    ...//some maze-relate staff
    void LoadMazeToGL(Shader* shader)
   {
        // load and create a texture 
        // -------------------------
        glGenTextures(1, &screenTex1);
        glBindTexture(GL_TEXTURE_1D, screenTex1);
        // set the texture wrapping parameters
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // set texture wrapping to GL_REPEAT (default wrapping method)
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        // set texture filtering parameters
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        GLint curMapId = glGetUniformLocation(shader->ID, "shaderInternalVarName");
        glUniform1i(curMapId, 2); // Texture unit 2 is for current map.

        ... //define and fill tex1data using Maze private information
    
        glActiveTexture(GL_TEXTURE0 + 2);
        glBindTexture(GL_TEXTURE_1D, screenTex1);
        glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, MazeIntegerSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex1data);
    
    }
}
4

1 回答 1

0

To check for problems like this use the -E flag of GCC/g++ or an alternative for your compiler. This outputs the result of the pre-processor (so the includes etc). You can then have a look at the files produced and look for the double include. You may have just forgotten an include guard somewhere :)

于 2021-01-25T18:18:27.310 回答