Implemented first version of translate tool for editor that translated selected object in the xz axis by default and added manual breakpoint trigger option on gl error

dev
Shariq Shah 6 years ago
parent f7a1223d98
commit da1bf26b41
  1. 2
      build/genie.lua
  2. 2
      src/common/linmath.h
  3. 20
      src/game/bounding_volumes.c
  4. 15
      src/game/bounding_volumes.h
  5. 2
      src/game/camera.c
  6. 1
      src/game/camera.h
  7. 151
      src/game/editor.c
  8. 4
      src/game/editor.h
  9. 4
      src/game/geometry.c
  10. 3
      src/game/geometry.h
  11. 7
      src/game/gl_load.c
  12. 36
      src/game/im_render.c
  13. 2
      src/game/im_render.h
  14. 9
      src/game/scene.c
  15. 87
      src/game/shader.c
  16. 4
      src/system/config_vars.c
  17. 15
      todo.txt
  18. 2
      tools/regen_project.bat

@ -78,7 +78,7 @@ solution "Symmetry"
configuration "Debug" configuration "Debug"
links {"soloud_x64_d"} links {"soloud_x64_d"}
defines {"GL_DEBUG_CONTEXT"} defines {"GL_DEBUG_CONTEXT", "GL_BREAK_ON_ERROR"}
configuration "Release" configuration "Release"
links {"soloud_x64"} links {"soloud_x64"}

@ -13,7 +13,7 @@ i'll make my own additions like SIMD etc later on.
#ifndef M_PI #ifndef M_PI
#define M_PI 3.14159265358979323846f #define M_PI 3.14159265358979323846f
#endif #endif
#define EPSILON 0.0001 #define EPSILON 0.000001
#define TO_RADIANS(degrees) ((degrees * M_PI) / 180.0) #define TO_RADIANS(degrees) ((degrees * M_PI) / 180.0)
#define TO_DEGREES(radians) ((radians * 180.0) / M_PI) #define TO_DEGREES(radians) ((radians * 180.0) / M_PI)

@ -142,4 +142,22 @@ bool bv_intersect_sphere_ray(struct Bounding_Sphere* sphere, vec3* sphere_abs_po
//} //}
//return true; //return true;
} }
float bv_distance_ray_plane(struct Ray* ray, struct Plane* plane)
{
float dot = vec3_dot(&plane->normal, &ray->direction);
float abs_dot = fabsf(dot);
if(abs_dot >= EPSILON)
{
float dot_origin = vec3_dot(&plane->normal, &ray->origin);
float t = -(dot_origin + plane->constant) / dot;
if(t >= 0.0f)
return t;
else
return INFINITY;
}
else
return INFINITY;
}

@ -42,15 +42,22 @@ struct Ray
vec3 origin; vec3 origin;
}; };
struct Plane
{
vec3 normal;
float constant;
};
struct Raycast_Result struct Raycast_Result
{ {
struct Entity* entities_intersected[MAX_RAYCAST_ENTITIES_INTERSECT]; struct Entity* entities_intersected[MAX_RAYCAST_ENTITIES_INTERSECT];
int num_entities_intersected; int num_entities_intersected;
}; };
int bv_intersect_frustum_box(vec4* frustum, struct Bounding_Box* box, vec3* box_abs_position, vec3* box_abs_scale); int bv_intersect_frustum_box(vec4* frustum, struct Bounding_Box* box, vec3* box_abs_position, vec3* box_abs_scale);
int bv_intersect_frustum_sphere(vec4* frustum, struct Bounding_Sphere* sphere, vec3* sphere_abs_pos, vec3* sphere_abs_scale); int bv_intersect_frustum_sphere(vec4* frustum, struct Bounding_Sphere* sphere, vec3* sphere_abs_pos, vec3* sphere_abs_scale);
bool bv_intersect_frustum_point(vec4* frustum, const vec3* point); bool bv_intersect_frustum_point(vec4* frustum, const vec3* point);
bool bv_intersect_sphere_ray(struct Bounding_Sphere* sphere, vec3* sphere_abs_position, struct Ray* ray); bool bv_intersect_sphere_ray(struct Bounding_Sphere* sphere, vec3* sphere_abs_position, struct Ray* ray);
float bv_distance_ray_plane(struct Ray* ray, struct Plane* plane);
#endif #endif

@ -210,7 +210,7 @@ struct Ray camera_screen_coord_to_ray(struct Camera* camera, int mouse_x, int mo
int win_width = 0, win_height = 0; int win_width = 0, win_height = 0;
struct Game_State* game_state = game_state_get(); struct Game_State* game_state = game_state_get();
window_get_size(game_state->window, &win_width, &win_height); window_get_drawable_size(game_state->window, &win_width, &win_height);
float normalized_x = (2.f * (float)mouse_x) / (float)win_width - 1.f; float normalized_x = (2.f * (float)mouse_x) / (float)win_width - 1.f;
float normalized_y = 1.f - (2.f * (float)mouse_y) / (float)win_height; float normalized_y = 1.f - (2.f * (float)mouse_y) / (float)win_height;

@ -18,6 +18,5 @@ void camera_attach_fbo(struct Camera* camera,
bool has_color, bool has_color,
bool resizeable); bool resizeable);
struct Ray camera_screen_coord_to_ray(struct Camera* camera, int mouse_x, int mouse_y); struct Ray camera_screen_coord_to_ray(struct Camera* camera, int mouse_x, int mouse_y);
/* void camera_resize_all(int width, int height); */
#endif #endif

