rev |
line source |
nuclear@1
|
1 #include "opengl.h"
|
nuclear@1
|
2 #include "renderer.h"
|
nuclear@3
|
3 #include "sdr.h"
|
nuclear@1
|
4
|
nuclear@1
|
5 Renderer::Renderer()
|
nuclear@1
|
6 {
|
nuclear@1
|
7 leveltex = 0;
|
nuclear@1
|
8 sdrprog = 0;
|
nuclear@3
|
9
|
nuclear@3
|
10 level = 0;
|
nuclear@3
|
11
|
nuclear@3
|
12 fov = M_PI / 4.0;
|
nuclear@3
|
13 aspect = 1.0;
|
nuclear@1
|
14 }
|
nuclear@1
|
15
|
nuclear@1
|
16 Renderer::~Renderer()
|
nuclear@1
|
17 {
|
nuclear@1
|
18 shutdown();
|
nuclear@1
|
19 }
|
nuclear@1
|
20
|
nuclear@3
|
21 bool Renderer::init(Level *level)
|
nuclear@1
|
22 {
|
nuclear@3
|
23 if(!(sdrprog = create_program_load("sdr/bloboray.v.glsl", "sdr/bloboray.p.glsl"))) {
|
nuclear@1
|
24 return false;
|
nuclear@3
|
25 }
|
nuclear@3
|
26
|
nuclear@3
|
27 Volume *vol = level->terrain;
|
nuclear@1
|
28
|
nuclear@1
|
29 leveltex = new Texture3D;
|
nuclear@3
|
30 leveltex->create(vol->get_size(0), vol->get_size(1), vol->get_size(2));
|
nuclear@3
|
31
|
nuclear@3
|
32 this->level = level;
|
nuclear@1
|
33
|
nuclear@1
|
34 return true;
|
nuclear@1
|
35 }
|
nuclear@1
|
36
|
nuclear@1
|
37 void Renderer::shutdown()
|
nuclear@1
|
38 {
|
nuclear@1
|
39 delete leveltex;
|
nuclear@3
|
40 if(sdrprog) {
|
nuclear@3
|
41 free_program(sdrprog);
|
nuclear@3
|
42 }
|
nuclear@3
|
43 }
|
nuclear@3
|
44
|
nuclear@3
|
45 void Renderer::set_fov(float fov)
|
nuclear@3
|
46 {
|
nuclear@3
|
47 this->fov = fov;
|
nuclear@3
|
48 }
|
nuclear@3
|
49
|
nuclear@3
|
50 void Renderer::set_aspect(float aspect)
|
nuclear@3
|
51 {
|
nuclear@3
|
52 this->aspect = aspect;
|
nuclear@1
|
53 }
|
nuclear@1
|
54
|
nuclear@1
|
55 static void draw_cube(const Vector3 &pos, float sz);
|
nuclear@1
|
56
|
nuclear@3
|
57 void Renderer::render() const
|
nuclear@1
|
58 {
|
nuclear@3
|
59 leveltex->update((float*)level->terrain->get_data_ptr());
|
nuclear@3
|
60 bind_texture(leveltex);
|
nuclear@1
|
61
|
nuclear@3
|
62 set_uniform_float3(sdrprog, "volsize", level->terrain->get_size(0),
|
nuclear@3
|
63 level->terrain->get_size(1), level->terrain->get_size(2));
|
nuclear@3
|
64 set_uniform_float4(sdrprog, "camprop", fov, aspect, 0, 0);
|
nuclear@3
|
65 bind_program(sdrprog);
|
nuclear@3
|
66
|
nuclear@3
|
67 glBegin(GL_QUADS);
|
nuclear@3
|
68 glTexCoord2f(0, 0);
|
nuclear@3
|
69 glVertex2f(-1, -1);
|
nuclear@3
|
70 glTexCoord2f(1, 0);
|
nuclear@3
|
71 glVertex2f(1, -1);
|
nuclear@3
|
72 glTexCoord2f(1, 1);
|
nuclear@3
|
73 glVertex2f(1, 1);
|
nuclear@3
|
74 glTexCoord2f(0, 1);
|
nuclear@3
|
75 glVertex2f(-1, 1);
|
nuclear@3
|
76 glEnd();
|
nuclear@3
|
77
|
nuclear@3
|
78 bind_program(0);
|
nuclear@3
|
79 bind_texture(0);
|
nuclear@3
|
80
|
nuclear@3
|
81 /*glEnable(GL_COLOR_MATERIAL);
|
nuclear@1
|
82 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
|
nuclear@1
|
83
|
nuclear@1
|
84 glBegin(GL_QUADS);
|
nuclear@1
|
85 for(int i=0; i<vol->get_size(0); i++) {
|
nuclear@1
|
86 for(int j=0; j<vol->get_size(1); j++) {
|
nuclear@1
|
87 for(int k=0; k<vol->get_size(2) - 1; k++) {
|
nuclear@1
|
88 Vector4 vox = vol->get_voxel(i, j, k);
|
nuclear@1
|
89 Vector4 next = vol->get_voxel(i, j, k + 1);
|
nuclear@1
|
90
|
nuclear@1
|
91 if(vox.w > 0.1 && next.w < 0.1) {
|
nuclear@1
|
92 glColor3f(vox.x, vox.y, vox.z);
|
nuclear@1
|
93
|
nuclear@1
|
94 float x = ((float)i - vol->get_size(0) / 2) * 0.1;
|
nuclear@1
|
95 float z = ((float)j - vol->get_size(1) / 2) * 0.1;
|
nuclear@1
|
96 float y = ((float)k - vol->get_size(2) / 2) * 0.1;
|
nuclear@1
|
97 draw_cube(Vector3(x, y, z), 0.1);
|
nuclear@1
|
98 }
|
nuclear@1
|
99 }
|
nuclear@1
|
100 }
|
nuclear@1
|
101 }
|
nuclear@1
|
102 glEnd();
|
nuclear@1
|
103
|
nuclear@1
|
104 glDisable(GL_COLOR_MATERIAL);
|
nuclear@3
|
105 */
|
nuclear@1
|
106 }
|
nuclear@1
|
107
|
nuclear@1
|
108
|
nuclear@1
|
109 static void draw_cube(const Vector3 &pos, float sz)
|
nuclear@1
|
110 {
|
nuclear@1
|
111 sz /= 2.0;
|
nuclear@1
|
112
|
nuclear@1
|
113 glNormal3f(0, 0, 1);
|
nuclear@1
|
114 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
115 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
116 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
117 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
118
|
nuclear@1
|
119 glNormal3f(1, 0, 0);
|
nuclear@1
|
120 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
121 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
122 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
123 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
124
|
nuclear@1
|
125 glNormal3f(0, 0, -1);
|
nuclear@1
|
126 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
127 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
128 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
129 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
130
|
nuclear@1
|
131 glNormal3f(-1, 0, 0);
|
nuclear@1
|
132 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
133 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
134 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
135 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
136
|
nuclear@1
|
137 glNormal3f(0, 1, 0);
|
nuclear@1
|
138 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
139 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
140 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
141 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
142
|
nuclear@1
|
143 glNormal3f(0, -1, 0);
|
nuclear@1
|
144 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
145 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
146 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
147 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
148
|
nuclear@1
|
149 }
|