Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. void GraphicsSystem::Initialize()
  2. {
  3.     //create frame buffer
  4.     glGenFramebuffers(1, &frameBuffer);
  5.     glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  6.  
  7.     //create texture for frame buffer
  8.     glGenTextures(1, &sourceTexture);
  9.     glActiveTexture(GL_TEXTURE0);
  10.     glBindTexture(GL_TEXTURE_2D, sourceTexture);
  11.     glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, 0); //note GL_RED internal format, to save memory.
  12.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  13.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  14.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);      //GL_REPEAT specified so that CGOL is simulated on a torus.
  15.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  16.     glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, sourceTexture, 0);
  17.     glBindTexture(GL_TEXTURE_2D, 0);
  18.    
  19.     GL_ERROR(); //asserts of glGetError returns an error code.
  20.  
  21.     //create texture for frame buffer
  22.     glGenTextures(1, &destinationTexture);
  23.     glActiveTexture(GL_TEXTURE0);
  24.     glBindTexture(GL_TEXTURE_2D, destinationTexture);
  25.     glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, 0); //note GL_RED internal format, to save memory.
  26.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  27.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  28.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);      //GL_REPEAT specified so that CGOL is simulated on a torus.
  29.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  30.     glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, destinationTexture, 0);
  31.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  32.     glBindTexture(GL_TEXTURE_2D, 0);
  33.    
  34.     GL_ERROR(); //asserts of glGetError returns an error code.
  35.    
  36.     colorAttachment = GL_COLOR_ATTACHMENT1;
  37. }
  38.  
  39. void GraphicsSystem::Draw()
  40. {
  41.     const vec3 verts[4] = { vec3(0    , 0     , 0),
  42.                             vec3(width, 0     , 0),
  43.                             vec3(width, height, 0),
  44.                             vec3(0    , height, 0) };
  45.  
  46.     const vec2 uv[4] = { vec2(0   , 0   ),
  47.                          vec2(1.0f, 0   ),
  48.                          vec2(1.0f, 1.0f),
  49.                          vec2(0   , 1.0f) };
  50.  
  51.     //==========================//
  52.     //CGOL SIMULATION
  53.     glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  54.     glDrawBuffer(colorAttachment);
  55.  
  56.     cgolShader.Execute();
  57.  
  58.     glActiveTexture(GL_TEXTURE0);
  59.     glBindTexture(GL_TEXTURE_2D, sourceTexture);
  60.     cgolShader.UniformMat4("u_transformation", &projection.Top()[0][0]);
  61.     cgolShader.UniformInt("u_source_texture", 0);
  62.  
  63.     glEnableVertexAttribArray(0);
  64.     glEnableVertexAttribArray(1);
  65.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, verts);
  66.         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, uv);
  67.         glDrawArrays(GL_QUADS, 0, 4);
  68.     glDisableVertexAttribArray(0);
  69.     glDisableVertexAttribArray(1);
  70.     cgolShader.Terminate();
  71.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  72.     glBindTexture(GL_TEXTURE_2D, 0);
  73.    
  74.     GL_ERROR(); //asserts of glGetError returns an error code.
  75.     //==========================//
  76.  
  77.  
  78.    
  79.     //==========================//
  80.     //DRAW SOME GEOMETRY
  81.     glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  82.     glDrawBuffer(colorAttachment);
  83.    
  84.     //draw some simple geometry however you wish here.
  85.  
  86.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  87.     GL_ERROR(); //asserts of glGetError returns an error code.
  88.     //==========================//
  89.  
  90.  
  91.  
  92.     //==========================//
  93.     //FINAL PASS
  94.     //render destinationTexture to screen however you wish here
  95.     //==========================//
  96.  
  97.    
  98.    
  99.     //==========================//
  100.     //SWAP TEXTURES
  101.     GLint temp = outputFrameTexture;
  102.     outputFrameTexture = nextGenerationTexture;
  103.     nextGenerationTexture = temp;
  104.  
  105.     //switch color attachments since we swapped the textures.
  106.     glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  107.     if(colorAttachment == GL_COLOR_ATTACHMENT1)
  108.     {
  109.         glDrawBuffer(GL_COLOR_ATTACHMENT0);
  110.         glClear(GL_COLOR_BUFFER_BIT);
  111.         colorAttachment = GL_COLOR_ATTACHMENT0;
  112.     }
  113.     else
  114.     {
  115.         glDrawBuffer(GL_COLOR_ATTACHMENT1);
  116.         glClear(GL_COLOR_BUFFER_BIT);
  117.         colorAttachment = GL_COLOR_ATTACHMENT1;
  118.     }
  119.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  120.     GL_ERROR(); //asserts of glGetError returns an error code.
  121.     //==========================//
  122. }