Skip to content

Commit

Permalink
add 3ds loader
Browse files Browse the repository at this point in the history
  • Loading branch information
mohlek committed Aug 5, 2017
1 parent 2778ef7 commit c9c0723
Show file tree
Hide file tree
Showing 18 changed files with 358 additions and 9 deletions.
23 changes: 22 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 2.8)

PROJECT(MEDIEVAL_SOMETHING)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")

set(SOURCE
src/engine/Window.cpp
src/engine/Buffer.cpp
Expand All @@ -10,6 +12,7 @@ set(SOURCE
src/engine/shader/ShaderLoader.cpp
src/engine/shader/Shader.cpp
src/engine/shader/ShaderProgram.cpp
src/engine/model/3ds/Model3DS.cpp
src/main.cpp
)

Expand All @@ -18,6 +21,10 @@ set(SHADERS
src/shader/main.vert
)

set(RESOURCES
resource/dragon.3ds
)

#########################################################
# FIND OpenGL
#########################################################
Expand Down Expand Up @@ -53,6 +60,17 @@ if(NOT GLFW_FOUND)
message(ERROR " GLFW not found!")
endif(NOT GLFW_FOUND)

########################################################
# FIND LIB3DS
########################################################
find_package(Lib3ds)
include_directories(${LIB3DS_INCLUDE_DIRS})
link_directories(${LIB3DS_LIBRARY_DIRS})
if(NOT LIB3DS_FOUND)
message(ERROR " LIB3DS not found!")
endif(NOT LIB3DS_FOUND)


########################################################
# COPY SHADER
########################################################
Expand All @@ -62,6 +80,9 @@ foreach(SHADER ${SHADERS})
file(COPY ${SHADER} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/shader/)
endforeach(SHADER)

message(STATUS "copy resources")
file(COPY ${RESOURCES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/resource/)

add_executable(medievalSomething ${SOURCE})

target_link_libraries(medievalSomething ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${GLFW_LIBRARIES})
target_link_libraries(medievalSomething ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${GLFW_LIBRARIES} ${LIB3DS_LIBRARY})
37 changes: 37 additions & 0 deletions cmake/modules/FindLib3ds.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# find lib3ds includes and library
#
# LIB3DS_INCLUDE_DIR - where the lib3ds directory containing the headers can be
# found
# LIB3DS_LIBRARY - full path to the lib3ds library
# LIB3DS_FOUND - TRUE if lib3ds was found

FIND_PATH(LIB3DS_INCLUDE_DIR lib3ds/file.h
/usr/include
/usr/local/include
$ENV{INCLUDE}
)
FIND_LIBRARY(LIB3DS_LIBRARY NAMES 3ds lib3ds)

IF(LIB3DS_INCLUDE_DIR)
MESSAGE(STATUS "Found lib3ds include dir: ${LIB3DS_INCLUDE_DIR}")
ELSE(LIB3DS_INCLUDE_DIR)
MESSAGE(STATUS "Could NOT find lib3ds headers.")
ENDIF(LIB3DS_INCLUDE_DIR)

IF(LIB3DS_LIBRARY)
MESSAGE(STATUS "Found lib3ds library: ${LIB3DS_LIBRARY}")
ELSE(LIB3DS_LIBRARY)
MESSAGE(STATUS "Could NOT find lib3ds library.")
ENDIF(LIB3DS_LIBRARY)


IF(LIB3DS_INCLUDE_DIR AND LIB3DS_LIBRARY)
SET(LIB3DS_FOUND TRUE)
ELSE(LIB3DS_INCLUDE_DIR AND LIB3DS_LIBRARY)
SET(LIB3DS_FOUND FALSE)
IF(Lib3ds_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find lib3ds. Please install lib3ds (http://lib3ds.sourceforge.net)")
ENDIF(Lib3ds_FIND_REQUIRED)
ENDIF(LIB3DS_INCLUDE_DIR AND LIB3DS_LIBRARY)

# vim: et sw=4 ts=4
Binary file added resource/dragon.3ds
Binary file not shown.
Binary file added resource/textures/Ani_Fire_A.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/textures/Dragon_Bump_Col2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/textures/Dragon_Nor.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/textures/Dragon_Nor_mirror2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/textures/Dragon_ground_color.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/textures/Fire_A_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/textures/Floor_C.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/textures/Floor_N.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resource/textures/Floor_S.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 24 additions & 5 deletions src/engine/Buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Buffer.h"

#include <cstring>

using namespace Engine;

Buffer::Buffer(GLenum bufferType) : bufferType(bufferType) {
Expand All @@ -10,20 +12,37 @@ Buffer::~Buffer() {
glDeleteBuffers(1, &this->bufferId);
}

void* Buffer::map() {
void Buffer::bind() {
glBindBuffer(this->bufferType, this->bufferId);
}

void* Buffer::map(GLenum access = GL_READ_WRITE) {
if (glMapNamedBuffer) {
return glMapNamedBuffer(this->bufferId, GL_READ_WRITE);
if (access != GL_READ_ONLY) {
glNamedBufferData(this->bufferId, this->size, NULL, GL_STATIC_DRAW);
}
return glMapNamedBuffer(this->bufferId, access);
}

glBindBuffer(this->bufferType, this->bufferId);
return glMapBuffer(this->bufferType, GL_READ_WRITE);
bind();
if (access != GL_READ_ONLY) {
glBufferData(this->bufferType, this->size, NULL, GL_STATIC_DRAW);
}
return glMapBuffer(this->bufferType, access);
}

void Buffer::pushData(void* data, int size) {
this->size = size;
void* addr = map(GL_WRITE_ONLY);

std::memcpy(addr, data, this->size);
}

bool Buffer::unmap() {
if (glUnmapNamedBuffer) {
return glUnmapNamedBuffer(this->bufferId);
}

glBindBuffer(this->bufferType, this->bufferId);
bind();
return glUnmapBuffer(this->bufferId);
}
8 changes: 6 additions & 2 deletions src/engine/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ namespace Engine {
const GLenum bufferType;

GLint size;
GLenum dataType;
GLenum dataType = GL_FLOAT;
GLsizei stride;

Buffer(GLenum bufferType);
Buffer() : Buffer(GL_ARRAY_BUFFER) {};
virtual ~Buffer();

void* map();
void bind();

void pushData(void* data, int size);

void* map(GLenum access);
bool unmap();
};
}
74 changes: 74 additions & 0 deletions src/engine/model/3ds/Model3DS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "Model3DS.h"

#include <lib3ds/mesh.h>
#include <lib3ds/vector.h>

#include <assert.h>
#include <cstring>

using namespace Engine;

Model3DS::Model3DS(std::string&& filename) {
this->model = lib3ds_file_load(filename.c_str());

if (!model) {
throw "Unable to load model";
}
}

Model3DS::~Model3DS() {

}

unsigned int Model3DS::getTotalFaces() {
assert(model != NULL);

unsigned int totalFaces = 0;
Lib3dsMesh* mesh;
for (mesh = this->model->meshes; mesh != NULL; mesh = mesh->next) {
totalFaces += mesh->faces;
}

return totalFaces;
}

void Model3DS::createVBO() {
unsigned int totalFaces = getTotalFaces();

Lib3dsVector* vertices = new Lib3dsVector[totalFaces * 3];
Lib3dsVector* normals = new Lib3dsVector[totalFaces * 3];
Lib3dsMesh* mesh;

unsigned int finishedFaces = 0;

for (mesh = this->model->meshes; mesh != NULL; mesh = mesh->next) {
lib3ds_mesh_calculate_normals(mesh, &normals[finishedFaces * 3]);

for (unsigned int curFace = 0; curFace < mesh->faces; ++curFace) {
Lib3dsFace* face = &mesh->faceL[curFace];

for (unsigned int i = 0; i < 3; ++i) {
std::memcpy(&vertices[finishedFaces * 3 + 1], mesh->pointL[face->points[i]].pos, sizeof(Lib3dsVector));
}
finishedFaces++;
}
}

unsigned int size = sizeof(Lib3dsVector) * 3 * totalFaces;

this->vertexVBO.stride = sizeof(Lib3dsVector) * 3;
this->vertexVBO.pushData(vertices, size);

this->normalsVBO.stride = sizeof(Lib3dsVector) * 3;
this->normalsVBO.pushData(normals, size);

delete vertices;
delete normals;

lib3ds_file_free(this->model);
this->model = NULL;
}

void Model3DS::render() {
}

23 changes: 23 additions & 0 deletions src/engine/model/3ds/Model3DS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <lib3ds/file.h>
#include <GL/glew.h>

#include "../../Buffer.h"

#include <string>

namespace Engine {
class Model3DS {
private:
Lib3dsFile* model;

unsigned int getTotalFaces();
public:
Engine::Buffer vertexVBO;
Engine::Buffer normalsVBO;

Model3DS(std::string&& filename);
~Model3DS();
void render();
void createVBO();
};
}
Loading

0 comments on commit c9c0723

Please sign in to comment.