Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL.h>
  3.  
  4. #include <bgfx/bgfx.h>
  5. #include <bx/math.h>
  6.  
  7.  
  8. struct PosColorVertex
  9. {
  10. float x;
  11. float y;
  12. float z;
  13. uint32_t abgr;
  14. };
  15.  
  16. static PosColorVertex cubeVertices[] =
  17. {
  18. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  19. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  20. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  21. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  22. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  23. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  24. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  25. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  26. };
  27.  
  28. static const uint16_t cubeTriList[] =
  29. {
  30. 0, 1, 2,
  31. 1, 3, 2,
  32. 4, 6, 5,
  33. 5, 6, 7,
  34. 0, 2, 4,
  35. 4, 2, 6,
  36. 1, 5, 3,
  37. 5, 7, 3,
  38. 0, 4, 1,
  39. 4, 5, 1,
  40. 2, 3, 6,
  41. 6, 3, 7,
  42. };
  43.  
  44.  
  45. bgfx::ShaderHandle loadShader(const char *FILENAME)
  46. {
  47. FILE *file = fopen(FILENAME, "rb");
  48. fseek(file, 0, SEEK_END);
  49. long fileSize = ftell(file);
  50. fseek(file, 0, SEEK_SET);
  51.  
  52. const bgfx::Memory *mem = bgfx::alloc(fileSize + 1);
  53. fread(mem->data, 1, fileSize, file);
  54. mem->data[mem->size - 1] = '\0';
  55. fclose(file);
  56.  
  57. return bgfx::createShader(mem);
  58. }
  59.  
  60.  
  61. int main() {
  62.  
  63. SDL_Init(SDL_INIT_EVERYTHING);
  64.  
  65. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
  66. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  67. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  68. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  69. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  70.  
  71. SDL_Window *window = SDL_CreateWindow("title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
  72.  
  73. bgfx::Init bgfxInit;
  74. bgfxInit.type = bgfx::RendererType::OpenGL; // Automatically choose a renderer.
  75. bgfxInit.resolution.width = 640;
  76. bgfxInit.resolution.height = 360;
  77. bgfxInit.resolution.reset = BGFX_RESET_VSYNC;
  78. bgfx::init(bgfxInit);
  79.  
  80. bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f, 0);
  81. bgfx::setViewRect(0, 0, 0, 640, 360);
  82.  
  83.  
  84. bgfx::VertexDecl pcvDecl;
  85. pcvDecl.begin()
  86. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  87. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  88. .end();
  89. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(bgfx::makeRef(cubeVertices, sizeof(cubeVertices)), pcvDecl);
  90. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(cubeTriList, sizeof(cubeTriList)));
  91.  
  92. bgfx::ShaderHandle vsh = loadShader("/Users/sherjilozair/Desktop/Dev/bgfx/bgfx/examples/runtime/shaders/glsl/vs_cubes.bin");
  93. bgfx::ShaderHandle fsh = loadShader("/Users/sherjilozair/Desktop/Dev/bgfx/bgfx/examples/runtime/shaders/glsl/fs_cubes.bin");
  94. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh, true);
  95.  
  96.  
  97. unsigned int counter = 0;
  98. while(!SDL_QuitRequested()) {
  99.  
  100. const bx::Vec3 at = {0.0f, 0.0f, 0.0f};
  101. const bx::Vec3 eye = {0.0f, 0.0f, -5.0f};
  102. float view[16];
  103. bx::mtxLookAt(view, eye, at);
  104. float proj[16];
  105. bx::mtxProj(proj, 60.0f, float(640) / float(360), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  106. bgfx::setViewTransform(0, view, proj);
  107.  
  108. bgfx::setVertexBuffer(0, vbh);
  109. bgfx::setIndexBuffer(ibh);
  110.  
  111. bgfx::submit(0, program);
  112.  
  113. bgfx::frame();
  114. counter++;
  115.  
  116. SDL_GL_SwapWindow(window);
  117. }
  118.  
  119. bgfx::shutdown();
  120. SDL_Quit();
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement