Skip to content

Commit 7a3f627

Browse files
committed
Drawin3dText in place
1 parent be67a99 commit 7a3f627

File tree

7 files changed

+213
-0
lines changed

7 files changed

+213
-0
lines changed

sketches/Drawing3dText/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cmake_minimum_required (VERSION 3.2.2)
2+
3+
project(Drawing3dText)
4+
5+
set(INCLUDE_DIRS
6+
$ENV{CODE_PATH}
7+
)
8+
9+
set(SRC_FILES
10+
"src/Sketch.cpp"
11+
"$ENV{CODE_PATH}/common/xf/Font.cpp"
12+
"$ENV{CODE_PATH}/common/xf/FontManager.cpp"
13+
"$ENV{CODE_PATH}/common/xf/FontSequence.cpp"
14+
"$ENV{CODE_PATH}/common/quad/QuadBatch.cpp"
15+
)
16+
17+
#
18+
# For Mojave and up
19+
#
20+
add_compile_definitions(
21+
GL_SILENCE_DEPRECATION
22+
)
23+
24+
include("$ENV{CROSS_PATH}/core/cmake/sketch.cmake")
25+
include("$ENV{CROSS_PATH}/core/cmake/test.cmake")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
set(CTEST_PROJECT_NAME Drawing3dText)
3+
set(CTEST_CONFIGURATION_TYPE Release)
4+
5+
if (PLATFORM MATCHES osx)
6+
set(ARGS
7+
-DRUN=EXE
8+
)
9+
elseif (PLATFORM MATCHES emscripten)
10+
set(ARGS
11+
-DRUN=BROWSER
12+
)
13+
endif()
14+
15+
include("$ENV{CROSS_PATH}/core/cmake/platforms.cmake")
Binary file not shown.

sketches/Drawing3dText/res/song.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Walking across the sitting-room, I turn the television off
2+
Sitting beside you, I look into your eyes
3+
As the sound of motorcars fades in the night time
4+
I swear I saw your face change, it didn't seem quite right
5+
And it's hello babe, with your guardian eyes so blue
6+
Hey my baby, don't you know our love is true

sketches/Drawing3dText/src/Sketch.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include "Sketch.h"
2+
3+
using namespace std;
4+
using namespace chr;
5+
using namespace gl;
6+
7+
static constexpr float FOVY = 45;
8+
static constexpr float CAMERA_DISTANCE = 180;
9+
static constexpr float CAMERA_ELEVATION = -85;
10+
11+
static constexpr float R = 50;
12+
static constexpr float TURNS = 10;
13+
static constexpr float H = 100;
14+
15+
void Sketch::setup()
16+
{
17+
auto lines = utils::readLines<u16string>(InputSource::resource("song.txt")); // Lyrics by Genesis
18+
19+
u16string text;
20+
for (const auto &line : lines)
21+
{
22+
text += line + u" ";
23+
}
24+
25+
font = fontManager.getFont(InputSource::resource("Helvetica_Bold_64.fnt"), XFont::Properties3d());
26+
font->setShader(textureAlphaShader);
27+
font->setSize(8);
28+
29+
float L = TWO_PI * TURNS * R;
30+
float offset = (L - font->getStringAdvance(text)) / 2;
31+
32+
font->beginSequence(sequence);
33+
drawTextHelix(*font, text, R, TURNS, -H, offset, font->getOffsetY(XFont::ALIGN_MIDDLE));
34+
font->endSequence();
35+
36+
// ---
37+
38+
glDisable(GL_DEPTH_TEST);
39+
glDepthMask(GL_FALSE);
40+
glEnable(GL_CULL_FACE);
41+
42+
glEnable(GL_BLEND);
43+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
44+
}
45+
46+
void Sketch::resize()
47+
{
48+
camera
49+
.setFov(FOVY)
50+
.setClip(1, 1000)
51+
.setWindowSize(windowInfo.size);
52+
}
53+
54+
void Sketch::draw()
55+
{
56+
glClearColor(0, 0, 0, 1);
57+
glClear(GL_COLOR_BUFFER_BIT);
58+
59+
// ---
60+
61+
camera.getViewMatrix()
62+
.setIdentity()
63+
.scale(1, -1, 1)
64+
.translate(0, 0, -CAMERA_DISTANCE)
65+
.rotateX(-CAMERA_ELEVATION * D2R)
66+
.rotateZ(clock()->getTime() * 0.5f)
67+
.translate(0, 0, H / 2);
68+
69+
State()
70+
.setShaderMatrix(camera.getViewProjectionMatrix())
71+
.apply();
72+
73+
// ---
74+
75+
glCullFace(GL_FRONT);
76+
font->setColor(1, 1, 1, 0.333f);
77+
font->replaySequence(sequence);
78+
79+
glCullFace(GL_BACK);
80+
font->setColor(1, 1, 1, 1);
81+
font->replaySequence(sequence);
82+
}
83+
84+
void Sketch::drawTextHelix(XFont &font, const u16string &text, float r, float turns, float h, float offsetX, float offsetY)
85+
{
86+
float l = TWO_PI * turns;
87+
float L = PI * turns * (r + r);
88+
float dz = h / l;
89+
float ay = -atan2f(h, L);
90+
91+
Matrix matrix;
92+
93+
for (auto c : text)
94+
{
95+
auto glyphIndex = font.getGlyphIndex(c);
96+
float halfWidth = font.getGlyphAdvance(glyphIndex) / 2;
97+
offsetX += halfWidth;
98+
99+
if (glyphIndex >= 0)
100+
{
101+
float d = offsetX / r;
102+
matrix
103+
.setTranslate(-cosf(d) * r, +sinf(d) * r, d * dz)
104+
.rotateZ(HALF_PI - d)
105+
.rotateY(ay)
106+
.rotateX(-HALF_PI);
107+
108+
font.addGlyph(matrix, glyphIndex, -halfWidth, offsetY);
109+
}
110+
111+
offsetX += halfWidth;
112+
}
113+
}

sketches/Drawing3dText/src/Sketch.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include "chr/cross/Context.h"
4+
#include "chr/gl/Batch.h"
5+
#include "chr/gl/Camera.h"
6+
#include "chr/gl/shaders/TextureAlphaShader.h"
7+
8+
#include "common/xf/FontManager.h"
9+
10+
class Sketch : public chr::CrossSketch
11+
{
12+
public:
13+
void setup() final;
14+
void resize() final;
15+
void draw() final;
16+
17+
protected:
18+
chr::gl::Camera camera;
19+
20+
chr::xf::FontManager fontManager;
21+
std::shared_ptr<chr::xf::Font> font;
22+
chr::xf::FontSequence sequence;
23+
chr::gl::shaders::TextureAlphaShader textureAlphaShader;
24+
25+
static void drawTextHelix(chr::XFont &font, const std::u16string &text, float r, float turns, float h, float offsetX = 0, float offsetY = 0);
26+
};

sketches/Drawing3dText/src/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#include "Sketch.h"
3+
4+
namespace chr
5+
{
6+
CrossSketch* createSketch()
7+
{
8+
return new Sketch();
9+
}
10+
}
11+
12+
#if defined(CHR_PLATFORM_DESKTOP)
13+
14+
int main(int argc, char** argv)
15+
{
16+
chr::CrossSketch::run(1000, 1000, 4);
17+
return 0;
18+
}
19+
20+
#elif defined(CHR_PLATFORM_EMSCRIPTEN)
21+
22+
int main(int argc, char** argv)
23+
{
24+
chr::CrossSketch::run(4);
25+
return 0;
26+
}
27+
28+
#endif

0 commit comments

Comments
 (0)