Joshua Moerman
9 years ago
14 changed files with 187 additions and 212 deletions
@ -1 +1 @@ |
|||
Subproject commit bb8a9ab41750803845dfc540b183d2647e2da8e8 |
|||
Subproject commit 8845eefc8631ea9bdfc01ed5ff6b598cd65b69ce |
@ -1,91 +1,18 @@ |
|||
#pragma once |
|||
|
|||
#include <moggle/core/gl.hpp> |
|||
#include <moggle/core/fbo.hpp> |
|||
#include "renderbuffer.hpp" |
|||
#include "texture.hpp" |
|||
|
|||
#include <vector> |
|||
|
|||
/*
|
|||
* One can attach either an external renderbuffer/texture, or move one into fbo |
|||
* In the latter case, the fbo will own the renderbuffer/texture |
|||
* |
|||
* Attachment can be one of: GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT, or GL_STENCIL_ATTACHMENT |
|||
*/ |
|||
|
|||
namespace moggle { |
|||
|
|||
struct fbo { |
|||
explicit fbo(bool create_now = false) { |
|||
if(create_now) create(); |
|||
} |
|||
|
|||
~fbo() { destroy(); } |
|||
|
|||
fbo(fbo const &) = delete; |
|||
fbo & operator=(fbo const&) = delete; |
|||
|
|||
fbo(fbo && r) : id(r.id) { r.id = 0; } |
|||
fbo & operator=(fbo && r) { std::swap(id, r.id); return *this; } |
|||
|
|||
bool created() const { return id; } |
|||
explicit operator bool() const { return created(); } |
|||
|
|||
void create(){ if(!id) gl::generate_framebuffers(1, &id); } |
|||
void destroy(){ gl::delete_renderbuffers(1, &id); id = 0; } |
|||
|
|||
void bind() { |
|||
create(); |
|||
gl::bind_framebuffer(GL_FRAMEBUFFER, id); |
|||
} |
|||
|
|||
GLuint get_id() const { |
|||
return id; |
|||
} |
|||
|
|||
// Only well defined if the fbo owns the texture
|
|||
texture & get_texture() { |
|||
return textures.front(); |
|||
} |
|||
|
|||
void attach(GLenum attachment, renderbuffer const & rb){ |
|||
bind(); |
|||
gl::framebuffer_renderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, rb.get_id()); |
|||
} |
|||
|
|||
void attach(GLenum attachment, renderbuffer && rb){ |
|||
renderbuffers.push_back(std::move(rb)); |
|||
attach(attachment, renderbuffers.back()); |
|||
} |
|||
|
|||
void attach(GLenum attachment, GLenum textarget, texture const & t){ |
|||
bind(); |
|||
gl::framebuffer_texture_2d(GL_FRAMEBUFFER, attachment, textarget, t.get_id(), 0); |
|||
} |
|||
|
|||
void attach(GLenum attachment, GLenum textarget, texture && t){ |
|||
textures.push_back(std::move(t)); |
|||
attach(attachment, textarget, textures.back()); |
|||
} |
|||
|
|||
void clear(GLenum bufferbits){ |
|||
bind(); |
|||
gl::clear(bufferbits); |
|||
} |
|||
|
|||
static fbo create_default(GLsizei width, GLsizei height){ |
|||
fbo f; |
|||
f.attach(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture::create(width, height, GL_RGBA32F)); |
|||
f.attach(GL_DEPTH_ATTACHMENT, renderbuffer::create(width, height, 1, GL_DEPTH_COMPONENT16)); |
|||
f.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|||
return f; |
|||
} |
|||
|
|||
private: |
|||
std::vector<renderbuffer> renderbuffers; |
|||
std::vector<texture> textures; |
|||
|
|||
GLuint id = 0; |
|||
}; |
|||
inline fbo create_default_fbo(GLsizei width, GLsizei height){ |
|||
fbo f; |
|||
f.attach(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, create_texture(width, height, GL_RGBA32F)); |
|||
f.attach(GL_DEPTH_ATTACHMENT, create_renderbuffer(width, height, 1, GL_DEPTH_COMPONENT16)); |
|||
f.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|||
return f; |
|||
} |
|||
|
|||
} |
|||
|
@ -0,0 +1,45 @@ |
|||
#pragma once |
|||
|
|||
#include <moggle/math/matrix.hpp> |
|||
#include <boost/math/quaternion.hpp> |
|||
#include <limits> |
|||
#include <stdexcept> |
|||
|
|||
// Will create a R3 rotation if the quaternion is normalized
|
|||
// Typical use is: mat4 matrix = quaternion_to_R3_rotation(normalize(foo));
|
|||
template<typename T> |
|||
moggle::matrix4<T> quaternion_to_R3_rotation(boost::math::quaternion<T> const & q){ |
|||
using ::std::numeric_limits; |
|||
|
|||
T a = q.R_component_1(); |
|||
T b = q.R_component_2(); |
|||
T c = q.R_component_3(); |
|||
T d = q.R_component_4(); |
|||
|
|||
T aa = a*a; |
|||
T ab = a*b; |
|||
T ac = a*c; |
|||
T ad = a*d; |
|||
T bb = b*b; |
|||
T bc = b*c; |
|||
T bd = b*d; |
|||
T cc = c*c; |
|||
T cd = c*d; |
|||
T dd = d*d; |
|||
|
|||
return { |
|||
(aa+bb-cc-dd), 2*(-ad+bc), 2*(ac+bd), 0, |
|||
2*(ad+bc), (aa-bb+cc-dd), 2*(-ab+cd), 0, |
|||
2*(-ac+bd), 2*(ab+cd), (aa-bb-cc+dd), 0, |
|||
0, 0, 0, 1 |
|||
}; |
|||
} |
|||
|
|||
namespace boost { |
|||
namespace math { |
|||
template <typename T> |
|||
quaternion<T> normalize(quaternion<T> q){ |
|||
return q /= abs(q); |
|||
} |
|||
} |
|||
} |
@ -1,57 +1,14 @@ |
|||
#pragma once |
|||
|
|||
#include <moggle/core/gl.hpp> |
|||
#include <moggle/core/renderbuffer.hpp> |
|||
|
|||
namespace moggle { |
|||
|
|||
struct renderbuffer { |
|||
explicit renderbuffer(bool create_now = false) { |
|||
if(create_now) create(); |
|||
} |
|||
|
|||
~renderbuffer() { destroy(); } |
|||
|
|||
renderbuffer(renderbuffer const &) = delete; |
|||
renderbuffer & operator=(renderbuffer const&) = delete; |
|||
|
|||
renderbuffer(renderbuffer && r) : id(r.id) { r.id = 0; } |
|||
renderbuffer & operator=(renderbuffer && r) { std::swap(id, r.id); return *this; } |
|||
|
|||
bool created() const { return id; } |
|||
explicit operator bool() const { return created(); } |
|||
|
|||
void create(){ if(!id) gl::generate_renderbuffers(1, &id); } |
|||
void destroy(){ gl::delete_renderbuffers(1, &id); id = 0; } |
|||
|
|||
void bind() { |
|||
create(); |
|||
gl::bind_renderbuffer(GL_RENDERBUFFER, id); |
|||
} |
|||
|
|||
GLuint get_id() const { |
|||
return id; |
|||
} |
|||
|
|||
void storage(GLenum target, GLenum internalformat, GLsizei width, GLsizei height){ |
|||
bind(); |
|||
gl::renderbuffer_storage(target, internalformat, width, height); |
|||
} |
|||
|
|||
void storage_multisample(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height){ |
|||
bind(); |
|||
// TODO: wrap this call
|
|||
glRenderbufferStorageMultisample(target, samples, internalformat, width, height); |
|||
} |
|||
|
|||
static renderbuffer create(GLsizei width, GLsizei height, GLsizei samples = 1, GLenum format = GL_RGBA4){ |
|||
renderbuffer r; |
|||
if(samples <= 1) r.storage(GL_RENDERBUFFER, format, width, height); |
|||
else r.storage_multisample(GL_RENDERBUFFER, samples, format, width, height); |
|||
return r; |
|||
} |
|||
|
|||
private: |
|||
GLuint id = 0; |
|||
}; |
|||
inline renderbuffer create_renderbuffer(GLsizei width, GLsizei height, GLsizei samples = 1, GLenum format = GL_RGBA4){ |
|||
renderbuffer r; |
|||
if(samples <= 1) r.storage(GL_RENDERBUFFER, format, width, height); |
|||
else r.storage_multisample(GL_RENDERBUFFER, samples, format, width, height); |
|||
return r; |
|||
} |
|||
|
|||
} |
|||
|
@ -1,62 +1,14 @@ |
|||
#pragma once |
|||
|
|||
#include <moggle/core/gl.hpp> |
|||
#include <moggle/core/gl_type_traits.hpp> |
|||
|
|||
/*
|
|||
* NOTE: |
|||
* in glTexImage2D, border MUST be 0, so this parameter is left out in this interface. |
|||
*/ |
|||
#include <moggle/core/texture.hpp> |
|||
|
|||
namespace moggle { |
|||
|
|||
struct texture { |
|||
explicit texture(bool create_now = false) { |
|||
if(create_now) create(); |
|||
} |
|||
|
|||
~texture() { destroy(); } |
|||
|
|||
texture(texture const &) = delete; |
|||
texture & operator=(texture const&) = delete; |
|||
|
|||
texture(texture && t) : id(t.id) { t.id = 0; } |
|||
texture & operator=(texture && t) { std::swap(id, t.id); return *this; } |
|||
|
|||
bool created() const { return id; } |
|||
explicit operator bool() const { return created(); } |
|||
|
|||
void create(){ if(!id) gl::generate_textures(1, &id); } |
|||
void destroy(){ gl::delete_textures(1, &id); id = 0; } |
|||
|
|||
void bind(GLenum target) { |
|||
create(); |
|||
gl::bind_texture(target, id); |
|||
} |
|||
|
|||
GLuint get_id() const { return id; } |
|||
|
|||
template <typename T> |
|||
void image_2d(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLenum format, const T * data){ |
|||
bind(target); |
|||
auto type = gl_type_traits<T>::gl_constant; |
|||
gl::texture_image_2d(target, level, internalformat, width, height, 0, format, type, data); |
|||
} |
|||
|
|||
void image_2d(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, std::nullptr_t){ |
|||
bind(target); |
|||
gl::texture_image_2d(target, level, internalformat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
|||
} |
|||
|
|||
//! creates empty texture
|
|||
static texture create(GLsizei width, GLsizei height, GLint internalformat = GL_RGBA, GLenum target = GL_TEXTURE_2D){ |
|||
texture t; |
|||
t.image_2d(target, 0, internalformat, width, height, nullptr); |
|||
return t; |
|||
} |
|||
|
|||
private: |
|||
GLuint id = 0; |
|||
}; |
|||
//! creates empty texture
|
|||
inline texture create_texture(GLsizei width, GLsizei height, GLint internalformat = GL_RGBA, GLenum target = GL_TEXTURE_2D){ |
|||
texture t; |
|||
t.image_2d(target, 0, internalformat, width, height, nullptr); |
|||
return t; |
|||
} |
|||
|
|||
} |
|||
|
@ -1,10 +1,13 @@ |
|||
#version 330 |
|||
|
|||
in vec4 position; |
|||
uniform mat4 modelmatrix; |
|||
out float mx; |
|||
|
|||
void main(){ |
|||
gl_Position = vec4(1); |
|||
gl_Position.xy = 0.04*position.xy; |
|||
gl_Position = 0.04*modelmatrix*(position - vec4(0.0, 0.0, 25.0, 0.0)); |
|||
|
|||
// reset depth |
|||
gl_Position.zw = vec2(1.0); |
|||
mx = cos(0.1337*position.z); |
|||
} |
|||
|
Reference in new issue