File: fontShader.glsl

package info (click to toggle)
opensubdiv 3.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,132 kB
  • sloc: cpp: 137,820; python: 1,069; objc: 412; javascript: 361; lisp: 216; ansic: 170; makefile: 24
file content (172 lines) | stat: -rw-r--r-- 4,512 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
//   Copyright 2013 Pixar
//
//   Licensed under the Apache License, Version 2.0 (the "Apache License")
//   with the following modification; you may not use this file except in
//   compliance with the Apache License and the following modification to it:
//   Section 6. Trademarks. is deleted and replaced with:
//
//   6. Trademarks. This License does not grant permission to use the trade
//      names, trademarks, service marks, or product names of the Licensor
//      and its affiliates, except as required to comply with Section 4(c) of
//      the License and to reproduce the content of the NOTICE file.
//
//   You may obtain a copy of the Apache License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the Apache License with the above modification is
//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//   KIND, either express or implied. See the Apache License for the specific
//   language governing permissions and limitations under the Apache License.
//

#if __VERSION__ < 420
    #define centroid
#endif

layout(std140) uniform Transform {
    mat4 ModelViewMatrix;
    mat4 ProjectionMatrix;
    mat4 ModelViewProjectionMatrix;
};

//--------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------
#ifdef VERTEX_SHADER

layout (location=0) in vec4 position;
layout (location=1) in vec4 data;

out block {
    vec4 position;
    vec4 data;
} outpt;

void main()
{
    outpt.position = ModelViewMatrix * position;
    outpt.data = data;
}

#endif


//--------------------------------------------------------------
// Geometry Shader
//--------------------------------------------------------------
#ifdef GEOMETRY_SHADER

layout(points) in;

#define NVERTS 4

layout(triangle_strip, max_vertices = NVERTS) out;

in block {
    vec4 position;
    vec4 data;
} inpt[];

out block {
    vec4 position;
    centroid vec2 uv;
    flat int colorId;
} outpt;

void emit(int index, vec2 offset, vec2 uv)
{
    outpt.position = inpt[0].position;
    outpt.uv = uv;
    outpt.colorId = int(inpt[0].data.w);

    gl_Position = (ProjectionMatrix * inpt[0].position) + vec4(offset, -0.01, 0.0);
    EmitVertex();
}

#define FONT_TEXTURE_WIDTH 128
#define FONT_TEXTURE_HEIGHT 128
#define FONT_TEXTURE_COLUMNS 16
#define FONT_TEXTURE_ROWS 8

#define FONT_CHAR_WIDTH (FONT_TEXTURE_WIDTH/FONT_TEXTURE_COLUMNS)
#define FONT_CHAR_HEIGHT (FONT_TEXTURE_HEIGHT/FONT_TEXTURE_ROWS)

vec2 computeUV( int c ) 
{
    c = c % 0x7f;

    return vec2( float(c%FONT_TEXTURE_COLUMNS)/float(FONT_TEXTURE_COLUMNS),
                 float(c/FONT_TEXTURE_COLUMNS)/float(FONT_TEXTURE_ROWS)     );

}

uniform float scale=0.01;

void main()
{
    gl_PrimitiveID = gl_PrimitiveIDIn;

    vec2 uv = computeUV(int(inpt[0].data.z));

    vec2 dim = vec2(1.0/FONT_TEXTURE_COLUMNS, 
                    1.0/FONT_TEXTURE_ROWS);

    vec2 ofs = inpt[0].data.xy;

    vec4 clipPos = ProjectionMatrix * inpt[0].position;

    float s = scale * clipPos.w;

    emit(0, s * (vec2( 1.0, -2.0)+ofs), uv + dim);
    emit(1, s * (vec2( 1.0,  2.0)+ofs), vec2(uv.x+dim.x, uv.y));
    emit(2, s * (vec2(-1.0, -2.0)+ofs), vec2(uv.x,       uv.y+dim.y));
    emit(3, s * (vec2(-1.0,  2.0)+ofs), uv);

    EndPrimitive();
}

#endif

//--------------------------------------------------------------
// Fragment Shader
//--------------------------------------------------------------
#ifdef FRAGMENT_SHADER

in block {
    vec4 position;
    centroid vec2 uv;
    flat int colorId;
} inpt;

uniform sampler2D font;

out vec4 outColor;
out vec3 outNormal;

const vec4 colors[9] = vec4[9](vec4(0.9,0.9,0.9,1.0),

                               vec4(1.0,0.3,0.3,1.0),
                               vec4(0.3,1.0,0.3,1.0),
                               vec4(0.3,0.3,1.0,1.0),
                               
                               vec4(0.0,1.0,0.0,1.0),     // green --- yellow --- red
                               vec4(0.5,1.0,0.0,1.0),
                               vec4(1.0,1.0,0.0,1.0),
                               vec4(1.0,0.5,0.0,1.0),
                               vec4(1.0,0.0,0.0,1.0));

void main()
{
    vec4 bitmap = texture(font, inpt.uv);
    if (bitmap.a == 0.0) discard;

    outColor = bitmap * colors[inpt.colorId];
    outNormal = vec3(0.0,0.0,1.0);
    //outColor = vec4(inpt.v.uv,0.0,1.0);
}

#endif