some meshgen and map rendering updates
[voxelands-alt.git] / src / graphics / render.c
blob0372625b1cc014adbf247b60db80a1d248eb5b46
1 /************************************************************************
2 * render.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "graphics.h"
22 #include "ui.h"
24 #include <math.h>
26 static struct {
27 int ticks;
28 int tticks;
29 int fticks;
30 int font_init;
31 float dtime;
32 matrix_t projection;
33 } render_data = {
38 0.1
41 /* get client-side frame time */
42 float client_dtime()
44 return render_data.dtime;
47 /* get the current projection matrix */
48 matrix_t *render_get_projection_matrix()
50 return &render_data.projection;
53 void render_set_projection_matrix(matrix_t *m)
55 float fov;
56 float near_plane;
57 float far_plane;
58 float ratio;
59 float x;
60 float y;
61 float z;
63 if (m) {
64 render_data.projection = *m;
65 return;
68 fov = 70.0;
69 near_plane = 0.1;
70 far_plane = 1000.0;
72 fov = math_degrees_to_radians(fov/2.0);
74 ratio = (float)wm_data.size.width/(float)wm_data.size.height;
75 y = (1.0/tan(fov));
76 x = y/ratio;
77 z = far_plane-near_plane;
79 matrix_init(&render_data.projection);
81 render_data.projection.data[0] = x;
82 render_data.projection.data[5] = y;
83 render_data.projection.data[10] = -((far_plane+near_plane)/z);
84 render_data.projection.data[11] = -1;
85 render_data.projection.data[14] = -((2.0*near_plane*far_plane)/z);
86 render_data.projection.data[15] = 0;
89 /* clear the frame */
90 void render_pre()
92 if (!render_data.ticks)
93 render_data.ticks = time_ticks();
94 events_main();
97 /* render to frame */
98 void render_post()
100 camera_t *cam;
101 ui_render();
103 if (wm_data.cursor.mat) {
104 int mouse[2];
105 events_get_mouse(mouse);
106 render2d_quad_mat(wm_data.cursor.mat,mouse[0]+wm_data.cursor.x,mouse[1]+wm_data.cursor.y,wm_data.cursor.w,wm_data.cursor.h);
109 cam = camera_get();
111 /* should get this from sky when in-game */
112 glClearColor(0.0, 0.0, 0.0, 1.0);
113 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
114 glEnable(GL_DEPTH_TEST);
116 water_prerender(cam);
118 render_map(cam,NULL);
120 water_render();
122 render3d(cam,NULL);
124 particles_render();
126 glDisable(GL_DEPTH_TEST);
128 render2d();
130 wm_update();
132 /* calculate a scale factor for smooth animations */
133 render_data.tticks = interval_delay(render_data.ticks,wm_data.frame_cap);
134 wm_data.lfps_i++;
135 if (wm_data.lfps_i > 3)
136 wm_data.lfps_i = 0;
137 wm_data.lfps[wm_data.lfps_i] = calc_fps(render_data.ticks,render_data.tticks);
138 wm_data.fps = (wm_data.lfps[0]+wm_data.lfps[1]+wm_data.lfps[2]+wm_data.lfps[3])/4;
139 render_data.dtime = time_dtime(render_data.ticks);
140 render_data.ticks = render_data.tticks;