2012-09-10 18:10:59 +00:00
|
|
|
#include "platform.h"
|
2013-05-13 19:53:28 +00:00
|
|
|
#include "Renderer.h"
|
2012-09-10 18:10:59 +00:00
|
|
|
#include GLHEADER
|
2012-08-29 18:53:53 +00:00
|
|
|
#include <iostream>
|
2013-10-04 23:24:41 +00:00
|
|
|
#include "resources/Font.h"
|
2012-09-04 16:45:16 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
2013-06-14 15:48:13 +00:00
|
|
|
#include <stack>
|
2014-03-19 20:03:23 +00:00
|
|
|
#include "Util.h"
|
2012-08-29 18:53:53 +00:00
|
|
|
|
|
|
|
namespace Renderer {
|
2013-07-10 11:29:43 +00:00
|
|
|
std::stack<Eigen::Vector4i> clipStack;
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2012-10-17 18:21:56 +00:00
|
|
|
void setColor4bArray(GLubyte* array, unsigned int color)
|
2012-08-29 18:53:53 +00:00
|
|
|
{
|
2013-06-27 10:31:16 +00:00
|
|
|
array[0] = (color & 0xff000000) >> 24;
|
|
|
|
array[1] = (color & 0x00ff0000) >> 16;
|
|
|
|
array[2] = (color & 0x0000ff00) >> 8;
|
2012-10-17 18:21:56 +00:00
|
|
|
array[3] = (color & 0x000000ff);
|
2012-08-29 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
2012-10-17 18:21:56 +00:00
|
|
|
void buildGLColorArray(GLubyte* ptr, unsigned int color, unsigned int vertCount)
|
2012-08-29 18:53:53 +00:00
|
|
|
{
|
2015-03-03 22:36:21 +00:00
|
|
|
unsigned int colorGl;
|
|
|
|
setColor4bArray((GLubyte*)&colorGl, color);
|
2012-08-29 18:53:53 +00:00
|
|
|
for(unsigned int i = 0; i < vertCount; i++)
|
|
|
|
{
|
2015-03-03 22:36:21 +00:00
|
|
|
((GLuint*)ptr)[i] = colorGl;
|
2012-08-29 18:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void pushClipRect(Eigen::Vector2i pos, Eigen::Vector2i dim)
|
2013-06-02 19:34:50 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector4i box(pos.x(), pos.y(), dim.x(), dim.y());
|
|
|
|
if(box[2] == 0)
|
|
|
|
box[2] = Renderer::getScreenWidth() - box.x();
|
|
|
|
if(box[3] == 0)
|
|
|
|
box[3] = Renderer::getScreenHeight() - box.y();
|
2013-06-02 19:34:50 +00:00
|
|
|
|
2013-06-14 15:48:13 +00:00
|
|
|
//glScissor starts at the bottom left of the window
|
|
|
|
//so (0, 0, 1, 1) is the bottom left pixel
|
|
|
|
//everything else uses y+ = down, so flip it to be consistent
|
2013-07-10 11:29:43 +00:00
|
|
|
//rect.pos.y = Renderer::getScreenHeight() - rect.pos.y - rect.size.y;
|
|
|
|
box[1] = Renderer::getScreenHeight() - box.y() - box[3];
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
//make sure the box fits within clipStack.top(), and clip further accordingly
|
|
|
|
if(clipStack.size())
|
|
|
|
{
|
|
|
|
Eigen::Vector4i& top = clipStack.top();
|
|
|
|
if(top[0] > box[0])
|
|
|
|
box[0] = top[0];
|
|
|
|
if(top[1] > box[1])
|
|
|
|
box[1] = top[1];
|
|
|
|
if(top[0] + top[2] < box[0] + box[2])
|
|
|
|
box[2] = (top[0] + top[2]) - box[0];
|
|
|
|
if(top[1] + top[3] < box[1] + box[3])
|
|
|
|
box[3] = (top[1] + top[3]) - box[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(box[2] < 0)
|
|
|
|
box[2] = 0;
|
|
|
|
if(box[3] < 0)
|
|
|
|
box[3] = 0;
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
clipStack.push(box);
|
|
|
|
glScissor(box[0], box[1], box[2], box[3]);
|
2013-06-02 22:33:49 +00:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:48:13 +00:00
|
|
|
void popClipRect()
|
2013-06-02 22:33:49 +00:00
|
|
|
{
|
2013-06-14 15:48:13 +00:00
|
|
|
if(clipStack.empty())
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Tried to popClipRect while the stack was empty!";
|
|
|
|
return;
|
|
|
|
}
|
2013-07-10 11:29:43 +00:00
|
|
|
|
2013-06-14 15:48:13 +00:00
|
|
|
clipStack.pop();
|
|
|
|
if(clipStack.empty())
|
|
|
|
{
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}else{
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector4i top = clipStack.top();
|
|
|
|
glScissor(top[0], top[1], top[2], top[3]);
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
2013-06-02 22:33:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 20:03:23 +00:00
|
|
|
void drawRect(float x, float y, float w, float h, unsigned int color, GLenum blend_sfactor, GLenum blend_dfactor)
|
|
|
|
{
|
|
|
|
drawRect((int)round(x), (int)round(y), (int)round(w), (int)round(h), color, blend_sfactor, blend_dfactor);
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
void drawRect(int x, int y, int w, int h, unsigned int color, GLenum blend_sfactor, GLenum blend_dfactor)
|
2012-08-29 18:53:53 +00:00
|
|
|
{
|
2013-05-15 07:50:59 +00:00
|
|
|
#ifdef USE_OPENGL_ES
|
|
|
|
GLshort points[12];
|
|
|
|
#else
|
2013-05-13 19:53:28 +00:00
|
|
|
GLint points[12];
|
2013-05-15 07:50:59 +00:00
|
|
|
#endif
|
2012-08-29 18:53:53 +00:00
|
|
|
|
|
|
|
points[0] = x; points [1] = y;
|
|
|
|
points[2] = x; points[3] = y + h;
|
|
|
|
points[4] = x + w; points[5] = y;
|
|
|
|
|
|
|
|
points[6] = x + w; points[7] = y;
|
|
|
|
points[8] = x; points[9] = y + h;
|
|
|
|
points[10] = x + w; points[11] = y + h;
|
|
|
|
|
|
|
|
GLubyte colors[6*4];
|
2012-10-17 18:21:56 +00:00
|
|
|
buildGLColorArray(colors, color, 6);
|
2012-08-29 18:53:53 +00:00
|
|
|
|
2012-10-17 18:32:01 +00:00
|
|
|
glEnable(GL_BLEND);
|
2014-03-01 21:02:44 +00:00
|
|
|
glBlendFunc(blend_sfactor, blend_dfactor);
|
2012-08-29 18:53:53 +00:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
2012-10-17 18:32:01 +00:00
|
|
|
|
2013-05-15 07:50:59 +00:00
|
|
|
#ifdef USE_OPENGL_ES
|
|
|
|
glVertexPointer(2, GL_SHORT, 0, points);
|
|
|
|
#else
|
2013-05-13 19:53:28 +00:00
|
|
|
glVertexPointer(2, GL_INT, 0, points);
|
2013-05-15 07:50:59 +00:00
|
|
|
#endif
|
2012-08-29 18:53:53 +00:00
|
|
|
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
2013-08-21 17:40:39 +00:00
|
|
|
glDisable(GL_BLEND);
|
2012-08-29 18:53:53 +00:00
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
2013-08-21 17:40:39 +00:00
|
|
|
glDisableClientState(GL_COLOR_ARRAY);
|
2012-08-29 18:53:53 +00:00
|
|
|
}
|
2013-07-10 11:29:43 +00:00
|
|
|
|
|
|
|
void setMatrix(float* matrix)
|
|
|
|
{
|
|
|
|
glLoadMatrixf(matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMatrix(const Eigen::Affine3f& matrix)
|
|
|
|
{
|
|
|
|
setMatrix((float*)matrix.data());
|
|
|
|
}
|
2012-08-29 18:53:53 +00:00
|
|
|
};
|