patriciogonzalezvivo / glslViewer

Console-based GLSL Sandbox for 2D/3D shaders
BSD 3-Clause "New" or "Revised" License
4.65k stars 351 forks source link

Trying to write shader with lines from OBJ file #333

Open snibbe opened 1 year ago

snibbe commented 1 year ago

Hi, I'm just getting started with glslViewer (thanks @patriciogonzalezvivo!). I'm trying to work with an OBJ file consisting only of lines, but the lines won't render using a minimal shader. It looks like it might be an issue with the clipping planes and not computing a bounding box for line objects? But I am not certain. Any advice welcome. Minimal files include inline below. Scott

test.obj:

g body
v   0 0 0 
v 500 0 0
v 500 500 0
v 0 500 0
l 1 2 2 3 3 4 4 1

test.frag:

#ifdef GL_ES
precision mediump float;
#endif

uniform vec3        u_camera;
uniform vec3        u_light;

uniform vec2        u_resolution;
uniform vec2        u_mouse;
uniform float       u_time;

varying vec4        v_position;
varying vec3        v_normal;

#if defined(MODEL_VERTEX_TEXCOORD)
varying vec2        v_texcoord;
#endif

void main(void) {
    vec4 color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragColor = color;
}
patriciogonzalezvivo commented 1 year ago

Hi @snibbe! So happy and honored seeing you here! Just send you a quick mail, this is one of those cases. GlslViewer geometry loaders mostly know to work with points and triangles at the moment. I usually work with lines them by creating collapsed geometry composed of triangles. The trick require:

I know a bit convoluted. I will try to bring supporting for them when I have some time.

snibbe commented 1 year ago

I'll use the workaround. Thanks for such a quick answer!

snibbe commented 1 year ago

If you happen to have an example vertex shader that does this, I would be very grateful if you posted it here. The solution I just tried uses gl_VertexID, but for some reason the glsl version on my Mac in glsViewer is limited to 1.2 and doesn't support gl_VertexID.

patriciogonzalezvivo commented 1 year ago

I found this example, is slightly different because it encode the direction that the vertex needs to take on the a_normal vertex attribute.

shader.vert

#ifdef GL_ES
precision mediump float;
#endif

uniform mat4    u_projectionViewMatrix;

uniform float   u_radius;
uniform float   u_width;

attribute vec4  a_position;
attribute vec4  a_color;
attribute vec3  a_normal;
attribute vec2  a_texcoord;

varying vec4    v_position;
varying vec4    v_color;
varying vec2    v_texcoord;

void main() {
    v_texcoord = a_texcoord;
    v_position = a_position;
    v_color = a_color;

    v_position.xyz += a_normal * u_width;

    gl_Position = u_projectionViewMatrix * v_position;
}