13 Commits

Author SHA1 Message Date
Anakin
1ab385475b new release version,
Features:
- drag and drop
2016-11-12 12:30:54 +01:00
Anakin
9298d88260 cleaning up before loading new stuff,
next step: calculate best first view position
2016-11-12 12:17:46 +01:00
Anakin
91b65bf6e3 added drag n drop function,
need to clean up before loading the next mesh
2016-11-12 12:14:45 +01:00
Anakin
b4bd314450 clean up code 2016-11-12 12:05:03 +01:00
Anakin
2524971d19 include all parent modelmatrices 2016-11-12 11:54:44 +01:00
Anakin
15743e11fa new release version 2016-11-09 17:49:02 +01:00
Anakin
6107b648f9 now every model is painted with it's own texture 2016-11-09 17:48:28 +01:00
Anakin
ee8989b098 update README.md,
Cip hat nur OpenGl 2.1 -.-
2016-11-09 14:02:57 +01:00
Anakin
2f83d37e12 fixed the problem,
next solve texture name is taken only from the first element,
move some code around
2016-11-08 21:28:24 +01:00
Anakin
b4b2538ea6 fixed problem 2016-11-08 17:06:50 +01:00
Anakin
e5490b9451 collect all texture data and give it to the shader later,
solid color works,
texture crash
2016-11-08 16:42:05 +01:00
Anakin
9c7691df85 removed unused variables 2016-11-08 10:31:34 +01:00
Anakin
0fd3a7f9c8 next step: draw each model with it's own texture,
added new example mesh
2016-11-08 10:25:10 +01:00
16 changed files with 147 additions and 166 deletions

View File

@@ -3,7 +3,6 @@
#include <list>
#include <fstream>
#include <string>
#include <gl\glew.h>
#include <glm\gtc\matrix_transform.hpp>
enum Mtyp {
@@ -22,27 +21,16 @@ struct ChunkHeader {
};
struct Modl {
std::string name;
std::string parent;
Mtyp type;
std::uint32_t renderFlags;
glm::mat4 m4x4Translation;
struct {
float scale[3];
float rotation[4];
float translation[3];
} tran;
struct {
std::uint32_t type;
float data1;
float data2;
float data3;
} swci;
std::string texture;
float* vertex;
float* uv;
std::uint32_t* mesh;
std::uint32_t meshSize;
std::string name = "";
std::string parent = "";
Mtyp type = null;
std::int32_t renderFlags = -1;
glm::mat4 m4x4Translation = glm::mat4(1.0f);
std::string texture = "";
float* vertex = nullptr;
float* uv = nullptr;
std::uint32_t* mesh = nullptr;
std::uint32_t meshSize = 0;
};
@@ -60,7 +48,6 @@ private:
private:
void setModlDefault(Modl* model);
void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t end);
void analyseMsh2Chunks(std::list<ChunkHeader*> &chunkList);
void analyseMatdChunks(std::list<ChunkHeader*> &chunkList);

View File

@@ -1,10 +1,9 @@
#pragma once
#include <string>
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <glm\glm.hpp>
#include <vector>
#include "Object.h"
#include <vector>
#define VERTEX_INDEX_XYZ 0
#define VERTEX_INDEX_UV 1
@@ -23,6 +22,13 @@ struct Vertex {
GLfloat uv[2];
};
struct textureData {
bool alpha;
std::uint32_t width;
std::uint32_t height;
std::vector<std::uint8_t>* data;
};
class OpenGLController
{
////////////////////////////////////////////////////////////////////////////////////////////
@@ -65,6 +71,7 @@ private:
// data
std::vector<Modl*> vModels;
std::vector<textureData*> vTextures;
// transformation =========================
//values
@@ -87,12 +94,14 @@ private:
// private functions
private:
void processInit();
void deleteVectors();
void startGLFW();
void createWindow();
void startGLEW();
void setCallbackFunctions();
glm::mat4 getModelMatrix(unsigned int index);
glm::mat4 getMVPMatrix(unsigned int index);

View File

@@ -8,4 +8,6 @@ extern void mouseMove(GLFWwindow *window, double xpos, double ypos);
extern void mouseWheel(GLFWwindow *window, double xoffset, double yoffset);
extern void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods);
extern void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods);
extern void dragNdrop(GLFWwindow* window, int count, const char** paths);

View File

