2013-12-18 10:12:15 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-18 10:12:15 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-11-05 01:14:22 +08:00
|
|
|
|
|
|
|
|
2013-11-12 03:54:08 +08:00
|
|
|
#ifndef __CC_RENDERER_H_
|
|
|
|
#define __CC_RENDERER_H_
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <stack>
|
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCRenderCommand.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "platform/CCGL.h"
|
2013-11-08 07:48:37 +08:00
|
|
|
|
2013-11-05 01:14:22 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
class EventListenerCustom;
|
2014-01-22 15:18:27 +08:00
|
|
|
class QuadCommand;
|
2014-08-28 09:58:39 +08:00
|
|
|
class TrianglesCommand;
|
2014-06-23 23:58:45 +08:00
|
|
|
class MeshCommand;
|
2013-12-31 10:54:37 +08:00
|
|
|
|
2014-03-07 08:14:06 +08:00
|
|
|
/** Class that knows how to sort `RenderCommand` objects.
|
|
|
|
Since the commands that have `z == 0` are "pushed back" in
|
|
|
|
the correct order, the only `RenderCommand` objects that need to be sorted,
|
|
|
|
are the ones that have `z < 0` and `z > 0`.
|
2014-01-18 15:10:04 +08:00
|
|
|
*/
|
|
|
|
class RenderQueue {
|
|
|
|
|
|
|
|
public:
|
|
|
|
void push_back(RenderCommand* command);
|
|
|
|
ssize_t size() const;
|
|
|
|
void sort();
|
2014-01-19 03:35:27 +08:00
|
|
|
RenderCommand* operator[](ssize_t index) const;
|
2014-01-18 15:10:04 +08:00
|
|
|
void clear();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<RenderCommand*> _queueNegZ;
|
|
|
|
std::vector<RenderCommand*> _queue0;
|
|
|
|
std::vector<RenderCommand*> _queuePosZ;
|
|
|
|
};
|
2013-11-14 09:31:12 +08:00
|
|
|
|
2014-09-05 14:59:14 +08:00
|
|
|
//render queue for transparency object, NOTE that the _globalOrder of RenderCommand is the distance to the camera when added to the transparent queue
|
2014-08-27 17:53:46 +08:00
|
|
|
class TransparentRenderQueue {
|
|
|
|
public:
|
|
|
|
void push_back(RenderCommand* command);
|
2014-08-28 10:53:57 +08:00
|
|
|
ssize_t size() const
|
|
|
|
{
|
|
|
|
return _queueCmd.size();
|
|
|
|
}
|
2014-08-27 17:53:46 +08:00
|
|
|
void sort();
|
|
|
|
RenderCommand* operator[](ssize_t index) const;
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<RenderCommand*> _queueCmd;
|
|
|
|
};
|
|
|
|
|
2013-11-14 09:31:12 +08:00
|
|
|
struct RenderStackElement
|
|
|
|
{
|
|
|
|
int renderQueueID;
|
2014-01-17 07:02:39 +08:00
|
|
|
ssize_t currentIndex;
|
2013-11-14 09:31:12 +08:00
|
|
|
};
|
|
|
|
|
2014-04-12 13:01:42 +08:00
|
|
|
class GroupCommandManager;
|
|
|
|
|
2014-03-07 08:14:06 +08:00
|
|
|
/* Class responsible for the rendering in.
|
|
|
|
|
|
|
|
Whenever possible prefer to use `QuadCommand` objects since the renderer will automatically batch them.
|
2014-02-08 11:37:44 +08:00
|
|
|
*/
|
2014-07-14 17:02:06 +08:00
|
|
|
class CC_DLL Renderer
|
2013-11-05 01:14:22 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-09-05 16:01:17 +08:00
|
|
|
static const int VBO_SIZE = 65536;
|
|
|
|
static const int INDEX_VBO_SIZE = VBO_SIZE * 6 / 4;
|
2014-08-27 14:54:35 +08:00
|
|
|
|
2014-01-22 18:24:23 +08:00
|
|
|
static const int BATCH_QUADCOMMAND_RESEVER_SIZE = 64;
|
2013-12-18 09:50:17 +08:00
|
|
|
|
|
|
|
Renderer();
|
|
|
|
~Renderer();
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
//TODO: manage GLView inside Render itself
|
2013-11-22 08:36:19 +08:00
|
|
|
void initGLView();
|
2014-04-04 12:48:16 +08:00
|
|
|
|
2014-03-07 08:14:06 +08:00
|
|
|
/** Adds a `RenderComamnd` into the renderer */
|
2013-11-16 09:32:29 +08:00
|
|
|
void addCommand(RenderCommand* command);
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Adds a `RenderComamnd` into the renderer specifying a particular render queue ID */
|
2013-11-16 09:32:29 +08:00
|
|
|
void addCommand(RenderCommand* command, int renderQueue);
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Pushes a group into the render queue */
|
2013-11-21 03:05:01 +08:00
|
|
|
void pushGroup(int renderQueueID);
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Pops a group from the render queue */
|
2013-11-21 03:05:01 +08:00
|
|
|
void popGroup();
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Creates a render queue and returns its Id */
|
2013-11-16 03:29:11 +08:00
|
|
|
int createRenderQueue();
|
2014-03-07 08:14:06 +08:00
|
|
|
|
|
|
|
/** Renders into the GLView all the queued `RenderCommand` objects */
|
2013-11-05 01:14:22 +08:00
|
|
|
void render();
|
|
|
|
|
2014-04-04 12:48:16 +08:00
|
|
|
/** Cleans all `RenderCommand`s in the queue */
|
|
|
|
void clean();
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
/* returns the number of drawn batches in the last frame */
|
|
|
|
ssize_t getDrawnBatches() const { return _drawnBatches; }
|
|
|
|
/* RenderCommands (except) QuadCommand should update this value */
|
|
|
|
void addDrawnBatches(ssize_t number) { _drawnBatches += number; };
|
|
|
|
/* returns the number of drawn triangles in the last frame */
|
|
|
|
ssize_t getDrawnVertices() const { return _drawnVertices; }
|
|
|
|
/* RenderCommands (except) QuadCommand should update this value */
|
|
|
|
void addDrawnVertices(ssize_t number) { _drawnVertices += number; };
|
2014-09-02 11:12:15 +08:00
|
|
|
/* clear draw stats */
|
|
|
|
void clearDrawStats() { _drawnBatches = _drawnVertices = 0; }
|
2014-02-08 11:37:44 +08:00
|
|
|
|
2014-04-12 13:01:42 +08:00
|
|
|
inline GroupCommandManager* getGroupCommandManager() const { return _groupCommandManager; };
|
2014-04-15 07:46:19 +08:00
|
|
|
|
|
|
|
/** returns whether or not a rectangle is visible or not */
|
2014-05-15 01:07:09 +08:00
|
|
|
bool checkVisibility(const Mat4& transform, const Size& size);
|
2014-04-15 07:46:19 +08:00
|
|
|
|
2013-11-05 01:14:22 +08:00
|
|
|
protected:
|
2013-11-08 07:48:37 +08:00
|
|
|
|
2013-12-05 09:02:02 +08:00
|
|
|
//Setup VBO or VAO based on OpenGL extensions
|
|
|
|
void setupBuffer();
|
2013-11-08 08:50:53 +08:00
|
|
|
void setupVBOAndVAO();
|
2013-12-05 09:02:02 +08:00
|
|
|
void setupVBO();
|
|
|
|
void mapBuffers();
|
|
|
|
|
2013-11-22 08:36:19 +08:00
|
|
|
void drawBatchedQuads();
|
2014-01-16 06:35:26 +08:00
|
|
|
|
2013-11-09 04:06:39 +08:00
|
|
|
//Draw the previews queued quads and flush previous context
|
|
|
|
void flush();
|
2014-04-07 22:51:32 +08:00
|
|
|
|
2014-06-23 23:58:45 +08:00
|
|
|
void flush2D();
|
|
|
|
|
2014-06-23 19:08:26 +08:00
|
|
|
void flush3D();
|
|
|
|
|
2014-04-07 22:51:32 +08:00
|
|
|
void visitRenderQueue(const RenderQueue& queue);
|
2014-08-28 10:53:57 +08:00
|
|
|
|
|
|
|
void visitTransparentRenderQueue(const TransparentRenderQueue& queue);
|
2013-11-09 04:06:39 +08:00
|
|
|
|
2014-08-28 09:58:39 +08:00
|
|
|
void fillVerticesAndIndices(const TrianglesCommand* cmd);
|
2014-01-16 06:35:26 +08:00
|
|
|
|
2013-12-17 15:32:24 +08:00
|
|
|
std::stack<int> _commandGroupStack;
|
2013-11-21 03:05:01 +08:00
|
|
|
|
2013-12-17 15:32:24 +08:00
|
|
|
std::vector<RenderQueue> _renderGroups;
|
2014-08-27 17:53:46 +08:00
|
|
|
TransparentRenderQueue _transparentRenderGroups; //transparency objects
|
2013-11-14 09:31:12 +08:00
|
|
|
|
2014-03-31 13:47:07 +08:00
|
|
|
uint32_t _lastMaterialID;
|
2013-11-08 08:50:53 +08:00
|
|
|
|
2014-06-23 23:58:45 +08:00
|
|
|
MeshCommand* _lastBatchedMeshCommand;
|
2014-08-28 09:58:39 +08:00
|
|
|
std::vector<TrianglesCommand*> _batchedCommands;
|
2014-01-22 15:18:27 +08:00
|
|
|
|
2014-08-27 14:54:35 +08:00
|
|
|
V3F_C4B_T2F _verts[VBO_SIZE];
|
|
|
|
GLushort _indices[INDEX_VBO_SIZE];
|
2013-11-23 02:24:52 +08:00
|
|
|
GLuint _quadVAO;
|
2013-11-08 08:50:53 +08:00
|
|
|
GLuint _buffersVBO[2]; //0: vertex 1: indices
|
|
|
|
|
2014-08-27 14:54:35 +08:00
|
|
|
int _filledVertex;
|
|
|
|
int _filledIndex;
|
2013-11-22 08:36:19 +08:00
|
|
|
|
|
|
|
bool _glViewAssigned;
|
2014-02-08 11:37:44 +08:00
|
|
|
|
|
|
|
// stats
|
|
|
|
ssize_t _drawnBatches;
|
|
|
|
ssize_t _drawnVertices;
|
2014-04-11 17:31:35 +08:00
|
|
|
//the flag for checking whether renderer is rendering
|
2014-04-11 16:05:22 +08:00
|
|
|
bool _isRendering;
|
2013-12-31 10:54:37 +08:00
|
|
|
|
2014-04-12 13:01:42 +08:00
|
|
|
GroupCommandManager* _groupCommandManager;
|
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
EventListenerCustom* _cacheTextureListener;
|
|
|
|
#endif
|
2013-11-05 01:14:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2013-11-12 03:54:08 +08:00
|
|
|
#endif //__CC_RENDERER_H_
|