From 775aa5b4639faf708e1de7cc37e46a0f9bc98b18 Mon Sep 17 00:00:00 2001
From: Stuart Layton <stuart.layton@gmail.com>
Date: Tue, 10 Apr 2012 10:49:04 -0400
Subject: [PATCH] added check to ProjectionAxes::createFBO() such that
 glDeleteFramebuffers and glDeleteRenderbuffers are only called if the FBO and
 RBO have already been generated. OpenGL specs indicate it is fine to call
 these functions on non existant handles, like on the initial creation of the
 texture. This wasn't a problem on linux but it was for OSX. It was causing
 each projection plot to share a single texture and all spikes were going to a
 single plot. As when the 2nd projection plot called createFBO it deleted the
 FBO for projection plot 1 and 3 did it for 2, resulting in a single fbo for
 all plots.  This should resolve most of the projection plotting issues for
 mac

---
 .../SpikePlotting/ProjectionAxes.cpp              | 15 +++++++++++----
 .../Visualization/SpikePlotting/ProjectionAxes.h  |  1 +
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/Source/Processors/Visualization/SpikePlotting/ProjectionAxes.cpp b/Source/Processors/Visualization/SpikePlotting/ProjectionAxes.cpp
index ca7561395..e96d43132 100644
--- a/Source/Processors/Visualization/SpikePlotting/ProjectionAxes.cpp
+++ b/Source/Processors/Visualization/SpikePlotting/ProjectionAxes.cpp
@@ -8,7 +8,8 @@ ProjectionAxes::ProjectionAxes():
 					buffIdx(-1),
 					totalSpikes(0),
 					newSpike(false),
-					isTextureValid(false), 
+					isTextureValid(false),
+                    fboCreated(false),
 					allSpikesNextRender(false)
 {	
 	GenericAxes::type = PROJ1x2;
@@ -20,6 +21,7 @@ ProjectionAxes::ProjectionAxes():
 	n2ProjIdx(type, &ampDim1, &ampDim2);
 
 	clearOnNextDraw = false;
+
 }
 
 ProjectionAxes::ProjectionAxes(int x, int y, double w, double h, int t):
@@ -31,6 +33,7 @@ ProjectionAxes::ProjectionAxes(int x, int y, double w, double h, int t):
 					totalSpikes(0),
 					newSpike(false),
 					isTextureValid(false),
+                    fboCreated(false),
 					allSpikesNextRender(false)
 {	
 	GenericAxes::gotFirstSpike = false;
@@ -216,13 +219,15 @@ void ProjectionAxes::createTexture(){
 }
 
 void ProjectionAxes::createFBO(){
-	std::cout<<"Creating a new frame buffer object";//<<std::endl;
+	std::cout<<"Creating a new FBO, is already created?:"<<fboCreated<<" ";//<<std::endl;
 
 	// if (!isTextureValid)
 	// 	createTexture();
 	// Delete the old frame buffer, render buffer
-	glDeleteFramebuffers(1, &fboId);
-	glDeleteRenderbuffers(1, &rboId);
+    if (fboCreated){
+        glDeleteFramebuffers(1, &fboId);
+        glDeleteRenderbuffers(1, &rboId);
+    }
 
 	// Generate and Bind the frame buffer
 	glGenFramebuffersEXT(1, &fboId);
@@ -251,8 +256,10 @@ void ProjectionAxes::createFBO(){
     glClear(GL_COLOR_BUFFER_BIT);
     
 	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+    fboCreated = true;
 }
 
+
 void ProjectionAxes::drawSpikesToTexture(bool allSpikes){
 	
 	//std::cout<<"ProjectionAxes::drawSpikesToTexture() plotting all spikes:"<<allSpikes<<std::endl;
diff --git a/Source/Processors/Visualization/SpikePlotting/ProjectionAxes.h b/Source/Processors/Visualization/SpikePlotting/ProjectionAxes.h
index f3fdfd0c1..045ff811f 100644
--- a/Source/Processors/Visualization/SpikePlotting/ProjectionAxes.h
+++ b/Source/Processors/Visualization/SpikePlotting/ProjectionAxes.h
@@ -54,6 +54,7 @@ class ProjectionAxes: public GenericAxes{
 
   	bool clearOnNextDraw;
  	bool isTextureValid;
+    bool fboCreated;
   	void clearTexture();
 
   	void validateTexture();
-- 
GitLab