rev |
line source |
nuclear@1
|
1 #include "opengl.h"
|
nuclear@1
|
2 #include "renderer.h"
|
nuclear@1
|
3
|
nuclear@1
|
4 Renderer::Renderer()
|
nuclear@1
|
5 {
|
nuclear@1
|
6 leveltex = 0;
|
nuclear@1
|
7 sdrprog = 0;
|
nuclear@1
|
8 }
|
nuclear@1
|
9
|
nuclear@1
|
10 Renderer::~Renderer()
|
nuclear@1
|
11 {
|
nuclear@1
|
12 shutdown();
|
nuclear@1
|
13 }
|
nuclear@1
|
14
|
nuclear@1
|
15 bool Renderer::init()
|
nuclear@1
|
16 {
|
nuclear@1
|
17 sdrprog = new SdrProg;
|
nuclear@1
|
18 /*if(!sdrprog->load("sdr/bloboray.v.glsl", "sdr/bloboray.p.glsl")) {
|
nuclear@1
|
19 return false;
|
nuclear@1
|
20 }*/
|
nuclear@1
|
21
|
nuclear@1
|
22 leveltex = new Texture3D;
|
nuclear@1
|
23
|
nuclear@1
|
24 return true;
|
nuclear@1
|
25 }
|
nuclear@1
|
26
|
nuclear@1
|
27 void Renderer::shutdown()
|
nuclear@1
|
28 {
|
nuclear@1
|
29 delete leveltex;
|
nuclear@1
|
30 delete sdrprog;
|
nuclear@1
|
31 }
|
nuclear@1
|
32
|
nuclear@1
|
33 static void draw_cube(const Vector3 &pos, float sz);
|
nuclear@1
|
34
|
nuclear@1
|
35 void Renderer::render(const Level *lvl) const
|
nuclear@1
|
36 {
|
nuclear@1
|
37 Volume *vol = lvl->terrain;
|
nuclear@1
|
38
|
nuclear@1
|
39 glEnable(GL_COLOR_MATERIAL);
|
nuclear@1
|
40 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
|
nuclear@1
|
41
|
nuclear@1
|
42 glBegin(GL_QUADS);
|
nuclear@1
|
43 for(int i=0; i<vol->get_size(0); i++) {
|
nuclear@1
|
44 for(int j=0; j<vol->get_size(1); j++) {
|
nuclear@1
|
45 for(int k=0; k<vol->get_size(2) - 1; k++) {
|
nuclear@1
|
46 Vector4 vox = vol->get_voxel(i, j, k);
|
nuclear@1
|
47 Vector4 next = vol->get_voxel(i, j, k + 1);
|
nuclear@1
|
48
|
nuclear@1
|
49 if(vox.w > 0.1 && next.w < 0.1) {
|
nuclear@1
|
50 glColor3f(vox.x, vox.y, vox.z);
|
nuclear@1
|
51
|
nuclear@1
|
52 float x = ((float)i - vol->get_size(0) / 2) * 0.1;
|
nuclear@1
|
53 float z = ((float)j - vol->get_size(1) / 2) * 0.1;
|
nuclear@1
|
54 float y = ((float)k - vol->get_size(2) / 2) * 0.1;
|
nuclear@1
|
55 draw_cube(Vector3(x, y, z), 0.1);
|
nuclear@1
|
56 }
|
nuclear@1
|
57 }
|
nuclear@1
|
58 }
|
nuclear@1
|
59 }
|
nuclear@1
|
60 glEnd();
|
nuclear@1
|
61
|
nuclear@1
|
62 glDisable(GL_COLOR_MATERIAL);
|
nuclear@1
|
63 }
|
nuclear@1
|
64
|
nuclear@1
|
65
|
nuclear@1
|
66 static void draw_cube(const Vector3 &pos, float sz)
|
nuclear@1
|
67 {
|
nuclear@1
|
68 sz /= 2.0;
|
nuclear@1
|
69
|
nuclear@1
|
70 glNormal3f(0, 0, 1);
|
nuclear@1
|
71 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
72 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
73 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
74 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
75
|
nuclear@1
|
76 glNormal3f(1, 0, 0);
|
nuclear@1
|
77 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
78 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
79 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
80 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
81
|
nuclear@1
|
82 glNormal3f(0, 0, -1);
|
nuclear@1
|
83 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
84 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
85 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
86 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
87
|
nuclear@1
|
88 glNormal3f(-1, 0, 0);
|
nuclear@1
|
89 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
90 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
91 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
92 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
93
|
nuclear@1
|
94 glNormal3f(0, 1, 0);
|
nuclear@1
|
95 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
96 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
|
nuclear@1
|
97 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
98 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
|
nuclear@1
|
99
|
nuclear@1
|
100 glNormal3f(0, -1, 0);
|
nuclear@1
|
101 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
102 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
|
nuclear@1
|
103 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
104 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
|
nuclear@1
|
105
|
nuclear@1
|
106 }
|