@ -49,7 +49,8 @@ enum Editor_Mode
EDITOR_MODE_NORMAL = 0, EDITOR_MODE_NORMAL = 0,
EDITOR_MODE_TRANSLATE, EDITOR_MODE_TRANSLATE,
EDITOR_MODE_ROTATE, EDITOR_MODE_ROTATE,
EDITOR_MODE_SCALE EDITOR_MODE_SCALE,
EDITOR_MODE_MAX
}; };
enum Editor_Axis enum Editor_Axis
@ -69,6 +70,8 @@ static int window_flags = NK_WINDOW_BORDER |
NK_WINDOW_SCALABLE; NK_WINDOW_SCALABLE;
static void editor_on_mousebutton(const struct Event* event); static void editor_on_mousebutton(const struct Event* event);
static void editor_on_mousemotion(const struct Event* event);
static void editor_on_key_release(const struct Event* event);
static void editor_camera_update(struct Editor* editor, float dt); static void editor_camera_update(struct Editor* editor, float dt);
static void editor_show_entity_in_list(struct Editor* editor, struct nk_context* context, struct Scene* scene, struct Entity* entity); static void editor_show_entity_in_list(struct Editor* editor, struct nk_context* context, struct Scene* scene, struct Entity* entity);
static void editor_widget_color_combov3(struct nk_context* context, vec3* color, int width, int height); static void editor_widget_color_combov3(struct nk_context* context, vec3* color, int width, int height);
@ -107,14 +110,21 @@ void editor_init(struct Editor* editor)
editor->current_axis = EDITOR_AXIS_XY; editor->current_axis = EDITOR_AXIS_XY;
editor->grid_enabled = 1; editor->grid_enabled = 1;
editor->grid_num_lines = 50; editor->grid_num_lines = 50;
editor->grid_scale = 2.f; editor->grid_scale = 1.f;
editor->tool_mesh_draw_enabled = 0;
editor->tool_snap_enabled = 1;
vec3_fill(&editor->tool_mesh_position, 0.f, 0.f, 0.f);
vec4_fill(&editor->tool_mesh_color, 1.f, 0.f, 1.f, 1.f);
vec4_fill(&editor->selected_entity_colour, 0.f, 1.f, 0.f, 1.f); vec4_fill(&editor->selected_entity_colour, 0.f, 1.f, 0.f, 1.f);
vec4_fill(&editor->grid_color, 0.3f, 0.3f, 0.3f, 1.f); vec4_fill(&editor->grid_color, 0.3f, 0.3f, 0.3f, 1.f);
debug_vars_list = array_new(struct Debug_Variable); debug_vars_list = array_new(struct Debug_Variable);
empty_indices = array_new(int); empty_indices = array_new(int);
event_manager_subscribe(game_state_get()->event_manager, EVT_MOUSEBUTTON_PRESSED, &editor_on_mousebutton); struct Event_Manager* event_manager = game_state_get()->event_manager;
event_manager_subscribe(game_state_get()->event_manager, EVT_MOUSEBUTTON_RELEASED, &editor_on_mousebutton); event_manager_subscribe(event_manager, EVT_MOUSEBUTTON_PRESSED, &editor_on_mousebutton);
event_manager_subscribe(event_manager, EVT_MOUSEMOTION, &editor_on_mousemotion);
event_manager_subscribe(event_manager, EVT_MOUSEBUTTON_RELEASED, &editor_on_mousebutton);
event_manager_subscribe(event_manager, EVT_KEY_RELEASED, &editor_on_key_release);
} }
void editor_init_camera(struct Editor* editor, struct Hashmap* cvars) void editor_init_camera(struct Editor* editor, struct Hashmap* cvars)
@ -269,7 +279,6 @@ void editor_update(struct Editor* editor, float dt)
/* Top Panel */ /* Top Panel */
if(nk_begin(context, "Top Panel", nk_recti(0, 0, win_width, editor->top_panel_height), NK_WINDOW_NO_SCROLLBAR)) if(nk_begin(context, "Top Panel", nk_recti(0, 0, win_width, editor->top_panel_height), NK_WINDOW_NO_SCROLLBAR))
{ {
static float top_panel_ratios[] = { 0.1f, 0.1f, 0.7f, 0.1f };
static int frames = 0; static int frames = 0;
static int fps = 0; static int fps = 0;
static float seconds = 0.f; static float seconds = 0.f;
@ -282,13 +291,14 @@ void editor_update(struct Editor* editor, float dt)
frames = 0; frames = 0;
} }
const int row_height = 25.f;
nk_menubar_begin(context); nk_menubar_begin(context);
nk_layout_row_begin(context, NK_DYNAMIC, editor->top_panel_height, 5); nk_layout_row_begin(context, NK_DYNAMIC, editor->top_panel_height - 5, 6);
nk_layout_row_push(context, 0.1f); nk_layout_row_push(context, 0.03f);
if(nk_menu_begin_label(context, "File", NK_TEXT_CENTERED | NK_TEXT_ALIGN_MIDDLE, nk_vec2(150, 100))) if(nk_menu_begin_label(context, "File", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE, nk_vec2(150, 100)))
{ {
nk_layout_row_dynamic(context, 25, 1); nk_layout_row_dynamic(context, row_height, 1);
nk_menu_item_label(context, "Open", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE); nk_menu_item_label(context, "Open", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE);
nk_menu_item_label(context, "Save", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE); nk_menu_item_label(context, "Save", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE);
if(nk_menu_item_label(context, "Back to Game", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) if(nk_menu_item_label(context, "Back to Game", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE))
@ -299,19 +309,19 @@ void editor_update(struct Editor* editor, float dt)
nk_menu_end(context); nk_menu_end(context);
} }
nk_layout_row_push(context, 0.1f); nk_layout_row_push(context, 0.05f);
if(nk_menu_begin_label(context, "Settings", NK_TEXT_CENTERED | NK_TEXT_ALIGN_MIDDLE, nk_vec2(150, 100))) if(nk_menu_begin_label(context, "Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE, nk_vec2(150, 100)))
{ {
nk_layout_row_dynamic(context, 25, 1); nk_layout_row_dynamic(context, row_height, 1);
if(nk_menu_item_label(context, "Editor Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_editor = !editor->window_settings_editor; if(nk_menu_item_label(context, "Editor Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_editor = !editor->window_settings_editor;
if(nk_menu_item_label(context, "Render Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_renderer = !editor->window_settings_renderer; if(nk_menu_item_label(context, "Render Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_renderer = !editor->window_settings_renderer;
nk_menu_end(context); nk_menu_end(context);
} }
nk_layout_row_push(context, 0.1f); nk_layout_row_push(context, 0.05f);
if(nk_menu_begin_label(context, "Windows", NK_TEXT_CENTERED | NK_TEXT_ALIGN_MIDDLE, nk_vec2(180, 100))) if(nk_menu_begin_label(context, "Windows", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE, nk_vec2(180, 100)))
{ {
nk_layout_row_dynamic(context, 25, 1); nk_layout_row_dynamic(context, row_height, 1);
nk_checkbox_label(context, "Scene Heirarchy", &editor->window_scene_heirarchy); nk_checkbox_label(context, "Scene Heirarchy", &editor->window_scene_heirarchy);
nk_checkbox_label(context, "Property Inspector", &editor->window_property_inspector); nk_checkbox_label(context, "Property Inspector", &editor->window_property_inspector);
nk_checkbox_label(context, "Debug Variables", &editor->window_debug_variables); nk_checkbox_label(context, "Debug Variables", &editor->window_debug_variables);
@ -321,7 +331,12 @@ void editor_update(struct Editor* editor, float dt)
nk_layout_row_push(context, 0.6f); nk_layout_row_push(context, 0.6f);
nk_spacing(context, 1); nk_spacing(context, 1);
nk_layout_row_push(context, 0.1f); nk_layout_row_push(context, 0.20f);
static const char* editor_modes[] = { "Normal", "Translate", "Rotate", "Scale" };
static const char* editor_axis[] = { "XY", "X", "Y", "Z" };
nk_labelf(context, NK_TEXT_ALIGN_RIGHT | NK_TEXT_ALIGN_MIDDLE, "Mode : %s Axis: %s", editor_modes[editor->current_mode], editor_axis[editor->current_axis]);
nk_layout_row_push(context, 0.07f);
nk_labelf(context, NK_TEXT_ALIGN_RIGHT | NK_TEXT_ALIGN_MIDDLE, "FPS : %.d", fps); nk_labelf(context, NK_TEXT_ALIGN_RIGHT | NK_TEXT_ALIGN_MIDDLE, "FPS : %.d", fps);
nk_menubar_end(context); nk_menubar_end(context);
@ -334,6 +349,13 @@ void editor_update(struct Editor* editor, float dt)
if(editor->window_settings_renderer) editor_window_renderer_settings(context, editor, game_state); if(editor->window_settings_renderer) editor_window_renderer_settings(context, editor, game_state);
if(editor->window_settings_editor) editor_window_settings_editor(context, editor, game_state); if(editor->window_settings_editor) editor_window_settings_editor(context, editor, game_state);
if(editor->tool_mesh_draw_enabled)
{
if(editor->current_mode == EDITOR_MODE_TRANSLATE)
{
im_sphere(1.f, editor->tool_mesh_position, (quat) { 0.f, 0.f, 0.f, 1.f }, editor->tool_mesh_color, GDM_TRIANGLES);
}
}
} }
void editor_on_mousebutton(const struct Event* event) void editor_on_mousebutton(const struct Event* event)
@ -347,9 +369,9 @@ void editor_on_mousebutton(const struct Event* event)
return; return;
if(event->mousebutton.button == MSB_LEFT && if(event->mousebutton.button == MSB_LEFT &&
event->type == EVT_MOUSEBUTTON_RELEASED && event->type == EVT_MOUSEBUTTON_RELEASED &&
!editor->camera_looking_around && !editor->camera_looking_around &&
nk_item_is_any_active(&gui->context) == 0) nk_item_is_any_active(&gui->context) == 0)
{ {
log_message("Editor Picking"); log_message("Editor Picking");
struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR]; struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR];
@ -378,16 +400,99 @@ void editor_on_mousebutton(const struct Event* event)
} }
else else
{ {
//Deselect the currently selected entity if nothing was found
if(editor->selected_entity) if(editor->selected_entity)
{ {
editor->selected_entity->editor_selected = false; if(editor->current_mode == EDITOR_MODE_TRANSLATE)
editor->selected_entity = NULL; {
transform_set_position(editor->selected_entity, &editor->tool_mesh_position);
}
else
{
//Deselect the currently selected entity if nothing was found
editor->selected_entity->editor_selected = false;
editor->selected_entity = NULL;
}
} }
} }
} }
} }
void editor_on_mousemotion(const struct Event* event)
{
struct Game_State* game_state = game_state_get();
struct Editor* editor = game_state->editor;
switch(editor->current_mode)
{
case EDITOR_MODE_NORMAL:
{
}
break;
case EDITOR_MODE_TRANSLATE:
{
if(editor->selected_entity)
{
struct Camera* editor_camera = &game_state->scene->cameras[CAM_EDITOR];
vec3 position = { 0.f };
transform_get_absolute_position(editor->selected_entity, &position);
struct Ray cam_ray;
cam_ray = camera_screen_coord_to_ray(editor_camera, event->mousemotion.x, event->mousemotion.y);
struct Plane ground_plane;
vec3_fill(&ground_plane.normal, 0.f, 1.f, 0.f);
float dot = vec3_dot(&ground_plane.normal, &position);
ground_plane.constant = -dot;
float distance = bv_distance_ray_plane(&cam_ray, &ground_plane);
if(distance < INFINITY && distance > -INFINITY)
{
vec3 abs_cam_pos, projected_point, cam_forward;
transform_get_absolute_position(editor_camera, &abs_cam_pos);
transform_get_absolute_forward(editor_camera, &cam_forward);
vec3_scale(&projected_point, &cam_ray.direction, distance);
vec3_add(&position, &projected_point, &abs_cam_pos);
if(editor->tool_snap_enabled)
{
position.x = roundf(position.x / editor->grid_scale) * editor->grid_scale;
position.y = roundf(position.y / editor->grid_scale) * editor->grid_scale;
position.z = roundf(position.z / editor->grid_scale) * editor->grid_scale;
vec3_assign(&editor->tool_mesh_position, &position);
}
}
log_message("%.3f, %.3f, %.3f", position.x, position.y, position.z);
}
}
break;
default: break;
}
}
void editor_on_key_release(const struct Event* event)
{
struct Editor* editor = game_state_get()->editor;
if(event->key.key == KEY_TAB)
{
editor->current_mode++;
if(editor->current_mode == EDITOR_MODE_MAX)
editor->current_mode = EDITOR_MODE_NORMAL;
}
if(!editor->camera_looking_around)
{
if(event->key.key == KEY_Q) editor->current_mode = EDITOR_MODE_NORMAL;
if(event->key.key == KEY_W) editor->current_mode = EDITOR_MODE_TRANSLATE;
if(event->key.key == KEY_E) editor->current_mode = EDITOR_MODE_ROTATE;
if(event->key.key == KEY_R) editor->current_mode = EDITOR_MODE_SCALE;
}
if(editor->current_mode == EDITOR_MODE_TRANSLATE)
editor->tool_mesh_draw_enabled = 1;
else
editor->tool_mesh_draw_enabled = 0;
}
void editor_camera_update(struct Editor* editor, float dt) void editor_camera_update(struct Editor* editor, float dt)
{ {
struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR]; struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR];
@ -939,7 +1044,7 @@ void editor_window_settings_editor(struct nk_context* context, struct Editor* ed
editor_widget_color_combov4(context, &editor->grid_color, 200, 400); editor_widget_color_combov4(context, &editor->grid_color, 200, 400);
nk_layout_row_dynamic(context, row_height, 1); nk_layout_row_dynamic(context, row_height, 1);
nk_property_int(context, "Grid Lines", 10, &editor->grid_num_lines, 100, 1, 1); nk_property_int(context, "Grid Lines", 10, &editor->grid_num_lines, 200, 1, 1);
nk_layout_row_dynamic(context, row_height, 1); nk_layout_row_dynamic(context, row_height, 1);
nk_property_float(context, "Grid Scale", 0.25f, &editor->grid_scale, 10.f, 1, 0.25f); nk_property_float(context, "Grid Scale", 0.25f, &editor->grid_scale, 10.f, 1, 0.25f);

@ -28,6 +28,10 @@ struct Editor
vec4 grid_color; vec4 grid_color;
int grid_num_lines; int grid_num_lines;
float grid_scale; float grid_scale;
int tool_snap_enabled;
vec3 tool_mesh_position;
vec4 tool_mesh_color;
int tool_mesh_draw_enabled;
}; };
void editor_init(struct Editor* editor_state); void editor_init(struct Editor* editor_state);

