#include <iostream> #include <SDL.h> #include <bgfx/bgfx.h> #include <bx/math.h> struct PosColorVertex { float x; float y; float z; uint32_t abgr; }; static PosColorVertex cubeVertices[] = { {-1.0f, 1.0f, 1.0f, 0xff000000 }, { 1.0f, 1.0f, 1.0f, 0xff0000ff }, {-1.0f, -1.0f, 1.0f, 0xff00ff00 }, { 1.0f, -1.0f, 1.0f, 0xff00ffff }, {-1.0f, 1.0f, -1.0f, 0xffff0000 }, { 1.0f, 1.0f, -1.0f, 0xffff00ff }, {-1.0f, -1.0f, -1.0f, 0xffffff00 }, { 1.0f, -1.0f, -1.0f, 0xffffffff }, }; static const uint16_t cubeTriList[] = { 0, 1, 2, 1, 3, 2, 4, 6, 5, 5, 6, 7, 0, 2, 4, 4, 2, 6, 1, 5, 3, 5, 7, 3, 0, 4, 1, 4, 5, 1, 2, 3, 6, 6, 3, 7, }; bgfx::ShaderHandle loadShader(const char *FILENAME) { FILE *file = fopen(FILENAME, "rb"); fseek(file, 0, SEEK_END); long fileSize = ftell(file); fseek(file, 0, SEEK_SET); const bgfx::Memory *mem = bgfx::alloc(fileSize + 1); fread(mem->data, 1, fileSize, file); mem->data[mem->size - 1] = '\0'; fclose(file); return bgfx::createShader(mem); } int main() { SDL_Init(SDL_INIT_EVERYTHING); SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_Window *window = SDL_CreateWindow("title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL); bgfx::Init bgfxInit; bgfxInit.type = bgfx::RendererType::OpenGL; // Automatically choose a renderer. bgfxInit.resolution.width = 640; bgfxInit.resolution.height = 360; bgfxInit.resolution.reset = BGFX_RESET_VSYNC; bgfx::init(bgfxInit); bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f, 0); bgfx::setViewRect(0, 0, 0, 640, 360); bgfx::VertexDecl pcvDecl; pcvDecl.begin() .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) .end(); bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(bgfx::makeRef(cubeVertices, sizeof(cubeVertices)), pcvDecl); bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(cubeTriList, sizeof(cubeTriList))); bgfx::ShaderHandle vsh = loadShader("/Users/sherjilozair/Desktop/Dev/bgfx/bgfx/examples/runtime/shaders/glsl/vs_cubes.bin"); bgfx::ShaderHandle fsh = loadShader("/Users/sherjilozair/Desktop/Dev/bgfx/bgfx/examples/runtime/shaders/glsl/fs_cubes.bin"); bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh, true); unsigned int counter = 0; while(!SDL_QuitRequested()) { const bx::Vec3 at = {0.0f, 0.0f, 0.0f}; const bx::Vec3 eye = {0.0f, 0.0f, -5.0f}; float view[16]; bx::mtxLookAt(view, eye, at); float proj[16]; bx::mtxProj(proj, 60.0f, float(640) / float(360), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth); bgfx::setViewTransform(0, view, proj); bgfx::setVertexBuffer(0, vbh); bgfx::setIndexBuffer(ibh); bgfx::submit(0, program); bgfx::frame(); counter++; SDL_GL_SwapWindow(window); } bgfx::shutdown(); SDL_Quit(); return 0; }