我的目标是能够在加载时缩放纹理,所以我不必在每次渲染精灵的帧上都这样做。我认为最好的方法是将缩放纹理渲染到另一个纹理上,基本上缓存它。但是,使用下面的代码,我只得到红色四边形(由于glClearColor)所以我知道FBO正在工作,而不是我渲染新纹理的方法
Texture *Graphics::loadTexture(const std::string& filename, int scale = 0) {
SDL_Surface *surface;
GLuint texture;
if((surface = IMG_Load(filename.c_str()))) {
// Get the number of colors
GLint numberOfColors = surface->format->BytesPerPixel;
GLenum format;
// Set the format of the texture based on the number of channels
if(numberOfColors == 4) {
if(surface->format->Rmask == 0x000000ff) {
format = GL_RGBA;
} else {
format = GL_BGRA;
}
} else if(numberOfColors == 3) {
if(surface->format->Rmask == 0x000000FF) {
format = GL_RGB;
} else {
format = GL_BGR;
}
} else {
throw Exception("Invalid image type for image " + filename);
}
// Generate texture id
glGenTextures(1, &texture);
// Bind the texture
glBindTexture(GL_TEXTURE_2D, texture);
// Texture stretching properties
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Create the image
glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h,
0, format, GL_UNSIGNED_BYTE, surface->pixels);
glBindTexture(GL_TEXTURE_2D, 0);
} else {
return NULL;
}
Texture *result;
if(scale > 1) {
GLuint scaledTexture;
GLuint fbo;
GLuint fbod;
// First we setup the depth buffer //
// Create the framebuffer
glGenRenderbuffersEXT(1, &fbod);
// Bind the render buffer
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbod);
// Set the render buffer storage to be a depth component
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, surface->w*scale, surface->h*scale);
// Set the render buffer of this buffer to the depth buffer
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbod);
// Unbind the render buffer
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
// Next we setup the texture //
glGenTextures(1, &scaledTexture);
glBindTexture(GL_TEXTURE_2D, scaledTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w*scale, surface->h*scale, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
// Setup the frame buffer //
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
// Attach the texture and render buffer to the frame buffer
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, scaledTexture, 0);
// Attach the depth buffer
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbod);
glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT);
glViewport(0, 0, surface->w*scale, surface->h*scale);
glLoadIdentity();
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2i( 0, 0 );
glVertex3f( 0.f, 0.f, 0.0f );
glTexCoord2i( 1, 0 );
glVertex3f( (GLfloat)surface->w*scale, 0.0f, 0.0f );
glTexCoord2i( 1, 1 );
glVertex3f( (GLfloat)surface->w*scale, (GLfloat)surface->h*scale, 0.f );
glTexCoord2i( 0, 1 );
glVertex3f( 0.0f, (GLfloat)surface->h*scale, 0.f );
glEnd();
glPopMatrix();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glPopAttrib();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
result = new Texture(scaledTexture, surface->w, surface->h);
} else {
result = new Texture(texture, surface->w, surface->h);
}
//Texture *result = new Texture(texture, surface->w, surface->h);
if(surface) {
SDL_FreeSurface(surface);
}
return result;
}
答案 0 :(得分:1)
几条评论:
但无论如何,要清除两个矩阵,你应该在屏幕的右上角看到你的纹理(或者你正在使用生成的纹理的任何多边形);你只需要修复UVs et Graphics :: loadTexture:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
这应该可行,但我认为你应该在软件中进行扩展。
答案 1 :(得分:1)
好吧,我决定做的不是在加载纹理时实现缩放,而是在sprite对象中实现它。因此,当我将顶点数据提交给GPU时,它会根据它的顶点缩放精灵,而不是缩放实际纹理。
答案 2 :(得分:1)
如果没有试图告诉你该怎么做,而是告诉你最简单的方法来做你缩进的事(缩放纹理) - 答案是:
glBlitFramebuffer(sx0,sy0,sx1,sy1,dx0,dy0,dx1,dy1, GL_COLOR_BUFFER_BIT, GL_LINEAR)
如果尺寸不匹配,它将自动缩放读取的图像 为了使其工作,你应该为每个人设置GL_READ_FRAMEBUFFER,GL_DRAW_FRAMEBUFFER和glDrawBuffer()。