From 6b2ad3e55ddd0770ccd763a84c18657b12750a0e Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Wed, 11 Jan 2017 00:19:36 +0200 Subject: [PATCH] cubemap renderer progress (untested) --- src/rend_cubemap.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/rend_cubemap.h | 9 +++++- src/texture.cc | 10 ++++++ src/texture.h | 2 ++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/rend_cubemap.cc diff --git a/src/rend_cubemap.cc b/src/rend_cubemap.cc new file mode 100644 index 0000000..89d1b8b --- /dev/null +++ b/src/rend_cubemap.cc @@ -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); +} diff --git a/src/rend_cubemap.h b/src/rend_cubemap.h index 00995c7..4bacd9e 100644 --- a/src/rend_cubemap.h +++ b/src/rend_cubemap.h @@ -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_ diff --git a/src/texture.cc b/src/texture.cc index ff4d1b5..f4f7cfe 100644 --- a/src/texture.cc +++ b/src/texture.cc @@ -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; diff --git a/src/texture.h b/src/texture.h index 5e18e7a..5c4bf3c 100644 --- a/src/texture.h +++ b/src/texture.h @@ -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); -- 1.7.10.4