aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/je_drawable.cpp
blob: 7480a32b5caf4c658263bccbd61f3059e9f59562 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "../core/je_configuration.h"
#if defined(jin_graphics)

#include <stdlib.h>

#include "../math/je_matrix.h"

#include "shader/je_shader.h"
#include "je_drawable.h"

namespace JinEngine
{
    namespace Graphics
    {

        Drawable::Drawable(int w, int h)
            : mTexture(0)
            , mSize(w, h)
            , mOrigin(0, 0)
        {
            mVertexCoords[0] = 0; mVertexCoords[1] = 0;
            mVertexCoords[2] = 0; mVertexCoords[3] = h;
            mVertexCoords[4] = w; mVertexCoords[5] = h;
            mVertexCoords[6] = w; mVertexCoords[7] = 0;

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

        Drawable::Drawable(const Bitmap* bitmap)
            : mTexture(0)
            , mOrigin(0, 0)
        {
            uint32 w = mSize.w = bitmap->getWidth();
            uint32 h = mSize.h = bitmap->getHeight();

            mVertexCoords[0] = 0; mVertexCoords[1] = 0;
            mVertexCoords[2] = 0; mVertexCoords[3] = h;
            mVertexCoords[4] = w; mVertexCoords[5] = h;
            mVertexCoords[6] = w; mVertexCoords[7] = 0;

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

            const Color* pixels = bitmap->getPixels();

            mTexture = gl.genTexture();
            gl.bindTexture(mTexture);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            gl.texImage(GL_RGBA8, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
            gl.bindTexture(0);
        }

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

        void Drawable::setOrigin(int x, int y)
        {
            mOrigin.x = x; 
            mOrigin.y = y;
        }

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

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

            gl.bindTexture(mTexture);
            gl.drawArrays(GL_QUADS, 0, 4);
            gl.bindTexture(0);
        }
		
        void Drawable::draw(const Math::Quad& slice, int x, int y, float sx, float sy, float r, float ax, float ay)
        {
            float vertCoords[8] = {
                0,       0, 
                0,       slice.h,
                slice.w, slice.h,
                slice.w, 0
            };
            float slx = slice.x / mSize.w;
            float sly = slice.y / mSize.h;
            float slw = slice.w / mSize.w;
            float slh = slice.h / mSize.h;
            float texCoords[8] = {
                slx,       sly, 
                slx,       sly + slh,
                slx + slw, sly + slh, 
                slx + slw, sly
            };

            gl.ModelMatrix.setTransformation(x, y, r, sx, sy, ax, ay);

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

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

        //void Drawable::setFilter(GLint min, GLint max)
        //{
        //    glTexParameteri(GL_)
        //}

    } // namespace Graphics
} // namespace JinEngine

#endif // defined(jin_graphics)