-
Notifications
You must be signed in to change notification settings - Fork 7
/
render.h
143 lines (100 loc) · 2.81 KB
/
render.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright © 2015, The Network Protocol Company, Inc. All Rights Reserved.
#ifndef RENDER_H
#define RENDER_H
#include "core.h"
#include "const.h"
#include "vectorial/vec3f.h"
#include "vectorial/mat4f.h"
using namespace vectorial;
struct RenderCube
{
float r,g,b,a;
mat4f transform;
mat4f inverse_transform;
};
struct RenderState
{
RenderState()
{
num_cubes = 0;
}
int num_cubes;
RenderCube cube[MaxCubes];
};
void render_get_state( const struct World & world, RenderState & render_state );
struct RenderCubeInstance
{
float r,g,b,a;
mat4f model;
mat4f model_view;
mat4f model_view_projection;
};
class Render
{
public:
Render();
~Render();
void ResizeDisplay( int display_width, int display_height );
void SetLightPosition( const vec3f & position );
void SetCamera( const vec3f & position, const vec3f & lookat, const vec3f & up );
void ClearScreen();
void BeginScene( float x1, float y1, float x2, float y2 );
void RenderScene( const RenderState & render_state );
void RenderShadows( const RenderState & render_state );
void RenderShadowQuad();
void EndScene();
private:
void Initialize();
bool initialized;
int display_width;
int display_height;
vec3f camera_position;
vec3f camera_lookat;
vec3f camera_up;
vec3f light_position;
uint32_t shadow_shader;
uint32_t cubes_shader;
uint32_t debug_shader;
uint32_t cubes_vao;
uint32_t cubes_vbo;
uint32_t cubes_ibo;
uint32_t cubes_instance_buffer;
uint32_t shadow_vao;
uint32_t shadow_vbo;
uint32_t mask_vao;
uint32_t mask_vbo;
vec3f shadow_vertices[MaxCubeShadowVertices];
RenderCubeInstance instance_data[MaxCubes];
};
struct Camera
{
vec3f position;
vec3f lookat;
vec3f up;
Camera()
{
position = vec3f::zero();
lookat = vec3f::zero();
up = vec3f(0,0,1);
}
void EaseIn( const vec3f & new_lookat, const vec3f & new_position )
{
const float epsilon = 0.00001f;
if ( length_squared( new_lookat - lookat ) > epsilon )
lookat += ( new_lookat - lookat ) * 0.15f;
if ( length_squared( new_position - position ) > epsilon )
position += ( new_position - position ) * 0.15f;
up = cross( position - lookat, vec3f(1,0,0) );
}
void Snap( const vec3f & new_lookat, const vec3f & new_position )
{
lookat = new_lookat;
position = new_position;
up = cross( position - lookat, vec3f(1,0,0) );
}
};
void clear_opengl_error();
void check_opengl_error( const char * message );
uint32_t load_shader( const char * vertex_file_path, const char * fragment_file_path );
void delete_shader( uint32_t shader );
#endif // #ifndef RENDER_H