#define FLOOR_OFFS vec3(3.0, 0.0, 0.0)
#define FLOOR_SIZE vec2(5.5, 15.0)
+#define REFL_POS vec3(1.36, -0.5, 0.0)
#define GLASS_POS vec3(0.0, 0.2, 1.2)
bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
nearest.dist = 10000.0;
- if(isect_sphere(ro, rd, vec3(1.5, -0.5, 0.0), 0.85, hit)) {
+ if(isect_sphere(ro, rd, REFL_POS, 0.85, hit)) {
nearest = hit;
nearest.mtl = mtl_sph;
if(hit.dist < 1.0) {
long time_msec;
static int reshape_pending;
-static unsigned int fbtex;
static unsigned int sdr_gamma;
int demo_init(void)
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
- glEnable(GL_FRAMEBUFFER_SRGB);
- glEnable(GL_MULTISAMPLE);
-
- glGenTextures(1, &fbtex);
- glBindTexture(GL_TEXTURE_2D, fbtex);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- if(win_width) {
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, win_width, win_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+ if(opt.srgb) {
+ glEnable(GL_FRAMEBUFFER_SRGB);
}
+ if(opt.msaa) {
+ glEnable(GL_MULTISAMPLE);
+ }
+
+ post_init();
if(!(sdr_gamma = create_program_load("sdr/gamma.v.glsl", "sdr/gamma.p.glsl"))) {
fprintf(stderr, "Warning: failed to load the gamma correction shader\n");
if(sdr_gamma) {
glDeleteProgram(sdr_gamma);
}
- if(fbtex) {
- glDeleteTextures(1, &fbtex);
- }
+ post_cleanup();
for(i=0; i<num_parts; i++) {
parts[i]->destroy();
long part_time;
if(reshape_pending) {
- /* reshape fbtex */
- glBindTexture(GL_TEXTURE_2D, fbtex);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, win_width, win_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
- reshape_pending = 1;
+ post_reshape(win_width, win_height);
+ reshape_pending = 0;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* no-srgb gamma correction fallback */
if(!opt.srgb) {
- glBindTexture(GL_TEXTURE_2D, fbtex);
+ glBindTexture(GL_TEXTURE_2D, fbtex[0].id);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, win_width, win_height);
glUseProgram(sdr_gamma);
- overlay(fbtex, win_aspect, 1.0);
+ overlay_tex(fbtex, 1.0);
}
}
+#include <assert.h>
#include "opengl.h"
#include "texture.h"
#include "post.h"
#include "demo.h"
+#include "opt.h"
+
+static unsigned int fbtex_gltexid[2];
+static unsigned int fbo[2], rbuf_depth[2];
+struct texture fbtex[2];
+int fbtex_cur;
+
+int post_init(void)
+{
+ int i;
+
+ glGenTextures(2, fbtex_gltexid);
+ glGenRenderbuffers(2, rbuf_depth);
+
+ for(i=0; i<2; i++) {
+ fbtex[i].id = fbtex_gltexid[i];
+ fbtex[i].width = 0;
+ fbtex[i].height = 0;
+ fbtex[i].pixels = 0;
+
+ glBindTexture(GL_TEXTURE_2D, fbtex[i].id);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ }
+ return 0;
+}
+
+void post_cleanup(void)
+{
+ if(fbtex_gltexid[0]) {
+ glDeleteTextures(2, fbtex_gltexid);
+ }
+ if(rbuf_depth[0]) {
+ glDeleteRenderbuffers(2, rbuf_depth);
+ }
+ if(fbo[0]) {
+ glDeleteFramebuffers(2, fbo);
+ }
+}
+
+void post_reshape(int x, int y)
+{
+ int i, ifmt;
+
+ if(x != fbtex[0].width || y != fbtex[0].height) {
+ ifmt = opt.srgb ? GL_SRGB_ALPHA : GL_RGBA;
+
+ for(i=0; i<2; i++) {
+ glBindTexture(GL_TEXTURE_2D, fbtex[i].id);
+ glTexImage2D(GL_TEXTURE_2D, 0, ifmt, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+ fbtex[i].width = x;
+ fbtex[i].height = y;
+
+ glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth[i]);
+ glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, x, y);
+ }
+
+ if(!fbo[0]) {
+ glGenFramebuffers(2, fbo);
+ for(i=0; i<2; i++) {
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo[i]);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbtex_gltexid[i], 0);
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth[i]);
+ }
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ }
+ }
+}
void overlay(unsigned int tex, float aspect, float alpha)
{
#ifndef POST_H_
#define POST_H_
-struct texture;
+#include "texture.h"
+
+extern struct texture fbtex[2];
+extern int fbtex_cur;
+
+int post_init(void);
+void post_cleanup(void);
+void post_reshape(int x, int y);
void overlay(unsigned int tex, float aspect, float alpha);
void overlay_tex(struct texture *tex, float alpha);
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5
};
+
struct texture *load_texture(const char *fname)
{
struct img_pixmap img;