LeeMcQueen / GameDemo

Demo
0 stars 0 forks source link

guiTexture #27

Closed LeeMcQueen closed 3 years ago

LeeMcQueen commented 3 years ago

pragma once

include

class GuiTexture {

public: GuiTexture(unsigned int texture, glm::vec2 position, glm::vec2 scale) : texture(texture), position(position), scale_(scale) {}; ~GuiTexture();

unsigned int getTexture() { return texture_; }
glm::vec2 getPosition() { return position_; }
glm::vec2 getScale() { return scale_; }

private:

unsigned int texture_;
glm::vec2 position_;
glm::vec2 sacle_;

};

LeeMcQueen commented 3 years ago

GuiTexture.h

include

include

include

include

class GuiRenderer {

public:

static void initialise();
static void render(std::vector<GuiTexture> guiTextures);
static void cleanUp();

priavte:

static RawModel rawModel_;
static GuiShader guiShader_;

};

LeeMcQueen commented 3 years ago

GuiShader.h

include "ShaderProgram.h"

class GuiShader : public ShaderProgram {

public:

GuiShader();

static const char *GUI_VERTEX_SHADER;
static const char *GUI_FRAGMENT_SHADER;

void loadTransformationMatrix(glm::mat4 matrix);

protected:

void bindAttributes() override;
void getAllUniformLocation() override;

private:

GLuint Location_transformationMatrix;

}

LeeMcQueen commented 3 years ago

GuiShader.cpp

include "GuiShader.h"

const char GuiShader::GUI_VERTEX_SHADER = "guiVertexShader.txt"; const char GuiShader::GUI_FRAGMENT_SHADER = "guiFragmentShader.txt";

GuiShader::GuiShader() : ShaderProgram(GUI_VERTEX_SHADER, GUI_FRAGMENT_SHADER){

bindAttributes();

glLinkProgram(programID);
glValidateProgram(programID);

getAllUniformLocations();

}

void GuiShader::bindAttributes(){

bindAttribute(0, "position");

}

void GuiShader::loadTransformationMatrix(glm::mat4 matrix) {

loadMatrix4(Loaction_transformationMatirx, matrix);

}

void GuiShader::getAllUniformLocations() {

Location_transformationMatrix = getUniformLocation("transformationMatrix");

}

LeeMcQueen commented 3 years ago

GuiRenderer.cpp

include

RawModel GuiRenderer::rawModel; GuiShader GuiRenderer::guiShader;

void GuiRenderer::initialise(){

GuiShader guiShader;
Loader loader;
std::vector<float> position = { -1, 1, -1, -1, 1, 1, 1, -1 };
guiModel_ = loader.loadToVao(position, 2);

}

void GuiRenderer::render(std::vector guiTextures){

guiShader_.start();
glBindVertexArray(guiModel_.getVaoId());
glEnableVertexAttribArray(0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST)

for(GuiTexture &guiTexture : guiTextures){

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, guiTexture.getTexture());
    glm::mat4 transformMatrix = Maths::createTransformationMatrix(guiTexture.getPosition(), guiTexture.getScale());
    guiShader_.loadTransformationMatrix(transformMatrix);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, rawModel_.getVertexCount());
}
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glDisableVertexAttribArray(0);
glBindVertexArray(0);
guiShader_.stop();

}