@@ -1,5 +1,7 @@
#include "Object.h"
#include <iostream>
#define PI (4.0*atan(1.0))
@@ -72,34 +74,6 @@ Object::~Object()
/////////////////////////////////////////////////////////////////////////
// private functions
void Object::setModlDefault(Modl * model)
{
model->name = "";
model->parent = "";
model->type = null;
model->renderFlags = -1;
model->m4x4Translation = glm::mat4(1.0f);
model->tran.scale[0] = 0;
model->tran.scale[1] = 0;
model->tran.scale[2] = 0;
model->tran.rotation[0] = 0;
model->tran.rotation[1] = 0;
model->tran.rotation[2] = 0;
model->tran.rotation[3] = 0;
model->tran.translation[0] = 0;
model->tran.translation[1] = 0;
model->tran.translation[2] = 0;
model->swci.type = -1;
model->swci.data1 = -1;
model->swci.data2 = -1;
model->swci.data3 = -1;
model->texture = "";
model->vertex = NULL;
model->uv = NULL;
model->mesh = NULL;
model->meshSize = 0;
}
void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t end)
{
// jump to first chunk
@@ -184,7 +158,6 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
if (!strcmp("MODL", (*it)->name))
{
Modl* tempModl = new Modl;
setModlDefault(tempModl);
// get all subchunks
std::list<ChunkHeader*> tempChunks;
@@ -347,16 +320,6 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
continue;
}
if (!strcmp("SWCI", (*it)->name))
{
fsMesh.seekg((*it)->position);
fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.type), sizeof(dataDestination->swci.type));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data1), sizeof(dataDestination->swci.data1));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data2), sizeof(dataDestination->swci.data2));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data3), sizeof(dataDestination->swci.data3));
continue;
}
}
}

View File

@@ -1,17 +1,14 @@
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <Windows.h>
#include "OpenGLController.h"
#include "callback.h"
#include <glm\gtc\matrix_transform.hpp>
#include "shader.hpp"
#include "Texture.h"
#include <iostream>
#include <Windows.h>
#include <string>
#include <glm\gtc\matrix_transform.hpp>
#define VERTEX_SHADER "Shader/TextureShader.vert"
#define FRAGMENT_SHADER "Shader/TextureShader.frag"
#define MAX_MODEL_COUNT 1000
/////////////////////////////////////////////////////////////////////////
// public constructor/destructor
@@ -31,16 +28,7 @@ OpenGLController::~OpenGLController()
glDeleteTextures(1, &gluiSamplerID);
glfwTerminate();
while (!vModels.empty())
{
Modl* cursor = vModels.back();
vModels.pop_back();
delete cursor->uv;
delete cursor->mesh;
delete cursor->vertex;
delete cursor;
}
deleteVectors();
}
@@ -104,8 +92,41 @@ 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);
// 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);
}
void OpenGLController::deleteVectors()
{
while (!vModels.empty())
{
Modl* cursor = vModels.back();
vModels.pop_back();
delete cursor->uv;
delete cursor->mesh;
delete cursor->vertex;
delete cursor;
}
while (!vTextures.empty())
{
textureData* cursor = vTextures.back();
vTextures.pop_back();
delete cursor->data;
delete cursor;
}
}
void OpenGLController::startGLFW()
@@ -163,6 +184,23 @@ void OpenGLController::setCallbackFunctions()
glfwSetWindowSizeCallback(pWindow, windowResize);
glfwSetScrollCallback(pWindow, mouseWheel);
glfwSetKeyCallback(pWindow, keyPress);
glfwSetDropCallback(pWindow, dragNdrop);
}
glm::mat4 OpenGLController::getModelMatrix(unsigned int index)
{
glm::mat4 tempParentMatrix = glm::mat4(1.0f);
for (unsigned int loop = 0; loop < vModels.size(); loop++)
{
if (!strcmp(vModels[index]->parent.c_str(), vModels[loop]->name.c_str()))
{
tempParentMatrix = getModelMatrix(loop);
break;
}
}
return tempParentMatrix * vModels[index]->m4x4Translation;
}
glm::mat4 OpenGLController::getMVPMatrix(unsigned int index)
@@ -177,10 +215,6 @@ glm::mat4 OpenGLController::getMVPMatrix(unsigned int index)
glm::vec3(0, 1, 0)
);
// Model
//TODO for all
glm::mat4 m4x4Model = vModels[index]->m4x4Translation;
// User controlled rotation
glm::mat4 m4x4ModelRot = glm::mat4(1.0f);
m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationX, glm::vec3(1, 0, 0));
@@ -188,7 +222,7 @@ glm::mat4 OpenGLController::getMVPMatrix(unsigned int index)
m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationZ, glm::vec3(0, 0, 1));
// Return MVP
return m4x4Projection * m4x4View * m4x4ModelRot * m4x4Model;
return m4x4Projection * m4x4View * m4x4ModelRot * getModelMatrix(index);
}
@@ -251,8 +285,23 @@ void OpenGLController::updateScene()
int instanceOffset(0);
for (int modelIndex = 0; modelIndex < vModels.size(); modelIndex++)
for (unsigned int modelIndex = 0; modelIndex < vModels.size(); modelIndex++)
{
// give texture to the shader
glTexImage2D(GL_TEXTURE_2D,
0,
vTextures[modelIndex]->alpha ? GL_RGBA : GL_RGB,
vTextures[modelIndex]->width,
vTextures[modelIndex]->height,
0,
vTextures[modelIndex]->alpha ? GL_BGRA : GL_BGR,
GL_UNSIGNED_BYTE,
vTextures[modelIndex]->data->data()
);
glGenerateMipmap(GL_TEXTURE_2D);
// give the MVP to the shader
glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
glDrawArrays(GL_TRIANGLES, instanceOffset, vModels[modelIndex]->meshSize);
@@ -266,7 +315,10 @@ void OpenGLController::updateScene()
void OpenGLController::loadMsh(const char * path)
{
// get data
// clean up old stuff first
deleteVectors();
// get all models
try
{
Object obj(path);
@@ -278,55 +330,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 +357,7 @@ void OpenGLController::loadMsh(const char * path)
}
}
// fill the buffer
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
glBufferData(
GL_ARRAY_BUFFER,
@@ -360,4 +367,37 @@ 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 + it->texture).c_str());
tempData->alpha = tempTex.hasAlpha();
tempData->width = tempTex.getWidth();
tempData->height = tempTex.getHeight();
tempData->data = new std::vector<std::uint8_t>(tempTex.getData());
}
catch (std::invalid_argument e)
{
GLubyte solidColor[4] = { 0, 0, 255, 255 };
tempData->alpha = true;
tempData->width = 1;
tempData->height = 1;
tempData->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
}
vTextures.push_back(tempData);
}
}

