How to Draw Inside the Black Edges in iOS Sdk with Opengl Es

OpenGL ES Fill Effect

So you render a set of points and want OpenGL to "magically" fill the enclosed region. That's not possible. OpenGL doesn't realize that these points enclose some region. You don't even draw a line strip, it's just a set of points. Even a human has to put in a reasonable effort of thinking to see that the points "enclose" a region, let aside a computer, or just a simple interface for drawing points, lines and triangles onto the screen.

Instead of drawing points, just draw a polygon (use GL_POLYGON or GL_TRIANGLE_FAN instead of GL_POINTS). But if the enclosed region is non-convex that won't work in all cases. What you always have to realize is, that OpenGL is nothing more than a drawing API. It just draws points, lines and triangles to screen. Yes, with fancy effects, but all in all it just draws simple primitives. It doesn't manage any underlying graphics scene or something. The moment a primitive (like a single point, line or triangle) has been drawn, OpenGL doesn't remember it anymore.

What you want to achieve (given you at least change the point set to a line loop that really encloses a region), is not to be achieved by simple means. In the simplest case you need some kind of flood fill algorithm that fills the region you enclosed by the lines. But for this you don't profit from OpenGL in any way, as this requires you to analyse the image on the CPU and set individual pixels. And neither can shaders do this in a simple (or any?) way.

Drawing border of constant width around image in OpenGL on Android and iOS

The border is way thicker on iOS devices compared to Android devices probably because of the Retina display.

Determine the DPI using DisplayMetrics, and adjust your BORDER_WIDTH value as appropriate.

The width of the border on the top and the bottom of the image is different from that on the sides of the image.

Viewport coordinates are between zero and one, regardless of aspect ratio. If you're not correcting for aspect ratio, then everything is going to get scaled by that.

Alternatively, you are doing calculations assuming pixels are square. They usually aren't.

How do I draw a filled circle with OpenGL ES on iPhone?

For a truly smooth circle, you're going to want a custom fragment shader. For example, the following vertex shader:

 attribute vec4 position;
attribute vec4 inputTextureCoordinate;

varying vec2 textureCoordinate;

void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}

and fragment shader:

 varying highp vec2 textureCoordinate;

const highp vec2 center = vec2(0.5, 0.5);
const highp float radius = 0.5;

void main()
{
highp float distanceFromCenter = distance(center, textureCoordinate);
lowp float checkForPresenceWithinCircle = step(distanceFromCenter, radius);

gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) * checkForPresenceWithinCircle;
}

will draw a smooth red circle within a square that you draw to the screen. You'll need to supply vertices for your square to the position attribute and coordinates that range from 0.0 to 1.0 in X and Y to the inputTextureCoordinate attribute, but this will draw a circle that's as sharp as your viewport's resolution allows and do so very quickly.

How do I get a colored texture brush to show up in Open GL ES on white background?

Ok, so you're trying to "paint" a given colour (in this case "red") on to a background, using a mask for the brush shape.

You need to do the following before you start rendering the "paint":

  1. First make sure your brush has an alpha channel that corresponds with its shape - that is the alpha channel should look similar to the brush image you posed.

  2. Render with these states set (note space to get around wiki markup):

// Make the current material colour track the current color
glEnable( GL_COLOR_MATERIAL );

// Multiply the texture colour by the material colour.
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// Alpha blend each "dab" of paint onto background
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

See also:

http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/colormaterial.html

http://www.khronos.org/opengles/documentation/opengles1_0/html/glTexEnv.html



Related Topics



Leave a reply



Submit