@ -5,7 +5,6 @@
#include "renderer.h" #include "renderer.h"
#include "transform.h" #include "transform.h"
#include "../system/file_io.h" #include "../system/file_io.h"
#include "gl_load.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -14,10 +13,9 @@
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
/* Data */ GLenum* draw_modes = NULL;
static struct Geometry* geometry_list; static struct Geometry* geometry_list;
static int* empty_indices; static int* empty_indices;
static GLenum* draw_modes;
static int load_from_file(struct Geometry* geometry, const char* filename); static int load_from_file(struct Geometry* geometry, const char* filename);
static void create_vao(struct Geometry* geometry); static void create_vao(struct Geometry* geometry);

@ -4,9 +4,12 @@
#include "../common/num_types.h" #include "../common/num_types.h"
#include "../common/linmath.h" #include "../common/linmath.h"
#include "bounding_volumes.h" #include "bounding_volumes.h"
#include "gl_load.h"
struct Entity; struct Entity;
extern GLenum* draw_modes;
enum Geometry_Draw_Mode enum Geometry_Draw_Mode
{ {
GDM_TRIANGLES = 0, GDM_TRIANGLES = 0,

@ -2,6 +2,8 @@
#include "../common/log.h" #include "../common/log.h"
#include "../system/platform.h" #include "../system/platform.h"
#include <SDL_assert.h>
#ifndef USE_GLAD #ifndef USE_GLAD
#define GLE(ret, name, ...) name##proc * gl##name; #define GLE(ret, name, ...) name##proc * gl##name;
@ -62,7 +64,12 @@ void gl_check_error(const char * expression, unsigned int line, const char * fil
} }
if(error_code != GL_NO_ERROR) if(error_code != GL_NO_ERROR)
{
log_error("GL", "(%s:%d:%s) : %s", file, line, expression, error_string); log_error("GL", "(%s:%d:%s) : %s", file, line, expression, error_string);
#ifdef GL_BREAK_ON_ERROR
SDL_TriggerBreakpoint();
#endif
}
else else
error = 0; error = 0;
} }

