[openrndr-demos] Add DemoTessShader03
This commit is contained in:
57
openrndr-demos/src/demo/kotlin/DemoTessShader03.kt
Normal file
57
openrndr-demos/src/demo/kotlin/DemoTessShader03.kt
Normal file
@@ -0,0 +1,57 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.draw.DrawPrimitive
|
||||
import org.openrndr.draw.Shader
|
||||
import org.openrndr.draw.vertexBuffer
|
||||
import org.openrndr.draw.vertexFormat
|
||||
import org.openrndr.resourceUrl
|
||||
import org.openrndr.shape.Ellipse
|
||||
|
||||
fun main() {
|
||||
application {
|
||||
program {
|
||||
|
||||
val ellipse = Ellipse(width/2.0, height/2.0, 100.0, 200.0).contour
|
||||
|
||||
val vb = vertexBuffer(vertexFormat {
|
||||
position(3)
|
||||
}, ellipse.segments.size * 4)
|
||||
|
||||
val shader = Shader.createFromUrls(
|
||||
vsUrl = resourceUrl("/shaders/ts-03.vert"),
|
||||
tcsUrl = resourceUrl("/shaders/ts-03.tesc"),
|
||||
tesUrl = resourceUrl("/shaders/ts-03.tese"),
|
||||
gsUrl = resourceUrl("/shaders/ts-03.geom"),
|
||||
fsUrl = resourceUrl("/shaders/ts-03.frag")
|
||||
)
|
||||
|
||||
vb.put {
|
||||
for (segment in ellipse.segments) {
|
||||
val cubic = segment.cubic
|
||||
write(cubic.start.xy0)
|
||||
write(cubic.control[0].xy0)
|
||||
write(cubic.control[1].xy0)
|
||||
write(cubic.end.xy0)
|
||||
}
|
||||
}
|
||||
|
||||
extend {
|
||||
drawer.clear(ColorRGBa.PINK)
|
||||
|
||||
drawer.translate(width/2.0, height/2.0, 0.0)
|
||||
drawer.rotate(seconds*45.0)
|
||||
drawer.translate(-width/2.0, -height/2.0, 0.0)
|
||||
|
||||
shader.begin()
|
||||
shader.uniform("offset", mouse.position.xy0)
|
||||
shader.uniform("view", drawer.view)
|
||||
shader.uniform("proj", drawer.projection)
|
||||
shader.uniform("model", drawer.model)
|
||||
shader.uniform("resolution", ((mouse.position.x / width) * 63 + 1).toInt())
|
||||
shader.uniform("weight",((mouse.position.y / height) * 128 + 1) )
|
||||
driver.drawVertexBuffer(shader, listOf(vb), DrawPrimitive.PATCHES, 0, vb.vertexCount)
|
||||
shader.end()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
layout(vertices = 4) out; // 4 points per patch
|
||||
|
||||
uniform int resolution;
|
||||
|
||||
|
||||
in vec3 va_position[];
|
||||
@@ -11,6 +12,6 @@ void main() {
|
||||
cva_position[gl_InvocationID] = va_position[gl_InvocationID];
|
||||
if(gl_InvocationID == 0) { // levels only need to be set once per patch
|
||||
gl_TessLevelOuter[0] = 1; // we're only tessellating one line
|
||||
gl_TessLevelOuter[1] = 4; // tessellate the line into 100 segments
|
||||
gl_TessLevelOuter[1] = resolution; // tessellate the line into 100 segments
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ layout(isolines) in;
|
||||
in vec3 cva_position[];
|
||||
|
||||
|
||||
|
||||
uniform mat4 proj;
|
||||
uniform mat4 view;
|
||||
uniform mat4 model;
|
||||
@@ -32,8 +33,6 @@ void main() {
|
||||
cva_position[1],
|
||||
cva_position[2],
|
||||
cva_position[3],
|
||||
|
||||
|
||||
t);
|
||||
|
||||
|
||||
|
||||
11
openrndr-demos/src/demo/resources/shaders/ts-03.frag
Normal file
11
openrndr-demos/src/demo/resources/shaders/ts-03.frag
Normal file
@@ -0,0 +1,11 @@
|
||||
#version 430 core
|
||||
|
||||
out vec4 o_color;
|
||||
|
||||
//in vec3 va_position;
|
||||
//in vec3 va_normal;
|
||||
//in vec4 v_addedProperty;
|
||||
|
||||
void main() {
|
||||
o_color = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
62
openrndr-demos/src/demo/resources/shaders/ts-03.geom
Normal file
62
openrndr-demos/src/demo/resources/shaders/ts-03.geom
Normal file
@@ -0,0 +1,62 @@
|
||||
#version 430 core
|
||||
|
||||
layout (lines) in;
|
||||
layout (triangle_strip, max_vertices = 4) out;
|
||||
|
||||
in InVertex {
|
||||
vec3 va_position;
|
||||
vec3 va_normal;
|
||||
vec4 v_addedProperty;
|
||||
} vertices[];
|
||||
|
||||
out vec3 va_position;
|
||||
out vec3 va_normal;
|
||||
out vec4 v_addedProperty;
|
||||
|
||||
|
||||
uniform vec3 offset;
|
||||
in vec3 derivative[];
|
||||
in vec3 position[];
|
||||
|
||||
uniform mat4 proj;
|
||||
uniform mat4 view;
|
||||
uniform mat4 model;
|
||||
uniform float weight;
|
||||
|
||||
void main() {
|
||||
mat4 pvm = proj * view * model;
|
||||
|
||||
vec2 direction0 = normalize(derivative[0].xy);
|
||||
vec4 perp0 = vec4(direction0.y, -direction0.x, 0.0, 0.0);
|
||||
|
||||
vec2 direction1 = normalize(derivative[1].xy);
|
||||
vec4 perp1 = vec4(direction1.y, -direction1.x, 0.0, 0.0);
|
||||
|
||||
// output a triangle strip encoded quad
|
||||
|
||||
v_addedProperty = vertices[0].v_addedProperty;
|
||||
va_normal = vertices[0].va_normal;
|
||||
va_position = vertices[0].va_position;
|
||||
gl_Position = pvm * vec4( vec4(position[0],1.0) + perp0 * weight);
|
||||
EmitVertex();
|
||||
|
||||
v_addedProperty = vertices[0].v_addedProperty;
|
||||
va_normal = vertices[0].va_normal;
|
||||
va_position = vertices[0].va_position;
|
||||
gl_Position = pvm * vec4( vec4(position[0],1.0) - perp0 * weight);
|
||||
EmitVertex();
|
||||
|
||||
v_addedProperty = vertices[1].v_addedProperty;
|
||||
va_normal = vertices[1].va_normal;
|
||||
va_position = vertices[1].va_position;
|
||||
gl_Position = pvm * vec4( vec4(position[1],1.0) + perp1 * weight);
|
||||
EmitVertex();
|
||||
|
||||
v_addedProperty = vertices[1].v_addedProperty;
|
||||
va_normal = vertices[1].va_normal;
|
||||
va_position = vertices[1].va_position;
|
||||
gl_Position = pvm * vec4( vec4(position[1],1.0) - perp1 * weight);
|
||||
EmitVertex();
|
||||
|
||||
EndPrimitive();
|
||||
}
|
||||
17
openrndr-demos/src/demo/resources/shaders/ts-03.tesc
Normal file
17
openrndr-demos/src/demo/resources/shaders/ts-03.tesc
Normal file
@@ -0,0 +1,17 @@
|
||||
#version 430 core
|
||||
|
||||
layout(vertices = 4) out; // 4 points per patch
|
||||
|
||||
uniform int resolution;
|
||||
|
||||
|
||||
in vec3 va_position[];
|
||||
out vec3 cva_position[];
|
||||
|
||||
void main() {
|
||||
cva_position[gl_InvocationID] = va_position[gl_InvocationID];
|
||||
if(gl_InvocationID == 0) { // levels only need to be set once per patch
|
||||
gl_TessLevelOuter[0] = 1; // we're only tessellating one line
|
||||
gl_TessLevelOuter[1] = resolution; // tessellate the line into 100 segments
|
||||
}
|
||||
}
|
||||
48
openrndr-demos/src/demo/resources/shaders/ts-03.tese
Normal file
48
openrndr-demos/src/demo/resources/shaders/ts-03.tese
Normal file
@@ -0,0 +1,48 @@
|
||||
#version 430 core
|
||||
|
||||
vec3 bezier2(vec3 a, vec3 b, float t) {
|
||||
return mix(a, b, t);
|
||||
}
|
||||
vec3 bezier3(vec3 a, vec3 b, vec3 c, float t) {
|
||||
return mix(bezier2(a, b, t), bezier2(b, c, t), t);
|
||||
}
|
||||
vec3 bezier4(vec3 a, vec3 b, vec3 c, vec3 d, float t) {
|
||||
return mix(bezier3(a, b, c, t), bezier3(b, c, d, t), t);
|
||||
}
|
||||
|
||||
struct Vertex {
|
||||
vec3 va_position;
|
||||
vec3 va_normal;
|
||||
vec4 v_addedProperty;
|
||||
};
|
||||
|
||||
layout(isolines) in;
|
||||
in vec3 cva_position[];
|
||||
|
||||
out vec3 derivative;
|
||||
out vec3 position;
|
||||
|
||||
uniform int resolution;
|
||||
|
||||
uniform mat4 proj;
|
||||
uniform mat4 view;
|
||||
uniform mat4 model;
|
||||
|
||||
void main() {
|
||||
float t = gl_TessCoord.x;
|
||||
vec3 ePos = bezier4(
|
||||
cva_position[0],
|
||||
cva_position[1],
|
||||
cva_position[2],
|
||||
cva_position[3],
|
||||
t);
|
||||
|
||||
// calculate derivative using Hodograph
|
||||
derivative = bezier3(cva_position[1] - cva_position[0], cva_position[2]-cva_position[1], cva_position[3]-cva_position[2], t);
|
||||
|
||||
// output model space positions
|
||||
position = ePos;
|
||||
|
||||
float r = resolution + 1.0;
|
||||
//gl_Position = proj * view * model * vec4(ePos, 1);
|
||||
}
|
||||
16
openrndr-demos/src/demo/resources/shaders/ts-03.vert
Normal file
16
openrndr-demos/src/demo/resources/shaders/ts-03.vert
Normal file
@@ -0,0 +1,16 @@
|
||||
#version 430 core
|
||||
|
||||
in vec3 a_position;
|
||||
|
||||
|
||||
out vec3 va_position;
|
||||
|
||||
uniform mat4 view;
|
||||
uniform mat4 proj;
|
||||
uniform mat4 model;
|
||||
|
||||
void main() {
|
||||
va_position = a_position;
|
||||
gl_Position = proj * view * model * vec4(a_position, 1.0);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user