Note that there are some explanatory texts on larger screens.

plurals
  1. POMinimum size of rendered object using GL_LINES in iOS Open GL ES
    primarykey
    data
    text
    <p>I have just completed the first version of my iOS app, <a href="http://itunes.apple.com/us/app/corebox/id503591554" rel="nofollow noreferrer">Corebox</a>, and am now working on some new features.</p> <p>One of the new features is a "small" tweak to the OpenGL rendering to force some objects to never be drawn smaller than a minimum size. All of the objects needing this treatment are simple 2 point lines drawn with GL_LINES.</p> <p>This annotated screenshot explains what I'm after. Ignore the grey lines, the only objects I'm interested in altering are the yellow wider lines.</p> <p><img src="https://i.stack.imgur.com/cjkKM.png" alt="Corebox OpenGL scene"></p> <p>I have googled this extensively and it seems what I need to do is alter the geometry of the lines using a vertex shader. I'm quite new to GLSL and most shader examples I can find deal with applying lighting and other effects, eg: <a href="http://glsl.heroku.com" rel="nofollow noreferrer">GLSL Heroku Editor</a> and <a href="http://www.kickjs.org/example/shader_editor/shader_editor.html" rel="nofollow noreferrer">KicksJS shader editor</a>.</p> <p>My current vertex shader is extremely basic:</p> <pre><code>// GL_LINES vertex shader uniform mat4 Projection; uniform mat4 Modelview; attribute vec4 Position; attribute vec4 SourceColor; varying vec4 DestinationColor; void main(void) { DestinationColor = SourceColor; gl_Position = Projection * Modelview * Position; } </code></pre> <p>As is my fragment shader:</p> <pre><code>// GL_LINES fragment shader varying lowp vec4 DestinationColor; void main(void) { gl_FragColor = DestinationColor; } </code></pre> <p>My guess as to what is required:</p> <ul> <li>Determine the distance between the viewer (camera position) and the object</li> <li>Determine how big the object is on the screen, based on its size and distance from camera</li> <li>If the object will be too small then adjust its vertices such that it becomes large enough to easily see on the screen.</li> </ul> <p>Caveats and other notes:</p> <ol> <li>But if you zoom out won't this cause the model to be just a blob of orange on the screen? Yes, this is exactly the effect I'm after.</li> </ol> <p><strong>Edit: Here is the final working version implementing suggestions by mifortin</strong></p> <pre><code>uniform mat4 Projection; uniform mat4 Modelview; uniform float MinimumHeight; attribute vec4 Position; attribute vec4 ObjectCenter; attribute vec4 SourceColor; varying vec4 DestinationColor; void main(void) { // screen-space position of this vertex vec4 screenPosition = Projection * Modelview * Position; // screen-space mid-point of the object this vertex belongs to vec4 screenObjectCenter = Projection * Modelview * ObjectCenter; // Z should be 0 by this time and the projective transform in w. // scale so w = 1 (these two should be in screen-space) vec2 newScreenPosition = screenPosition.xy / screenPosition.w; vec2 newObjectCenter = screenObjectCenter.xy / screenObjectCenter.w; float d = distance(newScreenPosition, newObjectCenter); if (d &lt; MinimumHeight &amp;&amp; d &gt; 0.0) { // Direction of this object, this really only makes sense in the context // of a line (eg: GL_LINES) vec2 towards = normalize(newScreenPosition - newObjectCenter); // Shift the center point then adjust the vertex position accordingly // Basically this converts: *--x--* into *--------x--------* newObjectCenter = newObjectCenter + towards * MinimumHeight; screenPosition.xy = newObjectCenter.xy * screenPosition.w; } gl_Position = screenPosition; DestinationColor = SourceColor; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload