Want more features on Pastebin? Sign Up, it's FREE!
Guest

ecglrenderer.cpp

By: a guest on Oct 29th, 2012  |  syntax: C++  |  size: 4.21 KB  |  views: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "ecglrenderer.h"
  2.  
  3. #include "GL/glew.h"
  4.  
  5. #include "glshaderprogram.h"
  6. #include "cjblog.h"
  7.  
  8. void ECGLRenderer::preLoop()
  9. {
  10.         m_stateManager = new GLStateManager();
  11.         m_stateManager->init();
  12.  
  13.         IECRenderer::preLoop();
  14. }
  15.  
  16. void ECGLRenderer::shutdown()
  17. {
  18.         if (m_stateManager)
  19.         {
  20.                 delete m_stateManager;
  21.                 m_stateManager = NULL;
  22.         }
  23.  
  24.         IECRenderer::shutdown();
  25. }
  26.  
  27. void ECGLRenderer::clearBuffers(CJB_UINT32 buffers)
  28. {
  29.         GLuint clearBuffers = 0;
  30.  
  31.         clearBuffers |= (clearBuffers & CJB_COLOR_BUFFER) ? 0 : GL_COLOR_BUFFER_BIT;
  32.         clearBuffers |= (clearBuffers & CJB_DEPTH_BUFFER) ? 0 : GL_DEPTH_BUFFER_BIT;
  33.         clearBuffers |= (clearBuffers & CJB_STENCIL_BUFFER) ? 0 : GL_STENCIL_BUFFER_BIT;
  34.  
  35.         glClear(clearBuffers);
  36. }
  37.  
  38. void ECGLRenderer::setClearColor(float red, float green, float blue, float alpha)
  39. {
  40.         glClearColor(red,green,blue,alpha);
  41. }
  42.  
  43. ITexture* ECGLRenderer::createTextureWithImage(const char* imageFile)
  44. {
  45.         GLTexture* newTexture = new GLTexture(imageFile);
  46.  
  47.         return newTexture;
  48. }
  49.  
  50. void ECGLRenderer::bindTexture(ITexture* texture)
  51. {
  52.         glBindTexture(GL_TEXTURE_2D,((GLTexture*)texture)->getTextureID());
  53. }
  54.  
  55. IShaderProgram* ECGLRenderer::createShaderProgram(const char* vertexShader, const char* fragmentShader)
  56. {
  57.         GLShaderProgram* ret = new GLShaderProgram(vertexShader,fragmentShader);
  58.  
  59.         if (ret == NULL)
  60.         {
  61.                 CJBLOG_WARNING("Failed to create shader program with %s %s\n",vertexShader, fragmentShader);
  62.                 return NULL;
  63.         }
  64.  
  65.         return ret;
  66. }
  67.  
  68. void ECGLRenderer::deleteShaderProgram(IShaderProgram* program)
  69. {
  70.         glDeleteProgram(((GLShaderProgram*)program)->getProgramID());
  71. }
  72.  
  73. void ECGLRenderer::useShaderProgram(IShaderProgram* program)
  74. {
  75.         glUseProgram(((GLShaderProgram*)program)->getProgramID());
  76. }
  77.  
  78. IVertexBufferObject* ECGLRenderer::generateVBO()
  79. {
  80.         GLVertexBufferObject* newVBO = new GLVertexBufferObject;
  81.         glGenBuffers(1,&newVBO->bufferID);
  82.         return newVBO;
  83. }
  84.  
  85. IVertexArrayObject* ECGLRenderer::generateVAO()
  86. {
  87.         GLVertexArrayObject* newVAO = new GLVertexArrayObject;
  88.         glGenVertexArrays(1,&newVAO->arrayID);
  89.         return newVAO;
  90. }
  91.  
  92. void ECGLRenderer::bindVBO(CJB_UINT32 bufferType, IVertexBufferObject* pVBO)
  93. {
  94.         if (bufferType == VBO_ARRAY)
  95.                 glBindBuffer(GL_ARRAY_BUFFER,((GLVertexBufferObject*)pVBO)->bufferID);
  96.         else
  97.                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,((GLVertexBufferObject*)pVBO)->bufferID);
  98. }
  99.  
  100. void ECGLRenderer::bindVAO(IVertexArrayObject* pVAO)
  101. {
  102.         glBindVertexArray(((GLVertexArrayObject*)pVAO)->arrayID);
  103. }
  104.  
  105. void ECGLRenderer::deleteVBO(IVertexBufferObject*& pVBO)
  106. {
  107.         glDeleteBuffers(1,&((GLVertexBufferObject*)pVBO)->bufferID);
  108.         delete pVBO;
  109.         pVBO = NULL;
  110. }
  111.  
  112. void ECGLRenderer::deleteVAO(IVertexArrayObject*& pVAO)
  113. {
  114.         glDeleteVertexArrays(1,&((GLVertexArrayObject*)pVAO)->arrayID);
  115.         delete pVAO;
  116.         pVAO = NULL;
  117. }
  118.  
  119. void ECGLRenderer::setBufferData(CJB_UINT32 target,size_t size, void* data, CJB_UINT32 usage)
  120. {
  121.         CJB_UINT32 glTarget = (target == VBO_ARRAY) ? GL_ARRAY_BUFFER : GL_ELEMENT_ARRAY_BUFFER;
  122.         CJB_UINT32 glUsage = (usage == VBO_STATIC_DRAW) ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW;
  123.  
  124.         glBufferData(glTarget,size,data,glUsage);
  125. }
  126.  
  127. void ECGLRenderer::enableVertexAttributeArray(CJB_UINT32 attributeIndex)
  128. {
  129.         glEnableVertexAttribArray(attributeIndex);
  130. }
  131.  
  132. void ECGLRenderer::setVertexAttributePointer(CJB_UINT32 attributeIndex, CJB_UINT32 size, CJB_UINT32 type, bool isNormalized, size_t stride, void* pointer)
  133. {
  134.         glVertexAttribPointer(attributeIndex,size,convertTypeToGLType(type),isNormalized,stride,pointer);
  135. }
  136.  
  137. CJB_UINT32 ECGLRenderer::convertTypeToGLType(CJB_UINT32 elementType)
  138. {
  139.         switch(elementType)
  140.         {
  141.         case ET_FLOAT:
  142.                 return GL_FLOAT;
  143.         case ET_USHORT:
  144.                 return GL_UNSIGNED_SHORT;
  145.         case ET_UINT32:
  146.                 return GL_UNSIGNED_INT;
  147.         }
  148. }
  149.  
  150. void ECGLRenderer::unbindVBO(CJB_UINT32 bufferType)
  151. {
  152.         CJB_UINT32 glTarget = (bufferType == VBO_ARRAY) ? GL_ARRAY_BUFFER : GL_ELEMENT_ARRAY_BUFFER;
  153.         glBindBuffer(glTarget,0);
  154. }
  155.  
  156. void ECGLRenderer::unbindVAO()
  157. {
  158.         glBindVertexArray(0);
  159. }
  160.  
  161. void ECGLRenderer::drawElements(CJB_UINT32 mode, size_t triCount, CJB_UINT32 type, void* INDICES )
  162. {
  163.         CJB_UINT32 glMode = (mode == DRAW_MODE_TRIANGLES) ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
  164.  
  165.         glDrawElements(glMode,triCount*3,convertTypeToGLType(type),INDICES);
  166. }
clone this paste RAW Paste Data