#include "Texture.h"
Texture::Texture() {
//Something
}
Texture::Texture(std::string filename) {
h = Hilfer();
std::cout << filename << std::endl;
ILuint ImgId = 0;
ilGenImages(1, &ImgId);
ilBindImage(ImgId);
// Lame conversion to wchar_t*
std::wstring widestring = std::wstring(filename.begin(), filename.end());
const wchar_t* widecstr = widestring.c_str();
ilLoadImage(widecstr);
ILubyte *Data = ilGetData();
//ilDeleteImages(1, &ImgId);
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
// Set filter
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Set image
glTexImage2D(
GL_TEXTURE_2D,
GLint(0),
ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL),
GLsizei(ilGetInteger(IL_IMAGE_WIDTH)),
GLsizei(ilGetInteger(IL_IMAGE_HEIGHT)),
0,
ilGetInteger(IL_IMAGE_FORMAT),
ilGetInteger(IL_IMAGE_TYPE),
Data);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
// Initialize stuff here
float vertexData[] = {
0.5f, 0.5f, 0.0f, 1.0f, // Screen coordinates
0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, // Texture coordinates
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
};
shaderList.push_back(shader.LoadShader(GL_VERTEX_SHADER, "texture.vert"));
shaderList.push_back(shader.LoadShader(GL_FRAGMENT_SHADER, "texture.frag"));
theProgram = shader.CreateProgram(shaderList);
glGenBuffers(1, &vertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
texture_shader = glGetUniformLocation(theProgram, "texture");
}
void Texture::Load(std::string filename) {
}
void Texture::Draw() {
// something something
Refresh();
}
void Texture::Refresh() {
glUseProgram(theProgram);
glUniform1i(texture_shader, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)64);
glDrawArrays(GL_QUADS, 0, 4);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glUseProgram(0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
h.checkGLError();
}
Texture::~Texture() {
}
void Texture::checkError() {
ILenum Error;
if((Error = ilGetError()) != IL_NO_ERROR) {
std::cout << Error << " : " << iluErrorString(Error) << std::endl;
}
}