use "mspgl";
};
- program "texturing"
- {
- source "demos/texturing.cpp";
- use "mspgl";
- };
-
program "cubemap"
{
source "demos/cubemap.cpp";
+++ /dev/null
-#include <cmath>
-#include <msp/graphics/simplewindow.h>
-#include <msp/gl/framebuffer.h>
-#include <msp/gl/matrix.h>
-#include <msp/gl/mesh.h>
-#include <msp/gl/meshbuilder.h>
-#include <msp/gl/texenv.h>
-#include <msp/gl/texture2d.h>
-#include <msp/gl/texturing.h>
-#include <msp/time/timestamp.h>
-#include <msp/time/utils.h>
-#include <msp/time/units.h>
-
-using namespace Msp;
-
-int main()
-{
- Graphics::SimpleGLWindow wnd(400, 400);
-
- GL::Texture2D tex1;
- GL::Texture2D tex2;
-
- unsigned char *data = new unsigned char[256*256*4];
- for(unsigned y=0; y<256; ++y)
- for(unsigned x=0; x<256; ++x)
- {
- unsigned i = (x+y*256)*3;
- data[i] = 255;
- data[i+1] = (((x/32)+(y/32))&1 ? 255 : 0);
- data[i+2] = 0;
- }
- tex1.storage(GL::RGB, 256, 256);
- tex1.set_min_filter(GL::LINEAR);
- tex1.image(0, GL::RGB, GL::UNSIGNED_BYTE, data);
-
- for(unsigned y=0; y<256; ++y)
- for(unsigned x=0; x<256; ++x)
- {
- unsigned i = (x+y*256)*4;
- data[i] = data[i+1] = data[i+2] = 0;
- data[i+3] = (((x/32)+(y/32))&1 ? 255 : 0);
- }
- tex2.storage(GL::RGBA, 256, 256);
- tex2.set_min_filter(GL::LINEAR);
- tex2.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data);
- delete[] data;
-
- GL::Texturing texturing;
- texturing.attach(0, tex1);
- GL::TexEnv texenv;
- texenv.set_mode(GL::DECAL);
- texturing.attach(1, tex2, texenv);
-
- GL::MatrixStack::projection() = GL::Matrix::frustum_centered(0.2, 0.2, 0.2, 10);
- GL::MatrixStack::modelview() = GL::Matrix::translation(0, 0, -3);
- GL::MatrixStack::modelview() *= GL::Matrix::rotation(-45, 1, 0, 0);
-
- GL::Mesh mesh((GL::TEXCOORD2, GL::TEXCOORD2,1, GL::VERTEX3));
- GL::MeshBuilder bld(mesh);
- bld.begin(GL::QUADS);
- bld.texcoord(0, 0);
- bld.multitexcoord(1, -0.2071, 0.5);
- bld.vertex(-1, -1);
- bld.texcoord(1, 0);
- bld.multitexcoord(1, 0.5, -0.2071);
- bld.vertex(1, -1);
- bld.texcoord(1, 1);
- bld.multitexcoord(1, 1.2071, 0.5);
- bld.vertex(1, 1);
- bld.texcoord(0, 1);
- bld.multitexcoord(1, 0.5, 1.2071);
- bld.vertex(-1, 1);
- bld.end();
-
- wnd.show();
- float angle = 0;
- Time::TimeStamp last;
- while(1)
- {
- for(unsigned i=0; i<4; ++i)
- {
- float *v = mesh.modify_vertex(i);
- v[2] = 0.5+sin(angle)*0.1+cos(angle+i*M_PI/2)*0.7071;
- v[3] = 0.5+cos(angle)*0.1+sin(angle+i*M_PI/2)*0.7071;
- }
- wnd.get_display().tick();
- GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT);
- {
- GL::Bind bind_tex(texturing);
- mesh.draw();
- }
- wnd.swap_buffers();
- Time::TimeStamp t = Time::now();
- if(last)
- angle += 0.5*((t-last)/Time::sec);
- last = t;
- }
-
- return 0;
-}
{
if(other_type!=prim_type)
return false;
- else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN || prim_type==POLYGON)
+ else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
return NV_primitive_restart;
else
return true;
{
if(other.prim_type!=prim_type)
throw invalid_argument("Batch::append");
- if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN || prim_type==POLYGON)
+ if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
static Require _req(NV_primitive_restart);
if(other.data.empty())
+++ /dev/null
-#include "clip.h"
-#include "gl.h"
-
-namespace Msp {
-namespace GL {
-
-ClipPlane::ClipPlane(double a, double b, double c, double d)
-{
- eq[0] = a;
- eq[1] = b;
- eq[2] = c;
- eq[3] = d;
-}
-
-void ClipPlane::apply_to(unsigned n)
-{
- glClipPlane(GL_CLIP_PLANE0+n, eq);
- glEnable(GL_CLIP_PLANE0+n);
-}
-
-void ClipPlane::disable(unsigned n)
-{
- glDisable(GL_CLIP_PLANE0+n);
-}
-
-} // namespace GL
-} // namespace Msp
+++ /dev/null
-#ifndef MSP_GL_CLIP_H_
-#define MSP_GL_CLIP_H_
-
-namespace Msp {
-namespace GL {
-
-class ClipPlane
-{
-public:
- double eq[4];
-
- ClipPlane(double, double, double, double);
- void apply_to(unsigned);
-
- static void disable(unsigned);
-};
-
-} // namespace GL
-} // namespace Msp
-
-#endif
UNSIGNED_SHORT = GL_UNSIGNED_SHORT,
INT = GL_INT,
UNSIGNED_INT = GL_UNSIGNED_INT,
- FLOAT = GL_FLOAT,
- DOUBLE = GL_DOUBLE
+ FLOAT = GL_FLOAT
};
} // namespace GL
{
COLOR_BUFFER_BIT = GL_COLOR_BUFFER_BIT,
DEPTH_BUFFER_BIT = GL_DEPTH_BUFFER_BIT,
- STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT,
- ACCUM_BUFFER_BIT = GL_ACCUM_BUFFER_BIT
+ STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT
};
enum RWBuffer
void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
{
- if(conv.get()=="COLOR_INDEX")
- fmt = COLOR_INDEX;
- else if(conv.get()=="STENCIL_INDEX")
+ if(conv.get()=="STENCIL_INDEX")
fmt = STENCIL_INDEX;
else if(conv.get()=="DEPTH_COMPONENT")
fmt = DEPTH_COMPONENT;
- else if(conv.get()=="RED")
- fmt = RED;
- else if(conv.get()=="GREEN")
- fmt = GREEN;
- else if(conv.get()=="BLUE")
- fmt = BLUE;
- else if(conv.get()=="ALPHA")
- fmt = ALPHA;
else if(conv.get()=="RGB")
fmt = RGB;
else if(conv.get()=="RGBA")
{
switch(pf)
{
- case Graphics::COLOR_INDEX: return COLOR_INDEX;
case Graphics::LUMINANCE: return LUMINANCE;
case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
case Graphics::RGB: return RGB;
{
switch(get_base_pixelformat(pf))
{
- case COLOR_INDEX:
case STENCIL_INDEX:
case DEPTH_COMPONENT:
- case RED:
- case GREEN:
- case BLUE:
case LUMINANCE:
case SLUMINANCE:
return 1;
enum PixelFormat
{
- COLOR_INDEX = GL_COLOR_INDEX,
STENCIL_INDEX = GL_STENCIL_INDEX,
DEPTH_COMPONENT = GL_DEPTH_COMPONENT,
- RED = GL_RED,
- GREEN = GL_GREEN,
- BLUE = GL_BLUE,
- ALPHA = GL_ALPHA,
RGB = GL_RGB,
RGBA = GL_RGBA,
RGB8 = GL_RGB8,
pt = QUADS;
else if(conv.get()=="QUAD_STRIP")
pt = QUAD_STRIP;
- else if(conv.get()=="POLYGON")
- pt = POLYGON;
else
throw lexical_error(format("conversion of '%s' to PrimitiveType", conv.get()));
}
TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
TRIANGLE_FAN = GL_TRIANGLE_FAN,
QUADS = GL_QUADS,
- QUAD_STRIP = GL_QUAD_STRIP,
- POLYGON = GL_POLYGON
+ QUAD_STRIP = GL_QUAD_STRIP
};
void operator>>(const LexicalConverter &, PrimitiveType &);
{ FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", 0 },
{ VERTEX, "gl_Position", "vec4", "projection_matrix*eye_vertex", 0 },
- { VERTEX, "shd_vertex", "vec3", "vec3(dot(eye_vertex, gl_EyePlaneS[shadow_unit]), dot(eye_vertex, gl_EyePlaneT[shadow_unit]), dot(eye_vertex, gl_EyePlaneR[shadow_unit]))", "g" },
- { VERTEX, "shd_vertex", "vec3", "(shd_eye_matrix*eye_vertex).xyz", "!g" },
+ { VERTEX, "shd_vertex", "vec3", "(shd_eye_matrix*eye_vertex).xyz", 0 },
{ VERTEX, "tbn_sky_dir", "vec3", "eye_sky_dir*eye_tbn_matrix", "n" },
{ VERTEX, "tbn_light_dir[i]", "vec3", "eye_light_dir[i]*eye_tbn_matrix", 0 },
{ VERTEX, "eye_light_dir[i]", "vec3", "normalize(eye_light_position[i].xyz-eye_vertex.xyz*eye_light_position[i].w)", 0 },
{ ATTRIBUTE, "tangent", "vec3", 0, 0 },
{ ATTRIBUTE, "binormal", "vec3", 0, 0 },
- { UNIFORM, "ShadowMap::shadow_unit", "int", 0, 0 },
{ UNIFORM, "texture", "sampler2D", 0, 0 },
{ UNIFORM, "shadow", "sampler2DShadow", 0, 0 },
{ UNIFORM, "ShadowMap::shadow_darkness", "float", 0, 0 },
#include "program.h"
#include "programdata.h"
#include "renderer.h"
-#include "texenv.h"
#include "texture.h"
#include "texture2d.h"
#include "texturing.h"
{
if(tex)
{
- if(env)
- obj.attach(index, *tex, *env);
- else
- obj.attach(index, *tex);
+ obj.attach(index, *tex);
tex.release();
- env.release();
}
}
-void RenderPass::TextureLoader::texenv()
-{
- throw runtime_error("TexEnvs can't be loaded yet");
- /*env = new TexEnv;
- load_sub(*env);*/
-}
-
void RenderPass::TextureLoader::texture(const string &name)
{
tex = &get_collection().get<Texture>(name);
class Program;
class ProgramData;
class Renderer;
-class TexEnv;
class Texture;
class Texturing;
private:
unsigned index;
RefPtr<Texture> tex;
- RefPtr<TexEnv> env;
public:
TextureLoader(Texturing &, unsigned, Collection *);
private:
virtual void finish();
- void texenv();
void texture(const std::string &);
void texture2d();
};
#include "scene.h"
#include "shadowmap.h"
#include "tests.h"
-#include "texgen.h"
#include "texunit.h"
using namespace std;
unsigned unit = renderer.allocate_effect_texunit();
int iunit = unit;
shdata.uniform("shadow", iunit);
- shdata.uniform("shadow_unit", iunit);
Bind _bind_depth(depth_buf, unit);
- TexGen tg_s, tg_t, tg_r;
- tg_s.set_plane(Vector4(shadow_matrix(0, 0), shadow_matrix(0, 1), shadow_matrix(0, 2), shadow_matrix(0, 3)));
- tg_t.set_plane(Vector4(shadow_matrix(1, 0), shadow_matrix(1, 1), shadow_matrix(1, 2), shadow_matrix(1, 3)));
- tg_r.set_plane(Vector4(shadow_matrix(2, 0), shadow_matrix(2, 1), shadow_matrix(2, 2), shadow_matrix(2, 3)));
- tg_s.bind_to(unit, SCOORD);
- tg_t.bind_to(unit, TCOORD);
- tg_r.bind_to(unit, RCOORD);
if(const Camera *camera = renderer.get_camera())
/* Multiply by camera's object matrix to form a matrix that transforms
renderer.add_shader_data(shdata);
renderer.render(renderable, tag);
-
- TexGen::unbind_from(unit, SCOORD);
- TexGen::unbind_from(unit, TCOORD);
- TexGen::unbind_from(unit, RCOORD);
}
} // namespace GL
namespace Msp {
namespace GL {
-AlphaTest::AlphaTest():
- pred(ALWAYS),
- ref(0)
-{ }
-
-AlphaTest::AlphaTest(Predicate p, float r):
- pred(p),
- ref(r)
-{ }
-
-void AlphaTest::bind() const
-{
- if(set_current(this))
- {
- glEnable(GL_ALPHA_TEST);
- glAlphaFunc(pred, ref);
- }
-}
-
-void AlphaTest::unbind()
-{
- if(set_current(0))
- glDisable(GL_ALPHA_TEST);
-}
-
-
DepthTest::DepthTest():
write(true),
pred(LESS)
namespace Msp {
namespace GL {
-/**
-Tests incoming fragment alpha values against a reference. If the test fails,
-the fragment is discarded.
-*/
-class AlphaTest: public Bindable<AlphaTest>
-{
-private:
- Predicate pred;
- float ref;
-
-public:
- AlphaTest();
- AlphaTest(Predicate, float);
-
- void bind() const;
-
- static void unbind();
-};
-
-
/**
Tests incoming fragment depth values against the depth buffer. If the test
fails, the fragment is discarded.
+++ /dev/null
-#include "texenv.h"
-#include "texunit.h"
-
-namespace Msp {
-namespace GL {
-
-TexEnv::TexEnv():
- mode(MODULATE),
- color(0, 0, 0, 0)
-{ }
-
-TexEnv::~TexEnv()
-{
- if(this!=&default_object())
- {
- while(TexUnit *unit = TexUnit::find_unit(this))
- unbind_from(unit->get_index());
- }
-}
-
-const TexEnv &TexEnv::default_object()
-{
- static TexEnv obj;
- return obj;
-}
-
-void TexEnv::update_parameter(int mask) const
-{
- if(TexUnit::current().get_texenv()!=this)
- {
- TexUnit *unit = TexUnit::find_unit(this);
- if(!unit)
- return;
-
- unit->bind();
- }
-
- if(mask&MODE)
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode);
- if(mask&COLOR)
- glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &color.r);
-}
-
-void TexEnv::set_mode(TexEnvMode m)
-{
- mode = m;
- update_parameter(MODE);
-}
-
-void TexEnv::set_color(const Color &c)
-{
- color = c;
- update_parameter(COLOR);
-}
-
-void TexEnv::bind_to(unsigned i) const
-{
- TexUnit &unit = TexUnit::get_unit(i);
- if(unit.set_texenv(this))
- {
- unit.bind();
- update_parameter(-1);
- }
-}
-
-const TexEnv *TexEnv::current(unsigned i)
-{
- return TexUnit::get_unit(i).get_texenv();
-}
-
-void TexEnv::unbind_from(unsigned i)
-{
- default_object().bind_to(i);
-}
-
-} // namespace GL
-} // namespace Msp
+++ /dev/null
-#ifndef MSP_GL_TEXENV_H_
-#define MSP_GL_TEXENV_H_
-
-#include "color.h"
-#include "gl.h"
-
-namespace Msp {
-namespace GL {
-
-enum TexEnvMode
-{
- REPLACE = GL_REPLACE,
- MODULATE = GL_MODULATE,
- DECAL = GL_DECAL,
- BLEND = GL_BLEND,
- ADD = GL_ADD,
- COMBINE = GL_COMBINE
-};
-
-class TexEnv
-{
-private:
- enum ParameterMask
- {
- MODE = 1,
- COLOR = 2
- };
-
- TexEnvMode mode;
- Color color;
-
-public:
- TexEnv();
- ~TexEnv();
-
- static const TexEnv &default_object();
-
-private:
- void update_parameter(int) const;
-
-public:
- void set_mode(TexEnvMode);
- void set_color(const Color &);
- TexEnvMode get_mode() const { return mode; }
- const Color &get_color() const { return color; }
- void bind() const { bind_to(0); }
- void bind_to(unsigned) const;
-
- static const TexEnv *current(unsigned = 0);
- static void unbind() { unbind_from(0); }
- static void unbind_from(unsigned);
-};
-
-} // namespace GL
-} // namespace Msp
-
-#endif
+++ /dev/null
-#include <stdexcept>
-#include "misc.h"
-#include "texgen.h"
-#include "texunit.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-TexGen::TexGen():
- mode(EYE_LINEAR)
-{ }
-
-TexGen::~TexGen()
-{
- // TODO unbind
-}
-
-void TexGen::set_mode(TexGenMode m)
-{
- mode = m;
-}
-
-void TexGen::set_plane(const Vector4 &p)
-{
- plane = p;
-}
-
-void TexGen::bind_to(unsigned i, TexCoordComponent c) const
-{
- TexUnit &unit = TexUnit::get_unit(i);
- if(unit.set_texgen(coord_index(c), this))
- {
- unit.bind();
- glTexGeni(c, GL_TEXTURE_GEN_MODE, mode);
- if(mode==EYE_LINEAR)
- glTexGenfv(c, GL_EYE_PLANE, &plane.x);
- else if(mode==OBJECT_LINEAR)
- glTexGenfv(c, GL_OBJECT_PLANE, &plane.x);
- enable(GL_TEXTURE_GEN_S+coord_index(c));
- }
-}
-
-const TexGen *TexGen::current(unsigned i, TexCoordComponent c)
-{
- return TexUnit::get_unit(i).get_texgen(coord_index(c));
-}
-
-void TexGen::unbind_from(unsigned i, TexCoordComponent c)
-{
- TexUnit &unit = TexUnit::get_unit(i);
- if(unit.set_texgen(coord_index(c), 0))
- {
- unit.bind();
- disable(GL_TEXTURE_GEN_S+coord_index(c));
- }
-}
-
-unsigned TexGen::coord_index(TexCoordComponent c)
-{
- switch(c)
- {
- case SCOORD: return 0;
- case TCOORD: return 1;
- case RCOORD: return 2;
- case QCOORD: return 3;
- default: throw invalid_argument("TexGen::coord_index");
- }
-}
-
-} // namespace GL
-} // namespace Msp
+++ /dev/null
-#ifndef MSP_GL_TEXGEN_H_
-#define MSP_GL_TEXGEN_H_
-
-#include "gl.h"
-#include "vector.h"
-
-namespace Msp {
-namespace GL {
-
-enum TexCoordComponent
-{
- SCOORD = GL_S,
- TCOORD = GL_T,
- RCOORD = GL_R,
- QCOORD = GL_Q
-};
-
-enum TexGenMode
-{
- EYE_LINEAR = GL_EYE_LINEAR,
- OBJECT_LINEAR = GL_OBJECT_LINEAR,
- REFLECTION_MAP = GL_REFLECTION_MAP,
- NORMAL_MAP = GL_NORMAL_MAP
-};
-
-class TexGen
-{
-private:
- TexGenMode mode;
- Vector4 plane;
-
-public:
- TexGen();
- ~TexGen();
-
- void set_mode(TexGenMode);
- void set_plane(const Vector4 &);
-
- void bind_to(TexCoordComponent c) const { bind_to(0, c); }
- void bind_to(unsigned, TexCoordComponent) const;
-
- static const TexGen *current(TexCoordComponent c) { return current(0, c); }
- static const TexGen *current(unsigned, TexCoordComponent);
- static void unbind_from(TexCoordComponent c) { unbind_from(0, c); }
- static void unbind_from(unsigned, TexCoordComponent);
-private:
- static unsigned coord_index(TexCoordComponent);
-};
-
-} // namespace GL
-} // namespace Msp
-
-#endif
-#include "texenv.h"
#include "texture.h"
#include "texturing.h"
#include "texunit.h"
void Texturing::attach(unsigned attch, const Texture &tex)
{
- set_attachment(attch, &tex, 0);
-}
-
-void Texturing::attach(unsigned attch, const Texture &tex, const TexEnv &env)
-{
- set_attachment(attch, &tex, &env);
+ set_attachment(attch, &tex);
}
void Texturing::detach(unsigned attch)
{
- set_attachment(attch, 0, 0);
+ set_attachment(attch, 0);
}
-void Texturing::set_attachment(unsigned attch, const Texture *tex, const TexEnv *env)
+void Texturing::set_attachment(unsigned attch, const Texture *tex)
{
if(attch>=TexUnit::get_n_units())
throw out_of_range("Texturing::set_attachment");
attachments.resize(attch+1);
attachments[attch].tex = tex;
- attachments[attch].env = env;
if(current()==this)
bind_attachment(attch);
attch.tex->bind_to(i);
else
Texture::unbind_from(i);
- if(attch.env)
- attch.env->bind_to(i);
- else
- TexEnv::unbind_from(i);
}
void Texturing::unbind_attachment(unsigned i)
{
Texture::unbind_from(i);
- TexEnv::unbind_from(i);
}
void Texturing::bind() const
Texturing::Attachment::Attachment():
- tex(0),
- env(0)
+ tex(0)
{ }
} // namespace GL
namespace Msp {
namespace GL {
-class TexEnv;
class Texture;
class Texturing: public Bindable<Texturing>
struct Attachment
{
const Texture *tex;
- const TexEnv *env;
Attachment();
};
~Texturing();
void attach(unsigned, const Texture &);
- void attach(unsigned, const Texture &, const TexEnv &);
void detach(unsigned);
private:
- void set_attachment(unsigned, const Texture *, const TexEnv *);
+ void set_attachment(unsigned, const Texture *);
void bind_attachment(unsigned) const;
static void unbind_attachment(unsigned);
TexUnit *TexUnit::cur_unit = 0;
TexUnit::TexUnit():
- texture(0),
- texenv(0)
-{
- fill(texgen, texgen+4, static_cast<const TexGen *>(0));
-}
+ texture(0)
+{ }
bool TexUnit::set_texture(const Texture *tex)
{
return result;
}
-bool TexUnit::set_texenv(const TexEnv *env)
-{
- bool result = (texenv!=env);
- texenv = env;
- return result;
-}
-
-bool TexUnit::set_texgen(unsigned i, const TexGen *gen)
-{
- if(i>=4)
- throw out_of_range("TexUnit::set_texgen");
- bool result = (texgen[i]!=gen);
- texgen[i] = gen;
- return result;
-}
-
-const TexGen *TexUnit::get_texgen(unsigned i)
-{
- if(i>=4)
- throw out_of_range("TexUnit::get_texgen");
- return texgen[i];
-}
-
void TexUnit::bind()
{
if(cur_unit!=this && (cur_unit || index))
return 0;
}
-TexUnit *TexUnit::find_unit(const TexEnv *env)
-{
- for(vector<TexUnit>::iterator i=units.begin(); i!=units.end(); ++i)
- if(i->texenv==env)
- return &*i;
- return 0;
-}
-
} // namespace GL
} // namespace Msp
private:
unsigned index;
const Texture *texture;
- const TexEnv *texenv;
- const TexGen *texgen[4];
static std::vector<TexUnit> units;
static TexUnit *cur_unit;
unsigned get_index() const { return index; }
bool set_texture(const Texture *);
const Texture *get_texture() const { return texture; }
- bool set_texenv(const TexEnv *);
- const TexEnv *get_texenv() const { return texenv; }
- bool set_texgen(unsigned, const TexGen *);
- const TexGen *get_texgen(unsigned);
void bind();
static unsigned get_n_units();
static TexUnit &get_unit(unsigned);
static TexUnit ¤t();
static TexUnit *find_unit(const Texture *);
- static TexUnit *find_unit(const TexEnv *);
};
} // namespace GL