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
|
|
|
|
|
|
|
#include "CCPlatformMacros.h"
|
2013-12-18 10:41:09 +08:00
|
|
|
#include "CCRenderCommand.h"
|
2013-11-05 01:14:22 +08:00
|
|
|
#include "CCGLProgram.h"
|
2013-11-09 04:06:39 +08:00
|
|
|
#include "CCGL.h"
|
2013-11-12 03:54:08 +08:00
|
|
|
#include <vector>
|
2013-11-14 09:31:12 +08:00
|
|
|
#include <stack>
|
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;
|
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
|
|
|
|
|
|
|
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
|
|
|
*/
|
2013-12-31 10:54:37 +08:00
|
|
|
class Renderer
|
2013-11-05 01:14:22 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-12-19 05:52:10 +08:00
|
|
|
static const int VBO_SIZE = 65536 / 6;
|
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();
|
|
|
|
|
2013-11-22 08:36:19 +08:00
|
|
|
//TODO manage GLView inside Render itself
|
|
|
|
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-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 */
|
|
|
|
bool checkVisibility(const kmMat4& transform, const Size& size);
|
|
|
|
|
2013-11-05 01:14:22 +08:00
|
|
|
protected:
|
2013-11-08 07:48:37 +08:00
|
|
|
|
2013-11-08 08:50:53 +08:00
|
|
|
void setupIndices();
|
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
|
|
|
|
|
|
|
void visitRenderQueue(const RenderQueue& queue);
|
2013-11-09 04:06:39 +08:00
|
|
|
|
2014-04-09 11:53:01 +08:00
|
|
|
void convertToWorldCoordinates(V3F_C4B_T2F_Quad* quads, ssize_t quantity, const Matrix& modelView);
|
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;
|
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-01-22 15:18:27 +08:00
|
|
|
std::vector<QuadCommand*> _batchedQuadCommands;
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
V3F_C4B_T2F_Quad _quads[VBO_SIZE];
|
|
|
|
GLushort _indices[6 * 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
|
|
|
|
|
2013-11-08 07:48:37 +08:00
|
|
|
int _numQuads;
|
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_
|