axmol/cocos/2d/renderer/Renderer.cpp

119 lines
3.1 KiB
C++
Raw Normal View History

//
// 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"
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()
:_currentMaterialID(0)
{
_shaderProgram = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
}
void Renderer::setOpenGLView(EGLView *newOpenGLView)
{
//TODO move render related logic to render
_openGLView = newOpenGLView;
}
void Renderer::addRenderCommand(RenderCommand *command)
{
2013-11-06 09:00:34 +08:00
command->generateID();
_renderQueue.push_back(command);
}
2013-11-06 09:00:34 +08:00
bool compareRenderCommand(RenderCommand* a, RenderCommand* b)
{
return a->getID() < b->getID();
}
void Renderer::render()
{
2013-11-07 06:57:42 +08:00
//Uncomment this once everything is rendered by new renderer
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//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);
//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;
//Transform
kmGLMatrixMode(KM_GL_MODELVIEW);
kmGLLoadMatrix(cmd->getTransform());
2013-11-07 06:57:42 +08:00
//Set Shader
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();
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-07 06:57:42 +08:00
#define kQuadSize sizeof(cmd->getQuad()->bl)
long offset = (long)cmd->getQuad();
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-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-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-07 06:57:42 +08:00
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2013-11-07 06:57:42 +08:00
CHECK_GL_ERROR_DEBUG();
CC_INCREMENT_GL_DRAWS(1);
2013-11-07 06:57:42 +08:00
kmGLLoadIdentity();
break;
}
default:
break;
}
delete command;
}
_renderQueue.clear();
}
NS_CC_END