summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/device.c34
-rw-r--r--src/core/shader.h1
-rw-r--r--src/example/03_texture/03_texture.c51
-rw-r--r--src/extend/camera.c30
-rw-r--r--src/extern/wog.c47
-rw-r--r--src/extern/wog.h2
-rw-r--r--src/main.c2
-rw-r--r--src/platform/win.c20
-rw-r--r--src/platform/win.h6
-rw-r--r--src/shaders/common/core.c10
-rw-r--r--src/shaders/common/core.h18
-rw-r--r--src/shaders/pbr.c26
12 files changed, 135 insertions, 112 deletions
diff --git a/src/core/device.c b/src/core/device.c
index 5ff39ef..b810c04 100644
--- a/src/core/device.c
+++ b/src/core/device.c
@@ -500,7 +500,23 @@ static struct {
extern ClippedBuffer clip_buffer; /*clipping result*/
static bool is_back_face(Vec4* c0, Vec4* c1, Vec4*c2) {
+#define OPENGL_BACKFACE_CULLING 0
/*cull in ndc*/
+#if OPENGL_BACKFACE_CULLING
+ /*opengl backface culling algrithm*/
+ float w0 = max(1 / c0->w, 0), w1 = 1 / c1->w, w2 = 1 / c2->w;
+ Vec2 a = { c0->x * w0, c0->y * w0 };
+ Vec2 b = { c1->x * w1, c1->y * w1 };
+ Vec2 c = { c2->x * w2, c2->y * w2 };
+ float signed_area = 0;
+ signed_area += a.x * b.y - a.y * b.x;
+ signed_area += b.x * c.y - b.y * c.x;
+ signed_area += c.x * a.y - c.y * a.x;
+ if (signed_area <= 0) {
+ return TRUE;
+ }
+ return FALSE;
+#else
float w0 = 1 / c0->w, w1 = 1 / c1->w, w2 = 1 / c2->w;
bool all_front = w0 > 0 && w1 > 0 && w2 > 0;
if (all_front)
@@ -511,25 +527,11 @@ static bool is_back_face(Vec4* c0, Vec4* c1, Vec4*c2) {
ac.x = c2->x * w2 - c0->x * w0;
ac.y = c2->y * w2 - c0->y * w0;
if (ab.x * ac.y - ab.y * ac.x <= 0) {
- return TRUE;
+ return TRUE;
}
}
return FALSE;
- /*OpenGL algrithm*/
- /*
- float w0 = max(1 / c0->w, 0), w1 = 1 / c1->w, w2 = 1 / c2->w;
- Vec2 a = { c0->x * w0, c0->y * w0 };
- Vec2 b = { c1->x * w1, c1->y * w1 };
- Vec2 c = { c2->x * w2, c2->y * w2 };
- float signed_area = 0;
- signed_area += a.x * b.y - a.y * b.x;
- signed_area += b.x * c.y - b.y * c.x;
- signed_area += c.x * a.y - c.y * a.x;
- if (signed_area <= 0) {
- return TRUE;
- }
- return FALSE;
- */
+#endif
}
static void render_prims_triangle(uint varying_flag) {
diff --git a/src/core/shader.h b/src/core/shader.h
index be3819f..335eead 100644
--- a/src/core/shader.h
+++ b/src/core/shader.h
@@ -160,6 +160,7 @@ ActiveReg active_regs[REG_TOTAL];
int open_regsi[REG_TOTAL]; /*active registers during a draw call*/
+/*reference pointers*/
float *reg_num_00, *reg_num_01, *reg_num_02, *reg_num_03;
Vec2 *reg_v2_00, *reg_v2_01, *reg_v2_02, *reg_v2_03, *reg_v2_04, *reg_v2_05, *reg_v2_06, *reg_v2_07;
Vec3 *reg_v3_00, *reg_v3_01, *reg_v3_02, *reg_v3_03, *reg_v3_04, *reg_v3_05, *reg_v3_06, *reg_v3_07, *reg_v3_08, *reg_v3_09, *reg_v3_10, *reg_v3_11;
diff --git a/src/example/03_texture/03_texture.c b/src/example/03_texture/03_texture.c
index 23597c9..66643bb 100644
--- a/src/example/03_texture/03_texture.c
+++ b/src/example/03_texture/03_texture.c
@@ -48,23 +48,24 @@ Texture* base_color_texs[];
EXAMPLE void onload_texture(void* data) {
CameraConfig* conf = (CameraConfig*)data;
- //mech_albedo = texture_loadfromfile("res/dieselpunk/mech_basecolor.tga");
- //mech_albedo = texture_loadfromfile("res/station/textures/projekt_Gas_BaseColor.png");
- mech_albedo = texture_loadfromfile("res/townscaper/town_diffuse.jpg");
- mech_normal = texture_loadfromfile("res/dieselpunk/mech_normal.tga");
- mech_roughness = texture_loadfromfile("res/dieselpunk/mech_roughness.tga");
- mech_metalness = texture_loadfromfile("res/dieselpunk/mech_metalness.tga");
- mech_mesh = mesh_loadfromobj("res/townscaper/town.obj");
- //mech_mesh = mesh_loadfromobj("res/gun/gun.obj");
- //mech_mesh = mesh_loadfromobj("res/station/station.obj");
- //mech_mesh = mesh_loadfromobj("res/gun/triangle.obj");
- ground_albedo = texture_loadfromfile("res/dieselpunk/ground_basecolor.tga");
- ground_mesh = mesh_loadfromobj("res/dieselpunk/ground.obj");
- yingham_albedo = texture_loadfromfile("res/dieselpunk/yingham_basecolor.tga");
- yingham_mesh = mesh_loadfromobj("res/dieselpunk/yingham.obj");
-
- cyborg_albedo = texture_loadfromfile("res/cyborg/cyborg_diffuse.png");
- cyborg_mesh = mesh_loadfromobj("res/cyborg/cyborg.obj");
+ mech_albedo = texture_loadfromfile("assets/dieselpunk/mech_basecolor.tga");
+ //mech_albedo = texture_loadfromfile("assets/station/textuassets/projekt_Gas_BaseColor.png");
+ //mech_albedo = texture_loadfromfile("assets/townscaper/town_diffuse.jpg");
+ mech_normal = texture_loadfromfile("assets/dieselpunk/mech_normal.tga");
+ mech_roughness = texture_loadfromfile("assets/dieselpunk/mech_roughness.tga");
+ mech_metalness = texture_loadfromfile("assets/dieselpunk/mech_metalness.tga");
+ mech_mesh = mesh_loadfromobj("assets/dieselpunk/mech.obj");
+ //mech_mesh = mesh_loadfromobj("assets/townscaper/town.obj");
+ //mech_mesh = mesh_loadfromobj("assets/gun/gun.obj");
+ //mech_mesh = mesh_loadfromobj("assets/station/station.obj");
+ //mech_mesh = mesh_loadfromobj("assets/gun/triangle.obj");
+ ground_albedo = texture_loadfromfile("assets/dieselpunk/ground_basecolor.tga");
+ ground_mesh = mesh_loadfromobj("assets/dieselpunk/ground.obj");
+ yingham_albedo = texture_loadfromfile("assets/dieselpunk/yingham_basecolor.tga");
+ yingham_mesh = mesh_loadfromobj("assets/dieselpunk/yingham.obj");
+
+ cyborg_albedo = texture_loadfromfile("assets/cyborg/cyborg_diffuse.png");
+ cyborg_mesh = mesh_loadfromobj("assets/cyborg/cyborg.obj");
}
EXAMPLE void onevent_texture(void* data) {
@@ -74,7 +75,7 @@ EXAMPLE void onevent_texture(void* data) {
EXAMPLE void onupdate_texture(void*data) {
ssr_matrixmode(MATRIX_MODEL);
ssr_loadidentity();
- ssr_scale(100,100,100);
+ //ssr_scale(100,100,100);
Vec3 light = {1, 0, 0};
ssr_setuniformvec3(0, &light);
@@ -120,14 +121,16 @@ EXAMPLE void ondraw_texture(void*data) {
ssr_setstencilop(STENCILOP_KEEP, STENCILOP_KEEP, STENCILOP_KEEP);
*/
/*render yingham*/
- //ssr_setuniformtex(0, yingham_albedo);
- //ssr_bindvertices(yingham_mesh->vertices, yingham_mesh->vert_count, yingham_mesh->triangles, yingham_mesh->tris_count);
- //ssr_draw(PRIMITIVE_TRIANGLE);
+ ssr_setuniformtex(0, yingham_albedo);
+ ssr_bindvertices(yingham_mesh->vertices, yingham_mesh->vert_count);
+ ssr_bindindices(yingham_mesh->triangles, yingham_mesh->tris_count);
+ ssr_draw(PRIMITIVE_TRIANGLE);
/*render ground*/
- //ssr_setuniformtex(0, ground_albedo);
- //ssr_bindvertices(ground_mesh->vertices, ground_mesh->vert_count, ground_mesh->triangles, ground_mesh->tris_count);
- //ssr_draw(PRIMITIVE_TRIANGLE);
+ ssr_setuniformtex(0, ground_albedo);
+ ssr_bindvertices(ground_mesh->vertices, ground_mesh->vert_count);
+ ssr_bindindices(ground_mesh->triangles, ground_mesh->tris_count);
+ ssr_draw(PRIMITIVE_TRIANGLE);
//draw_tbn(mech_mesh, VISUAL_ALL, 4);
diff --git a/src/extend/camera.c b/src/extend/camera.c
index d148cd8..e481831 100644
--- a/src/extend/camera.c
+++ b/src/extend/camera.c
@@ -1,6 +1,7 @@
#include "../shaders/common/core.h"
#include "../core/device.h"
#include "camera.h"
+#include "../platform/win.h"
//#define USE_CURSOR_WHEEL_ICON
@@ -8,14 +9,10 @@ typedef enum {
CursorType_Arrow = 0,
CursorType_Hand,
CursorType_Eye,
- CursorType_ZoomIn,
- CursorType_ZoomOut,
}CursorType;
static Wog_Cursor cursor_hand;
static Wog_Cursor cursor_eye;
-static Wog_Cursor cursor_zoomIn;
-static Wog_Cursor cursor_zoomOut;
// A unity editor style camera
typedef struct Camera {
@@ -71,10 +68,14 @@ Camera* camera_create(wog_Window* wnd, CameraConfig* config) {
cam->wheel_scroll = 0;
- cursor_hand = LoadImageA(NULL, "PanView.ico", IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_LOADFROMFILE);
- cursor_eye = LoadImageA(NULL, "OrbitView.ico", IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_LOADFROMFILE);
- cursor_zoomIn = LoadImageA(NULL, "ZoomIn.ico", IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_LOADFROMFILE);
- cursor_zoomOut = LoadImageA(NULL, "ZoomOut.ico", IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_LOADFROMFILE);
+ HINSTANCE hInstance = wog_get_instance();
+
+// cursor_hand = LoadImage(hInstance, "icon.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_SHARED);
+ cursor_hand = LoadIcon(hInstance, "pan_view.ico");
+ cursor_eye = LoadIcon(hInstance, "orbit_view.ico");
+
+ //int size;
+ //void *p = resource_get("pan_view.ico", &size);
return cam;
}
@@ -173,19 +174,6 @@ void camera_onevent(Camera* cam, wog_Event* e, float dt) {
return ;
//printf("%d\n",e->type);
if (e->type == WOG_EMOUSEWHEEL) {
-#if defined(USE_CURSOR_WHEEL_ICON)
- if (e->wheel > 0)
- {
- setMouseCursor(CursorType_ZoomIn, cursor_zoomIn);
- }
- else if (e->wheel < 0)
- {
- setMouseCursor(CursorType_ZoomOut, cursor_zoomOut);
- }
- if(cam->wheel_scroll <= 0)
- wog_setMouseCapture(cam->wnd);
- cam->wheel_scroll = 0.3;
-#endif
_onwheelscroll(cam, e->wheel, dt);
}
else if (e->type == WOG_EMOUSEBUTTONDOWN) {
diff --git a/src/extern/wog.c b/src/extern/wog.c
index 4892f99..194f893 100644
--- a/src/extern/wog.c
+++ b/src/extern/wog.c
@@ -37,9 +37,12 @@
#define min(a, b) (a < b ? a : b)
#define clamp(x, minv, maxv) (max(minv, min(x, maxv)))
+static HINSTANCE g_hinstance = NULL;
+
typedef struct wog_Window
{
HWND hwnd; // Window handler
+ HINSTANCE hInstance;
HDC hdc; // Device Context
HDC mdc; // Memory Device Contexts
#if WOG_API == WOG_GDI
@@ -55,7 +58,6 @@ typedef struct wog_GLContext
}wog_GLContext;
#endif
-
// A mouse capture count is used
void wog_setMouseCapture(wog_Window* wnd)
{
@@ -299,10 +301,11 @@ static int registerWindowClass()
windowClass.cbSize = sizeof(WNDCLASSEX); // Size Of The windowClass Structure
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraws The Window For Any Movement / Resizing
windowClass.lpfnWndProc = (WNDPROC)(WindowProc); // WindowProc Handles Messages
- windowClass.hInstance = GetModuleHandle(0); // Set The Instance
+ windowClass.hInstance = g_hinstance; // Set The Instance
windowClass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE); // Class Background Brush Color
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
windowClass.lpszClassName = WINDOW_CLASS; // Sets The Applications Classname
+ windowClass.hIcon = (HICON)LoadIcon(g_hinstance, "icon.ico");
if (RegisterClassEx(&windowClass) == 0) // Did Registering The Class Fail?
{
@@ -357,6 +360,11 @@ wog_Window* wog_createWindow(const char* title, int width, int height, int x, in
{
int client_w = width, client_h = height;
+ if (g_hinstance == NULL)
+ {
+ g_hinstance = GetModuleHandle(NULL);
+ }
+
if (! registerWindowClass())
{
printf("Register window class failed.\n");
@@ -395,6 +403,8 @@ wog_Window* wog_createWindow(const char* title, int width, int height, int x, in
wnd
);
+ wnd->hInstance = g_hinstance;
+
if (wnd->hwnd == 0)
return 0;
@@ -625,22 +635,24 @@ int console_main(int argc, char* argv[])
*/
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
- char* temp = GetCommandLine();
- int len = strlen(temp) + 1;
- char* cmd = stack_alloc(char, len);
- strcpy(cmd, temp);
- cmd[len - 1] = '\0';
+ g_hinstance = hInst;
- int argc = 0;
- char** argv = 0;
- argc = ParseCommandLine(cmd, 0);
- ParseCommandLine(cmd, 0);
- argv = stack_alloc(char*, argc + 1);
- ParseCommandLine(cmd, argv);
+ char* temp = GetCommandLine();
+ int len = strlen(temp) + 1;
+ char* cmd = stack_alloc(char, len);
+ strcpy(cmd, temp);
+ cmd[len - 1] = '\0';
- int status = console_main(argc, argv);
+ int argc = 0;
+ char** argv = 0;
+ argc = ParseCommandLine(cmd, 0);
+ ParseCommandLine(cmd, 0);
+ argv = stack_alloc(char*, argc + 1);
+ ParseCommandLine(cmd, argv);
- return status;
+ int status = console_main(argc, argv);
+
+ return status;
}
@@ -714,3 +726,8 @@ void wog_setcursorImage(wog_Window* wnd, Wog_Cursor icon)
{
SetCursor(icon);
}
+
+HINSTANCE wog_get_instance()
+{
+ return g_hinstance;
+}
diff --git a/src/extern/wog.h b/src/extern/wog.h
index c5e5c06..89c98fb 100644
--- a/src/extern/wog.h
+++ b/src/extern/wog.h
@@ -214,4 +214,6 @@ void wog_setcursorImage(wog_Window* wnd, Wog_Cursor icon);
void wog_setMouseCapture(wog_Window* wnd);
void wog_releaseMouseCapture(wog_Window* wnd);
+HINSTANCE wog_get_instance();
+
#endif \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 4e5fa0a..7e85fcc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -59,7 +59,7 @@ bool setup(const char* name) {
_setup(onupdate);
_setup(onevent);
_setup(ondraw);
-#undef _set
+#undef _setup
return onload && onupdate && onevent && ondraw;
}
diff --git a/src/platform/win.c b/src/platform/win.c
new file mode 100644
index 0000000..da864b2
--- /dev/null
+++ b/src/platform/win.c
@@ -0,0 +1,20 @@
+#include "win.h"
+#include "../util/assert.h"
+#include <windows.h>
+
+void* resource_get(const char* name, int* out_size) {
+ HRSRC handle = FindResource(0, name, "RESOURCE");
+ assert(handle);
+
+ HGLOBAL data = LoadResource(0, handle);
+ assert(data);
+
+ void *ptr = LockResource(data);
+ assert(ptr);
+
+ DWORD t_size = SizeofResource(0, handle);
+ assert(t_size);
+
+ *out_size = t_size;
+ return ptr;
+}
diff --git a/src/platform/win.h b/src/platform/win.h
new file mode 100644
index 0000000..bc2a10c
--- /dev/null
+++ b/src/platform/win.h
@@ -0,0 +1,6 @@
+#ifndef RESOURCE_H_
+#define RESOURCE_H_
+
+void* resource_get(const char* name, int* out_size);
+
+#endif \ No newline at end of file
diff --git a/src/shaders/common/core.c b/src/shaders/common/core.c
index ffaa649..91bb6c0 100644
--- a/src/shaders/common/core.c
+++ b/src/shaders/common/core.c
@@ -4,7 +4,7 @@ Vec4 _proj_params;
Vec2 _time;
Vec4 _screen_params;
-Vec3 unpacknormal(Color32 c32) {
+Vec3 decode_normal(Color32 c32) {
Vec3 normal = {
c32.r * 2 - 1,
c32.g * 2 - 1,
@@ -15,16 +15,16 @@ Vec3 unpacknormal(Color32 c32) {
Mat4 mat4(Vec4* c1, Vec4* c2, Vec4* c3, Vec4* c4);
-Vec2 texsize(Texture* texture) {
+Vec2 texture_size(Texture* texture) {
Vec2 size = {texture->width, texture->height};
return size;
}
-float linear01Depth(float depth) {
+float linear_01_depth(float depth) {
float n = _proj_params.x, f = _proj_params.y;
return n / ((n-f)*depth + f);
}
-float linearEyeDepth(float depth) {
- return _proj_params.y * linear01Depth(depth);
+float linear_eye_depth(float depth) {
+ return _proj_params.y * linear_01_depth(depth);
} \ No newline at end of file
diff --git a/src/shaders/common/core.h b/src/shaders/common/core.h
index ef58897..216af15 100644
--- a/src/shaders/common/core.h
+++ b/src/shaders/common/core.h
@@ -34,24 +34,24 @@ Vec4 _screen_params;
/************************************************************************/
/*shader built in functions*/
-Vec3 unpacknormal(Color32 c32);
+Vec3 decode_normal(Color32 c32);
Mat4 mat4(Vec4* c1, Vec4* c2, Vec4* c3, Vec4* c4);
Mat3 mat3(Vec3* c1, Vec3* c2, Vec3* c3);
-Vec2 texsize(Texture* texture);
+Vec2 texture_size(Texture* texture);
#define discardif(cond) \
-do{ \
-if(cond) return 0; \
+do{ \
+if(cond) return 0; \
}while(0)
#define discard() return 0
-#define keep() return 1
+#define put() return 1
#define MVP_PROCESS \
do{ \
-static Vec4 p; p.xyz = in->position; p.w = 1; \
-internal_mat4_mulvec4(g_uniforms->mvp, &p, clipcoord); \
+static Vec4 p; p.xyz = in->position; p.w = 1; \
+internal_mat4_mulvec4(g_uniforms->mvp, &p, clipcoord); \
}while(0)
#define object2clip(pos, out) internal_mat4_mulvec4(_mvp_matrix, pos, out);
@@ -66,7 +66,7 @@ out_v3->x = color.x * 2 - 1; \
out_v3->y = color.y * 2 - 1; \
out_v3->z = color.z * 2 - 1;
-float linear01Depth(float depth);
-float linearEyeDepth(float depth);
+float linear_01_depth(float depth);
+float linear_eye_depth(float depth);
#endif \ No newline at end of file
diff --git a/src/shaders/pbr.c b/src/shaders/pbr.c
index a917f95..9f148ee 100644
--- a/src/shaders/pbr.c
+++ b/src/shaders/pbr.c
@@ -24,46 +24,30 @@ static void vert( Vertex* in, Vec4* clipcoord) {
object2clip(&p, clipcoord);
Vec3 worldnormal = mat4_mulvec3(*_object2world, in->normal);
worldnormal = vec3_normalize(worldnormal);
- //*rough = 1 - internal_vec3_dot(&worldnormal, light);
- //*vnormal = in->normal;
*_texcoord = in->texcoord;
_clip_pos->x = clipcoord->z;
_clip_pos->y = clipcoord->w;
}
static bool frag( Vec4* color) {
- //internal_vec3_normalize(light, light);
- //internal_vec3_normalize(vnormal, vnormal);
- //float roughness = *rough;
- //(*color).r = 1;
- //(*color).g = 1;
- //(*color).b = 1;
- //(*color).a = 1;
- //return 1;
- //float rough = 1- internal_vec3_dot(&in->normal, light);
float depth = _clip_pos->x / _clip_pos->y;
depth = (depth + 1) / 2;
- depth = linear01Depth(depth);
+ depth = linear_01_depth(depth);
Vec4 c = tex2d(_albedo_tex, _texcoord);
- //Color32 nc = tex2d(noramltex, in->texcoord);
- //internal_vec3_scale(&c, roughness, &c);
*color = vec4_saturate(c);
- //*color = vec4(1,1,1,1);
- //*color = vec4(depth, depth, depth, 1);
- //internal_vec3_scale(color, 1 - depth, color);
- return 1;
+ put();
}
Program ssr_built_in_shader_pbr = {
vert, frag,
- VARYING_NUM_00 |
+ //VARYING_NUM_00 |
//VARYING_V3_00 |
//VARYING_V3_01 |
//VARYING_V3_02 |
//VARYING_V3_03 |
//VARYING_V3_04 |
//VARYING_V4_00 |
- VARYING_V2_00 |
- VARYING_V3_05 |
+ VARYING_V2_00 |
+ //VARYING_V3_05 |
VARYING_V2_01
};