diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 64ad5ad..b3fc550 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -85,7 +85,9 @@ resetHexes() hex_info * getSingleHex(int32 x, int32 y) { - Point p(x, y); + v2i dims = g_render_state->viewport_dims; + v2f v = getUnprojectedCoords(x, y, dims.x, dims.y); + Point p(v.x, v.y); Hex h = hex_round(pixel_to_hex(g_game_state->hex_layout, p)); for (hex_info &hxi : *g_game_state->hex_array) diff --git a/src/renderer.h b/src/renderer.h index 449d02b..b96ccb6 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -155,6 +155,8 @@ initRenderer(SDL_Handles &handles, v2i vpDims) SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); SDL_GL_SetSwapInterval(1); // vsync SDL_GetCurrentDisplayMode(0, &handles.currentDisplayMode); handles.window = @@ -187,6 +189,9 @@ initRenderer(SDL_Handles &handles, v2i vpDims) LOG(INFO)<< "opengl renderer: " << glGetString(GL_RENDERER) << "\n"; LOG(INFO) << "opengl version: " << glGetString(GL_VERSION) << "\n"; + glEnable(GL_DEPTH_TEST); + glEnable(GL_LINE_SMOOTH); + // TODO: blending, these options break the http://www.opengl-tutorial.org tutorials atm // Setup render state: alpha-blending enabled, polygon fill #if 1 @@ -239,6 +244,20 @@ addTexture(SDL_Handles &handles, std::string path) return true; } +v2f +getUnprojectedCoords(int32 x, int32 y, int32 vp_width, int32 vp_height) +{ + // NOTE: using depth buffer may not be as accurate as doing ray-cast + GLfloat depth; + glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth); + glm::vec4 viewport = glm::vec4(0, 0, vp_width, vp_height); + glm::vec3 wincoord = glm::vec3(x, y, depth); + glm::vec3 vU = glm::unProject(wincoord, g_scene_matrices.view, g_scene_matrices.projection, viewport); + v2f v(vU.x, vU.y); + + return v; +} + // NOTE: mat_id should match the matrix variable name in the shader source bool initShaderProgram(gl_render_group &rg, const char * vertex_code, const char * frag_code, const char * mat_id) @@ -414,6 +433,8 @@ moveCamera(bool up, bool left, bool down, bool right, bool forward, bool backwar v -= r; } + // TODO: this still doesn't fix side to side movement magnitude when vAngle is + // close to +- 90 degrees glm::normalize(v); p += (v * MOVE_SPEED); glm::vec3 diff = old - p;