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

View file

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

View file

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

View file

@ -140,98 +140,98 @@ namespace Renderer
return texture; 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, void updateTexture(const unsigned int texture,
const Texture::Type _type, const Texture::Type type,
const unsigned int _x, const unsigned int x,
const unsigned _y, const unsigned y,
const unsigned int _width, const unsigned int width,
const unsigned int _height, const unsigned int height,
void* _data) void* data)
{ {
const GLenum type = convertTextureType(_type); const GLenum textureType = convertTextureType(type);
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture)); GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, _width, _height, type, GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, textureType,
GL_UNSIGNED_BYTE, _data)); GL_UNSIGNED_BYTE, data));
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture)); 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)); GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
else else
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture)); GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
} }
void drawLines(const Vertex* _vertices, void drawLines(const Vertex* vertices,
const unsigned int _numVertices, const unsigned int numVertices,
const Blend::Factor _srcBlendFactor, const Blend::Factor srcBlendFactor,
const Blend::Factor _dstBlendFactor) const Blend::Factor dstBlendFactor)
{ {
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos)); 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(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(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].col));
GL_CHECK_ERROR( 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, void drawTriangleStrips(const Vertex* vertices,
const unsigned int _numVertices, const unsigned int numVertices,
const glm::mat4& _trans, const glm::mat4& trans,
const Blend::Factor _srcBlendFactor, const Blend::Factor srcBlendFactor,
const Blend::Factor _dstBlendFactor, const Blend::Factor dstBlendFactor,
const shaderParameters& _parameters) const shaderParameters& parameters)
{ {
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos)); 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(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(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].col));
GL_CHECK_ERROR( 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(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}; glm::mat4 newMatrix{matrix};
matrix[3] = glm::round(matrix[3]); newMatrix[3] = glm::round(newMatrix[3]);
GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW)); 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. // glViewport starts at the bottom left of the window.
GL_CHECK_ERROR(glViewport(_viewport.x, getWindowHeight() - _viewport.y - _viewport.h, GL_CHECK_ERROR(glViewport(viewport.x, getWindowHeight() - viewport.y - viewport.h,
_viewport.w, _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)); GL_CHECK_ERROR(glDisable(GL_SCISSOR_TEST));
} }
else { else {
// glScissor starts at the bottom left of the window. // glScissor starts at the bottom left of the window.
GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() - _scissor.y - _scissor.h, GL_CHECK_ERROR(glScissor(scissor.x, getWindowHeight() - scissor.y - scissor.h,
_scissor.w, _scissor.h)); scissor.w, scissor.h));
GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST)); GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST));
} }
} }