#include #define GLFW_EXPOSE_NATIVE_COCOA #define GLFW_EXPOSE_NATIVE_NSGL // #include "bgfx_utils.h" #include #include #include #include #include #include #include struct PosColorVertex { float m_x; float m_y; float m_z; uint32_t m_abgr; static void init() { ms_decl.begin() .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) .end(); }; static bgfx::VertexDecl ms_decl; }; bgfx::VertexDecl PosColorVertex::ms_decl; static PosColorVertex s_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 s_cubeTriList[] = { 0, 1, 2, // 0 1, 3, 2, 4, 6, 5, // 2 5, 6, 7, 0, 2, 4, // 4 4, 2, 6, 1, 5, 3, // 6 5, 7, 3, 0, 4, 1, // 8 4, 5, 1, 2, 3, 6, // 10 6, 3, 7, }; static const bgfx::ShaderHandle loadShader(bx::FileReaderI *_reader, const char *_filePath) { if (bx::open(_reader, _filePath)) { uint32_t size = (uint32_t)bx::getSize(_reader); const bgfx::Memory *mem = bgfx::alloc(size + 1); bx::read(_reader, mem->data, size); bx::close(_reader); mem->data[mem->size - 1] = '\0'; return bgfx::createShader(mem); } return BGFX_INVALID_HANDLE; } bgfx::ProgramHandle loadProgram(const char *_vsName, const char *_fsName) { bx::FileReader reader; bgfx::ShaderHandle vs = loadShader(&reader, "build/vs_cubes.bin"); bgfx::ShaderHandle fs = loadShader(&reader, "build/fs_cubes.bin"); return bgfx::createProgram(vs, fs, true); } static void error_callback(int error, const char *description) { fprintf(stderr, "Error: %s\n", description); } static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE); } int main(int argc, char const *argv[]) { glfwSetErrorCallback(error_callback); if (!glfwInit()) { return -1; } GLFWwindow *window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); { bgfx::PlatformData pd; bx::memSet(&pd, 0, sizeof(pd)); pd.nwh = glfwGetCocoaWindow(window); bgfx::setPlatformData(pd); } { bgfx::Init init; init.type = bgfx::RendererType::OpenGL; init.resolution.width = 640; init.resolution.height = 480; init.resolution.reset = BGFX_RESET_VSYNC; bgfx::init(init); } bgfx::reset(640, 480); bgfx::setDebug(BGFX_DEBUG_TEXT); bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, (uint32_t)0x303030ff, 1.0, 0); PosColorVertex::init(); bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices)), PosColorVertex::ms_decl); bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeTriList, sizeof(s_cubeTriList))); bgfx::ProgramHandle program = loadProgram("vs_cubes", "fs_cubes"); float time = 0; while (!glfwWindowShouldClose(window)) { bgfx::dbgTextClear(); bgfx::dbgTextPrintf(0, 1, 0x4f, "HOGEEEeee!!"); bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Initialization and debug text."); float at[3] = {0.0f, 0.0f, 0.0f}; float eye[3] = {0.0f, 0.0f, -35.0f}; { float view[16]; bx::mtxLookAt(view, eye, at); float proj[16]; bx::mtxProj(proj, 60.0f, 640.0f / 480.0f, 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth); bgfx::setViewTransform(0, view, proj); // Set view 0 default viewport. bgfx::setViewRect(0, 0, 0, 640, 480); } bgfx::touch(0); uint64_t state = BGFX_STATE_WRITE_MASK | BGFX_STATE_DEPTH_TEST_LESS | BGFX_STATE_CULL_CW | BGFX_STATE_MSAA; // Submit 11x11 cubes. for (uint32_t yy = 0; yy < 11; ++yy) { for (uint32_t xx = 0; xx < 11; ++xx) { float mtx[16]; bx::mtxRotateXY(mtx, time + xx * 0.21f, time + yy * 0.37f); mtx[12] = -15.0f + float(xx) * 3.0f; mtx[13] = -15.0f + float(yy) * 3.0f; mtx[14] = 0.0f; bgfx::setTransform(mtx); bgfx::setVertexBuffer(0, vbh); bgfx::setIndexBuffer(ibh); bgfx::setState(state); bgfx::submit(0, program); } } bgfx::frame(); glfwPollEvents(); time += 0.001f; } bgfx::destroy(program); bgfx::destroy(ibh); bgfx::destroy(vbh); bgfx::shutdown(); glfwTerminate(); return 0; }