|
| 1 | +#include "Graphics.h" |
| 2 | + |
| 3 | +extern "C" { |
| 4 | +#include "graphics/transform.h" |
| 5 | +#include "util/logger.h" |
| 6 | +#include "util/log.h" |
| 7 | +} |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +void Graphics::free() |
| 12 | +{ |
| 13 | + ilG_shape_free(&box); |
| 14 | + ilG_shape_free(&ico); |
| 15 | + ilG_skybox_free(&skybox); |
| 16 | + ilG_ambient_free(&ambient); |
| 17 | + ilG_lighting_free(&sun); |
| 18 | + ilG_lighting_free(&point); |
| 19 | + ilG_tonemapper_free(&tonemapper); |
| 20 | + |
| 21 | + initialized = false; |
| 22 | +} |
| 23 | + |
| 24 | +bool Graphics::init(const Flags &flags) |
| 25 | +{ |
| 26 | + if (initialized) { |
| 27 | + free(); |
| 28 | + } else { |
| 29 | + ilG_renderman_init(rm); |
| 30 | + ilG_floatspace_init(&space, 2); |
| 31 | + } |
| 32 | + initialized = true; |
| 33 | + |
| 34 | + ilG_renderman_setup(rm, flags.msaa, flags.hdr); |
| 35 | + ilG_renderman_resize(rm, 800, 600); |
| 36 | + ilG_floatspace_build(&space, rm); |
| 37 | + glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE); |
| 38 | + |
| 39 | + ilG_box(&box); |
| 40 | + ilG_icosahedron(&ico); |
| 41 | + |
| 42 | + { |
| 43 | + ilG_tex skytex; |
| 44 | + ilA_img skyfaces[6]; |
| 45 | + static const char *const files[6] = { |
| 46 | + "north.png", |
| 47 | + "south.png", |
| 48 | + "up.png", |
| 49 | + "down.png", |
| 50 | + "west.png", |
| 51 | + "east.png" |
| 52 | + }; |
| 53 | + bool cont = true; |
| 54 | + for (unsigned i = 0; i < 6; i++) { |
| 55 | + ilA_imgerr res = ilA_img_loadfile(skyfaces + i, &demo_fs, files[i]); |
| 56 | + if (res) { |
| 57 | + il_log("%s: %s", files[i], ilA_img_strerror(res)); |
| 58 | + cont = false; |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + if (!cont) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + ilG_tex_loadcube(&skytex, skyfaces); |
| 66 | + char *error; |
| 67 | + if (!ilG_skybox_build(&skybox, rm, skytex, &box, &error)) { |
| 68 | + il_error("skybox: %s", error); |
| 69 | + ::free(error); |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + char *error; |
| 75 | + if (!ilG_ambient_build(&ambient, rm, &error)) { |
| 76 | + il_error("ambient: %s", error); |
| 77 | + ::free(error); |
| 78 | + return false; |
| 79 | + } |
| 80 | + if (!ilG_lighting_build(&sun, rm, &ico, ILG_SUN, flags.msaa, &error)) { |
| 81 | + il_error("sunlighting: %s", error); |
| 82 | + ::free(error); |
| 83 | + return false; |
| 84 | + } |
| 85 | + if (!ilG_lighting_build(&point, rm, &ico, ILG_POINT, flags.msaa, &error)) { |
| 86 | + il_error("lighting: %s", error); |
| 87 | + ::free(error); |
| 88 | + return false; |
| 89 | + } |
| 90 | + if (!ilG_tonemapper_build(&tonemapper, rm, flags.msaa, &error)) { |
| 91 | + il_error("tonemapper: %s", error); |
| 92 | + ::free(error); |
| 93 | + return false; |
| 94 | + } |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +void Graphics::draw(State &state) |
| 99 | +{ |
| 100 | + SDL_SetWindowGrab(window.window, SDL_bool(state.mouse_grab)); |
| 101 | + SDL_SetRelativeMouseMode(SDL_bool(state.mouse_grab)); |
| 102 | + SDL_GL_SetSwapInterval(state.vsync); |
| 103 | + |
| 104 | + auto s = window.resize(); |
| 105 | + unsigned width = s.first, height = s.second; |
| 106 | + if (unsigned(width) != rm->width || unsigned(height) != rm->height) { |
| 107 | + ilG_renderman_resize(rm, width, height); |
| 108 | + ilG_tonemapper_resize(&tonemapper, width, height); |
| 109 | + } |
| 110 | + space.projection = il_mat_perspective(state.fov, width / float(height), state.zmin, state.zfar); |
| 111 | + |
| 112 | + il_mat skybox_vp = viewmat(ILG_VIEW_R | ILG_PROJECTION); |
| 113 | + auto slocs = state.sunlight_locs; |
| 114 | + auto plocs = state.point_locs; |
| 115 | + std::vector<il_mat> |
| 116 | + /* ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION |
| 117 | + ILG_MODEL_T | ILG_VIEW_T |
| 118 | + ILG_MODEL_T | ILG_VP */ |
| 119 | + sun_ivp = objmats(slocs, ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION, state.sunlight_count), |
| 120 | + sun_mv = objmats(slocs, ILG_MODEL_T | ILG_VIEW_T, state.sunlight_count), |
| 121 | + sun_vp = objmats(slocs, ILG_MODEL_T | ILG_VP, state.sunlight_count), |
| 122 | + pnt_ivp = objmats(plocs, ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION, state.point_count), |
| 123 | + pnt_mv = objmats(plocs, ILG_MODEL_T | ILG_VIEW_T, state.point_count), |
| 124 | + pnt_vp = objmats(plocs, ILG_MODEL_T | ILG_VP, state.point_count); |
| 125 | + |
| 126 | + const float fovsquared = state.fov * state.fov; |
| 127 | + ambient.color = state.ambient_col; |
| 128 | + ambient.fovsquared = fovsquared; |
| 129 | + sun.gbuffer = &rm->gbuffer; |
| 130 | + sun.accum = &rm->accum; |
| 131 | + sun.width = width; |
| 132 | + sun.height = height; |
| 133 | + sun.fovsquared = fovsquared; |
| 134 | + point.gbuffer = &rm->gbuffer; |
| 135 | + point.accum = &rm->accum; |
| 136 | + point.width = width; |
| 137 | + point.height = height; |
| 138 | + point.fovsquared = fovsquared; |
| 139 | + tonemapper.exposure = state.exposure; |
| 140 | + tonemapper.gamma = state.gamma; |
| 141 | + |
| 142 | +#define push(n) glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, n) |
| 143 | +#define pop() glPopDebugGroup() |
| 144 | +#define with(n) for (bool cont = (push(n), true); cont; pop(), cont = false) |
| 145 | + |
| 146 | + with("Geometry") { |
| 147 | + ilG_geometry_bind(&rm->gbuffer); |
| 148 | + } |
| 149 | + with("Skybox") { |
| 150 | + ilG_skybox_draw(&skybox, skybox_vp); |
| 151 | + } |
| 152 | + for (auto &d : drawables) { |
| 153 | + with(d->name()) { |
| 154 | + d->draw(*this); |
| 155 | + } |
| 156 | + } |
| 157 | + with("Ambient Lighting") { |
| 158 | + ilG_ambient_draw(&ambient); |
| 159 | + } |
| 160 | + with("Sunlights") { |
| 161 | + ilG_lighting_draw(&sun, sun_ivp.data(), sun_mv.data(), sun_vp.data(), |
| 162 | + state.sunlight_lights, state.sunlight_count); |
| 163 | + } |
| 164 | + with("Point Lights") { |
| 165 | + ilG_lighting_draw(&point, pnt_ivp.data(), pnt_mv.data(), pnt_vp.data(), |
| 166 | + state.point_lights, state.point_count); |
| 167 | + } |
| 168 | + with("Tone Mapping") { |
| 169 | + ilG_tonemapper_draw(&tonemapper); |
| 170 | + } |
| 171 | + window.swap(); |
| 172 | + |
| 173 | +#undef push |
| 174 | +#undef pop |
| 175 | +#undef with |
| 176 | +} |
| 177 | + |
| 178 | +il_mat Graphics::viewmat(int type) |
| 179 | +{ |
| 180 | + return ilG_floatspace_viewmat(&space, type); |
| 181 | +} |
| 182 | + |
| 183 | +std::vector<il_mat> Graphics::objmats(unsigned *objects, int type, unsigned count) |
| 184 | +{ |
| 185 | + std::vector<il_mat> mats; |
| 186 | + mats.reserve(count); |
| 187 | + ilG_floatspace_objmats(&space, mats.data(), objects, type, count); |
| 188 | + return mats; |
| 189 | +} |
0 commit comments