-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.cpp
More file actions
71 lines (57 loc) · 2.57 KB
/
texture.cpp
File metadata and controls
71 lines (57 loc) · 2.57 KB
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
///////////////////////////////////////////////////////////////////////
// A slight encapsulation of an OpenGL texture. This contains a method
// to read an image file into a texture, and methods to bind a texture
// to a shader for use, and unbind when done.
////////////////////////////////////////////////////////////////////////
#include "math.h"
#include <fstream>
#include <stdlib.h>
#include <glbinding/gl/gl.h>
#include <glbinding/Binding.h>
using namespace gl;
#define GLM_FORCE_CTOR_INIT
#define GLM_FORCE_RADIANS
#define GLM_SWIZZLE
#include <glm/glm.hpp>
#include "texture.h"
#define STB_IMAGE_IMPLEMENTATION
#define STBI_FAILURE_USERMSG
#include "stb_image.h"
#include <glu.h> // For gluErrorString
#define CHECKERROR {GLenum err = glGetError(); if (err != GL_NO_ERROR) { fprintf(stderr, "OpenGL error (at line texture.cpp:%d): %s\n", __LINE__, gluErrorString(err)); exit(-1);} }
Texture::Texture() : textureId(0) {}
Texture::Texture(const std::string &path) : textureId(0)
{
stbi_set_flip_vertically_on_load(true);
image = stbi_load(path.c_str(), &width, &height, &depth, 4);
printf("%d %d %d %s\n", depth, width, height, path.c_str());
if (!image) {
printf("\nRead error on file %s:\n %s\n\n", path.c_str(), stbi_failure_reason());
exit(-1); }
glGenTextures(1, &textureId); // Get an integer id for this texture from OpenGL
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, (GLint)GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 10);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)GL_LINEAR_MIPMAP_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(image);
}
// Make a texture availabe to a shader program. The unit parameter is
// a small integer specifying which texture unit should load the
// texture. The name parameter is the sampler2d in the shader program
// which will provide access to the texture.
void Texture::BindTexture(const int unit, const int programId, const std::string& name)
{
glActiveTexture((gl::GLenum)((int)GL_TEXTURE0 + unit));
glBindTexture(GL_TEXTURE_2D, textureId);
int loc = glGetUniformLocation(programId, name.c_str());
glUniform1i(loc, unit);
}
// Unbind a texture from a texture unit whne no longer needed.
void Texture::UnbindTexture(const int unit)
{
glActiveTexture((gl::GLenum)((int)GL_TEXTURE0 + unit));
glBindTexture(GL_TEXTURE_2D, 0);
}