我是opengl的新手,我有这个木偶(头部,躯干,腿,手臂),我只想在此木偶的躯干部分设置纹理。
我当时的假设是,在绘制木偶的躯干部分之前,我可以简单地调用glEnable(GL_TEXTURE_2D)
,然后在绘制调用glDisable(GL_TEXTURE_2D)
之后,但是似乎出现了GL_INVALID_ENUM
错误。
//用于在启动时加载init函数中调用的纹理的代码
void loadTextures()
{
// load and create a texture
// -------------------------
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
//绘制木偶的代码
void A3::renderNode(const SceneNode * root, mat4 view, mat4 model) {
if (root == nullptr) return;
mat4 tmp_model = model * root->get_transform();
if (root->m_nodeType == NodeType::GeometryNode) {
const GeometryNode * geometryNode = static_cast<const GeometryNode *>(root);
updateShaderUniforms(m_shader, *geometryNode, view, tmp_model , picking);
// Get the BatchInfo corresponding to the GeometryNode's unique MeshId.
BatchInfo batchInfo = m_batchInfoMap[geometryNode->meshId];
m_shader.enable();
if (root->m_name == "torso")
glEnable(GL_TEXTURE_2D);
glDrawArrays(GL_TRIANGLES, batchInfo.startIndex, batchInfo.numIndices);
glDisable(GL_TEXTURE_2D);
m_shader.disable();
}
for (const SceneNode * node : root->children) {
renderNode(node, view, tmp_model); // recursion
}
}