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-18 15:10:04 +08:00
|
|
|
/** Class that knows how to sort the Commands.
|
|
|
|
Since the commands that have z==0 are "pushed back" in
|
|
|
|
the correct order, the only Commands that need to be sorted,
|
|
|
|
are the ones that have z <0 and z >0.
|
|
|
|
And that is what this class does.
|
|
|
|
*/
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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;
|
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();
|
|
|
|
|
2013-11-08 02:09:53 +08:00
|
|
|
//TODO support multiple viewport
|
2013-11-16 09:32:29 +08:00
|
|
|
void addCommand(RenderCommand* command);
|
|
|
|
void addCommand(RenderCommand* command, int renderQueue);
|
2013-11-21 03:05:01 +08:00
|
|
|
void pushGroup(int renderQueueID);
|
|
|
|
void popGroup();
|
|
|
|
|
2013-11-16 03:29:11 +08:00
|
|
|
int createRenderQueue();
|
2013-11-05 01:14:22 +08:00
|
|
|
void render();
|
|
|
|
|
|
|
|
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-01-16 06:35:26 +08:00
|
|
|
void convertToWorldCoordiantes(V3F_C4B_T2F_Quad* quads, ssize_t quantity, const kmMat4& modelView);
|
|
|
|
|
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::stack<RenderStackElement> _renderStack;
|
|
|
|
std::vector<RenderQueue> _renderGroups;
|
2013-11-14 09:31:12 +08:00
|
|
|
|
2014-01-17 07:02:39 +08:00
|
|
|
uint32_t _lastMaterialID;
|
2013-11-08 08:50:53 +08:00
|
|
|
|
2014-01-17 07:02:39 +08:00
|
|
|
ssize_t _firstCommand;
|
|
|
|
ssize_t _lastCommand;
|
2013-11-11 16:14:29 +08:00
|
|
|
|
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;
|
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_
|