-
Notifications
You must be signed in to change notification settings - Fork 0
/
PongTextures.h
43 lines (33 loc) · 1.04 KB
/
PongTextures.h
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
#ifndef PongTextures
#define PongTextures
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include<string>
#include<exception>
#include <GL/glew.h>
class Texture{
private:
GLuint ID;
public:
Texture(std::string filename);
inline void use();
};
Texture::Texture(std::string){
glGenTextures(1, this->ID);
glBindTexture(GL_TEXTURE_2D, this->ID);
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, nrChannels;
unsigned char *data = stbi_load(filename, &width, &height, &nrChannels, 0);
if (!data)
throw std::runtime_error(std::string("Failed to generate texture: ") + filename);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
inline void Texture::use(){
glBindTexture(GL_TEXTURE_2D, this->ID);
}
#endif