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