Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Opengl渲染到纹理,但纹理为空_C++_Opengl_Textures - Fatal编程技术网

C++ Opengl渲染到纹理,但纹理为空

C++ Opengl渲染到纹理,但纹理为空,c++,opengl,textures,C++,Opengl,Textures,我试图渲染到一个纹理,然后渲染到一个正方形上。但结果是一个粉红色的正方形 我创建纹理、帧缓冲区等 texture renderTexture = texture(); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1000, 600, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0); unsigned int fb; glGenFramebuffers(1, &fb); glBindRenderbuffer(GL_RENDERBUF

我试图渲染到一个纹理,然后渲染到一个正方形上。但结果是一个粉红色的正方形

我创建纹理、帧缓冲区等

texture renderTexture = texture();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1000, 600, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);

unsigned int fb;
glGenFramebuffers(1, &fb);
glBindRenderbuffer(GL_RENDERBUFFER, fb);
glFramebufferTexture2D(GL_RENDERBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTexture.getId(), 0);
然后在游戏循环中,我称之为:

show(fb);
showMenu(renderTexture.getId());

这是纹理类:

#include "texture.h"

texture::texture(){
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
}

texture::~texture(){
    glDeleteTextures(1, &id);
}

void texture::loadImage(const char* filename){
    SDL_Surface* img = SDL_LoadBMP(filename);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, img->pixels);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    SDL_FreeSurface(img);
}

unsigned int texture::getId(){
    return id;
}
我在编译时没有任何错误

我有什么遗漏吗? 有人知道怎么解决吗

非常感谢。

问题就在这里

texture renderTexture = texture();
您缺少
new
关键字。这将阻止调用纹理构造函数,因为它似乎正在执行纹理生成

换成


texture*renderTexture=new texture()

纹理渲染器纹理=纹理()
看起来非常错误。如果希望它位于堆上,那么它应该是
texture renderTexture()。但我想主要的问题是,在您尝试使用纹理之前,纹理超出了范围。但随后出现了以下错误:错误:从“纹理*”转换为非标量类型“纹理”请求这是因为他忘了说您必须使用指针(
texture*renderTexture=new texture()但是,如果这是新的,我建议在使用OpenGL之前先读一个好的C++教程。谢谢,我忘了。我不是C++新手,但这是我的第一个“项目”。.但问题仍然存在…当我运行它时程序崩溃…然后请提供一个最小但完整的代码示例。Atm,不清楚这些代码部分之间的关系。在析构函数中设置一个断点,查看纹理是否过早删除。然后确保设置了适当的过滤器。现在我更详细地看一看:你的四边形是可见的吗?可见区域从0到1000(或0到600),从-2到2的四边形听起来很小。深度值看起来也很奇怪。你在z=-5处绘制,但将投影设置为从-1到1的可见范围。为什么你的纹理坐标从0到2?
texture renderTexture = texture();