diff --git a/build/android/jni/SDL2_image b/build/android/jni/SDL2_image new file mode 120000 index 0000000..2291953 --- /dev/null +++ b/build/android/jni/SDL2_image @@ -0,0 +1 @@ +../../../../android/SDL2_image \ No newline at end of file diff --git a/build/android/jni/src/Android.mk b/build/android/jni/src/Android.mk index 0c3810f..f28e889 100755 --- a/build/android/jni/src/Android.mk +++ b/build/android/jni/src/Android.mk @@ -5,8 +5,10 @@ include $(CLEAR_VARS) LOCAL_MODULE := main SDL_PATH := ../SDL2 +SDL_IMAGE_PATH := ../SDL2_image LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include +LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_IMAGE_PATH) LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../../src LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../../src/states LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../../src/gui @@ -19,6 +21,8 @@ LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \ ../../../../src/Mat4.hpp \ ../../../../src/Mesh.cpp \ ../../../../src/Mesh.hpp \ + ../../../../src/Texture.cpp \ + ../../../../src/Texture.hpp \ ../../../../src/RenderCamera.cpp \ ../../../../src/RenderCamera.hpp \ ../../../../src/RenderObject.cpp \ @@ -60,7 +64,7 @@ LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \ ../../../../src/gui/GuiElement.hpp \ ../../../../src/main.cpp -LOCAL_SHARED_LIBRARIES := SDL2 +LOCAL_SHARED_LIBRARIES := SDL2 SDL2_image LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog -landroid diff --git a/build/android/src/org/libsdl/app/SDLActivity.java b/build/android/src/org/libsdl/app/SDLActivity.java index 49a1d38..a26b6e7 100755 --- a/build/android/src/org/libsdl/app/SDLActivity.java +++ b/build/android/src/org/libsdl/app/SDLActivity.java @@ -47,7 +47,7 @@ public class SDLActivity extends Activity { // Load the .so static { System.loadLibrary("SDL2"); - //System.loadLibrary("SDL2_image"); + System.loadLibrary("SDL2_image"); //System.loadLibrary("SDL2_mixer"); //System.loadLibrary("SDL2_net"); //System.loadLibrary("SDL2_ttf"); diff --git a/build/ios/Roll them Bones.xcodeproj/project.pbxproj b/build/ios/Roll them Bones.xcodeproj/project.pbxproj index 9ae9a52..7487b40 100755 --- a/build/ios/Roll them Bones.xcodeproj/project.pbxproj +++ b/build/ios/Roll them Bones.xcodeproj/project.pbxproj @@ -32,6 +32,12 @@ 205723E11A9B4053001400FA /* chest.obj in Resources */ = {isa = PBXBuildFile; fileRef = 205723DB1A9B4053001400FA /* chest.obj */; }; 2078E3811AA5CE4700172D34 /* GuiElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2078E37F1AA5CE4700172D34 /* GuiElement.cpp */; }; 2078E3841AA5CE5500172D34 /* Gui.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2078E3821AA5CE5500172D34 /* Gui.cpp */; }; + 208B63BF1AA69C28008D37B5 /* libSDL2_image.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 208B63BE1AA69C28008D37B5 /* libSDL2_image.a */; }; + 208B64331AA6B8C4008D37B5 /* cube.png in Resources */ = {isa = PBXBuildFile; fileRef = 208B64321AA6B8C4008D37B5 /* cube.png */; }; + 208B64361AA6B8FD008D37B5 /* cube.png in Copy Textures */ = {isa = PBXBuildFile; fileRef = 208B64321AA6B8C4008D37B5 /* cube.png */; }; + 208B64421AA6B93E008D37B5 /* Texture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 208B64401AA6B93E008D37B5 /* Texture.cpp */; }; + 208B64551AA6B9D4008D37B5 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 208B64541AA6B9D4008D37B5 /* ImageIO.framework */; }; + 208B645E1AA6B9F7008D37B5 /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 208B645D1AA6B9F7008D37B5 /* MobileCoreServices.framework */; }; 20A7A91D1A89F91300EDC1A0 /* RenderCamera.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 20A7A9151A89F91300EDC1A0 /* RenderCamera.cpp */; }; 20A7A91E1A89F91300EDC1A0 /* RenderScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 20A7A9171A89F91300EDC1A0 /* RenderScene.cpp */; }; 20A7A91F1A89F91300EDC1A0 /* RenderSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 20A7A9191A89F91300EDC1A0 /* RenderSet.cpp */; }; @@ -80,6 +86,17 @@ name = "Copy Shaders"; runOnlyForDeploymentPostprocessing = 0; }; + 208B64351AA6B8EB008D37B5 /* Copy Textures */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = data/textures; + dstSubfolderSpec = 7; + files = ( + 208B64361AA6B8FD008D37B5 /* cube.png in Copy Textures */, + ); + name = "Copy Textures"; + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ @@ -171,6 +188,13 @@ 2078E3801AA5CE4700172D34 /* GuiElement.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = GuiElement.hpp; path = ../../src/gui/GuiElement.hpp; sourceTree = SOURCE_ROOT; }; 2078E3821AA5CE5500172D34 /* Gui.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Gui.cpp; path = ../../src/Gui.cpp; sourceTree = SOURCE_ROOT; }; 2078E3831AA5CE5500172D34 /* Gui.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Gui.hpp; path = ../../src/Gui.hpp; sourceTree = SOURCE_ROOT; }; + 208B63B41AA69BEE008D37B5 /* SDL_image.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_image.h; path = ../../../android/SDL2_image/SDL_image.h; sourceTree = SOURCE_ROOT; }; + 208B63BE1AA69C28008D37B5 /* libSDL2_image.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libSDL2_image.a; path = ../../../android/SDL2_image/libSDL2_image.a; sourceTree = SOURCE_ROOT; }; + 208B64321AA6B8C4008D37B5 /* cube.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = cube.png; path = textures/cube.png; sourceTree = ""; }; + 208B64401AA6B93E008D37B5 /* Texture.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Texture.cpp; path = ../../src/Texture.cpp; sourceTree = SOURCE_ROOT; }; + 208B64411AA6B93E008D37B5 /* Texture.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Texture.hpp; path = ../../src/Texture.hpp; sourceTree = SOURCE_ROOT; }; + 208B64541AA6B9D4008D37B5 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; }; + 208B645D1AA6B9F7008D37B5 /* MobileCoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MobileCoreServices.framework; path = System/Library/Frameworks/MobileCoreServices.framework; sourceTree = SDKROOT; }; 20A7A9151A89F91300EDC1A0 /* RenderCamera.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RenderCamera.cpp; path = ../../src/RenderCamera.cpp; sourceTree = SOURCE_ROOT; }; 20A7A9161A89F91300EDC1A0 /* RenderCamera.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = RenderCamera.hpp; path = ../../src/RenderCamera.hpp; sourceTree = SOURCE_ROOT; }; 20A7A9171A89F91300EDC1A0 /* RenderScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RenderScene.cpp; path = ../../src/RenderScene.cpp; sourceTree = SOURCE_ROOT; }; @@ -220,6 +244,9 @@ FD77A0850E26BDB800F39101 /* AudioToolbox.framework in Frameworks */, FDB8BFC60E5A0F6A00980157 /* CoreGraphics.framework in Frameworks */, 0097E2D912F70C4E00724AC5 /* libSDL2.a in Frameworks */, + 208B63BF1AA69C28008D37B5 /* libSDL2_image.a in Frameworks */, + 208B64551AA6B9D4008D37B5 /* ImageIO.framework in Frameworks */, + 208B645E1AA6B9F7008D37B5 /* MobileCoreServices.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -319,6 +346,7 @@ 205723D91A9B4053001400FA /* data */ = { isa = PBXGroup; children = ( + 208B64311AA6B8B7008D37B5 /* textures */, 205723DC1A9B4053001400FA /* shaders */, 205723DA1A9B4053001400FA /* models */, ); @@ -356,9 +384,44 @@ name = Gui; sourceTree = ""; }; + 208B63B21AA69BAB008D37B5 /* SDL2_image */ = { + isa = PBXGroup; + children = ( + 208B63B31AA69BE2008D37B5 /* include */, + 208B63B51AA69BF3008D37B5 /* lib */, + ); + name = SDL2_image; + sourceTree = ""; + }; + 208B63B31AA69BE2008D37B5 /* include */ = { + isa = PBXGroup; + children = ( + 208B63B41AA69BEE008D37B5 /* SDL_image.h */, + ); + name = include; + sourceTree = ""; + }; + 208B63B51AA69BF3008D37B5 /* lib */ = { + isa = PBXGroup; + children = ( + 208B63BE1AA69C28008D37B5 /* libSDL2_image.a */, + ); + name = lib; + sourceTree = ""; + }; + 208B64311AA6B8B7008D37B5 /* textures */ = { + isa = PBXGroup; + children = ( + 208B64321AA6B8C4008D37B5 /* cube.png */, + ); + name = textures; + sourceTree = ""; + }; 20A7A9131A89F8F600EDC1A0 /* Classes */ = { isa = PBXGroup; children = ( + 208B64401AA6B93E008D37B5 /* Texture.cpp */, + 208B64411AA6B93E008D37B5 /* Texture.hpp */, 2078E3821AA5CE5500172D34 /* Gui.cpp */, 2078E3831AA5CE5500172D34 /* Gui.hpp */, 2078E37E1AA5CE3700172D34 /* Gui */, @@ -427,8 +490,11 @@ 20A7A9221A89F91B00EDC1A0 /* Other Sources */, 29B97317FDCFA39411CA2CEA /* Resources */, 0097E29812F70C4D00724AC5 /* SDL */, + 208B63B21AA69BAB008D37B5 /* SDL2_image */, 29B97323FDCFA39411CA2CEA /* Frameworks */, 19C28FACFE9D520D11CA2CBB /* Products */, + 208B64541AA6B9D4008D37B5 /* ImageIO.framework */, + 208B645D1AA6B9F7008D37B5 /* MobileCoreServices.framework */, ); name = CustomTemplate; sourceTree = ""; @@ -481,6 +547,7 @@ 1D60588F0D05DD3D006BFB54 /* Frameworks */, 20517CBD1A9B411300DE49E9 /* Copy Models */, 20517CBE1A9B411300DE49E9 /* Copy Shaders */, + 208B64351AA6B8EB008D37B5 /* Copy Textures */, ); buildRules = ( ); @@ -528,6 +595,7 @@ 20517D181A9B449200DE49E9 /* fb_fs.100.glsl in Resources */, 20517D191A9B449200DE49E9 /* fb_vs.100.glsl in Resources */, 205183891A9DE71F00DE49E9 /* cube.obj in Resources */, + 208B64331AA6B8C4008D37B5 /* cube.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -560,6 +628,7 @@ 20FC425D1AA56A150083B64C /* TestState.cpp in Sources */, 2078E3811AA5CE4700172D34 /* GuiElement.cpp in Sources */, 2078E3841AA5CE5500172D34 /* Gui.cpp in Sources */, + 208B64421AA6B93E008D37B5 /* Texture.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -579,6 +648,7 @@ LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/SDL/lib\"", + "\"$(SRCROOT)/../../../android/SDL2_image\"", ); PRODUCT_NAME = "Roll them Bones"; }; @@ -595,6 +665,7 @@ LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/SDL/lib\"", + "\"$(SRCROOT)/../../../android/SDL2_image\"", ); PRODUCT_NAME = "___PROJECTNAME___"; }; diff --git a/build/ios/SDL2_image b/build/ios/SDL2_image new file mode 120000 index 0000000..a58abfd --- /dev/null +++ b/build/ios/SDL2_image @@ -0,0 +1 @@ +../../../android/SDL2_image \ No newline at end of file diff --git a/build/osx/Roll them Bones.xcodeproj/project.pbxproj b/build/osx/Roll them Bones.xcodeproj/project.pbxproj index 8f03a1b..3720d3d 100644 --- a/build/osx/Roll them Bones.xcodeproj/project.pbxproj +++ b/build/osx/Roll them Bones.xcodeproj/project.pbxproj @@ -30,6 +30,9 @@ 205D370C1A674B1D00C05BD8 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 205D370B1A674B1D00C05BD8 /* OpenGL.framework */; }; 206662951A9305FF00631FC0 /* fb_fs.glsl in Copy Shaders */ = {isa = PBXBuildFile; fileRef = 209930A31A92CAB10089E661 /* fb_fs.glsl */; }; 206662961A9305FF00631FC0 /* fb_vs.glsl in Copy Shaders */ = {isa = PBXBuildFile; fileRef = 209930A41A92CAB10089E661 /* fb_vs.glsl */; }; + 208B63C51AA6A8B2008D37B5 /* Texture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 208B63C31AA6A8B2008D37B5 /* Texture.cpp */; }; + 208B63F21AA6B2F6008D37B5 /* cube.png in Resources */ = {isa = PBXBuildFile; fileRef = 208B63F11AA6B2F6008D37B5 /* cube.png */; }; + 208B63FB1AA6B32E008D37B5 /* cube.png in Copy Textures */ = {isa = PBXBuildFile; fileRef = 208B63F11AA6B2F6008D37B5 /* cube.png */; }; 209930831A92ADB00089E661 /* Program.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 209930811A92ADB00089E661 /* Program.cpp */; }; 20A7A8D51A89E11200EDC1A0 /* RenderScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 20A7A8D11A89E11200EDC1A0 /* RenderScene.cpp */; }; 20A7A8D61A89E11200EDC1A0 /* RenderSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 20A7A8D31A89E11200EDC1A0 /* RenderSet.cpp */; }; @@ -58,6 +61,17 @@ name = "Copy Models"; runOnlyForDeploymentPostprocessing = 0; }; + 208B64081AA6B343008D37B5 /* Copy Textures */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = data/textures; + dstSubfolderSpec = 7; + files = ( + 208B63FB1AA6B32E008D37B5 /* cube.png in Copy Textures */, + ); + name = "Copy Textures"; + runOnlyForDeploymentPostprocessing = 0; + }; 209930BA1A92CB1C0089E661 /* Copy Shaders */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -103,6 +117,9 @@ 205723A01A9B3F04001400FA /* Quat.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Quat.hpp; path = ../../src/Quat.hpp; sourceTree = SOURCE_ROOT; }; 205D36CD1A6749FB00C05BD8 /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = main.cpp; path = ../../src/main.cpp; sourceTree = SOURCE_ROOT; }; 205D370B1A674B1D00C05BD8 /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = /System/Library/Frameworks/OpenGL.framework; sourceTree = ""; }; + 208B63C31AA6A8B2008D37B5 /* Texture.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Texture.cpp; path = ../../src/Texture.cpp; sourceTree = SOURCE_ROOT; }; + 208B63C41AA6A8B2008D37B5 /* Texture.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Texture.hpp; path = ../../src/Texture.hpp; sourceTree = SOURCE_ROOT; }; + 208B63F11AA6B2F6008D37B5 /* cube.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = cube.png; path = textures/cube.png; sourceTree = ""; }; 209930811A92ADB00089E661 /* Program.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Program.cpp; path = ../../src/Program.cpp; sourceTree = SOURCE_ROOT; }; 209930821A92ADB00089E661 /* Program.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Program.hpp; path = ../../src/Program.hpp; sourceTree = SOURCE_ROOT; }; 209930A31A92CAB10089E661 /* fb_fs.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = fb_fs.glsl; sourceTree = ""; }; @@ -151,6 +168,8 @@ 080E96DDFE201D6D7F000001 /* Classes */ = { isa = PBXGroup; children = ( + 208B63C31AA6A8B2008D37B5 /* Texture.cpp */, + 208B63C41AA6A8B2008D37B5 /* Texture.hpp */, 20FC42861AA5BFF50083B64C /* Gui */, 20FC42631AA587F10083B64C /* Gui.cpp */, 20FC42641AA587F10083B64C /* Gui.hpp */, @@ -245,9 +264,18 @@ usesTabs = 0; wrapsLines = 1; }; + 208B63F01AA6B2ED008D37B5 /* textures */ = { + isa = PBXGroup; + children = ( + 208B63F11AA6B2F6008D37B5 /* cube.png */, + ); + name = textures; + sourceTree = ""; + }; 2099309F1A92CAB10089E661 /* data */ = { isa = PBXGroup; children = ( + 208B63F01AA6B2ED008D37B5 /* textures */, 205723721A9B3E90001400FA /* models */, 209930A01A92CAB10089E661 /* shaders */, ); @@ -339,6 +367,7 @@ 8D11072E0486CEB800E47090 /* Frameworks */, 209930BA1A92CB1C0089E661 /* Copy Shaders */, 204FF23E1A93F28B0006E1F0 /* Copy Models */, + 208B64081AA6B343008D37B5 /* Copy Textures */, ); buildRules = ( ); @@ -386,6 +415,7 @@ 2057237B1A9B3EB2001400FA /* default_fs.glsl in Resources */, 2057237C1A9B3EB2001400FA /* default_vs.glsl in Resources */, 205182A81A9DD62C00DE49E9 /* cube.obj in Resources */, + 208B63F21AA6B2F6008D37B5 /* cube.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -418,6 +448,7 @@ 20FC42581AA569FB0083B64C /* TestState.cpp in Sources */, 20FC42651AA587F10083B64C /* Gui.cpp in Sources */, 20FC42891AA5C00A0083B64C /* GuiElement.cpp in Sources */, + 208B63C51AA6A8B2008D37B5 /* Texture.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/data/models/cube.obj b/data/models/cube.obj index 3a60902..cdeb621 100644 --- a/data/models/cube.obj +++ b/data/models/cube.obj @@ -1,4 +1,4 @@ -# Blender v2.69 (sub 0) OBJ File: '' +# Blender v2.72 (sub 0) OBJ File: '' # www.blender.org o Cube v 1.000000 -1.000000 -1.000000 @@ -9,16 +9,36 @@ v 1.000000 1.000000 -0.999999 v 0.999999 1.000000 1.000001 v -1.000000 1.000000 1.000000 v -1.000000 1.000000 -1.000000 +vt 0.606653 0.502138 +vt 0.606653 0.743570 +vt 0.365222 0.743570 +vt 0.365222 0.019274 +vt 0.606653 0.019274 +vt 0.606653 0.260706 +vt 0.365222 0.502138 +vt 0.365222 0.260706 +vt 0.848085 0.502138 +vt 0.848085 0.743570 +vt 0.606653 0.985002 +vt 0.365222 0.985002 +vt 0.123790 0.743570 +vt 0.123790 0.502138 +vn 0.000000 -1.000000 0.000000 +vn 0.000000 1.000000 0.000000 +vn 1.000000 -0.000000 0.000000 +vn -0.000000 -0.000000 1.000000 +vn -1.000000 -0.000000 -0.000000 +vn 0.000000 0.000000 -1.000000 s off -f 1 2 4 -f 5 8 6 -f 1 5 2 -f 2 6 3 -f 3 7 4 -f 5 1 8 -f 2 3 4 -f 8 7 6 -f 5 6 2 -f 6 7 3 -f 7 8 4 -f 1 4 8 +f 2/1/1 3/2/1 4/3/1 +f 8/4/2 7/5/2 6/6/2 +f 1/7/3 5/8/3 6/6/3 +f 2/1/4 6/9/4 7/10/4 +f 7/11/5 8/12/5 4/3/5 +f 1/7/6 4/3/6 8/13/6 +f 1/7/1 2/1/1 4/3/1 +f 5/8/2 8/4/2 6/6/2 +f 2/1/3 1/7/3 6/6/3 +f 3/2/4 2/1/4 7/10/4 +f 3/2/5 7/11/5 4/3/5 +f 5/14/6 1/7/6 8/13/6 diff --git a/data/shaders/default_fs.100.glsl b/data/shaders/default_fs.100.glsl index 5056a67..84293b1 100644 --- a/data/shaders/default_fs.100.glsl +++ b/data/shaders/default_fs.100.glsl @@ -6,6 +6,6 @@ uniform sampler2D texture_sampler; varying vec2 frag_uv; void main() { - //gl_FragColor = texture2D(texture_sampler, frag_uv); - gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); + gl_FragColor = texture2D(texture_sampler, frag_uv); + //gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } diff --git a/data/shaders/default_fs.glsl b/data/shaders/default_fs.glsl index e5ca333..0836768 100644 --- a/data/shaders/default_fs.glsl +++ b/data/shaders/default_fs.glsl @@ -5,6 +5,6 @@ uniform sampler2D texture_sampler; varying vec2 frag_uv; void main() { - //gl_FragColor = texture2D(texture_sampler, frag_uv); - gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); + gl_FragColor = texture2D(texture_sampler, frag_uv); + //gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } diff --git a/data/shaders/default_vs.100.glsl b/data/shaders/default_vs.100.glsl index f03c1e8..2ba9af1 100644 --- a/data/shaders/default_vs.100.glsl +++ b/data/shaders/default_vs.100.glsl @@ -12,5 +12,6 @@ varying vec2 frag_uv; void main() { vec4 eye = mv_matrix * vec4(vp, 1.0); gl_Position = proj_matrix * eye; - frag_uv = uv; -} \ No newline at end of file + // 1.0 - uv.t = flipped texture + frag_uv = vec2(uv.s, 1.0 - uv.t); +} diff --git a/data/shaders/default_vs.glsl b/data/shaders/default_vs.glsl index 31a4a0e..ffb8620 100644 --- a/data/shaders/default_vs.glsl +++ b/data/shaders/default_vs.glsl @@ -12,5 +12,6 @@ varying vec2 frag_uv; void main() { vec4 eye = mv_matrix * vec4(vp, 1.0); gl_Position = proj_matrix * eye; - frag_uv = uv; + // 1.0 - uv.t = flipped texture + frag_uv = vec2(uv.s, 1.0 - uv.t); } diff --git a/data/textures/cube.png b/data/textures/cube.png new file mode 100644 index 0000000..f70694c Binary files /dev/null and b/data/textures/cube.png differ diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 758f99e..cc025bf 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -42,10 +42,11 @@ int Mesh::loadObj(const char *buffer, size_t buffer_size) { int offset = 0; size_t bpos = 0; #ifdef _WIN32 - while (sscanf_s(cpos, "%s %n", word, 128, &offset) == 1) { + while (sscanf_s(cpos, "%s %n", word, 128, &offset) == 1) #else - while (sscanf(cpos, "%s %n", word, &offset) == 1) { + while (sscanf(cpos, "%s %n", word, &offset) == 1) #endif + { bpos += offset; cpos += offset; if (strcmp(word, "v") == 0) { // vertices @@ -197,11 +198,26 @@ int Mesh::loadObj(const char *buffer, size_t buffer_size) { int i; int indices = vertex_indices.size(); // TODO: vertex index access should be checked for validity (>= 0 < temp_vertices.size()) + // FIXME: At the moment, we're creating "empty" values if uv_indices or normal_indices are less than vertex indices - this seems very strange. I don't think uvs or normals could ever exceed the vertex count, so it might be okay? for (i = 0; i < indices; i++) { Vec3 vertex = temp_vertices[vertex_indices[i]-1]; vertices.push_back(vertex); + if (uv_indices.size() <= i) { + Vec2 uv(0.0f, 0.0f); + uvs.push_back(uv); + } else { + Vec2 uv = temp_uvs[uv_indices[i]-1]; + uvs.push_back(uv); + } + if (normal_indices.size() <= i) { + Vec3 normal(0.0f, 0.0f, 0.0f); + normals.push_back(normal); + } else { + Vec3 normal = temp_normals[normal_indices[i]-1]; + normals.push_back(normal); + } } - indices = uv_indices.size(); + /*indices = uv_indices.size(); for (i = 0; i < indices; i++) { printf("%d/%d\n", i, indices); printf("%d\n", uv_indices[i]); @@ -212,7 +228,7 @@ int Mesh::loadObj(const char *buffer, size_t buffer_size) { for (i = 0; i < indices; i++) { Vec3 normal = temp_normals[normal_indices[i]-1]; normals.push_back(normal); - } + }*/ flags |= MESH_LOADED; return 0; } diff --git a/src/RenderObject.cpp b/src/RenderObject.cpp index 8740a3c..b840632 100644 --- a/src/RenderObject.cpp +++ b/src/RenderObject.cpp @@ -9,6 +9,7 @@ This header file defines the RenderObject class. /* ======== Constructors and Destructors ======== */ RenderObject::RenderObject() { mesh = NULL; + texture = NULL; translation = Mat4( Vec4(1.0, 0.0, 0.0, 0.0), Vec4(0.0, 1.0, 0.0, 0.0), @@ -23,6 +24,9 @@ RenderObject::~RenderObject() { void RenderObject::setMesh(Mesh *new_mesh) { mesh = new_mesh; } +void RenderObject::setTexture(Texture *new_texture) { + texture = new_texture; +} void RenderObject::setTranslation(float x, float y, float z) { translation[3][0] = x; translation[3][1] = y; @@ -39,4 +43,4 @@ void RenderObject::setScale(float x, float y, float z) { } void RenderObject::calcMatrix() { model = translation * rotation * scale; -} \ No newline at end of file +} diff --git a/src/RenderObject.hpp b/src/RenderObject.hpp index f355231..3940d21 100644 --- a/src/RenderObject.hpp +++ b/src/RenderObject.hpp @@ -11,6 +11,7 @@ a Mesh, a texture, a position, and an orientation. #define RENDEROBJECT_HPP #include "common.hpp" #include "Mesh.hpp" +#include "Texture.hpp" #include "Mat4.hpp" class RenderObject { friend class RenderSet; @@ -23,12 +24,13 @@ class RenderObject { void setRotation(float pitch, float roll, float yaw); void setScale(float x, float y, float z); void setMesh(Mesh *new_mesh); + void setTexture(Texture *new_texture); private: Mesh *mesh; - // GLuint texture; + Texture *texture; Mat4 translation; Mat4 rotation; Mat4 scale; Mat4 model; }; -#endif \ No newline at end of file +#endif diff --git a/src/RenderScene.cpp b/src/RenderScene.cpp index d3c26e7..08e6aa1 100644 --- a/src/RenderScene.cpp +++ b/src/RenderScene.cpp @@ -79,6 +79,7 @@ int RenderScene::doRender() { // FIXME: We are temporarily getting the locations here - this needs to be moved elsewhere (Program?) GLuint shader_projection = glGetUniformLocation(glsl_program, "proj_matrix"); GLuint shader_modelview = glGetUniformLocation(glsl_program, "mv_matrix"); + GLuint shader_texture = glGetUniformLocation(glsl_program, "texture_sampler"); GLuint shader_vp = glGetAttribLocation(glsl_program, "vp"); GLuint shader_uv = glGetAttribLocation(glsl_program, "uv"); GLuint shader_normal = glGetAttribLocation(glsl_program, "normal"); @@ -87,8 +88,8 @@ int RenderScene::doRender() { glUniformMatrix4fv(shader_projection, 1, GL_FALSE, camera->p_matrix.toFloats(pm)); // enable attribs glEnableVertexAttribArray(shader_vp); - //glEnableVertexAttribArray(shader_uv); - //glEnableVertexAttribArray(shader_normal); + glEnableVertexAttribArray(shader_uv); + glEnableVertexAttribArray(shader_normal); // Iterate over the set's render objects std::vector::iterator obj_it, obj_end; for (obj_it = set->objects.begin(), obj_end = set->objects.end(); obj_it != obj_end; ++obj_it) { @@ -96,7 +97,17 @@ int RenderScene::doRender() { Mat4 mv_matrix; // modelview matrix float mv[16]; // converted modelview to send to OpenGL Mesh *mesh; + Texture *texture; + GLuint gl_texture = 0; if ((mesh = object->mesh) == NULL) continue; // No mesh to render with, bail + if ((texture = object->texture) == NULL) { + // TODO: add a "missing texture" texture here + } else { + gl_texture = object->texture->texture; + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, gl_texture); + glUniform1i(shader_texture, 0); + } // TODO: if object is not in camera's view, continue (probably reverse projection on each screen corner) // send computed modelview to shader mv_matrix = camera->mv_matrix * object->model; @@ -111,6 +122,8 @@ int RenderScene::doRender() { // bind normals glBindBuffer(GL_ARRAY_BUFFER, mesh->nbo); glVertexAttribPointer(shader_normal, 3, GL_FLOAT, GL_FALSE, 0, NULL); + //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->ibo); + //glDrawElements(GL_TRIANGLES, mesh->indices.size(), GL_UNSIGNED_INT, 0); glDrawArrays(GL_TRIANGLES, 0, mesh->vertices.size()); // TODO: maybe other things? Should this be managed by Program somehow? } diff --git a/src/Texture.cpp b/src/Texture.cpp new file mode 100644 index 0000000..3eb4b9f --- /dev/null +++ b/src/Texture.cpp @@ -0,0 +1,90 @@ +#include "Texture.hpp" +#include "Log.hpp" +#include "SDL_image.h" + +/* ======== Constructors/Destructor ======== */ +Texture::Texture(std::string name_, const char *buffer, size_t buffer_size) { + name.assign(name_); + texture = 0; + flags = 0; + w = h = 0; + image = NULL; + loadImg(buffer, buffer_size); +} +Texture::~Texture() { + destroyTexture(); +} +/* ======== Image/Surface loading/unloading ======== */ +/* loadImg +This function takes in the given buffer that is composed of an SDL2_image compatible format, copies it to an SDL_RWops, loads it is an SDL_Surface, and then generates an OpenGL texture. + +Yes, this is wasteful. +*/ +int Texture::loadImg(const char *buffer, size_t buffer_size) { + if (flags & LOADED) { + LOG(LOG_WARNING) << FUNC_NAME << ": surface already loaded - unloading and replacing"; + unloadImg(); + } + if ((image = IMG_Load_RW(SDL_RWFromMem((char*)buffer, buffer_size), 1)) == NULL) { + LOG(LOG_ERROR) << FUNC_NAME << ": Could not load image from buffer: " << IMG_GetError(); + return 1; + } + flags |= LOADED; + w = image->w; + h = image->h; + return 0; +} +/* unloadImg +This function effectively frees the SDL_Surface allocated by loadImg. It should be called after buildTexture() +*/ +int Texture::unloadImg() { + if (flags & LOADED) { + SDL_FreeSurface(image); + flags &= ~LOADED; + return 0; + } + return 1; +} +/* ======== Texture generation/destruction ======== */ +/* buildTexture +This function generates the OpenGL texture is an image is loaded. If the texture is already built, it is destroyed and recreated. + +Returns 0 if no image is loaded, otherwise it returns the texture ID. +*/ +GLuint Texture::buildTexture() { + if (!(flags & LOADED)) { + LOG(LOG_WARNING) << FUNC_NAME << ": image is not loaded!"; + return 0; + } + if (flags & BUILT) { + LOG(LOG_WARNING) << FUNC_NAME << ": texture already built, destroying and recreating"; + destroyTexture(); + } + // Unbind any previous texture + glBindTexture(GL_TEXTURE_2D, 0); + // Create the texture + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + if (glIsTexture(texture) != 1) { + LOG(LOG_WARNING) << FUNC_NAME << ": failed to generate texture!"; + return 0; + } + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glBindTexture(GL_TEXTURE_2D, 0); + flags |= BUILT; + LOG(LOG_DEBUG) << FUNC_NAME << ": " << name << " built as " << texture; + return texture; +} +/* destroyTexture +Deletes the given texture from OpenGL if it was already built via buildTexture. +*/ +int Texture::destroyTexture() { + if (flags & BUILT) { + glDeleteTextures(1, &texture); + flags &= ~BUILT; + return 0; + } + return 1; +} diff --git a/src/Texture.hpp b/src/Texture.hpp new file mode 100644 index 0000000..0b5ff75 --- /dev/null +++ b/src/Texture.hpp @@ -0,0 +1,30 @@ +/* ================================================================ +Texture +---------------- +Texture.cpp/Texture.hpp provide the Texture class. This class is most akin to the Mesh object. +================================================================ */ +#ifndef TEXTURE_HPP +#define TEXTURE_HPP +#include "common.hpp" +#include +class Texture { + public: + Texture(std::string name_, const char *buffer, size_t buffer_size); + ~Texture(); + enum Flags { + LOADED = (1 << 1), + BUILT = (1 << 2) + }; + int loadImg(const char *buffer, size_t buffer_size); + int unloadImg(); + GLuint buildTexture(); + int destroyTexture(); + GLuint texture; // + private: + std::string name; + unsigned int flags; // status flags + SDL_Surface *image; // the SDL_Surface that contains the data for the texture - this is populated on loadImg and should be destroyed by unloadImg whenever is convenient (usually after buildTexture) + int w, h; // width and height of this image +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 9a1d5b4..e58fbee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,7 +58,9 @@ int main(int argc, char *argv[]) { core.doProcess(); SDL_Delay(1); } + #ifndef __ANDROID__ core.closeSystem(); + #endif #endif return 0; } diff --git a/src/states/TestState.cpp b/src/states/TestState.cpp index 57b27d9..0cb1e85 100644 --- a/src/states/TestState.cpp +++ b/src/states/TestState.cpp @@ -6,6 +6,7 @@ #include "RenderObject.hpp" #include "Program.hpp" #include "Mesh.hpp" +#include "Texture.hpp" /* ======== Construction and Destruction ======== */ TestState::TestState() { scene = NULL; @@ -189,13 +190,19 @@ int TestState::onInit() { sert->setProgram(program_model); Asset *asset_mesh; + Asset *asset_texture; RenderObject *objerct = new RenderObject(); asset_mesh = asset_manager->loadFile("models/cube.obj"); Mesh *mersh = new Mesh(asset_mesh->getData(), asset_mesh->getDataLength()); + asset_texture = asset_manager->loadFile("textures/cube.png"); + Texture *texture = new Texture("textures/cube.png", asset_texture->getData(), asset_texture->getDataLength()); + texture->buildTexture(); + asset_texture->unloadData(); asset_mesh->unloadData(); mersh->buildMesh(); objerct->setMesh(mersh); + objerct->setTexture(texture); sert->addObject(objerct); objerct = new RenderObject();