aboutsummaryrefslogtreecommitdiff
path: root/src/RenderBackend/OpenGL/ShaderVec.cpp
blob: 8897b84d24cf07da516c7cc94817a5b962788656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "../../../include/RenderBackend/OpenGL/ShaderVec.hpp"
#include "../../../include/RenderBackend/OpenGL/Shader.hpp"
#include "../../../include/RenderBackend/OpenGL/ShaderProgram.hpp"
#include "../../../include/RenderBackend/OpenGL/opengl.hpp"
#include <cstring>
#include <cassert>

using namespace std;

namespace amalgine
{    
    AttributeType getAttributeTypeByName(const char *attributeName)
    {
        if(strncmp(attributeName, "vec2", 4) == 0)
        {
            return AttributeType::VEC2;
        }
        else if(strncmp(attributeName, "vec3", 4) == 0)
        {
            return AttributeType::VEC3;
        }
        else if(strncmp(attributeName, "vec4", 4) == 0)
        {
            return AttributeType::VEC4;
        }
        
        assert(false);
        return AttributeType::NONE;
    }
    
    void ShaderProgramGlobalVec3::set(f32 x, f32 y, f32 z)
    {
        shaderProgram->use();
        glUniform3f(uniformId, x, y, z);
    }
    
    const string& ShaderInputVec2::getName() const
    {
        return shader->getInputAttributeName(attributeIndex);
    }
    
    void ShaderInputVec2::setData(const DeviceMemory &data)
    {
        data.use();
        AttributeType attributeType = shader->getInputAttributeType(attributeIndex);
        switch(attributeType)
        {
            case AttributeType::VEC2:
            {
                glVertexAttribPointer(attributeIndex, 2, GL_FLOAT, GL_FALSE, 0, 0);
                break;
            }
            default:
                assert(false);
                break;
        }
        glEnableVertexAttribArray(attributeIndex);
    }
    
    const string& ShaderOutputVec4::getName() const
    {
        return shader->getOutputAttributeName(attributeIndex);
    }
    
    void ShaderOutputVec4::operator=(const ShaderVec4 &shaderVec4)
    {
        shader->assign(*this, shaderVec4);
    }
}