aboutsummaryrefslogtreecommitdiffstats
path: root/yage/core/imageloader.cpp
blob: 4905bf5ee64f5a5a7dc8a071f678d0707d12155e (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
/** ---------------------------------------------------------------------------
 * -*- c++ -*-
 * @file: imageloader.cpp
 *
 * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
 * MIT License, see LICENSE file for more details.
 * ----------------------------------------------------------------------------
 */

#include "imageloader.h"
#include "../data/texture.h"
#include "logger.h"
#include "stb_image.h"
#include <iostream>

#include <glad/glad.h>

#include <iostream>
#include <stdexcept>

using std::cout;

namespace yage
{

Texture ImageLoader::loadPng(const std::string &file_path)
{
    int width, height, num_channels;
    yLogDebug << "Loading image from disk: " << file_path;
    unsigned char *data =
        stbi_load(file_path.c_str(), &width, &height, &num_channels, 0);
    yLogDebug << "Sucessfully loaded file";
    Texture texture(0, static_cast<int>(width), static_cast<int>(height));
    yLogDebug << "Creating texture";
    cout << "Hello";
    glGenTextures(1, &texture.id);

    glBindTexture(GL_TEXTURE_2D, texture.id);

    if (num_channels == 4) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
                     GL_UNSIGNED_BYTE, data);
    } else {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB,
                     GL_UNSIGNED_BYTE, data);
    }

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_LINEAR);

    glGenerateMipmap(GL_TEXTURE_2D);

    yLogDebug << "Successfully loaded texture: " << file_path;

    return texture;
}

} // namespace yage