Cleaned up some rendering code.

This commit is contained in:
Leon Styhre 2021-08-19 21:39:01 +02:00
parent 9546eb00ba
commit 91879c9b4a
4 changed files with 218 additions and 218 deletions

View file

@ -392,9 +392,9 @@ namespace Renderer
destroyWindow();
}
void pushClipRect(const glm::ivec2& _pos, const glm::ivec2& _size)
void pushClipRect(const glm::ivec2& pos, const glm::ivec2& size)
{
Rect box(_pos.x, _pos.y, _size.x, _size.y);
Rect box(pos.x, pos.y, size.x, size.y);
if (box.w == 0)
box.w = screenWidth - box.x;
@ -457,51 +457,51 @@ namespace Renderer
setScissor(clipStack.top());
}
void drawRect(const float _x,
const float _y,
const float _w,
const float _h,
void drawRect(const float x,
const float y,
const float w,
const float h,
const unsigned int _color,
const unsigned int _colorEnd,
bool horizontalGradient,
const float _opacity,
const glm::mat4& _trans,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor)
const float opacity,
const glm::mat4& trans,
const Blend::Factor srcBlendFactor,
const Blend::Factor dstBlendFactor)
{
const unsigned int color = convertRGBAToABGR(_color);
const unsigned int colorEnd = convertRGBAToABGR(_colorEnd);
const unsigned int rColor = convertRGBAToABGR(_color);
const unsigned int rColorEnd = convertRGBAToABGR(_colorEnd);
Vertex vertices[4];
float _wL = _w;
float _hL = _h;
float wL = w;
float hL = h;
// If the width or height was scaled down to less than 1 pixel, then set it to
// 1 pixel so that it will still render on lower resolutions.
if (_wL > 0.0f && _wL < 1.0f)
_wL = 1.0f;
if (_hL > 0.0f && _hL < 1.0f)
_hL = 1.0f;
if (wL > 0.0f && wL < 1.0f)
wL = 1.0f;
if (hL > 0.0f && hL < 1.0f)
hL = 1.0f;
// clang-format off
vertices[0] = {{_x , _y }, {0.0f, 0.0f}, color};
vertices[1] = {{_x , _y + _hL}, {0.0f, 0.0f}, horizontalGradient ? colorEnd : color};
vertices[2] = {{_x + _wL, _y }, {0.0f, 0.0f}, horizontalGradient ? color : colorEnd};
vertices[3] = {{_x + _wL, _y + _hL}, {0.0f, 0.0f}, colorEnd};
vertices[0] = {{x, y }, {0.0f, 0.0f}, rColor};
vertices[1] = {{x, y + hL}, {0.0f, 0.0f}, horizontalGradient ? rColorEnd : rColor};
vertices[2] = {{x + wL, y }, {0.0f, 0.0f}, horizontalGradient ? rColor : rColorEnd};
vertices[3] = {{x + wL, y + hL}, {0.0f, 0.0f}, rColorEnd};
// clang-format on
// Round vertices.
for (int i = 0; i < 4; i++)
vertices[i].pos = glm::round(vertices[i].pos);
if (_opacity < 1.0) {
if (opacity < 1.0) {
vertices[0].shaders = SHADER_OPACITY;
vertices[0].opacity = _opacity;
vertices[0].opacity = opacity;
}
else {
bindTexture(0);
}
drawTriangleStrips(vertices, 4, _trans, _srcBlendFactor, _dstBlendFactor);
drawTriangleStrips(vertices, 4, trans, srcBlendFactor, dstBlendFactor);
}
unsigned int convertRGBAToABGR(const unsigned int _color)

View file

@ -20,12 +20,12 @@ struct SDL_Window;
namespace Renderer
{
const unsigned int SHADER_DESATURATE = 1;
const unsigned int SHADER_OPACITY = 2;
const unsigned int SHADER_DIM = 4;
const unsigned int SHADER_BLUR_HORIZONTAL = 8;
const unsigned int SHADER_BLUR_VERTICAL = 16;
const unsigned int SHADER_SCANLINES = 32;
const unsigned int SHADER_DESATURATE{1};
const unsigned int SHADER_OPACITY{2};
const unsigned int SHADER_DIM{4};
const unsigned int SHADER_BLUR_HORIZONTAL{8};
const unsigned int SHADER_BLUR_VERTICAL{16};
const unsigned int SHADER_SCANLINES{32};
struct shaderParameters {
std::array<GLfloat, 2> textureSize;
@ -36,12 +36,12 @@ namespace Renderer
unsigned int blurPasses;
shaderParameters()
: textureSize({0.0f, 0.0f})
, textureCoordinates({0.0f, 0.0f, 0.0f, 0.0f})
, fragmentSaturation(1.0f)
, fragmentDimValue(0.4f)
, fragmentOpacity(1.0f)
, blurPasses(1)
: textureSize{0.0f, 0.0f}
, textureCoordinates{0.0f, 0.0f, 0.0f, 0.0f}
, fragmentSaturation{1.0f}
, fragmentDimValue{0.4f}
, fragmentOpacity{1.0f}
, blurPasses{1}
{
}
};
@ -75,33 +75,33 @@ namespace Renderer
namespace Blend
{
enum Factor {
ZERO = 0,
ONE = 1,
SRC_COLOR = 2,
ONE_MINUS_SRC_COLOR = 3,
SRC_ALPHA = 4,
ONE_MINUS_SRC_ALPHA = 5,
DST_COLOR = 6,
ONE_MINUS_DST_COLOR = 7,
DST_ALPHA = 8,
ONE_MINUS_DST_ALPHA = 9
ZERO,
ONE,
SRC_COLOR,
ONE_MINUS_SRC_COLOR,
SRC_ALPHA,
ONE_MINUS_SRC_ALPHA,
DST_COLOR,
ONE_MINUS_DST_COLOR,
DST_ALPHA,
ONE_MINUS_DST_ALPHA
};
}
namespace Texture
{
enum Type {
RGBA = 0, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
ALPHA = 1
RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
ALPHA
};
}
struct Rect {
Rect(const int _x, const int _y, const int _w, const int _h)
: x(_x)
, y(_y)
, w(_w)
, h(_h)
Rect(const int xValue, const int yValue, const int wValue, const int hValue)
: x(xValue)
, y(yValue)
, w(wValue)
, h(hValue)
{
}
int x;
@ -112,35 +112,35 @@ namespace Renderer
struct Vertex {
Vertex() {}
Vertex(const glm::vec2& _pos, const glm::vec2& _tex, const unsigned int _col)
: pos(_pos)
, tex(_tex)
, col(_col)
Vertex(const glm::vec2& position, const glm::vec2& textureCoord, const unsigned int color)
: pos(position)
, tex(textureCoord)
, col(color)
{
}
glm::vec2 pos;
glm::vec2 tex;
unsigned int col;
float saturation = 1.0;
float opacity = 1.0;
unsigned int shaders = 0;
float saturation{1.0};
float opacity{1.0};
unsigned int shaders{0};
};
bool init();
void deinit();
void pushClipRect(const glm::ivec2& _pos, const glm::ivec2& _size);
void pushClipRect(const glm::ivec2& pos, const glm::ivec2& size);
void popClipRect();
void drawRect(const float _x,
const float _y,
const float _w,
const float _h,
const unsigned int _color,
const unsigned int _colorEnd,
void drawRect(const float x,
const float y,
const float w,
const float h,
const unsigned int color,
const unsigned int colorEnd,
bool horizontalGradient = false,
const float _opacity = 1.0,
const glm::mat4& _trans = getIdentity(),
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
const float opacity = 1.0,
const glm::mat4& trans = getIdentity(),
const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
SDL_Window* getSDLWindow();
int getWindowWidth();
int getWindowHeight();
@ -174,29 +174,29 @@ namespace Renderer
const unsigned int width,
const unsigned int height,
void* data);
void destroyTexture(const unsigned int _texture);
void updateTexture(const unsigned int _texture,
const Texture::Type _type,
const unsigned int _x,
const unsigned _y,
const unsigned int _width,
const unsigned int _height,
void* _data);
void bindTexture(const unsigned int _texture);
void drawLines(const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
void drawTriangleStrips(const Vertex* _vertices,
const unsigned int _numVertices,
const glm::mat4& _trans = getIdentity(),
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA,
const shaderParameters& _parameters = shaderParameters());
void setProjection(const glm::mat4& _projection);
void setMatrix(const glm::mat4& _matrix);
void setViewport(const Rect& _viewport);
void setScissor(const Rect& _scissor);
void destroyTexture(const unsigned int texture);
void updateTexture(const unsigned int texture,
const Texture::Type type,
const unsigned int x,
const unsigned y,
const unsigned int width,
const unsigned int height,
void* data);
void bindTexture(const unsigned int texture);
void drawLines(const Vertex* vertices,
const unsigned int numVertices,
const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
void drawTriangleStrips(const Vertex* vertices,
const unsigned int numVertices,
const glm::mat4& trans = getIdentity(),
const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA,
const Blend::Factor dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA,
const shaderParameters& parameters = shaderParameters());
void setProjection(const glm::mat4& projection);
void setMatrix(const glm::mat4& matrix);
void setViewport(const Rect& viewport);
void setScissor(const Rect& scissor);
void setSwapInterval();
void swapBuffers();

View file

@ -195,133 +195,133 @@ namespace Renderer
return texture;
}
void destroyTexture(const unsigned int _texture)
void destroyTexture(const unsigned int texture)
{
GL_CHECK_ERROR(glDeleteTextures(1, &_texture));
GL_CHECK_ERROR(glDeleteTextures(1, &texture));
}
void updateTexture(const unsigned int _texture,
const Texture::Type _type,
const unsigned int _x,
const unsigned _y,
const unsigned int _width,
const unsigned int _height,
void* _data)
void updateTexture(const unsigned int texture,
const Texture::Type type,
const unsigned int x,
const unsigned y,
const unsigned int width,
const unsigned int height,
void* data)
{
const GLenum type = convertTextureType(_type);
const GLenum textureType = convertTextureType(type);
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, _width, _height, type,
GL_UNSIGNED_BYTE, _data));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, textureType,
GL_UNSIGNED_BYTE, data));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
}
void bindTexture(const unsigned int _texture)
void bindTexture(const unsigned int texture)
{
if (_texture == 0)
if (texture == 0)
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
else
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
}
void drawLines(const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor)
void drawLines(const Vertex* vertices,
const unsigned int numVertices,
const Blend::Factor srcBlendFactor,
const Blend::Factor dstBlendFactor)
{
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col));
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].col));
GL_CHECK_ERROR(
glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor)));
glBlendFunc(convertBlendFactor(srcBlendFactor), convertBlendFactor(dstBlendFactor)));
GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, _numVertices));
GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, numVertices));
}
void drawTriangleStrips(const Vertex* _vertices,
const unsigned int _numVertices,
const glm::mat4& _trans,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor,
const shaderParameters& _parameters)
void drawTriangleStrips(const Vertex* vertices,
const unsigned int numVertices,
const glm::mat4& trans,
const Blend::Factor srcBlendFactor,
const Blend::Factor dstBlendFactor,
const shaderParameters& parameters)
{
float width = _vertices[3].pos[0];
float height = _vertices[3].pos[1];
float width = vertices[3].pos[0];
float height = vertices[3].pos[1];
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col));
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].col));
GL_CHECK_ERROR(
glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor)));
glBlendFunc(convertBlendFactor(srcBlendFactor), convertBlendFactor(dstBlendFactor)));
#if defined(USE_OPENGL_21)
if (_vertices[0].shaders == 0) {
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
if (vertices[0].shaders == 0) {
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
}
else {
// If saturation is set below the maximum (default) value, run the
// desaturation shader.
if (_vertices->saturation < 1.0f || _parameters.fragmentSaturation < 1.0f) {
if (vertices->saturation < 1.0f || parameters.fragmentSaturation < 1.0f) {
Shader* runShader = getShaderProgram(SHADER_DESATURATE);
// Only try to use the shader if it has been loaded properly.
if (runShader) {
runShader->activateShaders();
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * _trans);
runShader->setSaturation(_vertices->saturation);
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
runShader->setSaturation(vertices->saturation);
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
runShader->deactivateShaders();
}
}
if (_vertices->shaders & SHADER_OPACITY) {
if (vertices->shaders & SHADER_OPACITY) {
Shader* runShader = getShaderProgram(SHADER_OPACITY);
if (runShader) {
runShader->activateShaders();
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * _trans);
_vertices->opacity < 1.0f ? runShader->setOpacity(_vertices->opacity) :
runShader->setOpacity(_parameters.fragmentOpacity);
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
vertices->opacity < 1.0f ? runShader->setOpacity(vertices->opacity) :
runShader->setOpacity(parameters.fragmentOpacity);
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
runShader->deactivateShaders();
}
}
// Check if any other shaders are set to be used and if so, run them.
if (_vertices->shaders & SHADER_DIM) {
if (vertices->shaders & SHADER_DIM) {
Shader* runShader = getShaderProgram(SHADER_DIM);
if (runShader) {
runShader->activateShaders();
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * _trans);
runShader->setDimValue(_parameters.fragmentDimValue);
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
runShader->setDimValue(parameters.fragmentDimValue);
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
runShader->deactivateShaders();
}
}
if (_vertices->shaders & SHADER_BLUR_HORIZONTAL) {
if (vertices->shaders & SHADER_BLUR_HORIZONTAL) {
Shader* runShader = getShaderProgram(SHADER_BLUR_HORIZONTAL);
if (runShader) {
runShader->activateShaders();
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * _trans);
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
runShader->setTextureSize({width, height});
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
runShader->deactivateShaders();
}
}
if (_vertices->shaders & SHADER_BLUR_VERTICAL) {
if (vertices->shaders & SHADER_BLUR_VERTICAL) {
Shader* runShader = getShaderProgram(SHADER_BLUR_VERTICAL);
if (runShader) {
runShader->activateShaders();
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * _trans);
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
runShader->setTextureSize({width, height});
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
runShader->deactivateShaders();
}
}
if (_vertices->shaders & SHADER_SCANLINES) {
if (vertices->shaders & SHADER_SCANLINES) {
Shader* runShader = getShaderProgram(SHADER_SCANLINES);
float shaderWidth = width * 1.2f;
// Scale the scanlines relative to screen resolution.
@ -345,9 +345,9 @@ namespace Renderer
}
if (runShader) {
runShader->activateShaders();
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * _trans);
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
runShader->setTextureSize({shaderWidth, shaderHeight});
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
runShader->deactivateShaders();
}
}
@ -355,37 +355,37 @@ namespace Renderer
#endif
}
void setProjection(const glm::mat4& _projection)
void setProjection(const glm::mat4& projection)
{
GL_CHECK_ERROR(glMatrixMode(GL_PROJECTION));
GL_CHECK_ERROR(glLoadMatrixf(reinterpret_cast<const GLfloat*>(&_projection)));
GL_CHECK_ERROR(glLoadMatrixf(reinterpret_cast<const GLfloat*>(&projection)));
}
void setMatrix(const glm::mat4& _matrix)
void setMatrix(const glm::mat4& matrix)
{
glm::mat4 matrix{_matrix};
matrix[3] = glm::round(matrix[3]);
glm::mat4 newMatrix{matrix};
newMatrix[3] = glm::round(newMatrix[3]);
GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW));
GL_CHECK_ERROR(glLoadMatrixf(reinterpret_cast<const GLfloat*>(&matrix)));
GL_CHECK_ERROR(glLoadMatrixf(reinterpret_cast<const GLfloat*>(&newMatrix)));
}
void setViewport(const Rect& _viewport)
void setViewport(const Rect& viewport)
{
// glViewport starts at the bottom left of the window.
GL_CHECK_ERROR(glViewport(_viewport.x, getWindowHeight() - _viewport.y - _viewport.h,
_viewport.w, _viewport.h));
GL_CHECK_ERROR(glViewport(viewport.x, getWindowHeight() - viewport.y - viewport.h,
viewport.w, viewport.h));
}
void setScissor(const Rect& _scissor)
void setScissor(const Rect& scissor)
{
if ((_scissor.x == 0) && (_scissor.y == 0) && (_scissor.w == 0) && (_scissor.h == 0)) {
if ((scissor.x == 0) && (scissor.y == 0) && (scissor.w == 0) && (scissor.h == 0)) {
GL_CHECK_ERROR(glDisable(GL_SCISSOR_TEST));
}
else {
// glScissor starts at the bottom left of the window.
GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() - _scissor.y - _scissor.h,
_scissor.w, _scissor.h));
GL_CHECK_ERROR(glScissor(scissor.x, getWindowHeight() - scissor.y - scissor.h,
scissor.w, scissor.h));
GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST));
}
}

