Advertisement
atm959

Renderer.cpp

Apr 2nd, 2019
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. #include "Renderer.h"
  2.  
  3. Renderer::Renderer(){
  4.     WHBLogPrintf("New Renderer");
  5.     GX2InitSampler(&this->sampler, GX2_TEX_CLAMP_MODE_WRAP, GX2_TEX_XY_FILTER_MODE_POINT);
  6.     GX2InitSamplerLOD(&this->sampler, 0, 13, -3.0f);
  7. }
  8.  
  9. Renderer::~Renderer(){
  10.     WHBLogPrintf("Destroy Renderer");
  11.     for(unsigned int i = 0; i < this->vertexBuffers.size(); i++) GX2RDestroyBufferEx(this->vertexBuffers.at(i), (GX2RResourceFlags) 0);
  12.     for(unsigned int i = 0; i < this->texCoordBuffers.size(); i++) GX2RDestroyBufferEx(this->texCoordBuffers.at(i), (GX2RResourceFlags) 0);
  13.     for(unsigned int i = 0; i < this->shaderGroups.size(); i++) WHBGfxFreeShaderGroup(this->shaderGroups.at(i));
  14. }
  15.  
  16. void Renderer::loadVertexData(float vertexData[]){
  17.     WHBLogPrintf("Load Vertex Data");
  18.     GX2RBuffer positionBuffer;
  19.     void *buffer = NULL;
  20.     positionBuffer.flags = (GX2RResourceFlags)(GX2R_RESOURCE_BIND_VERTEX_BUFFER |
  21.                         GX2R_RESOURCE_USAGE_CPU_READ |
  22.                         GX2R_RESOURCE_USAGE_CPU_WRITE |
  23.                         GX2R_RESOURCE_USAGE_GPU_READ);
  24.     positionBuffer.elemSize = 3 * sizeof(float);
  25.     positionBuffer.elemCount = (sizeof(&vertexData) / sizeof(float)) / 3;
  26.     GX2RCreateBuffer(&positionBuffer);
  27.     buffer = GX2RLockBufferEx(&positionBuffer, (GX2RResourceFlags)0);
  28.     memcpy(buffer, vertexData, positionBuffer.elemSize * positionBuffer.elemCount);
  29.     GX2RUnlockBufferEx(&positionBuffer, (GX2RResourceFlags)0);
  30.     this->vertexBuffers.push_back(&positionBuffer);
  31. }
  32.  
  33. void Renderer::loadTexCoordData(float texCoordData[]){
  34.     WHBLogPrintf("Load Tex Coord Data");
  35.     GX2RBuffer texCoordBuffer;
  36.     void *buffer = NULL;
  37.     texCoordBuffer.flags = (GX2RResourceFlags)(GX2R_RESOURCE_BIND_VERTEX_BUFFER |
  38.                             GX2R_RESOURCE_USAGE_CPU_READ |
  39.                             GX2R_RESOURCE_USAGE_CPU_WRITE |
  40.                             GX2R_RESOURCE_USAGE_GPU_READ);
  41.     texCoordBuffer.elemSize = 2 * sizeof(float);
  42.     texCoordBuffer.elemCount = (sizeof(&texCoordData) / sizeof(float)) / 2;
  43.     GX2RCreateBuffer(&texCoordBuffer);
  44.     buffer = GX2RLockBufferEx(&texCoordBuffer, (GX2RResourceFlags)0);
  45.     memcpy(buffer, texCoordData, texCoordBuffer.elemSize * texCoordBuffer.elemCount);
  46.     GX2RUnlockBufferEx(&texCoordBuffer, (GX2RResourceFlags)0);
  47.     this->texCoordBuffers.push_back(&texCoordBuffer);
  48. }
  49.  
  50. void Renderer::loadShaderGroup(char* filePath){
  51.     WHBLogPrintf("Load Shader Group");
  52. }
  53.  
  54. void Renderer::loadTexture(char* path){
  55.     Texture t;
  56.     t.load(path);
  57.     this->textures.push_back(&t);
  58. }
  59.  
  60. void Renderer::setActiveVertexBuffer(int vertexBufferID){
  61.     WHBLogPrintf("Set Active Vertex Buffer");
  62.     this->activeVertexBuffer = vertexBufferID;
  63. }
  64.  
  65. void Renderer::setActiveTexCoordBuffer(int texCoordBufferID){
  66.     WHBLogPrintf("Set Active Tex Coord Buffer");
  67.     this->activeTexCoordBuffer = texCoordBufferID;
  68. }
  69.  
  70. void Renderer::setActiveShader(int shaderID){
  71.     WHBLogPrintf("Set Active Shader Group");
  72.     this->activeShaderGroup = shaderID;
  73. }
  74.  
  75. void Renderer::setActiveTexture(int texID){
  76.     this->activeTexture = texID;
  77. }
  78.  
  79. void Renderer::prepare3DTV(){
  80.     WHBLogPrintf("Prepare 3D(TV)");
  81.     this->projection = glm::perspective(degToRad(45.0f), (float)1280 / (float)720, NEAR_PLANE, FAR_PLANE);
  82. }
  83.  
  84. void Renderer::prepare2DTV(){
  85.     WHBLogPrintf("Prepare 2D(TV)");
  86.     this->projection = glm::ortho(0.0f, 1280.0f, 720.0f, 0.0f, -1.0f, 1.0f);
  87. }
  88.  
  89. void Renderer::prepare3DDRC(){
  90.     WHBLogPrintf("Prepare 3D(DRC)");
  91.     this->projection = glm::perspective(degToRad(45.0f), (float)854 / (float)480, NEAR_PLANE, FAR_PLANE);
  92. }
  93.  
  94. void Renderer::prepare2DDRC(){
  95.     WHBLogPrintf("Prepare 2D(DRC)");
  96.     this->projection = glm::ortho(0.0f, 854.0f, 480.0f, 0.0f, -1.0f, 1.0f);
  97. }
  98.  
  99. void Renderer::startTV(){
  100.     WHBLogPrintf("Start TV Render");
  101.     WHBGfxBeginRenderTV();
  102. }
  103.  
  104. void Renderer::startDRC(){
  105.     WHBLogPrintf("Start DRC Render");
  106.     WHBGfxBeginRenderDRC();
  107. }
  108.  
  109. void Renderer::endTV(){
  110.     WHBLogPrintf("End TV Render");
  111.     WHBGfxFinishRenderTV();
  112. }
  113.  
  114. void Renderer::endDRC(){
  115.     WHBLogPrintf("End DRC Render");
  116.     WHBGfxFinishRenderDRC();
  117. }
  118.  
  119. void Renderer::renderTexture(){
  120.     WHBLogPrintf("Render Textured Triangles");
  121.     this->mvp = this->projection * this->view;
  122.     this->mvp = this->mvp * this->model;
  123.     GX2SetColorControl(GX2_LOGIC_OP_COPY, 0xFF, FALSE, TRUE);
  124.     GX2SetBlendControl(GX2_RENDER_TARGET_0,
  125.         /* RGB = [srcRGB * srcA] + [dstRGB * (1-srcA)] */
  126.         GX2_BLEND_MODE_SRC_ALPHA, GX2_BLEND_MODE_INV_SRC_ALPHA,
  127.         GX2_BLEND_COMBINE_MODE_ADD,
  128.         TRUE,
  129.         /* A = [srcA * 1] + [dstA * (1-srcA)] */
  130.         GX2_BLEND_MODE_ONE, GX2_BLEND_MODE_INV_SRC_ALPHA,
  131.         GX2_BLEND_COMBINE_MODE_ADD);
  132.     WHBGfxClearColor(0.5f, 0.5f, 1.0f, 1.0f);
  133.     GX2SetCullOnlyControl(GX2_FRONT_FACE_CCW, FALSE, TRUE);
  134.     GX2SetFetchShader(&this->shaderGroups.at(this->activeShaderGroup)->fetchShader);
  135.     GX2SetVertexShader(this->shaderGroups.at(this->activeShaderGroup)->vertexShader);
  136.     GX2SetPixelShader(this->shaderGroups.at(this->activeShaderGroup)->pixelShader);
  137.     GX2RSetAttributeBuffer(this->vertexBuffers.at(this->activeVertexBuffer), 0, this->vertexBuffers.at(this->activeVertexBuffer)->elemSize, 0);
  138.     GX2RSetAttributeBuffer(this->texCoordBuffers.at(this->activeTexCoordBuffer), 1, this->vertexBuffers.at(this->activeVertexBuffer)->elemSize, 0);
  139.     this->textures.at(this->activeTexture)->mapTo(0);
  140.     GX2SetPixelSampler(&this->sampler, 0);
  141.     GX2SetVertexUniformReg(this->shaderGroups.at(this->activeShaderGroup)->vertexShader->uniformVars[0].offset, 16, (uint32_t *)&this->mvp[0][0]);
  142.     GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, (64*64)*6, 0, 1);
  143. }
  144.  
  145. void Renderer::renderLines(){
  146.     WHBLogPrintf("Render Lines\n");
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement