Browse Source

fix 3d picking

master
cinnaboot 8 years ago
parent
commit
9bc0510483
  1. 4
      src/hexgame.cpp
  2. 21
      src/renderer.h

4
src/hexgame.cpp

@ -85,7 +85,9 @@ resetHexes()
hex_info * hex_info *
getSingleHex(int32 x, int32 y) 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)); Hex h = hex_round(pixel_to_hex(g_game_state->hex_layout, p));
for (hex_info &hxi : *g_game_state->hex_array) for (hex_info &hxi : *g_game_state->hex_array)

21
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_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_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_GL_SetSwapInterval(1); // vsync
SDL_GetCurrentDisplayMode(0, &handles.currentDisplayMode); SDL_GetCurrentDisplayMode(0, &handles.currentDisplayMode);
handles.window = handles.window =
@ -187,6 +189,9 @@ initRenderer(SDL_Handles &handles, v2i vpDims)
LOG(INFO)<< "opengl renderer: " << glGetString(GL_RENDERER) << "\n"; LOG(INFO)<< "opengl renderer: " << glGetString(GL_RENDERER) << "\n";
LOG(INFO) << "opengl version: " << glGetString(GL_VERSION) << "\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 // TODO: blending, these options break the http://www.opengl-tutorial.org tutorials atm
// Setup render state: alpha-blending enabled, polygon fill // Setup render state: alpha-blending enabled, polygon fill
#if 1 #if 1
@ -239,6 +244,20 @@ addTexture(SDL_Handles &handles, std::string path)
return true; 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 // NOTE: mat_id should match the matrix variable name in the shader source
bool bool
initShaderProgram(gl_render_group &rg, const char * vertex_code, const char * frag_code, const char * mat_id) 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; v -= r;
} }
// TODO: this still doesn't fix side to side movement magnitude when vAngle is
// close to +- 90 degrees
glm::normalize(v); glm::normalize(v);
p += (v * MOVE_SPEED); p += (v * MOVE_SPEED);
glm::vec3 diff = old - p; glm::vec3 diff = old - p;

Loading…
Cancel
Save