View file

@ -140,98 +140,98 @@ namespace Renderer
return texture;
}
void destroyTexture(const unsigned int _texture)
void destroyTexture(const unsigned int texture)
{
GL_CHECK_ERROR(glDeleteTextures(1, &_texture));
GL_CHECK_ERROR(glDeleteTextures(1, &texture));
}
void updateTexture(const unsigned int _texture,
const Texture::Type _type,
const unsigned int _x,
const unsigned _y,
const unsigned int _width,
const unsigned int _height,
void* _data)
void updateTexture(const unsigned int texture,
const Texture::Type type,
const unsigned int x,
const unsigned y,
const unsigned int width,
const unsigned int height,
void* data)
{
const GLenum type = convertTextureType(_type);
const GLenum textureType = convertTextureType(type);
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, _width, _height, type,
GL_UNSIGNED_BYTE, _data));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, textureType,
GL_UNSIGNED_BYTE, data));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
}
void bindTexture(const unsigned int _texture)
void bindTexture(const unsigned int texture)
{
if (_texture == 0)
if (texture == 0)
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
else
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
}
void drawLines(const Vertex* _vertices,
const unsigned int _numVertices,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor)
void drawLines(const Vertex* vertices,
const unsigned int numVertices,
const Blend::Factor srcBlendFactor,
const Blend::Factor dstBlendFactor)
{
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col));
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].col));
GL_CHECK_ERROR(
glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor)));
glBlendFunc(convertBlendFactor(srcBlendFactor), convertBlendFactor(dstBlendFactor)));
GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, _numVertices));
GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, numVertices));
}
void drawTriangleStrips(const Vertex* _vertices,
const unsigned int _numVertices,
const glm::mat4& _trans,
const Blend::Factor _srcBlendFactor,
const Blend::Factor _dstBlendFactor,
const shaderParameters& _parameters)
void drawTriangleStrips(const Vertex* vertices,
const unsigned int numVertices,
const glm::mat4& trans,
const Blend::Factor srcBlendFactor,
const Blend::Factor dstBlendFactor,
const shaderParameters& parameters)
{
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col));
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].pos));
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].tex));
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].col));
GL_CHECK_ERROR(
glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor)));
glBlendFunc(convertBlendFactor(srcBlendFactor), convertBlendFactor(dstBlendFactor)));
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices));
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
}
void setProjection(const glm::mat4& _projection)
void setProjection(const glm::mat4& projection)
{
GL_CHECK_ERROR(glMatrixMode(GL_PROJECTION));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&_projection));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&projection));
}
void setMatrix(const glm::mat4& _matrix)
void setMatrix(const glm::mat4& matrix)
{
glm::mat4 matrix{_matrix};
matrix[3] = glm::round(matrix[3]);
glm::mat4 newMatrix{matrix};
newMatrix[3] = glm::round(newMatrix[3]);
GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&matrix));
GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&newMatrix));
}
void setViewport(const Rect& _viewport)
void setViewport(const Rect& viewport)
{
// glViewport starts at the bottom left of the window.
GL_CHECK_ERROR(glViewport(_viewport.x, getWindowHeight() - _viewport.y - _viewport.h,
_viewport.w, _viewport.h));
GL_CHECK_ERROR(glViewport(viewport.x, getWindowHeight() - viewport.y - viewport.h,
viewport.w, viewport.h));
}
void setScissor(const Rect& _scissor)
void setScissor(const Rect& scissor)
{
if ((_scissor.x == 0) && (_scissor.y == 0) && (_scissor.w == 0) && (_scissor.h == 0)) {
if ((scissor.x == 0) && (scissor.y == 0) && (scissor.w == 0) && (scissor.h == 0)) {
GL_CHECK_ERROR(glDisable(GL_SCISSOR_TEST));
}
else {
// glScissor starts at the bottom left of the window.
GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() - _scissor.y - _scissor.h,
_scissor.w, _scissor.h));
GL_CHECK_ERROR(glScissor(scissor.x, getWindowHeight() - scissor.y - scissor.h,
scissor.w, scissor.h));
GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST));
}
}