Skip to content

Commit 972302e

Browse files
committed
Initial load of ilcontainer
1 parent f1dbe1c commit 972302e

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

ilcontainer.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <assert.h>
2+
#include "ilcontainer.h"
3+
4+
bool ILContainer::Initialize(const char * file_name)
5+
{
6+
// We are asserting that we have not initialized this object before.
7+
assert(this->il_handle == BAD_IL_VALUE);
8+
9+
if ((this->il_handle = ilGenImage()) == BAD_IL_VALUE)
10+
return false;
11+
ilBindImage(this->il_handle);
12+
if (!ilLoadImage(file_name))
13+
return false;
14+
15+
glGenTextures(1, &this->il_texture_handle);
16+
glBindTexture(GL_TEXTURE_2D, this->il_texture_handle);
17+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
18+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
19+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
20+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
21+
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), this->width = ilGetInteger(IL_IMAGE_WIDTH), this->height = ilGetInteger(IL_IMAGE_HEIGHT), 0, this->format = ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, this->data = ilGetData());
22+
return true;
23+
}
24+
25+
/* To use with a shader, the sampler uniform must be fed the
26+
same paramter value as you call this function with.
27+
*/
28+
29+
void ILContainer::Bind(GLuint texture_unit)
30+
{
31+
// We are asserting that we have initialized this object before.
32+
assert(this->il_handle != BAD_IL_VALUE);
33+
34+
glActiveTexture(GL_TEXTURE0 + texture_unit);
35+
glBindTexture(GL_TEXTURE_2D, this->il_texture_handle);
36+
}
37+
38+
/* This class assumes that the object will be dynamically created.
39+
This assumption is demonstrated in the destructor which calls
40+
OpenGL. If an object is declared in the global score, the
41+
destructor will run after the OpenGL context is destroyed,
42+
which would be an error.
43+
*/
44+
45+
ILContainer::~ILContainer()
46+
{
47+
if (this->il_texture_handle != BAD_GL_VALUE)
48+
glDeleteTextures(1, &this->il_texture_handle);
49+
if (this->il_handle != BAD_IL_VALUE)
50+
ilDeleteImage(this->il_handle);
51+
52+
this->il_handle = BAD_IL_VALUE;
53+
this->il_texture_handle = BAD_GL_VALUE;
54+
}
55+
56+
/* Do not use
57+
58+
bool ILCubemapContainer::Initialize(const char * file_name, int width, int height, int offsetx, int offsety)
59+
{
60+
assert(this->il_handle == BAD_IL_VALUE);
61+
if ((this->il_handle = ilGenImage()) == BAD_IL_VALUE)
62+
return false;
63+
ilBindImage(this->il_handle);
64+
if (!ilLoadImage(file_name))
65+
return false;
66+
67+
glGenTextures(7, this->il_texture_handle);
68+
glBindTexture(GL_TEXTURE_2D, this->il_texture_handle[0]);
69+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
70+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
71+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
72+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
73+
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), this->width = ilGetInteger(IL_IMAGE_WIDTH), this->height = ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
74+
return true;
75+
}
76+
77+
void ILCubemapContainer::Bind(GLuint texture_unit)
78+
{
79+
glActiveTexture(GL_TEXTURE0 + texture_unit);
80+
// glBindTexture(GL_TEXTURE_2D, this->il_texture_handle);
81+
}
82+
83+
ILCubemapContainer::~ILCubemapContainer()
84+
{
85+
if (this->il_texture_handle[0] != BAD_GL_VALUE)
86+
glDeleteTextures(7, this->il_texture_handle);
87+
if (this->il_handle != BAD_IL_VALUE)
88+
ilDeleteImage(this->il_handle);
89+
90+
this->il_handle = BAD_IL_VALUE;
91+
this->il_texture_handle[0] = BAD_GL_VALUE;
92+
}
93+
94+
*/

ilcontainer.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Perry Kivolowitz */
2+
3+
#pragma once
4+
#undef _UNICODE
5+
#include <GL/glew.h>
6+
#include <GL/freeglut.h>
7+
#include <IL/il.h>
8+
#include <IL/ilu.h>
9+
#include <IL/ilut.h>
10+
11+
#define BAD_IL_VALUE IL_INVALID_VALUE
12+
#ifndef BAD_GL_VALUE
13+
#define BAD_GL_VALUE GLuint(-1)
14+
#endif // !BAD_GL_VALUE
15+
16+
/* This class assumes that the object will be dynamically created.
17+
This assumption is demonstrated in the destructor which calls
18+
OpenGL. If an object is declared in the global score, the
19+
destructor will run after the OpenGL context is destroyed,
20+
which would be an error.
21+
*/
22+
23+
class ILContainer
24+
{
25+
public:
26+
ILContainer()
27+
{
28+
this->il_handle = BAD_IL_VALUE;
29+
this->il_texture_handle = BAD_GL_VALUE;
30+
}
31+
32+
~ILContainer();
33+
34+
bool Initialize(const char * file_name);
35+
void Bind(GLuint texture_unit = 0);
36+
37+
ILint width;
38+
ILint height;
39+
ILint format;
40+
ILuint il_handle;
41+
GLuint il_texture_handle;
42+
GLvoid * data;
43+
};
44+
45+
/* Do not use
46+
47+
class ILCubemapContainer
48+
{
49+
public:
50+
ILCubemapContainer()
51+
{
52+
this->il_handle = BAD_IL_VALUE;
53+
this->il_texture_handle[0] = BAD_GL_VALUE;
54+
}
55+
56+
~ILCubemapContainer();
57+
58+
bool Initialize(const char * file_name, int width, int height, int offsetx, int offsety);
59+
void Bind(GLuint texture_unit = 0);
60+
61+
ILint width;
62+
ILint height;
63+
ILuint il_handle;
64+
GLuint il_texture_handle[7];
65+
};
66+
67+
*/

0 commit comments

Comments
 (0)