我有以下简单的结构,它代表一个OpenGL纹理:
#pragma once
#include <GL/glew.h>
#include <string>
#include "Shader.hpp"
struct Texture
{
static constexpr GLenum UNIT[] = {GL_TEXTURE0, GL_TEXTURE1, GL_TEXTURE2, GL_TEXTURE3,
GL_TEXTURE4, GL_TEXTURE5, GL_TEXTURE6, GL_TEXTURE7,
GL_TEXTURE8, GL_TEXTURE9, GL_TEXTURE10, GL_TEXTURE11,
GL_TEXTURE12, GL_TEXTURE13, GL_TEXTURE14, GL_TEXTURE15};
int width, height;
int unit;
GLuint id;
void use(const Shader &shader, const std::string &name) const
{
glActiveTexture(UNIT[unit]);
glBindTexture(GL_TEXTURE_2D, id);
glUniform1i(shader.getLocation(name), unit);
}
};
变量GL_TEXTURE0
到GL_TEXTURE15
在glew.h中定义,如下摘录:
#define GL_TEXTURE0 0x84C0
#define GL_TEXTURE1 0x84C1
#define GL_TEXTURE2 0x84C2
//etc...
当我尝试编译代码时,出现以下错误:
Undefined symbols for architecture x86_64:
"Texture::UNIT", referenced from:
Texture::use(Shader const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const in main.cpp.o
ld: symbol(s) not found for architecture x86_64
我显然在结构中定义并初始化了UNIT
,但它声称它不存在。如果在UNIT
方法中访问Texture::UNIT
时将use
更改为UNIT
,则会出现此错误。如果我将{{1}}的类型更改为整数数组,它也会持续存在。为什么会生成此错误?