GoldFish Engine
Quite simple and lightweight game engine
Loading...
Searching...
No Matches
gf_graphic.c
1#define GF_EXPOSE_DRAW
2#define GF_EXPOSE_TEXTURE
3
4#include <gf_pre.h>
5
6/* External library */
7#include <gf_opengl.h>
8
9/* Interface */
10#include <gf_graphic.h>
11
12/* Engine */
13#include <gf_draw.h>
14#include <gf_texture.h>
15#include <gf_draw_driver.h>
16#include <gf_math.h>
17
18/* Standard */
19#include <stdarg.h>
20#include <stdio.h>
21
23 glDisable(GL_LIGHTING);
24 glDisable(GL_DEPTH_TEST);
25 glMatrixMode(GL_PROJECTION);
26 glPushMatrix();
27 glLoadIdentity();
28 glOrtho(0, draw->width, draw->height, 0, -1, 1);
29 glMatrixMode(GL_MODELVIEW);
30 glPushMatrix();
31 glLoadIdentity();
32}
33
34void gf_graphic_end_2d(gf_draw_t* draw) {
35 glMatrixMode(GL_MODELVIEW);
36 glPopMatrix();
37 glMatrixMode(GL_PROJECTION);
38 glPopMatrix();
39 glMatrixMode(GL_MODELVIEW);
40 glEnable(GL_DEPTH_TEST);
41 glEnable(GL_LIGHTING);
42}
43
44void gf_graphic_clear(gf_draw_t* draw) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); }
45
46void gf_graphic_draw_texture_polygon(gf_draw_t* draw, gf_texture_t* texture, gf_graphic_color_t color, int dim, int npair, ...) {
47 double tw = (double)texture->width / texture->internal_width;
48 double th = (double)texture->height / texture->internal_height;
49 int i;
50 va_list va;
51 va_start(va, npair);
52
53 if(dim == 2) gf_graphic_begin_2d(draw);
55
56 gf_draw_driver_set_color(draw, color);
57 glBegin(GL_TRIANGLE_FAN);
58
59 for(i = 0; i < npair; i++) {
60 double tx = va_arg(va, double) * tw;
61 double ty = va_arg(va, double) * th;
62 double x = va_arg(va, double);
63 double y = va_arg(va, double);
64 glTexCoord2f(tx, ty);
65 if(dim == GF_GRAPHIC_2D) {
66 glVertex2f(x, y);
67 } else if(dim == GF_GRAPHIC_3D) {
68 double z = va_arg(va, double);
69 glVertex3f(x, y, z);
70 }
71 }
72
73 glEnd();
74
76 if(dim == 2) gf_graphic_end_2d(draw);
77
78 va_end(va);
79}
80
81void gf_graphic_fill_polygon(gf_draw_t* draw, gf_graphic_color_t color, int dim, int npair, ...) {
82 int i;
83 va_list va;
84 va_start(va, npair);
85
86 if(dim == 2) gf_graphic_begin_2d(draw);
87
88 gf_draw_driver_set_color(draw, color);
89 glBegin(GL_TRIANGLE_FAN);
90
91 for(i = 0; i < npair; i++) {
92 double x = va_arg(va, double);
93 double y = va_arg(va, double);
94 if(dim == GF_GRAPHIC_2D) {
95 glVertex2f(x, y);
96 } else if(dim == GF_GRAPHIC_3D) {
97 double z = va_arg(va, double);
98 glVertex3f(x, y, z);
99 }
100 }
101
102 glEnd();
103 if(dim == 2) gf_graphic_end_2d(draw);
104
105 va_end(va);
106}
107
108void gf_graphic_perspective(gf_draw_t* draw, double fovy, double znear, double zfar) {
109 double aspect = (double)draw->width / (double)draw->height;
110 double f = gf_math_cot(fovy / 180 * GF_MATH_PI / 2);
111 GLdouble matrix[16];
112 int i;
113
114 for(i = 0; i < 16; i++) matrix[i] = 0;
115 matrix[4 * 0 + 0] = f / aspect;
116 matrix[4 * 1 + 1] = f;
117 matrix[4 * 2 + 2] = (zfar + znear) / (znear - zfar);
118 matrix[4 * 3 + 2] = ((double)2 * zfar * znear) / (znear - zfar);
119 matrix[4 * 2 + 3] = -1;
120
121 glLoadIdentity();
122 glLoadMatrixd(matrix);
123}
124
126 GLdouble matrix[16];
131 int i;
132
133 f[0] = draw->lookat[0] - draw->camera[0];
134 f[1] = draw->lookat[1] - draw->camera[1];
135 f[2] = draw->lookat[2] - draw->camera[2];
137
138 up[0] = 0;
139 up[1] = 1;
140 up[2] = 0;
142
143 gf_math_multiply(s, f, up);
145
146 gf_math_multiply(u, s, f);
147
148 for(i = 0; i < 16; i++) matrix[i] = 0;
149 matrix[4 * 0 + 0] = s[0];
150 matrix[4 * 1 + 0] = s[1];
151 matrix[4 * 2 + 0] = s[2];
152 matrix[4 * 0 + 1] = u[0];
153 matrix[4 * 1 + 1] = u[1];
154 matrix[4 * 2 + 1] = u[2];
155 matrix[4 * 0 + 2] = -f[0];
156 matrix[4 * 1 + 2] = -f[1];
157 matrix[4 * 2 + 2] = -f[2];
158 matrix[4 * 3 + 3] = 1;
159
160 glLoadIdentity();
161 glLoadMatrixd(matrix);
162 glTranslated(-draw->camera[0], -draw->camera[1], -draw->camera[2]);
163}
Drawing interface.
Drawing driver.
void gf_draw_driver_set_color(gf_draw_t *draw, gf_graphic_color_t color)
Set color.
Definition gf_driver.c:53
void gf_draw_driver_end_texture_2d(gf_draw_t *draw)
End 2D texture mode.
Definition gf_driver.c:51
void gf_draw_driver_begin_texture_2d(gf_draw_t *draw, gf_texture_t *texture)
Begin 2D texture mode.
Definition gf_driver.c:49
Graphic interface.
#define GF_GRAPHIC_3D
Dimension parameter for 3D.
Definition gf_graphic.h:32
void gf_graphic_draw_texture_polygon(gf_draw_t *draw, gf_texture_t *texture, gf_graphic_color_t color, int dim, int npair,...)
Draw polygon with texture.
Definition gf_graphic.c:28
void gf_graphic_clear(gf_draw_t *draw)
Clear the screen.
Definition gf_graphic.c:26
void gf_graphic_fill_polygon(gf_draw_t *draw, gf_graphic_color_t color, int dim, int npair,...)
Draw polygon.
Definition gf_graphic.c:30
void gf_graphic_perspective(gf_draw_t *draw, double fovy, double znear, double zfar)
Set perspective.
Definition gf_graphic.c:32
#define GF_GRAPHIC_2D
Dimension parameter for 2D.
Definition gf_graphic.h:26
void gf_graphic_end_2d(gf_draw_t *draw)
End 2D mode.
Definition gf_graphic.c:24
void gf_graphic_begin_2d(gf_draw_t *draw)
Start 2D mode.
Definition gf_graphic.c:22
void gf_graphic_set_camera(gf_draw_t *draw)
Set camera.
Definition gf_graphic.c:34
#define GF_EXPORT
Macro for platform-dependent symbol export/import.
Definition gf_macro.h:190
void gf_math_multiply(gf_math_vector_t r, gf_math_vector_t v0, gf_math_vector_t v1)
Multiply vector by vector.
Definition gf_math.c:56
void gf_math_normalize(gf_math_vector_t v)
Normalize vector.
Definition gf_math.c:17
#define GF_MATH_PI
Pi.
Definition gf_math.h:24
double gf_math_cot(double x)
Calculate cot(x)
Definition gf_math.c:62
OpenGL headers.
Required headers before anything.
Texture.
double gf_math_vector_t[4]
Vector.
Definition math.h:25
Drawing interface.
Definition draw.h:108
gf_math_vector_t lookat
Where to look at.
Definition draw.h:108
gf_math_vector_t camera
Camera location.
Definition draw.h:108
int width
Width of window.
Definition draw.h:108
int height
Height of window.
Definition draw.h:108
Texture.
Definition texture.h:49
int internal_width
Internal width of texture.
Definition texture.h:49
int width
Width of texture.
Definition texture.h:49