-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
#ifndef MSP_GL_MESH_H_
#define MSP_GL_MESH_H_
-#include <msp/datafile/loader.h>
+#include <msp/datafile/objectloader.h>
#include "batch.h"
+#include "resource.h"
#include "vertexarray.h"
+#include "vertexsetup.h"
+#include "windingtest.h"
namespace Msp {
namespace GL {
-class Mesh
+class Buffer;
+class Renderer;
+
+/**
+Raw mesh data, consisting of a VertexArray and one or more Batches. Though a
+Mesh can draw itself, it's usually used as part of Renderables rather than on
+its own.
+*/
+class Mesh: public Resource
{
friend class MeshBuilder;
public:
- class Loader: public DataFile::Loader
+ class Loader: public DataFile::ObjectLoader<Mesh>
{
- public:
- Loader(Mesh &);
private:
- Mesh &mesh;
+ bool allow_gl_calls;
- void vertices(VertexFormat);
+ public:
+ Loader(Mesh &, bool = true);
+ private:
+ void vertices(const std::vector<VertexComponent> &);
void batch(PrimitiveType);
+ void winding(FaceWinding);
};
private:
+ class AsyncLoader: public Resource::AsyncLoader
+ {
+ private:
+ Mesh &mesh;
+ IO::Seekable &io;
+ Bufferable::AsyncUpdater *vertex_updater;
+ Bufferable::AsyncUpdater *index_updater;
+ unsigned phase;
+
+ public:
+ AsyncLoader(Mesh &, IO::Seekable &);
+ ~AsyncLoader();
+
+ virtual bool needs_sync() const;
+ virtual bool process();
+ };
+
+ enum BufferMask
+ {
+ VERTEX_BUFFER = 1,
+ INDEX_BUFFER = 2
+ };
+
VertexArray vertices;
- std::list<Batch> batches;
+ std::vector<Batch> batches;
+ Buffer *vbuf;
+ Buffer *ibuf;
+ VertexSetup vtx_setup;
+ mutable unsigned short dirty;
+ bool disallow_rendering;
+ const WindingTest *winding;
public:
- Mesh();
- Mesh(VertexFormat f);
+ Mesh(ResourceManager * = 0);
+ Mesh(const VertexFormat &, ResourceManager * = 0);
+private:
+ void init(ResourceManager *);
+public:
+ ~Mesh();
+
+ void clear();
+private:
+ void check_buffers(unsigned);
- void use_vertex_buffer(bool);
+public:
const VertexArray &get_vertices() const { return vertices; }
+ const VertexSetup &get_vertex_setup() const { return vtx_setup; }
+ const Buffer *get_index_buffer() const { return ibuf; }
+ unsigned get_n_vertices() const;
+ float *modify_vertex(unsigned);
+
void add_batch(const Batch &b);
- const std::list<Batch> &get_batches() { return batches; }
- void clear();
- void draw() const;
+ const std::vector<Batch> &get_batches() const { return batches; }
+
+ void set_winding(const WindingTest *);
+
+ void draw(Renderer &) const;
+ void draw_instanced(Renderer &, const VertexSetup &, unsigned) const;
+private:
+ void draw(Renderer &, const VertexSetup *, unsigned) const;
+ void resize_buffers() const;
+
+public:
+ virtual int get_load_priority() const { return 1; }
+ virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
+ virtual UInt64 get_data_size() const;
+ virtual void unload();
};
} // namespace GL