diff --git a/CMakeLists.txt b/CMakeLists.txt index bc54d19..6c50b6e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ set(CMAKE_BUILD_TYPE Debug) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -pedantic") set(BUILD_SHARED_LIBS OFF) set(BUILD_TESTS ON) +set(BUILD_SAMPLES ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) cmake_policy(SET CMP0015 NEW) @@ -23,7 +24,7 @@ include_directories(external/glm/glm) include_directories(external/glad) include_directories(external/stb) -add_library(${PROJECT_NAME} SHARED external/glad/glad.c ${LIB_SRC}) +add_library(vb01 SHARED external/glad/glad.c ${LIB_SRC}) set(GLFW_DIR external/glfw) set(FREETYPE_DIR external/freetype) @@ -73,3 +74,8 @@ if(BUILD_TESTS) add_executable(vb01Tests ${TEST_SRC}) target_link_libraries(vb01Tests ${PROJECT_NAME} ${DEPS} cppunit) endif() + +if(BUILD_SAMPLES) + add_executable(textSample textSample.cpp) + target_link_libraries(textSample ${PROJECT_NAME}) +endif() diff --git a/samples/textSample/fonts/Amiri-Regular.ttf b/samples/textSample/fonts/Amiri-Regular.ttf new file mode 100644 index 0000000..50eaaa3 Binary files /dev/null and b/samples/textSample/fonts/Amiri-Regular.ttf differ diff --git a/samples/textSample/fonts/AwalRamadhan.ttf b/samples/textSample/fonts/AwalRamadhan.ttf new file mode 100644 index 0000000..7bd6420 Binary files /dev/null and b/samples/textSample/fonts/AwalRamadhan.ttf differ diff --git a/samples/textSample/fonts/Horev CLM Heavy.ttf b/samples/textSample/fonts/Horev CLM Heavy.ttf new file mode 100644 index 0000000..9927525 Binary files /dev/null and b/samples/textSample/fonts/Horev CLM Heavy.ttf differ diff --git a/samples/textSample/fonts/Palem-nm.ttf b/samples/textSample/fonts/Palem-nm.ttf new file mode 100644 index 0000000..4568cde Binary files /dev/null and b/samples/textSample/fonts/Palem-nm.ttf differ diff --git a/samples/textSample/fonts/batang.ttf b/samples/textSample/fonts/batang.ttf new file mode 100755 index 0000000..fb1a411 Binary files /dev/null and b/samples/textSample/fonts/batang.ttf differ diff --git a/samples/textSample/fonts/sazanami-mincho.ttf b/samples/textSample/fonts/sazanami-mincho.ttf new file mode 100755 index 0000000..69c9ca8 Binary files /dev/null and b/samples/textSample/fonts/sazanami-mincho.ttf differ diff --git a/samples/textSample/textures/back.jpg b/samples/textSample/textures/back.jpg new file mode 100644 index 0000000..470a679 Binary files /dev/null and b/samples/textSample/textures/back.jpg differ diff --git a/samples/textSample/textures/bottom.jpg b/samples/textSample/textures/bottom.jpg new file mode 100644 index 0000000..893f394 Binary files /dev/null and b/samples/textSample/textures/bottom.jpg differ diff --git a/samples/textSample/textures/bricks.jpg b/samples/textSample/textures/bricks.jpg new file mode 100644 index 0000000..d99c362 Binary files /dev/null and b/samples/textSample/textures/bricks.jpg differ diff --git a/samples/textSample/textures/bricksNormal.jpg b/samples/textSample/textures/bricksNormal.jpg new file mode 100644 index 0000000..b9c8b69 Binary files /dev/null and b/samples/textSample/textures/bricksNormal.jpg differ diff --git a/samples/textSample/textures/defaultTexture.jpg b/samples/textSample/textures/defaultTexture.jpg new file mode 100755 index 0000000..a1451cc Binary files /dev/null and b/samples/textSample/textures/defaultTexture.jpg differ diff --git a/samples/textSample/textures/front.jpg b/samples/textSample/textures/front.jpg new file mode 100644 index 0000000..4e17b77 Binary files /dev/null and b/samples/textSample/textures/front.jpg differ diff --git a/samples/textSample/textures/left.jpg b/samples/textSample/textures/left.jpg new file mode 100644 index 0000000..5750b91 Binary files /dev/null and b/samples/textSample/textures/left.jpg differ diff --git a/samples/textSample/textures/right.jpg b/samples/textSample/textures/right.jpg new file mode 100644 index 0000000..8963037 Binary files /dev/null and b/samples/textSample/textures/right.jpg differ diff --git a/samples/textSample/textures/top.jpg b/samples/textSample/textures/top.jpg new file mode 100644 index 0000000..4db3c2a Binary files /dev/null and b/samples/textSample/textures/top.jpg differ diff --git a/samples/textSample/textures/woodChips.jpg b/samples/textSample/textures/woodChips.jpg new file mode 100755 index 0000000..74c2d86 Binary files /dev/null and b/samples/textSample/textures/woodChips.jpg differ diff --git a/text.cpp b/text.cpp index 9fabf38..b888931 100755 --- a/text.cpp +++ b/text.cpp @@ -8,6 +8,7 @@ #include "glad.h" #include #include +#include #include #include FT_FREETYPE_H @@ -64,7 +65,7 @@ namespace vb01{ shader->setVec3(node->getPosition(), "pos"); Vector2 advanceOffset = Vector2::VEC_ZERO; - for(int i = 0; i < entry.length(); i++){ + for(int i = (leftToRight ? 0 : entry.length() - 1); (leftToRight ? (i < entry.length()) : (i >= 0)); (leftToRight ? i++ : i--)){ Glyph *glyphPtr = getGlyph(entry[i]); if(!glyphPtr) continue; @@ -81,8 +82,7 @@ namespace vb01{ void Text::prepareGlyphs(Glyph glyph, Vector2 advanceOffset){ Vector3 nodePos = node->getPosition(); - Vector2 origin = Vector2(nodePos.x, nodePos.y); - origin = origin + (advanceOffset * scale); + Vector2 origin = Vector2(nodePos.x, nodePos.y) + (advanceOffset * scale); Vector2 size = glyph.size * scale, bearing = glyph.bearing * scale; float data[] = { diff --git a/text.h b/text.h index 5e01359..84fdbc7 100755 --- a/text.h +++ b/text.h @@ -32,6 +32,8 @@ namespace vb01{ inline std::wstring getText(){return entry;} inline bool isHorizontal(){return horizontal;} inline void setHorizontal(bool h){this->horizontal = h;} + inline bool isLeftToRight(){return leftToRight;} + inline void setLeftToRight(bool ltr){this->leftToRight = ltr;} inline Material* getMaterial(){return material;} inline void setMaterial(Material *mat){this->material = mat;} private: @@ -46,7 +48,7 @@ namespace vb01{ std::vector characters; unsigned int VAO, VBO; float scale = 1; - bool horizontal = true; + bool horizontal = true, leftToRight = true; }; } diff --git a/textSample.cpp b/textSample.cpp new file mode 100644 index 0000000..ad0d5c7 --- /dev/null +++ b/textSample.cpp @@ -0,0 +1,114 @@ +#include + +#include "root.h" +#include "text.h" +#include "material.h" +#include "node.h" +#include "animationController.h" +#include "animationChannel.h" + +using namespace std; +using namespace vb01; + +int main(){ + Root *root = Root::getSingleton(); + Node *guiNode = root->getGuiNode(); + const string PATH = "../", FONT_PATH = PATH + "samples/textSample/fonts/", TEX_PATH = PATH + "samples/textSample/textures/"; + root->start(800, 600, PATH, "TextSample"); + + int numTexts = 4; + wstring texts[] = { + L"대한민국", + L"ĄČĘĖĮŠŲŪ„“", + L"عشر", + L"中華民國" + }; + string fonts[] = { + "batang.ttf", + "Palem-nm.ttf", + "Amiri-Regular.ttf", + "sazanami-mincho.ttf" + }; + bool leftToRight[] = { + true, + true, + false, + true + }; + bool horizontal[] = { + true, + true, + true, + false + }; + + Material *textMat = new Material(Material::MATERIAL_TEXT); + textMat->setTexturingEnabled(true); + string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"}; + Texture *texture = new Texture(frames, 2); + textMat->addDiffuseMap(texture); + + for(int i = 0; i < numTexts; i++){ + Text *text = new Text(FONT_PATH + fonts[i], texts[i]); + text->setLeftToRight(leftToRight[i]); + text->setScale(.5); + text->setHorizontal(horizontal[i]); + text->setMaterial(textMat); + Node *textNode = new Node(Vector3(0, 50 * (i + 1), 0), Quaternion::QUAT_W, Vector3::VEC_IJK, "", new AnimationController()); + textNode->addText(text); + guiNode->attachChild(textNode); + + KeyframeChannel kcA; + kcA.animatable = texture; + kcA.type = KeyframeChannel::TEXTURE_FRAME_A; + Keyframe k0; + k0.interpolation = Keyframe::CONSTANT; + k0.value = 0; + k0.frame = 1; + kcA.keyframes = vector({k0}); + + KeyframeChannel kcB; + kcB.animatable = texture; + kcB.type = KeyframeChannel::TEXTURE_FRAME_B; + Keyframe k1; + k1.interpolation = Keyframe::CONSTANT; + k1.value = 1; + k1.frame = 1; + kcB.keyframes = vector({k1}); + + KeyframeChannel kcC; + kcC.animatable = texture; + kcC.type = KeyframeChannel::TEXTURE_MIX_RATIO; + Keyframe k; + k.interpolation = Keyframe::LINEAR; + k.value = 0; + k.frame = 1; + Keyframe kk; + kk.interpolation = Keyframe::LINEAR; + kk.value = 1; + kk.frame = 15; + Keyframe kkk; + kkk.interpolation = Keyframe::LINEAR; + kkk.value = 0; + kkk.frame = 29; + kcC.keyframes = vector({k, kk, kkk}); + Animation *anim = new Animation("tex"); + anim->addKeyframeChannel(kcA); + anim->addKeyframeChannel(kcB); + anim->addKeyframeChannel(kcC); + + AnimationController *controller = textNode->getAnimationController(); + controller->addAnimation(anim); + AnimationChannel *channel = new AnimationChannel(controller); + channel->addAnimatable(texture); + channel->setAnimationName("tex"); + channel->setLoop(true); + controller->addAnimationChannel(channel); + } + + while(true){ + root->update(); + } + + return 0; +}