|
|
|
|
@ -12,6 +12,7 @@
|
|
|
|
|
|
|
|
|
|
#include <SDL.h> |
|
|
|
|
#include <glm/glm.hpp> |
|
|
|
|
#include <glm/geometric.hpp> |
|
|
|
|
#include <glm/gtc/matrix_transform.hpp> |
|
|
|
|
|
|
|
|
|
#include "hexlib.h" |
|
|
|
|
@ -72,6 +73,20 @@ typedef struct gl_matrix_info
|
|
|
|
|
glm::mat4 MVP; |
|
|
|
|
} gl_matrix_info; |
|
|
|
|
|
|
|
|
|
enum projection_type |
|
|
|
|
{ |
|
|
|
|
PERSPECTIVE, |
|
|
|
|
ORTHOGRAPHIC, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct camera |
|
|
|
|
{ |
|
|
|
|
glm::vec3 position; |
|
|
|
|
glm::vec3 forward; |
|
|
|
|
glm::vec3 target; |
|
|
|
|
glm::vec3 up; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
typedef struct gl_buffer |
|
|
|
|
{ |
|
|
|
|
GLuint buffer_id; |
|
|
|
|
@ -93,12 +108,15 @@ gl_matrix_info g_scene_matrices;
|
|
|
|
|
gl_render_group g_filled_hex_render_group; |
|
|
|
|
gl_render_group g_hex_line_render_group; |
|
|
|
|
gl_render_group g_debug_render_group; |
|
|
|
|
camera g_camera; |
|
|
|
|
|
|
|
|
|
// Interface
|
|
|
|
|
|
|
|
|
|
bool initRenderer(SDL_Handles &handles, v2i vpDims); |
|
|
|
|
bool addTexture(SDL_Handles &handles, std::string path); |
|
|
|
|
bool createScene(std::vector<hex_info>* hexes); |
|
|
|
|
void moveCamera(bool up, bool left, bool down, bool right); |
|
|
|
|
void rotateCamera(int32 xrel, int32 yrel); |
|
|
|
|
void renderFrame(const std::vector<hex_info> *hexes); |
|
|
|
|
void renderDebug(std::vector<Point> &vertices); |
|
|
|
|
void freeBuffers(); |
|
|
|
|
@ -116,6 +134,7 @@ void fillHexLineBuffer(GLfloat buf[], int len, std::vector<hex_info>* hexes);
|
|
|
|
|
bool checkGLBufferSize(GLenum buf_type, int expected_size, int line_num); |
|
|
|
|
bool initGLBufferObject(gl_buffer* buf_obj, int len, GLenum usage, GLfloat data[]); |
|
|
|
|
void drawRenderGroup(gl_render_group* rg, GLenum draw_mode, bool update_vertex_data); |
|
|
|
|
void initMatrices(projection_type p); |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
initRenderer(SDL_Handles &handles, v2i vpDims) |
|
|
|
|
@ -161,10 +180,10 @@ initRenderer(SDL_Handles &handles, v2i vpDims)
|
|
|
|
|
|
|
|
|
|
// TODO: blending, these options break the http://www.opengl-tutorial.org tutorials atm
|
|
|
|
|
// Setup render state: alpha-blending enabled, polygon fill
|
|
|
|
|
#if 0 |
|
|
|
|
#if 1 |
|
|
|
|
glEnable(GL_BLEND); |
|
|
|
|
glBlendEquation(GL_FUNC_ADD); |
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
|
|
glBlendFunc(GL_ONE, GL_SRC_ALPHA); |
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
@ -241,22 +260,82 @@ initShaderProgram(gl_render_group &rg, const char * vertex_code, const char * fr
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
createScene(std::vector<hex_info>* hexes) |
|
|
|
|
void |
|
|
|
|
initMatrices(projection_type p) |
|
|
|
|
{ |
|
|
|
|
// Model, View, Projection Matrices
|
|
|
|
|
if (p == PERSPECTIVE) |
|
|
|
|
{ |
|
|
|
|
g_scene_matrices.projection = glm::perspective( |
|
|
|
|
glm::radians(45.f), // FoV
|
|
|
|
|
16.f / 9.f, // ascpect ratio
|
|
|
|
|
0.1f, // near clip plane
|
|
|
|
|
1000.0f // far clip plane
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
g_camera.position = glm::vec3(640,0,100);
|
|
|
|
|
g_camera.target = glm::vec3(640,500,0); |
|
|
|
|
g_camera.up = glm::vec3(0,1,0); |
|
|
|
|
g_camera.forward = glm::normalize(g_camera.target - g_camera.position); |
|
|
|
|
|
|
|
|
|
glm::vec3 P, T, U; |
|
|
|
|
P = normalize(g_camera.position); |
|
|
|
|
T = normalize(g_camera.target); |
|
|
|
|
U = normalize(g_camera.up); |
|
|
|
|
//g_camera.forward = glm::cross(T - P, U);
|
|
|
|
|
#if 1 |
|
|
|
|
glm::mat4 mV = glm::lookAt( |
|
|
|
|
g_camera.position, // camera position
|
|
|
|
|
g_camera.target, // look at position
|
|
|
|
|
g_camera.up // "up" vector
|
|
|
|
|
); |
|
|
|
|
g_scene_matrices.view = mV; |
|
|
|
|
#else |
|
|
|
|
// get normalized vector from pos to target
|
|
|
|
|
glm::vec3 N = g_camera.target; |
|
|
|
|
glm::normalize(N); |
|
|
|
|
//glm::vec3 U = glm::vec3(0,1,0);
|
|
|
|
|
U = glm::cross(U, N); |
|
|
|
|
glm::vec3 V = glm::cross(N, U); |
|
|
|
|
glm::mat4 mR = glm::mat4(); |
|
|
|
|
|
|
|
|
|
mR[0][0] = U.x; mR[0][1] = U.y; mR[0][2] = U.z; mR[0][3] = 0.f; |
|
|
|
|
mR[1][0] = V.x; mR[1][1] = V.y; mR[1][2] = V.z; mR[1][3] = 0.f; |
|
|
|
|
mR[2][0] = N.x; mR[2][1] = N.y; mR[2][2] = N.z; mR[2][3] = 0.f; |
|
|
|
|
mR[3][0] = 0.f; mR[3][1] = 0.f; mR[3][2] = 0.f; mR[3][3] = 1.f; |
|
|
|
|
|
|
|
|
|
// get translation xfrom
|
|
|
|
|
glm::vec3 p = g_camera.position; |
|
|
|
|
glm::mat4 mT = glm::translate(glm::mat4(1.0f), glm::vec3(-1 * p.x, -1 * p.y, -1 * p.z)); |
|
|
|
|
|
|
|
|
|
glm::mat4 mVtemp = mR * mT; |
|
|
|
|
//g_scene_matrices.view = mR * mT;
|
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
else // ORTHO
|
|
|
|
|
{ |
|
|
|
|
// left, right, bottom, top, zNear, zFar
|
|
|
|
|
g_scene_matrices.projection = glm::ortho(0.f, 1280.0f, 0.f, 720.0f, 0.1f, 100.0f); |
|
|
|
|
|
|
|
|
|
g_scene_matrices.view = glm::lookAt( |
|
|
|
|
glm::vec3(0.0f, 0.0f, 2.0f), // camera position
|
|
|
|
|
glm::vec3(0.0f, 0.0f, 1.0f), // camera position
|
|
|
|
|
glm::vec3(0.0f, 0.0f, 0.0f), // look at position
|
|
|
|
|
glm::vec3(0,1,0) // "up" vector
|
|
|
|
|
); |
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_scene_matrices.model = glm::mat4(1.0f); |
|
|
|
|
g_scene_matrices.MVP = g_scene_matrices.projection * g_scene_matrices.view * g_scene_matrices.model;
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
|
#define PROJ_TYPE ORTHOGRAPHIC |
|
|
|
|
#else |
|
|
|
|
#define PROJ_TYPE PERSPECTIVE |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
createScene(std::vector<hex_info>* hexes) |
|
|
|
|
{ |
|
|
|
|
initMatrices(PROJ_TYPE); |
|
|
|
|
|
|
|
|
|
// Vertex Data
|
|
|
|
|
|
|
|
|
|
@ -313,6 +392,64 @@ createScene(std::vector<hex_info>* hexes)
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
moveCamera(bool up, bool left, bool down, bool right) |
|
|
|
|
{ |
|
|
|
|
if (!up && !left && !down && !right) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
glm::mat4 &m = g_scene_matrices.view; |
|
|
|
|
float d = 5.f; // units per frame
|
|
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
|
if (up) m = glm::translate(m, glm::vec3(0, -1 * d, 0)); |
|
|
|
|
if (left) m = glm::translate(m, glm::vec3(d, 0, 0)); |
|
|
|
|
if (down) m = glm::translate(m, glm::vec3(0, d, 0)); |
|
|
|
|
if (right) m = glm::translate(m, glm::vec3(-1 * d, 0, 0)); |
|
|
|
|
#endif |
|
|
|
|
glm::vec3 f = g_camera.forward; |
|
|
|
|
glm::vec3 u = g_camera.up; |
|
|
|
|
glm::vec3 old = g_camera.position; |
|
|
|
|
glm::vec3 &p = g_camera.position; |
|
|
|
|
if (up) p += (f * d); |
|
|
|
|
if (down) p -= (f * d); |
|
|
|
|
if (left) |
|
|
|
|
{ |
|
|
|
|
glm::vec3 l = glm::cross(u, f); |
|
|
|
|
p += l * d; |
|
|
|
|
} |
|
|
|
|
if (right) |
|
|
|
|
{ |
|
|
|
|
glm::vec3 r = glm::cross(f, u); |
|
|
|
|
p += r * d; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
glm::vec3 diff = old - p; |
|
|
|
|
m = glm::translate(m, diff); |
|
|
|
|
g_scene_matrices.MVP = g_scene_matrices.projection * m * g_scene_matrices.model;
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
rotateCamera(int32 xrel, int32 yrel) |
|
|
|
|
{ |
|
|
|
|
#if 1 |
|
|
|
|
glm::mat4 &m = g_scene_matrices.view; |
|
|
|
|
glm::vec3 &c = g_camera.position; |
|
|
|
|
|
|
|
|
|
m = glm::translate(m, glm::vec3(c.x, c.y, c.z)); |
|
|
|
|
m = glm::rotate(m, glm::radians(0.5f * xrel), glm::vec3(0.f,0.f,1.f)); |
|
|
|
|
m = glm::translate(m, glm::vec3(-1.f * c.x, -1.f * c.y, -1.f * c.z)); |
|
|
|
|
g_scene_matrices.MVP = g_scene_matrices.projection * m * g_scene_matrices.model;
|
|
|
|
|
#else |
|
|
|
|
glm::mat4 &m = g_scene_matrices.view; |
|
|
|
|
glm::vec3 &c = g_camera.position; |
|
|
|
|
|
|
|
|
|
m = glm::translate(m, glm::vec3(-1.f * c.x, -1.f * c.y, -1.f * c.z)); |
|
|
|
|
m = glm::rotate(m, glm::radians(0.5f * xrel), glm::vec3(0.f,0.f,1.f)); |
|
|
|
|
g_scene_matrices.MVP = g_scene_matrices.projection * m * g_scene_matrices.model;
|
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
fillTriangleBufferFromHex(GLfloat buf[], int idx, const hex_info &hex) |
|
|
|
|
{ |
|
|
|
|
|