aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/render/image.cpp
blob: 7955aa23a30c19416888bcf9ccfce4bbb61b1cab (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
#include "image.h"
#include "3rdparty/stb/stb_image.h"
#include "../utils/utils.h"
#include "../math/math.h"

namespace jin
{
namespace render
{

    Image::Image(const char* file)
        : Drawable(), pixels(0)
    {
        loadf(file);
    }

    Image::Image(const char* buffer, size_t size)
        : Drawable(), pixels(0)
    {
        loadb(buffer, size);
    }

    Image::~Image()
    {
        stbi_image_free(pixels);
    }

    color Image::getPixel(int x, int y)
    {
        if (without(x, 0, width) || without(y, 0, height))
        {
            return { 0 };
        }
        return pixels[x + y * width];
    }
    
    bool Image::loadf(const char* f)
    {
        unsigned char* imageData = stbi_load(f, &width, &height, NULL, STBI_rgb_alpha);
        if (imageData == 0) return false; 
        pixels = (color*)imageData; 

        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width,
            height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

        // set render vertices 
        Drawable::setVertices(
            new float [DRAWABLE_V_SIZE] {
                0, 0,
                0, (float)height,
                (float)width, (float)height,
                (float)width, 0,
            },
            new float [DRAWABLE_V_SIZE] {
                0, 0,
                0, 1,
                1, 1,
                1, 0
            }
        );

        return true;
    }

    bool Image::loadb(const char* b, size_t size)
    {
        // ʹstbi_load_from_memory
        unsigned char* imageData = stbi_load_from_memory((unsigned char *)b, size, &width, &height, NULL, STBI_rgb_alpha);
        if (imageData == 0) return false;
        pixels = (color*)imageData;

        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width,
            height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

        // set render vertices 
        Drawable::setVertices(
            new float [DRAWABLE_V_SIZE] {
            0, 0,
                0, (float)height,
                (float)width, (float)height,
                (float)width, 0,
        },
            new float [DRAWABLE_V_SIZE] {
                0, 0,
                    0, 1,
                    1, 1,
                    1, 0
            }
            );

        return true; 
    }
}
}