WoW Model Viewer
Your premiere tool for viewing, equipping and animating World of Warcraft models.
Loading...
Searching...
No Matches
ViewportFBO.h
Go to the documentation of this file.
1#pragma once
2
3#include <glad/gl.h>
4
10{
11 GLuint fbo = 0;
12 GLuint colorTex = 0;
13 GLuint depthRbo = 0;
14 int width = 0;
15 int height = 0;
16
18 void create(int w, int h)
19 {
20 width = w;
21 height = h;
22 glGenFramebuffers(1, &fbo);
23 glGenTextures(1, &colorTex);
24 glGenRenderbuffers(1, &depthRbo);
25
26 glBindTexture(GL_TEXTURE_2D, colorTex);
27 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
28 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
29 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
30 glBindTexture(GL_TEXTURE_2D, 0);
31
32 glBindRenderbuffer(GL_RENDERBUFFER, depthRbo);
33 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, w, h);
34 glBindRenderbuffer(GL_RENDERBUFFER, 0);
35
36 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
37 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTex, 0);
38 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRbo);
39 glBindFramebuffer(GL_FRAMEBUFFER, 0);
40 }
41
43 void resize(int w, int h)
44 {
45 if (w == width && h == height)
46 return;
47 destroy();
48 if (w > 0 && h > 0)
49 create(w, h);
50 }
51
53 void bind() const { glBindFramebuffer(GL_FRAMEBUFFER, fbo); }
54
56 void unbind() const { glBindFramebuffer(GL_FRAMEBUFFER, 0); }
57
59 void destroy()
60 {
61 if (fbo) { glDeleteFramebuffers(1, &fbo); fbo = 0; }
62 if (colorTex) { glDeleteTextures(1, &colorTex); colorTex = 0; }
63 if (depthRbo) { glDeleteRenderbuffers(1, &depthRbo); depthRbo = 0; }
64 width = height = 0;
65 }
66};
Simple OpenGL framebuffer object wrapper for off-screen rendering.
Definition ViewportFBO.h:10
void destroy()
Release all GPU resources.
Definition ViewportFBO.h:59
int width
Current width in pixels.
Definition ViewportFBO.h:14
void bind() const
Bind this FBO as the current render target.
Definition ViewportFBO.h:53
void resize(int w, int h)
Resize the FBO; destroys and recreates if dimensions changed.
Definition ViewportFBO.h:43
void create(int w, int h)
Allocate GPU resources at the given resolution.
Definition ViewportFBO.h:18
GLuint fbo
Framebuffer object handle.
Definition ViewportFBO.h:11
GLuint colorTex
Colour attachment (GL_RGBA8 texture).
Definition ViewportFBO.h:12
int height
Current height in pixels.
Definition ViewportFBO.h:15
GLuint depthRbo
Depth attachment (GL_DEPTH_COMPONENT24 renderbuffer).
Definition ViewportFBO.h:13
void unbind() const
Unbind (revert to the default framebuffer).
Definition ViewportFBO.h:56