gpt4 book ai didi

c++ - OpenGL 纹理触发错误 1281 和奇怪的背景行为

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:15 25 4
gpt4 key购买 nike

我正在使用 SOIL 将纹理应用于 VBO,没有纹理我可以轻松更改背景并显示黑色(默认颜色)vbo,但现在有了纹理,openGL 给出错误 1281,背景是黑色的,一些纹理是没有申请。关于应用/加载纹理,一定有一些我不明白的地方。但是应用了纹理(虽然没有其他工作),当我尝试使用着色器程序时应用了错误,但是我检查了这些的编译并且没有写入任何问题。

这是我用来加载纹理的代码,一旦加载它就会保存在内存中,它主要来自 SOIL 的示例:

    texture = SOIL_load_OGL_single_cubemap(
filename,
SOIL_DDS_CUBEMAP_FACE_ORDER,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
| SOIL_FLAG_MIPMAPS
| SOIL_FLAG_DDS_LOAD_DIRECT
);
if( texture > 0 )
{
glEnable( GL_TEXTURE_CUBE_MAP );
glEnable( GL_TEXTURE_GEN_S );
glEnable( GL_TEXTURE_GEN_T );
glEnable( GL_TEXTURE_GEN_R );
glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
glBindTexture( GL_TEXTURE_CUBE_MAP, texture );
std::cout << "the loaded single cube map ID was " << texture << std::endl;
} else {
std::cout << "Attempting to load as a HDR texture" << std::endl;
texture = SOIL_load_OGL_HDR_texture(
filename,
SOIL_HDR_RGBdivA2,
0,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
| SOIL_FLAG_MIPMAPS
);
if( texture < 1 )
{
std::cout << "Attempting to load as a simple 2D texture" << std::endl;
texture = SOIL_load_OGL_texture(
filename,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
| SOIL_FLAG_MIPMAPS
| SOIL_FLAG_DDS_LOAD_DIRECT
);
}
if( texture > 0 ) {
// enable texturing
glEnable( GL_TEXTURE_2D );
// bind an OpenGL texture ID
glBindTexture( GL_TEXTURE_2D, texture );
std::cout << "the loaded texture ID was " << texture << std::endl;
} else {
glDisable( GL_TEXTURE_2D );
std::cout << "Texture loading failed: '" << SOIL_last_result() << "'" << std::endl;
}
}

以及我在绘图时如何应用它:

        GLuint TextureID  = glGetUniformLocation(shaderProgram, "myTextureSampler");
if(!TextureID)
cout << "TextureID not found ..." << endl;
// glEnableVertexAttribArray(TextureID);
glActiveTexture(GL_TEXTURE0);
if(SFML)
sf::Texture::bind(sfml_texture);
else {
glBindTexture (GL_TEXTURE_2D, texture);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 768, 0, GL_RGB, GL_UNSIGNED_BYTE, &texture);
}

glUniform1i(TextureID, 0);

我不确定 SOIL 是否适合我的程序,因为我想要尽可能简单的东西(我使用了 sfml 的纹理对象,这是最好的但我不能再使用了),但如果我能让它工作,它会很棒。

编辑:

缩小错误隐含的代码后,这里是引发错误的代码,它在纹理加载和 bos 绘制之间调用:

    glEnableClientState(GL_VERTEX_ARRAY);
//this gives the error :
glUseProgram(this->shaderProgram);

if (!shaderLoaded)
{
std::cout << "Loading default shaders" << std::endl;
if(textured)
loadShaderProgramm(texture_vertexSource, texture_fragmentSource);
else
loadShaderProgramm(default_vertexSource,default_fragmentSource);
}

glm::mat4 Projection = camera->getPerspective();

glm::mat4 View = camera->getView();
glm::mat4 Model = glm::mat4(1.0f);
Model[0][0] *= scale_x;
Model[1][1] *= scale_y;
Model[2][2] *= scale_z;

glm::vec3 translate_vec(this->x,this->y,this->z);
glm::mat4 object_transform = glm::translate(glm::mat4(1.0f),translate_vec);
glm::quat rotation = QAccumulative.getQuat();
glm::mat4 matrix_rotation = glm::mat4_cast(rotation);
object_transform *= matrix_rotation;
Model *= object_transform;

glm::mat4 MVP = Projection * View * Model;

GLuint ModelID = glGetUniformLocation(this->shaderProgram, "M");
if(ModelID ==-1)
cout << "ModelID not found ..." << endl;
GLuint MatrixID = glGetUniformLocation(this->shaderProgram, "MVP");
if(MatrixID ==-1)
cout << "MatrixID not found ..." << endl;
GLuint ViewID = glGetUniformLocation(this->shaderProgram, "V");
if(ViewID ==-1)
cout << "ViewID not found ..." << endl;

glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(ModelID, 1, GL_FALSE, &Model[0][0]);
glUniformMatrix4fv(ViewID, 1, GL_FALSE, &View[0][0]);

drawObject();

最佳答案

我发现了问题,我曾经在绘制后停用着色器,在那个方法中我有调用:

        sf::Texture::bind(NULL);

这也以某种方式弄乱了着色器,我刚刚对此进行了评论,现在一切正常

关于c++ - OpenGL 纹理触发错误 1281 和奇怪的背景行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25447315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com