CS代考计算机代写 /*

/*
Copyright 2011 Etay Meiri

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/

#include “headers.h”
#include “gbuffer.h”

GBuffer::GBuffer( unsigned int width, unsigned int height, int nTextures, GLFWwindow *window )

{
// Check that we can attach at least 5 FBOs

GLint maxAttachments;
glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS, &maxAttachments );
if (maxAttachments < 5) { cerr << "Can only attach " << maxAttachments << " to an FBO in this version of OpenGL, but 5 are needed." << endl; exit(1); } // Get framebuffer size (which can be DIFFERENT than the window size, and *is* different on Macs!) glfwGetFramebufferSize( window, &fbWidth, &fbHeight ); numTextures = nTextures; // Create the FBO glGenFramebuffers( 1, &FBO ); glBindFramebuffer( GL_FRAMEBUFFER, FBO ); // Create the gbuffer textures textures = new GLuint[ numTextures ]; glGenTextures( numTextures, textures ); for (int i = 0 ; i < numTextures; i++) { glBindTexture( GL_TEXTURE_2D, textures[i] ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB16F, fbWidth, fbHeight, 0, GL_RGB, GL_FLOAT, NULL ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i], 0 ); } // depth glGenTextures( 1, &depthTexture ); glBindTexture( GL_TEXTURE_2D, depthTexture ); glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, fbWidth, fbHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0 ); // Declare the drawBuffers GLenum *drawBuffers = new GLenum[numTextures]; for (int i=0; i