View File

@@ -1,7 +1,5 @@
#include "Texture.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
TextureTGA::TextureTGA(const char * filePath)
{

View File

@@ -1,6 +1,3 @@
//#include "callback.h"
#include <gl\glew.h>
#include <gl\glfw3.h>
#include "OpenGLController.h"
@@ -101,3 +98,10 @@ void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods)
}
}
}
void dragNdrop(GLFWwindow* window, int count, const char** paths)
{
OpenGLController* controller = reinterpret_cast<OpenGLController*>(glfwGetWindowUserPointer(window));
if(count > 0)
controller->loadMsh(paths[0]);
}

View File

@@ -3,9 +3,6 @@
#endif // DEBUG
#include "OpenGLController.h"
#include "Object.h"
#include <iostream>
#include <Windows.h>
int main(int argc, char** argv)
{
@@ -20,25 +17,7 @@ int main(int argc, char** argv)
else
scene = OpenGLController::getInstance();
goto openGL;
try {
Object obj("..\\Release\\Msh\\cubeTex.msh");
}
catch (std::invalid_argument e)
{
std::cout << e.what() << std::endl;
}
system("pause");
return 0;
openGL:
scene->loadMsh("..\\Release\\Msh\\multipleModels.msh");
scene->loadMsh("..\\Release\\Msh\\structured.msh");
do {
scene->updateScene();

View File

@@ -13,7 +13,6 @@ Feel free to use my code the way you like. But remember i used some public libra
licence, too.
### To Do
- draw multiple models of one mesh,
- crashing sometimes - no idea why,
- bones are not triangulated,
- nulls are not triangulated,

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

BIN
Release/Msh/128x128Grau.tga Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

BIN
Release/Msh/128x128Rot.tga Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

BIN
Release/Msh/multiModTex.msh Normal file

Binary file not shown.

BIN
Release/Msh/structured.msh Normal file

Binary file not shown.

Binary file not shown.