Model + Texture

This commit is contained in:
ViktorLjung
2014-03-07 20:34:40 +01:00
parent 95a36794e3
commit 800b4a3045
22 changed files with 16705 additions and 60 deletions
+8 -6
View File
@@ -40,17 +40,17 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LibraryPath>$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath)</LibraryPath>
<LibraryPath>H:\Högskolan\Design projekt\Escape the Dawn\Escape-the-Dawn\Libraries\SOIL\lib\Debug;$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)bin\</OutDir>
<IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir>
<TargetName>$(ProjectName)-$(Configuration)</TargetName>
<IncludePath>$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
<IncludePath>H:\Högskolan\Design projekt\Escape the Dawn\Escape-the-Dawn\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IncludePath>$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
<IncludePath>H:\Högskolan\Design projekt\Escape the Dawn\Escape-the-Dawn\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LibraryPath>$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath)</LibraryPath>
<LibraryPath>H:\Högskolan\Design projekt\Escape the Dawn\Escape-the-Dawn\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)bin\</OutDir>
<IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir>
<TargetName>$(ProjectName)-$(Configuration)</TargetName>
@@ -70,7 +70,7 @@
<PostBuildEvent />
<PostBuildEvent />
<Lib>
<AdditionalDependencies>opengl32.lib;glu32.lib;glew32sd.lib;glfw3.lib</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;glu32.lib;soil.lib;glew32sd.lib;glfw3.lib</AdditionalDependencies>
<ForceSymbolReferences>
</ForceSymbolReferences>
</Lib>
@@ -93,7 +93,7 @@
<AdditionalDependencies>opengl32.lib;glu32.lib;glew32s.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Lib>
<AdditionalDependencies>opengl32.lib;glu32.lib;glew32s.lib;glfw3.lib;</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;glu32.lib;soil.lib;glew32s.lib;glfw3.lib;</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemGroup>
@@ -101,6 +101,7 @@
<ClCompile Include="Model.cpp" />
<ClCompile Include="Renderer\Renderer.cpp" />
<ClCompile Include="Systems\CollisionSystem.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="World.cpp" />
<ClCompile Include="Systems\InputSystem.cpp" />
<ClCompile Include="Systems\LevelGenerationSystem.cpp" />
@@ -142,6 +143,7 @@
<ClInclude Include="Systems\PlayerSystem.h" />
<ClInclude Include="Systems\RenderSystem.h" />
<ClInclude Include="Systems\SoundSystem.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="World.h" />
</ItemGroup>
<ItemGroup>
@@ -26,6 +26,7 @@
</ClCompile>
<ClCompile Include="Model.cpp" />
<ClCompile Include="Engine.h" />
<ClCompile Include="Texture.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Component.h" />
@@ -107,6 +108,7 @@
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="Renderer.h" />
<ClInclude Include="Texture.h" />
</ItemGroup>
<ItemGroup>
<None Include="Shaders\Fragment.glsl">
+148 -42
View File
@@ -1,72 +1,177 @@
#include "Model.h"
Model::Model()
Model::Model(const char* path)
{
temp_CreateModelShit();
Loadobj(path, Vertices, Normals, TextureCoords);
CreateBuffers(Vertices, Normals, TextureCoords);
}
void Model::temp_CreateModelShit()
bool Model::Loadobj(const char* path, std::vector <glm::vec3> & out_vertices, std::vector <glm::vec3> &out_normals, std::vector <glm::vec2> & out_TextureCoords)
{
Vertices.push_back(0.0f);
Vertices.push_back(0.0f);
Vertices.push_back(2.0f);
std::vector< unsigned int > vertexIndices, TextureCoordIndices, normalIndices;
std::vector< glm::vec3 > temp_vertices;
std::vector< glm::vec2 > temp_TextureCoords;
std::vector< glm::vec3 > temp_normals;
Vertices.push_back(0.0f);
Vertices.push_back(1.0f);
Vertices.push_back(2.0f);
FILE * file = fopen(path, "r");
LOG_INFO("Loading .obj file");
if( file == NULL )
{
LOG_INFO("Load .obj file: failed");
return false;
}
char lineHeader[512];
Vertices.push_back(1.0f);
Vertices.push_back(0.0f);
Vertices.push_back(2.0f);
while(true)
{
//read the first word of the line
int res = fscanf(file, "%s", lineHeader);
Colors.push_back(1.0f);
Colors.push_back(1.0f);
Colors.push_back(1.0f);
Colors.push_back(1.0f);
if( res == EOF ) // EOF - End Of File
{
for( unsigned int i = 0; i < vertexIndices.size(); i++ )
{
unsigned int vertexIndex = vertexIndices[i];
glm::vec3 vertex = temp_vertices[ vertexIndex-1];
out_vertices.push_back(vertex);
Colors.push_back(1.0f);
Colors.push_back(1.0f);
Colors.push_back(1.0f);
Colors.push_back(1.0f);
}
Colors.push_back(1.0f);
Colors.push_back(1.0f);
Colors.push_back(1.0f);
Colors.push_back(1.0f);
for( unsigned int i = 0; i < TextureCoordIndices.size(); i++ )
{
unsigned int TextureCoordIndex = TextureCoordIndices[i];
glm::vec2 TextureCoord = temp_TextureCoords[ TextureCoordIndex-1];
out_TextureCoords.push_back(TextureCoord);
}
for( unsigned int i = 0; i < normalIndices.size(); i++ )
{
unsigned int normalIndex = normalIndices[i];
glm::vec3 normal = temp_normals[ normalIndex-1];
out_normals.push_back(normal);
}
LOG_INFO("Model Loaded\n");
break;
}
if( strcmp( lineHeader, "v" ) == 0 ) // vertex
{
glm::vec3 vertex;
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
temp_vertices.push_back(vertex);
}
else if ( strcmp( lineHeader, "vt" ) == 0 ) // texture coordinate
{
glm::vec2 TextureCoord;
fscanf(file, "%f %f\n", &TextureCoord.x, &TextureCoord.y );
temp_TextureCoords.push_back(TextureCoord);
}
else if( strcmp( lineHeader, "vn" ) == 0 ) // normal
{
glm::vec3 normal;
fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );
temp_normals.push_back(normal);
}
else if( strcmp( lineHeader, "f" ) == 0)
{
unsigned int vertexIndex[3], TextureCoordIndex[3], normalIndex[3];
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &TextureCoordIndex[0], &normalIndex[0], &vertexIndex[1], &TextureCoordIndex[1], &normalIndex[1],&vertexIndex[2], &TextureCoordIndex[2], &normalIndex[2]);
if(matches != 9)
{
printf("File can't be read, try exporting with other options\n");
return false;
}
vertexIndices.push_back(vertexIndex[0]);
vertexIndices.push_back(vertexIndex[1]);
vertexIndices.push_back(vertexIndex[2]);
TextureCoordIndices.push_back(TextureCoordIndex[0]);
TextureCoordIndices.push_back(TextureCoordIndex[1]);
TextureCoordIndices.push_back(TextureCoordIndex[2]);
normalIndices.push_back(normalIndex[0]);
normalIndices.push_back(normalIndex[1]);
normalIndices.push_back(normalIndex[2]);
}
else if ( strcmp( lineHeader, "mtllib" ) == 0 )
{
const char* fileName;
fscanf(file, "%s\n", &fileName);
FILE * mtlfile = fopen(fileName, "r");
LOG_INFO("Loading .mtl file");
if( mtlfile == NULL )
{
LOG_INFO("Load .mtl file: failed");
return false;
}
char mtllineHeader[512];
//read the first word of the line
while (true)
{
int mtlres = fscanf(mtlfile, "%s", mtllineHeader);
if( mtlres == EOF ) // EOF - End Of File
{
break;
}
else if ( strcmp( mtllineHeader, "map_Kd" ) == 0 )
{
const char* textureFileName;
fscanf(mtlfile, "%s", textureFileName);
texture.push_back(std::make_shared<Texture>(textureFileName));
LOG_INFO("Texture Loaded\n");
}
}
}
}
return true;
}
void Model::CreateBuffers( std::vector<float> _Vertices, std::vector<float> _Normals, std::vector<float> _TextureCoords )
void Model::CreateBuffers( std::vector<glm::vec3> _Vertices, std::vector<glm::vec3> _Normals, std::vector<glm::vec2>_TextureCoords)
{
glGenBuffers(1, &VertBuffer);
glBindBuffer(GL_ARRAY_BUFFER, VertBuffer);
glBufferData(GL_ARRAY_BUFFER, Vertices.size()*sizeof(float), Vertices.data(), GL_STATIC_DRAW);
GLERROR("GLEW: BufferFail1");
glGenBuffers(1, &Colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, Colorbuffer);
glBufferData(GL_ARRAY_BUFFER, Colors.size()*sizeof(float), Colors.data(), GL_STATIC_DRAW);
GLERROR("GLEW: BufferFail2");
LOG_INFO("Generating VertexBuffer");
glGenBuffers(1, &VertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
glBufferData(GL_ARRAY_BUFFER, _Vertices.size() * sizeof(glm::vec3), &_Vertices[0], GL_STATIC_DRAW);
GLERROR("GLEW: BufferFail, VertexBuffer");
// glGenBuffers(1, &TextureCoordBuffer);
// glBindBuffer(GL_ARRAY_BUFFER, TextureCoordBuffer);
// glBufferData(GL_ARRAY_BUFFER, TextureCoords.size()*sizeof(float), TextureCoords.data(), GL_STATIC_DRAW);
// GLERROR("GLEW: BufferFail3");
LOG_INFO("Generating NormalBuffer");
glGenBuffers(1, &NormalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, NormalBuffer);
glBufferData(GL_ARRAY_BUFFER, _Normals.size() * sizeof(glm::vec3), &_Normals[0], GL_STATIC_DRAW);
GLERROR("GLEW: BufferFail, NormalBuffer");
LOG_INFO("Generating textureCoordBuffer");
glGenBuffers(1, &TextureCoordBuffer);
glBindBuffer(GL_ARRAY_BUFFER, TextureCoordBuffer);
glBufferData(GL_ARRAY_BUFFER, _TextureCoords.size() * sizeof(glm::vec2), &_TextureCoords[0], GL_STATIC_DRAW);
GLERROR("GLEW: BufferFail, TextureCoordBuffer");
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
GLERROR("GLEW: BufferFail4");
glBindBuffer(GL_ARRAY_BUFFER, VertBuffer);
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLERROR("GLEW: BufferFail5");
glBindBuffer(GL_ARRAY_BUFFER, Colorbuffer);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
// glBindBuffer(GL_ARRAY_BUFFER, TextureCoordBuffer);
// glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
@@ -77,3 +182,4 @@ void Model::CreateBuffers( std::vector<float> _Vertices, std::vector<float> _Nor
GLERROR("GLEW: BufferFail5");
}
+21 -10
View File
@@ -18,24 +18,35 @@
#include "glerror.h"
#include <cstdlib>
#include "Texture.h"
class Model
{
public:
glm::vec3 Position;
std::vector<float> Colors;
std::vector<float> Vertices;
std::vector<float> Normals;
std::vector<float> TextureCoords;
std::vector<glm::vec3> Vertices;
std::vector<glm::vec3> Normals;
std::vector<glm::vec2> TextureCoords;
GLuint Colorbuffer;
GLuint VertBuffer;
GLuint VertexBuffer;
GLuint NormalBuffer;
GLuint TextureCoordBuffer;
GLuint VAO;
Model();
std::vector<std::shared_ptr<Texture>> texture;
void temp_CreateModelShit();
void CreateBuffers(std::vector<float>, std::vector<float>, std::vector<float>);
Model(const char* path);
bool Loadobj(
const char* path,
std::vector <glm::vec3> & out_vertices,
std::vector <glm::vec3> &out_normals,
std::vector <glm::vec2> & out_TextureCoords
);
void CreateBuffers(
std::vector<glm::vec3> _Vertices,
std::vector<glm::vec3> _Normals,
std::vector<glm::vec2>_TextureCoords
);
};
+2 -2
View File
@@ -5,7 +5,7 @@ uniform sampler2D texture0;
in VertexData {
vec3 Position;
vec3 Color;
vec3 Normal;
vec2 TextureCoord;
} Input;
@@ -14,5 +14,5 @@ out vec4 FragmentColor;
void main()
{
vec4 texel = texture2D(texture0, Input.TextureCoord);
FragmentColor = texel * vec4(Input.Color, 0);
FragmentColor = texel;
}
+25
View File
@@ -0,0 +1,25 @@
#include "Texture.h"
Texture::Texture( const char* path )
{
Load(path);
}
void Texture::Load( const char* path )
{
texture = SOIL_load_OGL_texture(path, 0, 0, SOIL_FLAG_INVERT_Y);
}
void Texture::Bind()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
}
Texture::~Texture()
{
glDeleteTextures(1, &texture);
}
+17
View File
@@ -0,0 +1,17 @@
#include <SOIL.h>
#include "Renderer.h"
class Texture
{
public:
Texture(const char* path);
~Texture();
GLuint texture;
void Load(const char* path);
void Bind();
};