Code Monkey home page Code Monkey logo

shader-3dcurve's Introduction

GLSL shader for 3D Bezier curves with added fog effect (using OpenSceneGraph)

GLSL shader for lines and curves

This is an example project that demonstrates how to draw thick and smooth lines / curves in 3D. It is known that the native GL_LINE_STRIP_ADJACENCY creates gaps when drawing a polyline. Another restriction of the above mode is that thickness cannot surpass certain threshold (e.g., 10.f, depending on the machine). This code demonstrates how a GLSL shaders can help to solve the above problems.

It is possible to draw two types of geometries with the provided shaders:

  1. Polylines
  2. Bezier curves

Both of the geometries can be placed in a 3D space with any line thickness.

As a bonus, I added a simplest fog effect inside the fragment shader for a Bezier curve.

3D polylines

The polyline shaders can be found as Shaders/polyline.* files. The vertex and fragment shaders are simple pass-through, while the geometry shader provides the functionality to turn the input geometry into a thich triangular strip which is always turned towards the screen.

3D Bezier curves

Refer to the Shaders/bezier.* set of files. The main principle is the same as for the polyline shaders. The vertex shader is a pass-through. The fragment shader demostrates a simple fog effect on the geometry, and the geometrical shader treats the input geometry as Bezier control points, breaks the input curve into the provided number of segments and draws each segment in the same manner as a polyline segment. I.e., it emits a rectangular strip which is always turned towards the camera.

Requirement

  • Compiler that supports C++, e.g., GCC
  • OpenSceneGraph library (>= 3.4.0)
  • Graphics card supporting OpenGL (>=3.3) - make sure your drivers are updated
  • CMake (>=2.8.11)

Troubleshoot

On slightly older machines, it is possible to encounter the Error C6033 : Hardware limitations reached, can only emit ... vertices of this size.. In this case, modify the number of maximum emitting vertices within the shader file.

In Stroke.geom file, seek for SegmentsMax and max_vertices variables, and, depending on the maximum number of vertices your hardware supports, change their values. E.g., if the number of maximum vertices is 102 on your machine (based on the error message), set:

const int SegmentsMax = 24; // max_vertices = (SegmentsMax+1)*4;
// ...
layout(triangle_strip, max_vertices = 100) out;

Build

Use CMakeLists.txt to do the build, then run shader-3dcurve.

shader-3dcurve's People

Contributors

bogdanni avatar vicrucann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shader-3dcurve's Issues

interpolation error

Hi, your idea is great! Howerve, I found that gl_Positions are used for interpolation before normalized by gl_Position.w, which is a non-linear behavior. I suppose they should be normalized by w and then interpolated to get bezier points.
Thanks for your sharing!

OpenGL Error when running in linux without qt

We compile this project without qt in linux and there is no compiler or linker error.When we try to run we got this error.

OpenGL error 'invalid enumerant' at after RenderBin::draw(..)
OpenGL error 'invalid enumerant' at after RenderBin::draw(..)
OpenGL error 'invalid enumerant' at after RenderBin::draw(..)
...
OpenGL error 'invalid enumerant' at after RenderBin::draw(..)

Windows pops up but there is no shape in the scene.

Harden against arbitrary inputs

The first three normalize() in drawSegment() of polyline.geom may have trouble with arbitrary input.

I had to define

vec2 safe_normalize(vec2 v)
{
    float len = length(v);
    if (len == 0)
        return vec2(0, 1); // arbitrary
    else
        return v / len;
}

In my observation, ATI drivers do the right thing, NVIDIA and Intel drivers return something unreasonable.

The second pair of normalize() are maybe safe (?).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.