fixed crash when glDebugCallbackARB is available but glDebugCallback isn't
[laserbrain_demo] / src / rend_cubemap.cc
1 #include "opengl.h"
2 #include "rend_cubemap.h"
3
4 RendCubemap::RendCubemap()
5 {
6         cubemap = 0;
7         fbo = zbuf = 0;
8         zbuf_width = zbuf_height = 0;
9
10         proj_matrix.perspective(M_PI / 2.0, 1, 0.5, 10000);
11 }
12
13 void RendCubemap::set_position(const Vec3 &pos)
14 {
15         this->pos = pos;
16 }
17
18 void RendCubemap::set_cubemap(Texture *cubemap)
19 {
20         this->cubemap = cubemap;
21 }
22
23 void RendCubemap::draw() const
24 {
25         static const Vec3 targ[] = {
26                 Vec3{1, 0, 0}, Vec3{-1, 0, 0},
27                 Vec3{0, 1, 0}, Vec3{0, -1, 0},
28                 Vec3{0, 0, 1}, Vec3{0, 0, -1}
29         };
30         static const Vec3 up[] = {
31                 Vec3{0, 1, 0}, Vec3{0, 1, 0},
32                 Vec3{0, 0, 1}, Vec3{0, 0, -1},
33                 Vec3{0, 1, 0}, Vec3{0, 1, 0}
34         };
35         if(!cubemap) return;
36
37         glMatrixMode(GL_PROJECTION);
38         glPushMatrix();
39         glLoadMatrixf(proj_matrix[0]);
40
41         for(int i=0; i<6; i++) {
42                 Mat4 vmat;
43                 vmat.inv_lookat(pos, targ[i], up[i]);
44
45                 glMatrixMode(GL_MODELVIEW);
46                 if(i == 0) glPushMatrix();
47                 glLoadMatrixf(vmat[0]);
48
49                 setup(i);
50
51                 Renderer::draw();
52         }
53
54         glMatrixMode(GL_PROJECTION);
55         glPopMatrix();
56         glMatrixMode(GL_MODELVIEW);
57         glPopMatrix();
58 }
59
60 void RendCubemap::draw_object(Object *obj) const
61 {
62         obj->draw();
63 }
64
65 void RendCubemap::setup(int face_idx) const
66 {
67         if(!cubemap) return;
68
69         if(!fbo) {
70                 glGenFramebuffers(1, &fbo);
71         }
72         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
73         if(!zbuf) {
74                 glGenRenderbuffers(1, &zbuf);
75                 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, zbuf);
76         }
77
78         if(zbuf_width != cubemap->get_width() || zbuf_height != cubemap->get_height()) {
79                 zbuf_width = cubemap->get_width();
80                 zbuf_height = cubemap->get_height();
81                 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, zbuf_width, zbuf_height);
82         }
83
84         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
85                         GL_TEXTURE_CUBE_MAP_POSITIVE_X + face_idx, cubemap->get_id(), 0);
86 }