Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FrameBuffer {
- public FrameBuffer(int width, int height, boolean hasDepth, boolean hasStencil) {
- this.width = width;
- this.height = height;
- if ((width + height) % 4 != 0) {
- new Exception("Width + Height must be divisible by 4");
- }
- colorTexture = GL11.glGenTextures();
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, colorTexture);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
- GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer)null);
- frameBufferID = GL30.glGenFramebuffers();
- GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBufferID);
- GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, colorTexture, 0);
- if (hasDepth && hasStencil) {
- int depthAndStencilBufferID = GL30.glGenRenderbuffers();
- GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthAndStencilBufferID);
- GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH24_STENCIL8, width, height);
- GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_STENCIL_ATTACHMENT, GL30.GL_RENDERBUFFER, depthAndStencilBufferID);
- }
- else if (hasDepth) {
- int depthBufferID = GL30.glGenRenderbuffers();
- GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBufferID);
- GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH_COMPONENT32F, width, height);
- GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, depthBufferID);
- }
- else if (hasStencil) {
- int stencilBufferID = GL30.glGenRenderbuffers();
- GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, stencilBufferID);
- GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_STENCIL_INDEX16, width, height);
- GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_STENCIL_ATTACHMENT, GL30.GL_RENDERBUFFER, stencilBufferID);
- }
- int result = GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER);
- if (result != GL30.GL_FRAMEBUFFER_COMPLETE) {
- if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) {
- new Exception("Frame Buffer Error: incomplete attachment").printStackTrace();
- }
- if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER) {
- new Exception("Frame Buffer Error: incomplete draw buffer").printStackTrace();
- }
- if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT) {
- new Exception("Frame Buffer Error: missing attachment").printStackTrace();
- }
- if (result == GL30.GL_FRAMEBUFFER_UNSUPPORTED) {
- new Exception("Frame Buffer Error: unsupported combination of formats").printStackTrace();
- }
- if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE) {
- new Exception("Frame Buffer Error: incomplete multisample").printStackTrace();
- }
- if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER) {
- new Exception("Frame Buffer Error: incomplete read buffer").printStackTrace();
- }
- new Exception("frame buffer couldn't be constructed: unknown error " + result).printStackTrace();
- }
- GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
- }
- private int colorTexture;
- private int width, height;
- private int frameBufferID;
- public void begin () {
- GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBufferID);
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
- GL11.glViewport(0, 0, width, height);
- }
- public void end () {
- GL11.glViewport(0, 0, RenderingEngine.getWidth(), RenderingEngine.getHeight());
- GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
- }
- public int getColorTexture () {
- return colorTexture;
- }
- public int getWidth () {
- return width;
- }
- public int getHeight () {
- return height;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement