aboutsummaryrefslogtreecommitdiffstats
path: root/src/graphics/Texture.cpp
blob: a7b2a9dad2d3cdc5a0794b95680366368ac16a0e (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
#include <GL/glew.h>

#define STB_IMAGE_IMPLEMENTATION
#include "../include/stb_image.h"

#include "Texture.hpp"

Texture::Texture(int type) : target(type)
{
	glGenTextures(1, &texture_id);
}

Texture::~Texture()
{
	glDeleteTextures(1, &texture_id);
}

int Texture::LoadImage(const char *path)
{
	int width, height, nrChannels;
	unsigned char *image;

	glBindTexture(target, texture_id);

		image = stbi_load(path, &width, &height, &nrChannels, 0);
		if(!image)
			return 1;

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
			GL_UNSIGNED_BYTE, image);
		stbi_image_free(image);

		glGenerateMipmap(target);
		glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
		glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
		glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	glBindTexture(target, 0);
	return 0;
}

int Texture::LoadImage(const char **paths)
{
	if(target != GL_TEXTURE_CUBE_MAP)
		return 1;

	int width, height, nrChannels;
	unsigned int i;
	unsigned char *image;

	glBindTexture(target, texture_id);

		for(i = 0; i < 6; i++) {
			image = stbi_load(paths[i], &width, &height, &nrChannels, 0);
			if(!image)
				return 1;

			glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
				0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
			stbi_image_free(image);
		}

		glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

	glBindTexture(target, 0);
	return 0;
}

void Texture::Bind()
{
	glBindTexture(target, texture_id);
}