openspades/Sources/Draw/GLImageRenderer.cpp
2013-08-18 16:18:06 +09:00

158 lines
3.9 KiB
C++

//
// GLImageRenderer.cpp
// OpenSpades
//
// Created by yvt on 7/18/13.
// Copyright (c) 2013 yvt.jp. All rights reserved.
//
#include "GLImageRenderer.h"
#include "GLRenderer.h"
#include "GLImage.h"
#include "IGLDevice.h"
#include "../Core/Debug.h"
#include "../Core/Debug.h"
#include "../Core/Exception.h"
namespace spades{
namespace draw {
GLImageRenderer::GLImageRenderer(GLRenderer *r):
renderer(r),
device(r->GetGLDevice()){
SPADES_MARK_FUNCTION();
image = NULL;
program = renderer->RegisterProgram("Shaders/BasicImage.program");
program->Use();
positionAttribute = new GLProgramAttribute
("positionAttribute");
colorAttribute = new GLProgramAttribute
("colorAttribute");
textureCoordAttribute = new GLProgramAttribute
("textureCoordAttribute");
screenSize = new GLProgramUniform
("screenSize");
textureSize = new GLProgramUniform
("textureSize");
texture = new GLProgramUniform
("texture");
(*positionAttribute)(program);
(*colorAttribute)(program);
(*textureCoordAttribute)(program);
(*screenSize)(program);
(*textureSize)(program);
(*texture)(program);
}
GLImageRenderer::~GLImageRenderer(){
delete positionAttribute;
delete colorAttribute;
delete textureCoordAttribute;
delete screenSize;
delete textureSize;
delete texture;
}
void GLImageRenderer::Flush(){
if(vertices.empty())
return;
SPADES_MARK_FUNCTION();
SPAssert(image);
program->Use();
device->ActiveTexture(0);
image->Bind(IGLDevice::Texture2D);
device->VertexAttribPointer((*positionAttribute)(),
2, IGLDevice::FloatType,
false, sizeof(ImageVertex),
vertices.data());
device->VertexAttribPointer((*colorAttribute)(),
4, IGLDevice::FloatType,
false, sizeof(ImageVertex),
(const char*)vertices.data() + sizeof(float) * 4);
device->VertexAttribPointer((*textureCoordAttribute)(),
2, IGLDevice::FloatType,
false, sizeof(ImageVertex),
(const char*)vertices.data() + sizeof(float) * 2);
device->EnableVertexAttribArray((*positionAttribute)(), true);
device->EnableVertexAttribArray((*colorAttribute)(), true);
device->EnableVertexAttribArray((*textureCoordAttribute)(), true);
screenSize->SetValue((float)device->ScreenWidth(),
(float)device->ScreenHeight());
textureSize->SetValue(image->GetWidth(),
image->GetHeight());
texture->SetValue(0);
device->DrawElements(IGLDevice::Triangles,
indices.size(),
IGLDevice::UnsignedInt,
indices.data());
device->EnableVertexAttribArray((*positionAttribute)(), false);
device->EnableVertexAttribArray((*colorAttribute)(), false);
device->EnableVertexAttribArray((*textureCoordAttribute)(), false);
vertices.clear();
indices.clear();
}
void GLImageRenderer::SetImage(spades::draw::GLImage *img){
if(img == image)
return;
Flush();
image = img;
}
void GLImageRenderer::Add(float dx1, float dy1,
float dx2, float dy2,
float dx3, float dy3,
float dx4, float dy4,
float sx1, float sy1,
float sx2, float sy2,
float sx3, float sy3,
float sx4, float sy4,
float r, float g, float b,
float a){
ImageVertex v;
v.r = r; v.g = g;
v.b = b; v.a = a;
uint32_t idx = (uint32_t)vertices.size();
v.x = dx1; v.y = dy1;
v.u = sx1; v.v = sy1;
vertices.push_back(v);
v.x = dx2; v.y = dy2;
v.u = sx2; v.v = sy2;
vertices.push_back(v);
v.x = dx3; v.y = dy3;
v.u = sx3; v.v = sy3;
vertices.push_back(v);
v.x = dx4; v.y = dy4;
v.u = sx4; v.v = sy4;
vertices.push_back(v);
indices.push_back(idx);
indices.push_back(idx+1);
indices.push_back(idx+2);
indices.push_back(idx);
indices.push_back(idx+2);
indices.push_back(idx+3);
}
}
}