collect all texture data and give it to the shader later,

solid color works,
texture crash
This commit is contained in:
Anakin 2016-11-08 16:42:05 +01:00
parent 9c7691df85
commit e5490b9451
2 changed files with 80 additions and 49 deletions

View File

@ -5,6 +5,7 @@
#include <glm\glm.hpp>
#include <vector>
#include "Object.h"
#include "Texture.h"
#define VERTEX_INDEX_XYZ 0
#define VERTEX_INDEX_UV 1
@ -23,6 +24,13 @@ struct Vertex {
GLfloat uv[2];
};
struct textureData {
bool alpha;
std::uint32_t width;
std::uint32_t height;
const GLvoid* data;
};
class OpenGLController
{
////////////////////////////////////////////////////////////////////////////////////////////
@ -65,6 +73,7 @@ private:
// data
std::vector<Modl*> vModels;
std::vector<textureData*> vTextures;
// transformation =========================
//values

View File

@ -41,6 +41,13 @@ OpenGLController::~OpenGLController()
delete cursor->vertex;
delete cursor;
}
while (!vTextures.empty())
{
textureData* cursor = vTextures.back();
vTextures.pop_back();
delete cursor;
}
}
@ -104,8 +111,13 @@ void OpenGLController::processInit()
exit(1);
}
// get some shader IDs
gluiMatrixID = glGetUniformLocation(gluiShaderPrgmID, "MVP");
gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler");
// generate texture
glGenTextures(1, &gluiTextureID);
glBindTexture(GL_TEXTURE_2D, gluiTextureID);
}
void OpenGLController::startGLFW()
@ -266,7 +278,7 @@ void OpenGLController::updateScene()
void OpenGLController::loadMsh(const char * path)
{
// get data
// get all models
try
{
Object obj(path);
@ -278,55 +290,9 @@ void OpenGLController::loadMsh(const char * path)
exit(1);
}
glGenTextures(1, &gluiTextureID);
glBindTexture(GL_TEXTURE_2D, gluiTextureID);
try
{
////TODO: for all
if (vModels.front()->texture == "")
throw std::invalid_argument("no texture name");
std::string tempPath = path;
while (tempPath.back() != '/' && tempPath.back() != '\\')
tempPath.pop_back();
TextureTGA tempTex(std::string(tempPath + vModels.front()->texture).c_str());
glTexImage2D(GL_TEXTURE_2D,
0,
tempTex.hasAlpha() ? GL_RGBA : GL_RGB,
tempTex.getWidth(),
tempTex.getHeight(),
0, tempTex.hasAlpha() ? GL_BGRA : GL_BGR,
GL_UNSIGNED_BYTE,
tempTex.getData().data()
);
}
catch (std::invalid_argument e)
{
GLubyte solidColor[4] = { 255, 0, 0, 255};
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
1,
1,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
(const GLvoid*)solidColor
);
}
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
////TODO: for all
// collect vertex data of all models
std::vector<Vertex> tempBufferData;
for (auto& it : vModels)
{
for (unsigned int i = 0; i < it->meshSize; i++)
@ -351,6 +317,7 @@ void OpenGLController::loadMsh(const char * path)
}
}
// fill the buffer
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
glBufferData(
GL_ARRAY_BUFFER,
@ -360,4 +327,59 @@ void OpenGLController::loadMsh(const char * path)
);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// get textures
for (auto& it : vModels)
{
textureData* tempData = new textureData;
try
{
if (it->texture == "")
throw std::invalid_argument("no texture name");
std::string tempPath = path;
while (tempPath.back() != '/' && tempPath.back() != '\\')
tempPath.pop_back();
TextureTGA tempTex(std::string(tempPath + vModels.front()->texture).c_str());
tempData->alpha = tempTex.hasAlpha();
tempData->width = tempTex.getWidth();
tempData->height = tempTex.getHeight();
tempData->data = tempTex.getData().data();
}
catch (std::invalid_argument e)
{
GLubyte solidColor[4] = { 0, 0, 255, 255 };
tempData->alpha = true;
tempData->width = 1;
tempData->height = 1;
tempData->data = (const GLvoid*)solidColor;
}
vTextures.push_back(tempData);
}
glTexImage2D(GL_TEXTURE_2D,
0,
vTextures.front()->alpha ? GL_RGBA : GL_RGB,
vTextures.front()->width,
vTextures.front()->height,
0,
vTextures.front()->alpha ? GL_BGRA : GL_BGR,
GL_UNSIGNED_BYTE,
vTextures.front()->data
);
// set some texture parameters
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
}