aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/Drawable.cpp
blob: abe2dc81bd5f8736fa866d5e45b6464c99d2cb2a (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
#include "../jin_configuration.h"
#if LIBJIN_MODULES_RENDER

#include "Shader.h"
#include "drawable.h"
#include "../math/matrix.h"
#include <stdlib.h>

namespace jin
{
namespace graphics
{
    
    Drawable::Drawable(int w, int h)
        : texture(0)
        , size(w, h)
        , anchor(0, 0)
    {
        vertex_coords[0] = 0; vertex_coords[1] = 0;
        vertex_coords[2] = 0; vertex_coords[3] = h;
        vertex_coords[4] = w; vertex_coords[5] = h;
        vertex_coords[6] = w; vertex_coords[7] = 0;

        texture_coords[0] = 0; texture_coords[1] = 0;
        texture_coords[2] = 0; texture_coords[3] = 1;
        texture_coords[4] = 1; texture_coords[5] = 1;
        texture_coords[6] = 1; texture_coords[7] = 0;
    }

    Drawable::~Drawable()
    {
        glDeleteTextures(1, &texture);
    }

    void Drawable::setAnchor(int x, int y)
    {
        anchor.x = x; 
        anchor.y = y;
    }

    void Drawable::draw(int x, int y, float sx, float sy, float r)
    {
        gl.ModelMatrix.setTransformation(x, y, r, sx, sy, anchor.x, anchor.y);

		Shader* shader = Shader::getCurrentShader();
		shader->sendMatrix4(Shader::SHADER_MODEL_MATRIX, &gl.ModelMatrix);
		shader->sendMatrix4(Shader::SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
		shader->bindVertexPointer(2, GL_FLOAT, 0, vertex_coords);
		shader->bindUVPointer(2, GL_FLOAT, 0, texture_coords);

        gl.bindTexture(texture);
        gl.drawArrays(GL_QUADS, 0, 4);
        gl.bindTexture(0);
    }

} // render
} // jin

#endif // LIBJIN_MODULES_RENDER