diff --git a/engine/gf_draw_common.c b/engine/gf_draw_common.c index ede351d..521160a 100644 --- a/engine/gf_draw_common.c +++ b/engine/gf_draw_common.c @@ -76,8 +76,8 @@ void gf_draw_reshape(gf_draw_t* draw) { gf_draw_driver_reshape(draw); } /* Runs every frame */ void gf_draw_frame(gf_draw_t* draw) { - gf_color_t color; - float z = 16; + gf_graphic_color_t color; + float z = 16; color.r = color.g = color.b = color.a = 255; if(draw->draw_3d) { } diff --git a/engine/gf_graphic_common.c b/engine/gf_graphic_common.c index f29a397..af0c8a1 100644 --- a/engine/gf_graphic_common.c +++ b/engine/gf_graphic_common.c @@ -14,9 +14,9 @@ /* Standard */ #include -void gf_graphic_fill_rect(gf_draw_t* draw, float x, float y, float w, float h, gf_color_t color) { gf_graphic_fill_polygon(draw, color, GF_GRAPHIC_2D, 4, x, y, x, y + h, x + w, y + h, x + w, y); } +void gf_graphic_fill_rect(gf_draw_t* draw, float x, float y, float w, float h, gf_graphic_color_t color) { gf_graphic_fill_polygon(draw, color, GF_GRAPHIC_2D, 4, x, y, x, y + h, x + w, y + h, x + w, y); } -void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* text, gf_color_t color) { +void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* text, gf_graphic_color_t color) { int i; for(i = 0; text[i] != 0; i++) { gf_graphic_draw_texture_2d(draw, x + i * (size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y), y, size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y, size, draw->font[text[i]], color); @@ -25,4 +25,4 @@ void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* float gf_graphic_text_width(gf_draw_t* draw, float size, const char* text) { return (float)strlen(text) * (size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y); } -void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_color_t color) { gf_graphic_draw_texture_polygon(draw, texture, color, GF_GRAPHIC_2D, 4, 0.0, 0.0, x, y, 0.0, 1.0, x, y + h, 1.0, 1.0, x + w, y + h, 1.0, 0.0, x + w, y); } +void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_graphic_color_t color) { gf_graphic_draw_texture_polygon(draw, texture, color, GF_GRAPHIC_2D, 4, 0.0, 0.0, x, y, 0.0, 1.0, x, y + h, 1.0, 1.0, x + w, y + h, 1.0, 0.0, x + w, y); } diff --git a/engine/gf_gui.c b/engine/gf_gui.c index f6757df..869df38 100644 --- a/engine/gf_gui.c +++ b/engine/gf_gui.c @@ -15,8 +15,8 @@ #include #include -gf_color_t gf_gui_base_color; -gf_color_t gf_gui_font_color; +gf_graphic_color_t gf_gui_base_color; +gf_graphic_color_t gf_gui_font_color; gf_gui_t* gf_gui_create(gf_draw_t* draw) { gf_gui_t* gui = malloc(sizeof(*gui)); @@ -34,9 +34,9 @@ gf_gui_t* gf_gui_create(gf_draw_t* draw) { /* note... left top should be the lightest in the border */ void gf_gui_draw_box(gf_gui_t* gui, int mul, float x, float y, float w, float h) { - const int color_diff = 32; /* color diff */ - const float bw = 2; /* border width */ - gf_color_t col; + const int color_diff = 32; /* color diff */ + const float bw = 2; /* border width */ + gf_graphic_color_t col; int cd = mul * color_diff; diff --git a/engine/gf_math.c b/engine/gf_math.c index a92fd78..2442550 100644 --- a/engine/gf_math.c +++ b/engine/gf_math.c @@ -1,7 +1,6 @@ #include /* External library */ -#include /* Interface */ #include @@ -13,34 +12,51 @@ #include #include -float gf_math_log2(float x) { return log(x) / log(2); } +double gf_math_log2(double x) { return log(x) / log(2); } -void gf_math_normal(gf_vector_t* r, gf_vector_t v0, gf_vector_t v1, gf_vector_t v2) { - int i; - dReal length; - gf_vector_t vec; - dReal res[3]; - dReal tmp0[3]; - dReal tmp1[3]; - dReal a[3]; /* v1 - v0 */ - dReal b[3]; /* v2 - v0 */ +void gf_math_normalize(gf_math_vector_t v) { + double x = v[0]; + double y = v[1]; + double z = v[2]; + double length; - GF_VECTOR_COPY(tmp0, v1); - GF_VECTOR_COPY(tmp1, v0); - dSubtractVectors3(a, tmp0, tmp1); /* v1 - v0 */ + x *= x; + y *= y; + z *= z; - GF_VECTOR_COPY(tmp0, v2); - GF_VECTOR_COPY(tmp1, v0); - dSubtractVectors3(b, tmp0, tmp1); /* v2 - v0 */ - - dCalcVectorCross3(res, a, b); - - length = dCalcVectorLength3(res); - GF_VECTOR_COPY(res, vec); - - vec[0] /= length; - vec[1] /= length; - vec[2] /= length; - - memcpy(*r, vec, sizeof(vec)); + length = sqrt(x + y + z); + if(length > 0) { + length = (double)1 / length; + } else { + length = 0; + } + v[0] *= length; + v[1] *= length; + v[2] *= length; } + +void gf_math_normal(gf_math_vector_t r, gf_math_vector_t v0, gf_math_vector_t v1, gf_math_vector_t v2) { + gf_math_vector_t tmp0; + gf_math_vector_t tmp1; + + gf_math_subtract(tmp0, v1, v0); + gf_math_subtract(tmp1, v2, v0); + + gf_math_multiply(r, tmp0, tmp1); + + gf_math_normalize(r); +} + +void gf_math_subtract(gf_math_vector_t r, gf_math_vector_t v0, gf_math_vector_t v1) { + r[0] = v0[0] - v1[0]; + r[1] = v0[1] - v1[1]; + r[2] = v0[2] - v1[2]; +} + +void gf_math_multiply(gf_math_vector_t r, gf_math_vector_t v0, gf_math_vector_t v1) { + r[0] = v0[1] * v1[2] - v0[2] * v1[1]; + r[1] = v0[2] * v1[0] - v0[0] * v1[2]; + r[2] = v0[0] * v1[1] - v0[1] * v1[0]; +} + +double gf_math_cot(double x) { return (double)1 / tan(x); } diff --git a/engine/graphic/opengl/gf_driver.c b/engine/graphic/opengl/gf_driver.c index b379536..61b82cc 100644 --- a/engine/graphic/opengl/gf_driver.c +++ b/engine/graphic/opengl/gf_driver.c @@ -24,35 +24,11 @@ #include #include -/** - * @~english - * @brief White light - */ GLfloat lightwht[] = {1.0, 1.0, 1.0, 1.0}; - -/** - * @~english - * @brief Gray light - */ GLfloat lightgry[] = {0.6, 0.6, 0.6, 1.0}; - -/** - * @~english - * @brief Dim light - */ GLfloat lightdim[] = {0.2, 0.2, 0.2, 1.0}; - -/** - * @~english - * @brief Black light - */ GLfloat lightblk[] = {0.0, 0.0, 0.0, 1.0}; -/** - * @~english - * @brief Calculate the nearest 2^n value to x - * @param x Number - */ #define NEAREST_POW2(x) pow((2), gf_math_log2((int)(x) + 1)) gf_draw_driver_texture_t* gf_draw_driver_register_texture(gf_draw_t* draw, int width, int height, int* iwidth, int* iheight, unsigned char* data) { @@ -98,6 +74,8 @@ void gf_draw_driver_init(gf_draw_t* draw) { int w, h, ch; draw->driver = malloc(sizeof(*draw->driver)); + gf_log_function(NULL, "OpenGL renderer: %s", (char*)glGetString(GL_RENDERER)); + glEnable(GL_BLEND); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); @@ -135,8 +113,6 @@ void gf_draw_driver_init(gf_draw_t* draw) { gf_log_function(NULL, "Registered %d glyphs", sizeof(gf_font) / sizeof(gf_font[0])); glClearColor(0, 0, 0, 1); - - draw->driver->quadric = gluNewQuadric(); } int gf_draw_driver_has_extension(gf_draw_t* draw, const char* query) { @@ -154,8 +130,7 @@ int gf_draw_driver_has_extension(gf_draw_t* draw, const char* query) { void gf_draw_driver_reshape(gf_draw_t* draw) { glViewport(0, 0, (GLint)draw->width, (GLint)draw->height); glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(30, (double)draw->width / (double)draw->height, 1.0, 1000.0); + gf_graphic_perspective(draw, 30, 1.0, 1000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -171,7 +146,7 @@ void gf_draw_driver_end_texture_2d(gf_draw_t* draw) { glBindTexture(GL_TEXTURE_2D, 0); } -void gf_draw_driver_set_color(gf_draw_t* draw, gf_color_t color) { glColor4f(color.r / 255, color.g / 255, color.b / 255, color.a / 255); } +void gf_draw_driver_set_color(gf_draw_t* draw, gf_graphic_color_t color) { glColor4f(color.r / 255, color.g / 255, color.b / 255, color.a / 255); } void gf_draw_driver_destroy(gf_draw_t* draw) { int i; @@ -182,12 +157,13 @@ void gf_draw_driver_destroy(gf_draw_t* draw) { void gf_draw_driver_before(gf_draw_t* draw) { GLfloat lightpos[4]; - GF_VECTOR_COPY(draw->light, lightpos); + GF_MATH_VECTOR_COPY(draw->light, lightpos); lightpos[3] = draw->light[3]; gf_draw_driver_reshape(draw); - gluLookAt(draw->camera[0], draw->camera[1], draw->camera[2], draw->lookat[0], draw->lookat[1], draw->lookat[2], 0, 1, 0); + gf_graphic_set_camera(draw); + glLightfv(GL_LIGHT0, GL_POSITION, lightpos); gf_graphic_clear(draw); } diff --git a/engine/graphic/opengl/gf_graphic.c b/engine/graphic/opengl/gf_graphic.c index d912c17..0b77d1a 100644 --- a/engine/graphic/opengl/gf_graphic.c +++ b/engine/graphic/opengl/gf_graphic.c @@ -13,9 +13,11 @@ #include #include #include +#include /* Standard */ #include +#include void gf_graphic_begin_2d(gf_draw_t* draw) { glDisable(GL_LIGHTING); @@ -41,7 +43,7 @@ void gf_graphic_end_2d(gf_draw_t* draw) { void gf_graphic_clear(gf_draw_t* draw) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); } -void gf_graphic_draw_texture_polygon(gf_draw_t* draw, gf_texture_t* texture, gf_color_t color, int dim, int npair, ...) { +void gf_graphic_draw_texture_polygon(gf_draw_t* draw, gf_texture_t* texture, gf_graphic_color_t color, int dim, int npair, ...) { double tw = (double)texture->width / texture->internal_width; double th = (double)texture->height / texture->internal_height; int i; @@ -76,7 +78,7 @@ void gf_graphic_draw_texture_polygon(gf_draw_t* draw, gf_texture_t* texture, gf_ va_end(va); } -void gf_graphic_fill_polygon(gf_draw_t* draw, gf_color_t color, int dim, int npair, ...) { +void gf_graphic_fill_polygon(gf_draw_t* draw, gf_graphic_color_t color, int dim, int npair, ...) { int i; va_list va; va_start(va, npair); @@ -102,3 +104,60 @@ void gf_graphic_fill_polygon(gf_draw_t* draw, gf_color_t color, int dim, int npa va_end(va); } + +void gf_graphic_perspective(gf_draw_t* draw, double fovy, double znear, double zfar) { + double aspect = (double)draw->width / (double)draw->height; + double f = gf_math_cot(fovy / 180 * GF_MATH_PI / 2); + GLdouble matrix[16]; + int i; + + for(i = 0; i < 16; i++) matrix[i] = 0; + matrix[4 * 0 + 0] = f / aspect; + matrix[4 * 1 + 1] = f; + matrix[4 * 2 + 2] = (zfar + znear) / (znear - zfar); + matrix[4 * 3 + 2] = ((double)2 * zfar * znear) / (znear - zfar); + matrix[4 * 2 + 3] = -1; + + glLoadIdentity(); + glLoadMatrixd(matrix); +} + +GF_EXPORT void gf_graphic_set_camera(gf_draw_t* draw) { + GLdouble matrix[16]; + gf_math_vector_t f; + gf_math_vector_t up; + gf_math_vector_t s; + gf_math_vector_t u; + int i; + + f[0] = draw->lookat[0] - draw->camera[0]; + f[1] = draw->lookat[1] - draw->camera[1]; + f[2] = draw->lookat[2] - draw->camera[2]; + gf_math_normalize(f); + + up[0] = 0; + up[1] = 1; + up[2] = 0; + gf_math_normalize(up); + + gf_math_multiply(s, f, up); + gf_math_normalize(s); + + gf_math_multiply(u, s, f); + + for(i = 0; i < 16; i++) matrix[i] = 0; + matrix[4 * 0 + 0] = s[0]; + matrix[4 * 1 + 0] = s[1]; + matrix[4 * 2 + 0] = s[2]; + matrix[4 * 0 + 1] = u[0]; + matrix[4 * 1 + 1] = u[1]; + matrix[4 * 2 + 1] = u[2]; + matrix[4 * 0 + 2] = -f[0]; + matrix[4 * 1 + 2] = -f[1]; + matrix[4 * 2 + 2] = -f[2]; + matrix[4 * 3 + 3] = 1; + + glLoadIdentity(); + glLoadMatrixd(matrix); + glTranslated(-draw->camera[0], -draw->camera[1], -draw->camera[2]); +} diff --git a/engine/include/gf_draw_driver.h b/engine/include/gf_draw_driver.h index 7523056..790f75c 100644 --- a/engine/include/gf_draw_driver.h +++ b/engine/include/gf_draw_driver.h @@ -92,7 +92,7 @@ GF_EXPORT void gf_draw_driver_end_texture_2d(gf_draw_t* draw); * @param draw Drawing interface * @param color Color */ -GF_EXPORT void gf_draw_driver_set_color(gf_draw_t* draw, gf_color_t color); +GF_EXPORT void gf_draw_driver_set_color(gf_draw_t* draw, gf_graphic_color_t color); /** * @~english diff --git a/engine/include/gf_graphic.h b/engine/include/gf_graphic.h index d2084c3..14e5b6b 100644 --- a/engine/include/gf_graphic.h +++ b/engine/include/gf_graphic.h @@ -46,7 +46,7 @@ /** * @~english * @brief Macro to set color safely and shorter - * @param color gf_color_t + * @param color gf_graphic_color_t */ #define GF_SET_COLOR(color, red, green, blue, alpha) \ color.r = (red); \ @@ -76,13 +76,30 @@ GF_EXPORT void gf_graphic_end_2d(gf_draw_t* draw); * @~english * @brief Draw polygon with texture */ -GF_EXPORT void gf_graphic_draw_texture_polygon(gf_draw_t* draw, gf_texture_t* texture, gf_color_t color, int dim, int npair, ...); +GF_EXPORT void gf_graphic_draw_texture_polygon(gf_draw_t* draw, gf_texture_t* texture, gf_graphic_color_t color, int dim, int npair, ...); /** * @~english * @brief Draw polygon */ -GF_EXPORT void gf_graphic_fill_polygon(gf_draw_t* draw, gf_color_t color, int dim, int npair, ...); +GF_EXPORT void gf_graphic_fill_polygon(gf_draw_t* draw, gf_graphic_color_t color, int dim, int npair, ...); + +/** + * @~english + * @brief Set perspective + * @param draw Drawing interface + * @param fovy FOV + * @param znear Distance from viewer to the near clipping plane + * @param zfar Distance from viewer to the far clipping plane + */ +GF_EXPORT void gf_graphic_perspective(gf_draw_t* draw, double fovy, double znear, double zfar); + +/** + * @~english + * @brief Set camera + * @param draw Drawing interface + */ +GF_EXPORT void gf_graphic_set_camera(gf_draw_t* draw); /* Common */ @@ -97,18 +114,18 @@ GF_EXPORT float gf_graphic_text_width(gf_draw_t* draw, float size, const char* t * @~english * @brief Draw text */ -GF_EXPORT void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* text, gf_color_t color); +GF_EXPORT void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* text, gf_graphic_color_t color); /** * @~english * @brief Draw filled rectangle */ -GF_EXPORT void gf_graphic_fill_rect(gf_draw_t* draw, float x, float y, float w, float h, gf_color_t color); +GF_EXPORT void gf_graphic_fill_rect(gf_draw_t* draw, float x, float y, float w, float h, gf_graphic_color_t color); /** * @~english * @brief Draw rectangle with texture */ -GF_EXPORT void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_color_t color); +GF_EXPORT void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_graphic_color_t color); #endif diff --git a/engine/include/gf_math.h b/engine/include/gf_math.h index 1a66506..ddd6c48 100644 --- a/engine/include/gf_math.h +++ b/engine/include/gf_math.h @@ -17,13 +17,19 @@ /* Standard */ +/** + * @~english + * @brief Pi + */ +#define GF_MATH_PI 3.14159265 + /** * @~english * @brief Copy vector * @param from Source * @param to Destination */ -#define GF_VECTOR_COPY(from, to) \ +#define GF_MATH_VECTOR_COPY(from, to) \ to[0] = from[0]; \ to[1] = from[1]; \ to[2] = from[2] @@ -33,7 +39,7 @@ * @brief Calculate log2(x) * @param x Input */ -GF_EXPORT float gf_math_log2(float x); +GF_EXPORT double gf_math_log2(double x); /** * @~english @@ -43,6 +49,38 @@ GF_EXPORT float gf_math_log2(float x); * @param v1 Input * @param v2 Input */ -GF_EXPORT void gf_math_normal(gf_vector_t* r, gf_vector_t v0, gf_vector_t v1, gf_vector_t v2); +GF_EXPORT void gf_math_normal(gf_math_vector_t r, gf_math_vector_t v0, gf_math_vector_t v1, gf_math_vector_t v2); + +/** + * @~english + * @brief Calculate cot(x) + * @param x Input + */ +GF_EXPORT double gf_math_cot(double x); + +/** + * @~english + * @brief Normalize vector + * @param v Input/Output + */ +GF_EXPORT void gf_math_normalize(gf_math_vector_t v); + +/** + * @~english + * @brief Multiply vector by vector + * @param r Result + * @param v0 Input + * @param v1 Input + */ +GF_EXPORT void gf_math_multiply(gf_math_vector_t r, gf_math_vector_t v0, gf_math_vector_t v1); + +/** + * @~english + * @brief Subtract vector by vector + * @param r Result + * @param v0 Input + * @param v1 Input + */ +GF_EXPORT void gf_math_subtract(gf_math_vector_t r, gf_math_vector_t v0, gf_math_vector_t v1); #endif diff --git a/engine/include/gf_opengl.h b/engine/include/gf_opengl.h index 53bc959..51937ff 100644 --- a/engine/include/gf_opengl.h +++ b/engine/include/gf_opengl.h @@ -12,7 +12,6 @@ #include #endif #include -#include #if defined(USE_GLX) #include #include diff --git a/engine/include/gf_type/draw.h b/engine/include/gf_type/draw.h index 21adbd4..2ad02c7 100644 --- a/engine/include/gf_type/draw.h +++ b/engine/include/gf_type/draw.h @@ -95,9 +95,9 @@ GF_DECLARE_TYPE(draw, { int draw_3d; char title[128]; gf_texture_t* font[128]; - gf_vector_t light; - gf_vector_t lookat; - gf_vector_t camera; + gf_math_vector_t light; + gf_math_vector_t lookat; + gf_math_vector_t camera; void (*draw)(gf_draw_t*); }); #else diff --git a/engine/include/gf_type/draw_driver.h b/engine/include/gf_type/draw_driver.h index 91de7e3..7f711a7 100644 --- a/engine/include/gf_type/draw_driver.h +++ b/engine/include/gf_type/draw_driver.h @@ -21,7 +21,7 @@ /* Standard */ #if defined(DRV_OPENGL) -GF_DECLARE_TYPE(draw_driver, { GLUquadric* quadric; }); +GF_DECLARE_TYPE(draw_driver, { int unused; }); GF_DECLARE_TYPE(draw_driver_texture, { GLuint id; int width; diff --git a/engine/include/gf_type/graphic.h b/engine/include/gf_type/graphic.h index 80bc700..08587fd 100644 --- a/engine/include/gf_type/graphic.h +++ b/engine/include/gf_type/graphic.h @@ -18,23 +18,23 @@ /* Standard */ /** - * @struct gf_color_t + * @struct gf_graphic_color_t * @~english * @brief Color * - * @var gf_color_t::r + * @var gf_graphic_color_t::r * @brief Red * - * @var gf_color_t::g + * @var gf_graphic_color_t::g * @brief Green * - * @var gf_color_t::b + * @var gf_graphic_color_t::b * @brief Blue * - * @var gf_color_t::a + * @var gf_graphic_color_t::a * @brief Alpha */ -GF_DECLARE_TYPE(color, { +GF_DECLARE_TYPE(graphic_color, { double r; double g; double b; diff --git a/engine/include/gf_type/math.h b/engine/include/gf_type/math.h index e627479..9caada1 100644 --- a/engine/include/gf_type/math.h +++ b/engine/include/gf_type/math.h @@ -20,9 +20,9 @@ /** * @~english * @brief Vector - * @note gf_vector_t[3] is used internally + * @note gf_math_vector_t[3] is used internally */ -typedef double gf_vector_t[4]; +typedef double gf_math_vector_t[4]; #else #error "should not happen!" #endif diff --git a/engine/include/gf_type/mesh.h b/engine/include/gf_type/mesh.h index 5bf3f09..843d073 100644 --- a/engine/include/gf_type/mesh.h +++ b/engine/include/gf_type/mesh.h @@ -15,6 +15,7 @@ /* Engine */ #include +#include /* Standard */ @@ -30,8 +31,8 @@ * @brief Triangle color */ GF_DECLARE_TYPE(triangle, { - gf_vector_t points[3]; - gf_vector_t color; + gf_math_vector_t points[3]; + gf_graphic_color_t color; }); /** diff --git a/engine/premake5.lua b/engine/premake5.lua index 4d33e2d..c786cb4 100644 --- a/engine/premake5.lua +++ b/engine/premake5.lua @@ -2,8 +2,8 @@ gf_backends = { opengl = { name = "OpenGL", default = "glfw", - unix = {"GL", "GLU"}, - windows = {"opengl32", "glu32"}, + unix = {"GL"}, + windows = {"opengl32"}, backends = { glx = {"GLX", {"X11"}}, wgl = {"WGL", {"gdi32"}},