refactor: Extract setting texture unit to a separate function

This commit is contained in:
Vsevolod Kremianskii 2021-01-07 00:21:00 +07:00
parent 05a8a6e87b
commit ca674076ec
13 changed files with 62 additions and 23 deletions

View file

@ -25,6 +25,7 @@
#include "../../render/shaders.h"
#include "../../render/texture.h"
#include "../../render/textures.h"
#include "../../render/util.h"
#include "../../resource/resources.h"
#include "../game.h"
@ -201,7 +202,8 @@ void SelectionOverlay::drawReticle(Texture &texture, const glm::vec3 &screenCoor
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
texture.bind(0);
setActiveTextureUnit(0);
texture.bind();
Quad::getDefault().renderTriangles();
}
@ -291,7 +293,8 @@ void SelectionOverlay::drawActionBar() const {
} else {
frameTexture = _friendlyScroll;
}
frameTexture->bind(0);
setActiveTextureUnit(0);
frameTexture->bind();
Quad::getDefault().renderTriangles();
@ -311,7 +314,8 @@ void SelectionOverlay::drawActionBar() const {
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
texture->bind(0);
setActiveTextureUnit(0);
texture->bind();
Quad::getDefault().renderTriangles();
}

View file

@ -28,6 +28,7 @@
#include "../common/log.h"
#include "../render/mesh/quad.h"
#include "../render/textures.h"
#include "../render/util.h"
#include "../resource/types.h"
#include "game.h"
@ -107,7 +108,8 @@ void Map::drawArea(Mode mode, const glm::vec4 &bounds) const {
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
_texture->bind(0);
setActiveTextureUnit(0);
_texture->bind();
// TODO: use librender abstraction
@ -134,7 +136,8 @@ void Map::drawArea(Mode mode, const glm::vec4 &bounds) const {
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
_texture->bind(0);
setActiveTextureUnit(0);
_texture->bind();
Quad::getDefault().renderTriangles();
}
@ -187,7 +190,8 @@ void Map::drawPartyLeader(Mode mode, const glm::vec4 &bounds) const {
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
_arrow->bind(0);
setActiveTextureUnit(0);
_arrow->bind();
Quad::getDefault().renderTriangles();
}

View file

@ -251,7 +251,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
border.fill->bind(0);
setActiveTextureUnit(0);
border.fill->bind();
bool additive = border.fill->isAdditive();
if (additive) {
@ -266,7 +267,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
int width = size.x - 2 * border.dimension;
int height = size.y - 2 * border.dimension;
border.edge->bind(0);
setActiveTextureUnit(0);
border.edge->bind();
if (height > 0.0f) {
int x = _extent.left + offset.x;
@ -341,7 +343,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
int x = _extent.left + offset.x;
int y = _extent.top + offset.y;
border.corner->bind(0);
setActiveTextureUnit(0);
border.corner->bind();
// Top left corner
{

View file

@ -19,6 +19,7 @@
#include "../../render/fonts.h"
#include "../../render/mesh/quad.h"
#include "../../render/util.h"
using namespace std;
@ -94,7 +95,8 @@ void ImageButton::drawIcon(
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
if (iconFrame) {
iconFrame->bind(0);
setActiveTextureUnit(0);
iconFrame->bind();
Quad::getDefault().renderTriangles();
}
@ -103,7 +105,8 @@ void ImageButton::drawIcon(
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
if (iconTexture) {
iconTexture->bind(0);
setActiveTextureUnit(0);
iconTexture->bind();
Quad::getDefault().renderTriangles();
}
if (!iconText.empty()) {

View file

@ -20,6 +20,7 @@
#include "../../render/mesh/quad.h"
#include "../../render/shaders.h"
#include "../../render/textures.h"
#include "../../render/util.h"
#include "../../resource/resources.h"
using namespace std;
@ -47,7 +48,8 @@ void ScrollBar::load(const GffStruct &gffs) {
void ScrollBar::render(const glm::ivec2 &offset, const string &textOverride) const {
if (!_dir.image) return;
_dir.image->bind(0);
setActiveTextureUnit(0);
_dir.image->bind();
if (_canScrollUp) drawUpArrow(offset);
if (_canScrollDown) drawDownArrow(offset);

View file

@ -23,6 +23,7 @@
#include "../render/mesh/quad.h"
#include "../render/shaders.h"
#include "../render/textures.h"
#include "../render/util.h"
#include "../resource/resources.h"
using namespace std;
@ -276,7 +277,8 @@ void GUI::drawBackground() const {
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
_background->bind(0);
setActiveTextureUnit(0);
_background->bind();
Quad::getDefault().renderTriangles();
}

View file

@ -25,6 +25,7 @@
#include "mesh/quad.h"
#include "shaders.h"
#include "texture.h"
#include "util.h"
using namespace std;
@ -53,7 +54,8 @@ void Cursor::render() const {
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
texture->bind(0);
setActiveTextureUnit(0);
texture->bind();
Quad::getDefault().renderTriangles();
}

View file

@ -26,6 +26,7 @@
#include "glm/ext.hpp"
#include "shaders.h"
#include "util.h"
using namespace std;
@ -108,7 +109,8 @@ void Font::initGL() {
void Font::render(const string &text, const glm::mat4 &transform, const glm::vec3 &color, TextGravity gravity) const {
if (text.empty()) return;
_texture->bind(0);
setActiveTextureUnit(0);
_texture->bind();
glBindVertexArray(_vertexArrayId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferId);

View file

@ -21,6 +21,8 @@
#include "SDL2/SDL_opengl.h"
#include "../util.h"
using namespace std;
namespace reone {
@ -38,20 +40,25 @@ void ModelMesh::render(const shared_ptr<Texture> &diffuseOverride) const {
bool additive = false;
if (diffuse) {
diffuse->bind(0);
setActiveTextureUnit(0);
diffuse->bind();
additive = diffuse->isAdditive();
}
if (_envmap) {
_envmap->bind(1);
setActiveTextureUnit(1);
_envmap->bind();
}
if (_lightmap) {
_lightmap->bind(2);
setActiveTextureUnit(2);
_lightmap->bind();
}
if (_bumpyShiny) {
_bumpyShiny->bind(3);
setActiveTextureUnit(3);
_bumpyShiny->bind();
}
if (_bumpmap) {
_bumpmap->bind(4);
setActiveTextureUnit(4);
_bumpmap->bind();
}
GLint blendSrcRgb, blendSrcAlpha, blendDstRgb, blendDstAlpha;

View file

@ -197,8 +197,11 @@ void Texture::refresh2D() {
}
}
void Texture::bind(int unit) {
glActiveTexture(GL_TEXTURE0 + unit);
void Texture::bind() {
glBindTexture(isCubeMap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D, _textureId);
}
void Texture::unbind() {
glBindTexture(isCubeMap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D, _textureId);
}

View file

@ -64,7 +64,8 @@ public:
void clearPixels(PixelFormat format);
void bind(int unit);
void bind();
void unbind();
bool isAdditive() const;

View file

@ -46,6 +46,10 @@ void withAdditiveBlending(const std::function<void()> &block) {
glBlendFuncSeparate(blendSrcRgb, blendDstRgb, blendSrcAlpha, blendDstAlpha);
}
void setActiveTextureUnit(int n) {
glActiveTexture(GL_TEXTURE0 + n);
}
} // namespace render
} // namespace reone

View file

@ -26,6 +26,8 @@ namespace render {
void withDepthTest(const std::function<void()> &block);
void withAdditiveBlending(const std::function<void()> &block);
void setActiveTextureUnit(int n);
} // namespace render
} // namespace reone