2013-11-05 01:14:22 +08:00
|
|
|
//
|
|
|
|
// Created by NiTe Luo on 10/31/13.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Renderer.h"
|
|
|
|
|
|
|
|
#include "CCGL.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "CCShaderCache.h"
|
|
|
|
#include "ccGLStateCache.h"
|
|
|
|
#include "CCGLProgram.h"
|
2013-11-07 06:57:42 +08:00
|
|
|
#include "QuadCommand.h"
|
2013-11-05 01:14:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
static Renderer* s_renderer;
|
|
|
|
|
|
|
|
Renderer *Renderer::getInstance()
|
|
|
|
{
|
|
|
|
if(!s_renderer)
|
|
|
|
{
|
|
|
|
s_renderer = new Renderer();
|
|
|
|
}
|
|
|
|
return s_renderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
Renderer::Renderer()
|
2013-11-08 02:09:53 +08:00
|
|
|
:_lastMaterialID(0)
|
2013-11-05 01:14:22 +08:00
|
|
|
{
|
|
|
|
_shaderProgram = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::addRenderCommand(RenderCommand *command)
|
|
|
|
{
|
2013-11-06 09:00:34 +08:00
|
|
|
command->generateID();
|
2013-11-05 01:14:22 +08:00
|
|
|
_renderQueue.push_back(command);
|
|
|
|
}
|
|
|
|
|
2013-11-06 09:00:34 +08:00
|
|
|
bool compareRenderCommand(RenderCommand* a, RenderCommand* b)
|
|
|
|
{
|
|
|
|
return a->getID() < b->getID();
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:14:22 +08:00
|
|
|
void Renderer::render()
|
|
|
|
{
|
2013-11-07 06:57:42 +08:00
|
|
|
//Uncomment this once everything is rendered by new renderer
|
2013-11-05 01:14:22 +08:00
|
|
|
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2013-11-07 08:39:20 +08:00
|
|
|
//TODO setup camera or MVP
|
|
|
|
|
2013-11-05 01:14:22 +08:00
|
|
|
//Process render commands
|
|
|
|
//1. Sort render commands based on ID
|
2013-11-06 09:00:34 +08:00
|
|
|
stable_sort(_renderQueue.begin(), _renderQueue.end(), compareRenderCommand);
|
2013-11-05 01:14:22 +08:00
|
|
|
|
|
|
|
//2. Process commands
|
|
|
|
for(vector<RenderCommand*>::iterator it = _renderQueue.begin(); it != _renderQueue.end(); ++it)
|
|
|
|
{
|
|
|
|
//TODO: Perform Sprite batching here
|
|
|
|
auto command = *it;
|
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
switch(command->getType())
|
|
|
|
{
|
|
|
|
case QUAD_COMMAND:
|
|
|
|
{
|
|
|
|
QuadCommand* cmd = (QuadCommand*)command;
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
//Set Shader
|
|
|
|
_shaderProgram->use();
|
|
|
|
_shaderProgram->setUniformsForBuiltins();
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
//Set Blend Mode
|
|
|
|
//Set texture
|
|
|
|
GL::bindTexture2D(cmd->getTextureID());
|
|
|
|
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
#define kQuadSize sizeof(cmd->getQuad()->bl)
|
|
|
|
long offset = (long)cmd->getQuad();
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
// vertex
|
|
|
|
int diff = offsetof( V3F_C4B_T2F, vertices);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
// texCoods
|
|
|
|
diff = offsetof( V3F_C4B_T2F, texCoords);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
// color
|
|
|
|
diff = offsetof( V3F_C4B_T2F, colors);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
|
2013-11-05 01:14:22 +08:00
|
|
|
|
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
CC_INCREMENT_GL_DRAWS(1);
|
2013-11-05 01:14:22 +08:00
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-11-05 01:14:22 +08:00
|
|
|
|
|
|
|
delete command;
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderQueue.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|