Commits

Anonymous committed 1bc9eac

Backport https://bitbucket.org/ecsirke/frontier rev a02d605436b1, to fix purple trees by using the framebuffer extension to render to the tree texture directly. Skip out on the formatting and header-file changes (similar to the previous commit), to focus the diff on just the behavior changes. Include the changes to glFunctions to abort if extensions are missing and log to the console if everything works.

Comments (0)

Files changed (5)

 #include "StdAfx.h"
 #include "Cg.h"
 #include "CTree.h"
+#include "glFunctions.h"
 #include "Math.h"
 #include "Render.h"
 #include "Terraform.h"
 
 -----------------------------------------------------------------------------*/
 
+CTree::CTree() : _texture(0) {
+}
+
 GLmesh* CTree::Mesh (unsigned alt, LOD lod)
 {
 
 void CTree::DoTexture ()
 {
 
+  GLuint frameBuffer;
   unsigned  i;
 
+  glGenFramebuffers( 1, &frameBuffer );
+  glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
+
   glDisable (GL_CULL_FACE);
   glDisable (GL_FOG);
   glDisable (GL_LIGHTING);
   RenderCanvasBegin (0, TEXTURE_SIZE, 0, TEXTURE_SIZE, TEXTURE_SIZE);
  	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);	
   glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);	
-  char* buffer = new char[TEXTURE_SIZE * TEXTURE_SIZE * 4];
+  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0);
+  glViewport(0, 0, TEXTURE_SIZE * 4, TEXTURE_SIZE);
+  glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
+  glClear (GL_COLOR_BUFFER_BIT);
   for (i = 0; i < 4; i++) {
-    glClearColor (1.0f, 0.0f, 1.0f, 0.0f);
-    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+    glViewport(TEXTURE_SIZE * i, 0, TEXTURE_SIZE, TEXTURE_SIZE);
     if (i == 0)       
       DrawBark ();
     else if (i == 1)
       DrawVines ();
     else
       DrawFacer ();    
-    //CgShaderSelect (FSHADER_MASK_TRANSFER);
-    glBindTexture(GL_TEXTURE_2D, _texture);
- 	  glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);	
-    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);	
-    //glCopyTexSubImage2D (GL_TEXTURE_2D, 0, TEXTURE_SIZE * i, 0, 0, 0, TEXTURE_SIZE, TEXTURE_SIZE);
-    glReadPixels (0, 0, TEXTURE_SIZE, TEXTURE_SIZE, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
-    //CgShaderSelect (FSHADER_MASK_TRANSFER);
-    glTexSubImage2D (GL_TEXTURE_2D, 0, TEXTURE_SIZE * i, 0, TEXTURE_SIZE, TEXTURE_SIZE, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
-    //CgShaderSelect (FSHADER_NONE);
   }
-  delete buffer;
   RenderCanvasEnd ();
   
+  glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+  glDeleteFramebuffers( 1, &frameBuffer );
 }
 
 void CTree::Info ()
   GLvector          TrunkPosition (float delta, float* radius);
   void              Build ();
 public:
+  CTree();
   unsigned          _texture;
   void              Create (bool canopy, float moisture, float temperature, int seed);
   void              Render (GLvector pos, unsigned alt, LOD lod);
     max_dimension = d;
     d *= 2;
   }
-  TexturePurge ();
   SceneTexturePurge ();
   WorldTexturePurge ();
   TextCreate (width, height);  

Terrain/glFunctions.cpp

 #include "glFunctions.h"
 
+#include <stdio.h>
+
 #ifndef WINDOWS
 #include <GL/glx.h>
 #endif
 
+#include "Console.h"
+#include "Sdl.h"
+
 // VBO Extension Function Pointers
-PFNGLGENBUFFERSARBPROC    glGenBuffersARB = NULL;					// VBO Name Generation Procedure
-PFNGLBINDBUFFERARBPROC    glBindBufferARB = NULL;					// VBO Bind Procedure
-PFNGLBUFFERDATAARBPROC    glBufferDataARB = NULL;					// VBO Data Loading Procedure
-PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL;				// VBO Deletion Procedure
+PFNGLGENBUFFERSARBPROC    glGenBuffersARB = NULL;
+PFNGLBINDBUFFERARBPROC    glBindBufferARB = NULL;
+PFNGLBUFFERDATAARBPROC    glBufferDataARB = NULL;
+PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL;
+
+// Framebuffer Function Pointers for tendering into textures
+PFNGLGENFRAMEBUFFERSPROC      glGenFramebuffers = NULL;
+PFNGLBINDFRAMEBUFFERPROC      glBindFramebuffer = NULL;
+PFNGLDELETEFRAMEBUFFERSPROC   glDeleteFramebuffers = NULL;
+PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D = NULL;
 
 void gl_functions_init ()
 {
   glBindBufferARB = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress("glBindBufferARB");
   glBufferDataARB = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress("glBufferDataARB");
   glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC) wglGetProcAddress("glDeleteBuffersARB");
+
+  glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffers");
+  glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebuffer");
+  glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffers");
+  glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2D");
 #else
   // Awesome, glXGetProcAddressARB takes a const GLubyte pointer, while string literals
   // turn into a const char array (note: signed!)  Whee.
   glBindBufferARB = (PFNGLBINDBUFFERARBPROC) glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glBindBufferARB"));
   glBufferDataARB = (PFNGLBUFFERDATAARBPROC) glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glBufferDataARB"));
   glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC) glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glDeleteBuffersARB"));
+
+  glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glGenFramebuffers"));
+  glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glBindFramebuffer"));
+  glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glDeleteFramebuffers"));
+  glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glFramebufferTexture2D"));
 #endif
 
+  if (!glGenBuffersARB || !glBindBufferARB || !glBufferDataARB || !glDeleteBuffersARB ||
+      !glGenFramebuffers || !glBindFramebuffer || !glDeleteFramebuffers || !glFramebufferTexture2D) {
+    printf("One of the necessary OpenGL functions is missing, can't render this way!\n");
+    SdlTerm();
+  }
+
+  ConsoleLog("OpenGL functions successfully loaded.");
 }

Terrain/glFunctions.h

 extern PFNGLBUFFERDATAARBPROC    glBufferDataARB;     // VBO Data Loading Procedure
 extern PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB;  // VBO Deletion Procedure
 
+// Framebuffer Function Pointers for tendering into textures
+extern PFNGLGENFRAMEBUFFERSPROC      glGenFramebuffers;
+extern PFNGLBINDFRAMEBUFFERPROC      glBindFramebuffer;
+extern PFNGLDELETEFRAMEBUFFERSPROC   glDeleteFramebuffers;
+extern PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D;
+
 // Only works after SDL OpenGL initialization
 void gl_functions_init ();