cubemap renderer progress (untested)
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 10 Jan 2017 22:19:36 +0000 (00:19 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 10 Jan 2017 22:19:36 +0000 (00:19 +0200)
src/rend_cubemap.cc [new file with mode: 0644]
src/rend_cubemap.h
src/texture.cc
src/texture.h

diff --git a/src/rend_cubemap.cc b/src/rend_cubemap.cc
new file mode 100644 (file)
index 0000000..89d1b8b
--- /dev/null
@@ -0,0 +1,86 @@
+#include "opengl.h"
+#include "rend_cubemap.h"
+
+RendCubemap::RendCubemap()
+{
+       cubemap = 0;
+       fbo = zbuf = 0;
+       zbuf_width = zbuf_height = 0;
+
+       proj_matrix.perspective(M_PI / 2.0, 1, 0.5, 10000);
+}
+
+void RendCubemap::set_position(const Vec3 &pos)
+{
+       this->pos = pos;
+}
+
+void RendCubemap::set_cubemap(Texture *cubemap)
+{
+       this->cubemap = cubemap;
+}
+
+void RendCubemap::draw() const
+{
+       static const Vec3 targ[] = {
+               Vec3{1, 0, 0}, Vec3{-1, 0, 0},
+               Vec3{0, 1, 0}, Vec3{0, -1, 0},
+               Vec3{0, 0, 1}, Vec3{0, 0, -1}
+       };
+       static const Vec3 up[] = {
+               Vec3{0, 1, 0}, Vec3{0, 1, 0},
+               Vec3{0, 0, 1}, Vec3{0, 0, -1},
+               Vec3{0, 1, 0}, Vec3{0, 1, 0}
+       };
+       if(!cubemap) return;
+
+       glMatrixMode(GL_PROJECTION);
+       glPushMatrix();
+       glLoadMatrixf(proj_matrix[0]);
+
+       for(int i=0; i<6; i++) {
+               Mat4 vmat;
+               vmat.inv_lookat(pos, targ[i], up[i]);
+
+               glMatrixMode(GL_MODELVIEW);
+               if(i == 0) glPushMatrix();
+               glLoadMatrixf(vmat[0]);
+
+               setup(i);
+
+               Renderer::draw();
+       }
+
+       glMatrixMode(GL_PROJECTION);
+       glPopMatrix();
+       glMatrixMode(GL_MODELVIEW);
+       glPopMatrix();
+}
+
+void RendCubemap::draw_object(Object *obj) const
+{
+       obj->draw();
+}
+
+void RendCubemap::setup(int face_idx) const
+{
+       if(!cubemap) return;
+
+       if(!fbo) {
+               glGenFramebuffers(1, &fbo);
+       }
+       glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+       if(!zbuf) {
+               glGenRenderbuffers(1, &zbuf);
+               glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, zbuf);
+       }
+
+       if(zbuf_width != cubemap->get_width() || zbuf_height != cubemap->get_height()) {
+               zbuf_width = cubemap->get_width();
+               zbuf_height = cubemap->get_height();
+               glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, zbuf_width, zbuf_height);
+       }
+
+       glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+                       GL_TEXTURE_CUBE_MAP_POSITIVE_X + face_idx, cubemap->get_id(), 0);
+}
index 00995c7..4bacd9e 100644 (file)
@@ -8,13 +8,20 @@ class RendCubemap : public Renderer {
 private:
        Vec3 pos;
        Texture *cubemap;
-       unsigned int fbo, zbuf;
+       mutable unsigned int fbo, zbuf;
+       mutable int zbuf_width, zbuf_height;
+       Mat4 proj_matrix;
+
+       void setup(int face_idx) const;
 
 public:
+       RendCubemap();
+
        void set_position(const Vec3 &pos);
        void set_cubemap(Texture *cubemap);
 
        void draw() const;
+       void draw_object(Object *obj) const;
 };
 
 #endif // REND_CUBEMAP_H_
index ff4d1b5..f4f7cfe 100644 (file)
@@ -126,6 +126,16 @@ int Texture::get_size(int dim) const
        return sz[dim];
 }
 
+int Texture::get_width() const
+{
+       return sz[0];
+}
+
+int Texture::get_height() const
+{
+       return sz[1];
+}
+
 unsigned int Texture::get_id() const
 {
        return id;
index 5e18e7a..5c4bf3c 100644 (file)
@@ -42,6 +42,8 @@ public:
        unsigned int get_format() const;
 
        int get_size(int dim) const;
+       int get_width() const;
+       int get_height() const;
 
        void create(int xsz, int ysz, TextureType type = TEX_2D, unsigned int ifmt = GL_RGBA);
        void create_default(TextureType type = TEX_2D);