@ -26,7 +26,9 @@ static struct
IM_State; IM_State;
static struct IM_Geom* active_geom = NULL; static struct IM_Geom* active_geom = NULL;
static int active_vertex_index = 0; static int active_vertex_index = 0;
static void im_geom_reset(struct IM_Geom* geom);
void im_init(void) void im_init(void)
{ {
@ -81,6 +83,7 @@ void im_begin(vec3 position, quat rotation, vec3 scale, vec4 color, int draw_mod
} }
IM_State.curr_geom++; IM_State.curr_geom++;
active_geom = &IM_State.geometries[IM_State.curr_geom]; active_geom = &IM_State.geometries[IM_State.curr_geom];
im_geom_reset(active_geom);
active_geom->start_index = IM_State.curr_vertex; active_geom->start_index = IM_State.curr_vertex;
active_geom->type = IGT_DYNAMIC; active_geom->type = IGT_DYNAMIC;
active_geom->draw_mode = draw_mode; active_geom->draw_mode = draw_mode;
@ -106,11 +109,12 @@ void im_box(float x, float y, float z, vec3 position, quat rotation, vec4 color,
{ {
if(active_geom) if(active_geom)
{ {
log_error("im_begin", "im_begin called before im_end"); log_error("im_box", "im_box called before im_end");
return; return;
} }
IM_State.curr_geom++; IM_State.curr_geom++;
active_geom = &IM_State.geometries[IM_State.curr_geom]; active_geom = &IM_State.geometries[IM_State.curr_geom];
im_geom_reset(active_geom);
active_geom->type = IGT_PRIMITIVE; active_geom->type = IGT_PRIMITIVE;
active_geom->draw_mode = draw_mode; active_geom->draw_mode = draw_mode;
active_geom->prim_geom_index = geom_create_from_file("cube.symbres"); active_geom->prim_geom_index = geom_create_from_file("cube.symbres");
@ -126,11 +130,12 @@ void im_sphere(float radius, vec3 position, quat rotation, vec4 color, int draw_
{ {
if(active_geom) if(active_geom)
{ {
log_error("im_begin", "im_begin called before im_end"); log_error("im_sphere", "im_sphere called before im_end");
return; return;
} }
IM_State.curr_geom++; IM_State.curr_geom++;
active_geom = &IM_State.geometries[IM_State.curr_geom]; active_geom = &IM_State.geometries[IM_State.curr_geom];
im_geom_reset(active_geom);
active_geom->type = IGT_PRIMITIVE; active_geom->type = IGT_PRIMITIVE;
active_geom->draw_mode = draw_mode; active_geom->draw_mode = draw_mode;
active_geom->prim_geom_index = geom_create_from_file("sphere.symbres"); active_geom->prim_geom_index = geom_create_from_file("sphere.symbres");
@ -145,7 +150,7 @@ void im_sphere(float radius, vec3 position, quat rotation, vec4 color, int draw_
void im_end(void) void im_end(void)
{ {
active_geom->num_vertices = active_vertex_index + 1; active_geom->num_vertices = active_vertex_index + 1;
glBindBuffer(GL_ARRAY_BUFFER, IM_State.vbo); GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, IM_State.vbo));
GL_CHECK(glBufferSubData(GL_ARRAY_BUFFER, GL_CHECK(glBufferSubData(GL_ARRAY_BUFFER,
sizeof(struct IM_Vertex) * active_geom->start_index, sizeof(struct IM_Vertex) * active_geom->start_index,
sizeof(struct IM_Vertex) * active_geom->num_vertices, sizeof(struct IM_Vertex) * active_geom->num_vertices,
@ -166,7 +171,6 @@ void im_render(struct Camera* active_viewer)
{ {
static mat4 mvp, translation, rotation, scale; static mat4 mvp, translation, rotation, scale;
glBindVertexArray(IM_State.vao);
for(int i = 0; i <= IM_State.curr_geom; i++) for(int i = 0; i <= IM_State.curr_geom; i++)
{ {
struct IM_Geom* geom = &IM_State.geometries[i]; struct IM_Geom* geom = &IM_State.geometries[i];
@ -190,16 +194,17 @@ void im_render(struct Camera* active_viewer)
shader_set_uniform_vec4(IM_State.im_shader, "geom_color", &geom->color); shader_set_uniform_vec4(IM_State.im_shader, "geom_color", &geom->color);
if(geom->type == IGT_DYNAMIC) if(geom->type == IGT_DYNAMIC)
{ {
glDrawArrays(geom->draw_mode, geom->start_index, geom->num_vertices); GL_CHECK(glBindVertexArray(IM_State.vao));
GL_CHECK(glDrawArrays(draw_modes[geom->draw_mode], geom->start_index, geom->num_vertices));
GL_CHECK(glBindVertexArray(0));
} }
else else
{ {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); GL_CHECK(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE));
geom_render(geom->prim_geom_index, geom->draw_mode); geom_render(geom->prim_geom_index, geom->draw_mode);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); GL_CHECK(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL));
} }
} }
glBindVertexArray(0);
} }
shader_unbind(); shader_unbind();
@ -207,3 +212,16 @@ void im_render(struct Camera* active_viewer)
IM_State.curr_geom = -1; IM_State.curr_geom = -1;
IM_State.curr_vertex = 0; IM_State.curr_vertex = 0;
} }
void im_geom_reset(struct IM_Geom* geom)
{
vec3_fill(&geom->position, 0.f, 0.f, 0.f);
quat_identity(&geom->rotation);
vec3_fill(&geom->scale, 1.f, 1.f, 1.f);
vec4_fill(&geom->color, 0.f, 0.f, 0.f, 0.f);
geom->type = -1;
geom->start_index = -1;
geom->num_vertices = 0;
geom->prim_geom_index = -1;
geom->draw_mode = -1;
}

@ -31,7 +31,7 @@ struct IM_Geom
int prim_geom_index; int prim_geom_index;
}; };
int draw_mode; int draw_mode;
}; };
struct Camera; struct Camera;

@ -14,6 +14,7 @@
#include "editor.h" #include "editor.h"
#include "../system/sound.h" #include "../system/sound.h"
#include "../system/physics.h" #include "../system/physics.h"
#include "../system/platform.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -22,6 +23,8 @@
void scene_init(struct Scene* scene) void scene_init(struct Scene* scene)
{ {
assert(scene); assert(scene);
struct Game_State* game_state = game_state_get();
//Initialize the root entity //Initialize the root entity
entity_init(&scene->root_entity, "ROOT_ENTITY", NULL); entity_init(&scene->root_entity, "ROOT_ENTITY", NULL);
scene->root_entity.active = true; scene->root_entity.active = true;
@ -45,15 +48,17 @@ void scene_init(struct Scene* scene)
mesh->model.material = NULL; mesh->model.material = NULL;
} }
for(int i = 0; i < MAX_SOUND_SOURCES; i++) entity_reset(&scene->sound_sources[i], i); for(int i = 0; i < MAX_SOUND_SOURCES; i++) entity_reset(&scene->sound_sources[i], i);
int width = 1280, height = 720;
window_get_drawable_size(game_state->window, &width, &height);
for(int i = 0; i < MAX_CAMERAS; i++) for(int i = 0; i < MAX_CAMERAS; i++)
{ {
entity_init(&scene->cameras[i], NULL, &scene->root_entity); entity_init(&scene->cameras[i], NULL, &scene->root_entity);
camera_init(&scene->cameras[i], 1024, 768); camera_init(&scene->cameras[i], width, height);
scene->cameras[i].base.id = i; scene->cameras[i].base.id = i;
} }
player_init(&scene->player, scene); player_init(&scene->player, scene);
struct Game_State* game_state = game_state_get();
editor_init_camera(game_state->editor, game_state->cvars); editor_init_camera(game_state->editor, game_state->cvars);
scene->active_camera_index = game_state_get()->game_mode == GAME_MODE_GAME ? CAM_GAME : CAM_EDITOR; scene->active_camera_index = game_state_get()->game_mode == GAME_MODE_GAME ? CAM_GAME : CAM_EDITOR;

@ -102,23 +102,23 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name)
const GLint* vert_size = &v_size; const GLint* vert_size = &v_size;
const GLint* frag_size = &f_size; const GLint* frag_size = &f_size;
glShaderSource(vert_shader, 1, &vert_sourcePtr, vert_size); GL_CHECK(glShaderSource(vert_shader, 1, &vert_sourcePtr, vert_size));
glShaderSource(frag_shader, 1, &frag_sourcePtr, frag_size); GL_CHECK(glShaderSource(frag_shader, 1, &frag_sourcePtr, frag_size));
glCompileShader(vert_shader); GL_CHECK(glCompileShader(vert_shader));
glCompileShader(frag_shader); GL_CHECK(glCompileShader(frag_shader));
GLint is_vert_compiled = 0; GLint is_vert_compiled = 0;
GLint is_frag_compiled = 0; GLint is_frag_compiled = 0;
glGetShaderiv(vert_shader, GL_COMPILE_STATUS, &is_vert_compiled); GL_CHECK(glGetShaderiv(vert_shader, GL_COMPILE_STATUS, &is_vert_compiled));
glGetShaderiv(frag_shader, GL_COMPILE_STATUS, &is_frag_compiled); GL_CHECK(glGetShaderiv(frag_shader, GL_COMPILE_STATUS, &is_frag_compiled));
if(!is_vert_compiled) if(!is_vert_compiled)
{ {
GLint log_size = 0; GLint log_size = 0;
glGetShaderiv(vert_shader, GL_INFO_LOG_LENGTH, &log_size); GL_CHECK(glGetShaderiv(vert_shader, GL_INFO_LOG_LENGTH, &log_size));
char* message = (char *)malloc(sizeof(char) * log_size); char* message = (char *)malloc(sizeof(char) * log_size);
glGetShaderInfoLog(vert_shader, log_size, NULL, message); GL_CHECK(glGetShaderInfoLog(vert_shader, log_size, NULL, message));
log_error("shader:create", "COMPILING VS %s : %s", vert_shader_name, message); log_error("shader:create", "COMPILING VS %s : %s", vert_shader_name, message);
debug_print_shader(vert_source); debug_print_shader(vert_source);
@ -128,9 +128,9 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name)
if(!is_frag_compiled) if(!is_frag_compiled)
{ {
GLint log_size = 0; GLint log_size = 0;
glGetShaderiv(frag_shader, GL_INFO_LOG_LENGTH, &log_size); GL_CHECK(glGetShaderiv(frag_shader, GL_INFO_LOG_LENGTH, &log_size));
char* message = (char *)malloc(sizeof(char) * log_size); char* message = (char *)malloc(sizeof(char) * log_size);
glGetShaderInfoLog(frag_shader, log_size, NULL, message); GL_CHECK(glGetShaderInfoLog(frag_shader, log_size, NULL, message));
log_error("shader:create", "COMPILING FS %s : %s", frag_shader_name, message); log_error("shader:create", "COMPILING FS %s : %s", frag_shader_name, message);
debug_print_shader(frag_source); debug_print_shader(frag_source);
@ -142,14 +142,14 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name)
if(!is_vert_compiled || !is_frag_compiled) if(!is_vert_compiled || !is_frag_compiled)
{ {
glDeleteShader(vert_shader); GL_CHECK(glDeleteShader(vert_shader));
glDeleteShader(frag_shader); GL_CHECK(glDeleteShader(frag_shader));
return -1; return -1;
} }
GLuint program = glCreateProgram(); GLuint program = glCreateProgram();
glAttachShader(program, vert_shader); GL_CHECK(glAttachShader(program, vert_shader));
glAttachShader(program, frag_shader); GL_CHECK(glAttachShader(program, frag_shader));
// Bind attribute locations // Bind attribute locations
GL_CHECK(glBindAttribLocation(program, ATTRIB_LOC_POSITION, "vPosition")); GL_CHECK(glBindAttribLocation(program, ATTRIB_LOC_POSITION, "vPosition"));
@ -159,26 +159,26 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name)
GL_CHECK(glLinkProgram(program)); GL_CHECK(glLinkProgram(program));
GLint is_linked = 0; GLint is_linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &is_linked); GL_CHECK(glGetProgramiv(program, GL_LINK_STATUS, &is_linked));
if(!is_linked) if(!is_linked)
{ {
GLint log_size = 0; GLint log_size = 0;
glGetProgramiv(program, GL_LINK_STATUS, &log_size); GL_CHECK(glGetProgramiv(program, GL_LINK_STATUS, &log_size));
char* message = (char *)malloc(sizeof(char) * log_size); char* message = (char *)malloc(sizeof(char) * log_size);
glGetProgramInfoLog(program, log_size, NULL, message); GL_CHECK(glGetProgramInfoLog(program, log_size, NULL, message));
log_error("shader:create", "LINK SHADER : %s", message); log_error("shader:create", "LINK SHADER : %s", message);
free(message); free(message);
glDeleteProgram(program); GL_CHECK(glDeleteProgram(program));
glDeleteShader(vert_shader); GL_CHECK(glDeleteShader(vert_shader));
glDeleteShader(frag_shader); GL_CHECK(glDeleteShader(frag_shader));
return -1; return -1;
} }
/* Safe to delete shaders now */ /* Safe to delete shaders now */
glDeleteShader(vert_shader); GL_CHECK(glDeleteShader(vert_shader));
glDeleteShader(frag_shader); GL_CHECK(glDeleteShader(frag_shader));
/* add new object or overwrite existing one */ /* add new object or overwrite existing one */
uint* new_shader = 0; uint* new_shader = 0;
@ -207,17 +207,18 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name)
void shader_bind(const int shader_index) void shader_bind(const int shader_index)
{ {
glUseProgram(shader_list[shader_index]); GL_CHECK(glUseProgram(shader_list[shader_index]));
} }
void shader_unbind(void) void shader_unbind(void)
{ {
glUseProgram(0); GL_CHECK(glUseProgram(0));
} }
int shader_get_uniform_location(const int shader_index, const char* name) int shader_get_uniform_location(const int shader_index, const char* name)
{ {
GLint handle = glGetUniformLocation(shader_list[shader_index], name); GLint handle = 0;
GL_CHECK(handle = glGetUniformLocation(shader_list[shader_index], name));
if(handle == -1) if(handle == -1)
log_error("shader:get_uniform_location", "Invalid uniform %s", name); log_error("shader:get_uniform_location", "Invalid uniform %s", name);
@ -228,42 +229,42 @@ void shader_set_uniform_int(const int shader_index, const char* name, const int
{ {
GLint location = shader_get_uniform_location(shader_index, name); GLint location = shader_get_uniform_location(shader_index, name);
if(location >= 0) if(location >= 0)
glUniform1i(location, value); GL_CHECK(glUniform1i(location, value));
} }
void shader_set_uniform_float(const int shader_index, const char* name, const float value) void shader_set_uniform_float(const int shader_index, const char* name, const float value)
{ {
GLint location = shader_get_uniform_location(shader_index, name); GLint location = shader_get_uniform_location(shader_index, name);
if(location >= 0) if(location >= 0)
glUniform1f(location, value); GL_CHECK(glUniform1f(location, value));
} }
void shader_set_uniform_vec2(const int shader_index, const char* name, const vec2* value) void shader_set_uniform_vec2(const int shader_index, const char* name, const vec2* value)
{ {
GLint location = shader_get_uniform_location(shader_index, name); GLint location = shader_get_uniform_location(shader_index, name);
if(location >= 0) if(location >= 0)
glUniform2fv(location, 1, &value->x); GL_CHECK(glUniform2fv(location, 1, &value->x));
} }
void shader_set_uniform_vec3(const int shader_index, const char* name, const vec3* value) void shader_set_uniform_vec3(const int shader_index, const char* name, const vec3* value)
{ {
GLint location = shader_get_uniform_location(shader_index, name); GLint location = shader_get_uniform_location(shader_index, name);
if(location >= 0) if(location >= 0)
glUniform3fv(location, 1, &value->x); GL_CHECK(glUniform3fv(location, 1, &value->x));
} }
void shader_set_uniform_vec4(const int shader_index, const char* name, const vec4* value) void shader_set_uniform_vec4(const int shader_index, const char* name, const vec4* value)
{ {
GLint location = shader_get_uniform_location(shader_index, name); GLint location = shader_get_uniform_location(shader_index, name);
if(location >= 0) if(location >= 0)
glUniform4fv(location, 1, &value->x); GL_CHECK(glUniform4fv(location, 1, &value->x));
} }
void shader_set_uniform_mat4(const int shader_index, const char* name, const mat4* value) void shader_set_uniform_mat4(const int shader_index, const char* name, const mat4* value)
{ {
GLint location = shader_get_uniform_location(shader_index, name); GLint location = shader_get_uniform_location(shader_index, name);
if(location >= 0) if(location >= 0)
glUniformMatrix4fv(location, 1, GL_FALSE, &value->mat[0]); GL_CHECK(glUniformMatrix4fv(location, 1, GL_FALSE, &value->mat[0]));
} }
void shader_remove(const int shader_index) void shader_remove(const int shader_index)
@ -271,10 +272,10 @@ void shader_remove(const int shader_index)
uint shader = shader_list[shader_index]; uint shader = shader_list[shader_index];
if(shader == 0) return; /* shader is already deleted or invalid */ if(shader == 0) return; /* shader is already deleted or invalid */
int curr_program = 0; int curr_program = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, &curr_program); GL_CHECK(glGetIntegerv(GL_CURRENT_PROGRAM, &curr_program));
if((uint)curr_program == shader) if((uint)curr_program == shader)
glUseProgram(0); GL_CHECK(glUseProgram(0));
glDeleteProgram(shader); GL_CHECK(glDeleteProgram(shader));
shader_list[shader_index] = 0; shader_list[shader_index] = 0;
array_push(empty_indices, shader_index, int); array_push(empty_indices, shader_index, int);
} }
@ -297,43 +298,43 @@ void shader_set_uniform(const int uniform_type, const int uniform_loc, void* val
{ {
case UT_INT: case UT_INT:
{ {
glUniform1i(uniform_loc, *((int*)value)); GL_CHECK(glUniform1i(uniform_loc, *((int*)value)));
break; break;
} }
case UT_FLOAT: case UT_FLOAT:
{ {
glUniform1f(uniform_loc, *((float*)value)); GL_CHECK(glUniform1f(uniform_loc, *((float*)value)));
break; break;
} }
case UT_VEC2: case UT_VEC2:
{ {
vec2* vector = (vec2*)value; vec2* vector = (vec2*)value;
glUniform2fv(uniform_loc, 1, &vector->x); GL_CHECK(glUniform2fv(uniform_loc, 1, &vector->x));
break; break;
} }
case UT_VEC3: case UT_VEC3:
{ {
vec3* vector = (vec3*)value; vec3* vector = (vec3*)value;
glUniform3fv(uniform_loc, 1, &vector->x); GL_CHECK(glUniform3fv(uniform_loc, 1, &vector->x));
break; break;
} }
case UT_VEC4: case UT_VEC4:
{ {
vec4* vector = (vec4*)value; vec4* vector = (vec4*)value;
glUniform4fv(uniform_loc, 1, &vector->x); GL_CHECK(glUniform4fv(uniform_loc, 1, &vector->x));
break; break;
} }
case UT_MAT4: case UT_MAT4:
{ {
mat4* mat = (mat4*)value; mat4* mat = (mat4*)value;
glUniformMatrix4fv(uniform_loc, 1, GL_FALSE, &mat->mat[0]); GL_CHECK(glUniformMatrix4fv(uniform_loc, 1, GL_FALSE, &mat->mat[0]));
break; break;
} }
case UT_TEX: case UT_TEX:
{ {
int texture_index = *((int*)value); int texture_index = *((int*)value);
int texture_unit = texture_get_textureunit(texture_index); int texture_unit = texture_get_textureunit(texture_index);
glUniform1i(uniform_loc, (GL_TEXTURE0 + texture_unit) - GL_TEXTURE0); GL_CHECK(glUniform1i(uniform_loc, (GL_TEXTURE0 + texture_unit) - GL_TEXTURE0));
texture_bind(texture_index); texture_bind(texture_index);
break; break;
} }
@ -343,5 +344,7 @@ void shader_set_uniform(const int uniform_type, const int uniform_loc, void* val
int shader_get_attribute_location(const int shader_index, const char* attrib_name) int shader_get_attribute_location(const int shader_index, const char* attrib_name)
{ {
assert(shader_index > -1 && shader_index < array_len(shader_list)); assert(shader_index > -1 && shader_index < array_len(shader_list));
return glGetAttribLocation(shader_list[shader_index], attrib_name); int location = 0;
GL_CHECK(location = glGetAttribLocation(shader_list[shader_index], attrib_name));
return location;
} }

@ -13,8 +13,8 @@
void config_vars_init(struct Hashmap* cvars) void config_vars_init(struct Hashmap* cvars)
{ {
/* Initialize with default values incase there is no config file */ /* Initialize with default values incase there is no config file */
hashmap_int_set(cvars, "render_width", 1024); hashmap_int_set(cvars, "render_width", 1280);
hashmap_int_set(cvars, "render_height", 768); hashmap_int_set(cvars, "render_height", 720);
hashmap_int_set(cvars, "fog_mode", 0); hashmap_int_set(cvars, "fog_mode", 0);
hashmap_vec3_setf(cvars, "fog_color", 0.9f, 0.2f, 0.2f); hashmap_vec3_setf(cvars, "fog_color", 0.9f, 0.2f, 0.2f);
hashmap_float_set(cvars, "fog_density", 0.1f); hashmap_float_set(cvars, "fog_density", 0.1f);

@ -1,8 +1,11 @@
Todo: Todo:
- Specific rendering mode for editor related rendering - Move plane creation into its own function
- Editor modes for Transform, Rotate, Scale that operate similar to vim or blender. - Vertical translation in translate mode
Modes can be toggled by hotkeys and the operation will be applied to the selected object. - Highlight if we are about to select an entity or perform the tool action like translate when mouse is hovered and an entity can be selected at that location
By default, we work on the ground axis i.e the xz plane and when we have alt pressed, we work in the vertical or y axis. - Display the projected position if we perform the action for example display what the new location would be right next to the tool mesh
- Editor Undo
- Scale Mode
- Rotate Mode
- Add warning to genie build script when running on windows and WindowsSdkVersion cannot be found. This happens when the script is not run from vcvarsall command prompt - Add warning to genie build script when running on windows and WindowsSdkVersion cannot be found. This happens when the script is not run from vcvarsall command prompt
- Improve README and add a screenshot to make the repository ready for making it public - Improve README and add a screenshot to make the repository ready for making it public
- Refactor all global application state into 'Application_Context' struct. A single global instance of which is available everywhere - Refactor all global application state into 'Application_Context' struct. A single global instance of which is available everywhere
@ -214,4 +217,8 @@ Done:
* Changed all usages of bool types and nuklear causing buggy behaviour in editor code by using int instead of bool or by using variants of the functions that do take int as parameter instead of int* * Changed all usages of bool types and nuklear causing buggy behaviour in editor code by using int instead of bool or by using variants of the functions that do take int as parameter instead of int*
* Added Grid to editor * Added Grid to editor
* Added Editor settings window and grid configuration to editor settings window * Added Editor settings window and grid configuration to editor settings window
* Specific rendering mode for editor related rendering
* Editor modes for Transform, Rotate, Scale that operate similar to vim or blender.
Modes can be toggled by hotkeys and the operation will be applied to the selected object.
By default, we work on the ground axis i.e the xz plane and when we have alt pressed, we work in the vertical or y axis.

@ -1,4 +1,4 @@
@echo off @echo off
cd W:\build\ chdir /D W:\build\
genie.exe vs2017 genie.exe vs2017

Loading…
Cancel
Save