Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Is there any way at all that I could make a checkerboard fill for a circle?

edited July 2014 in General Posts: 134

I made a very complex formula, connecting opposite sin,cos points on a circle but it takes up a lot of memory/space..could someone post a more efficient way of doing this if there is one? Thank you guys ^.^

Is there also any way to make the lines curve so the ball appears slightly 3 dimensional?

Tagged:

Comments

  • Posts: 1,595

    @Invad3rZIM There's definitely something of the sort on this forum, go digging in the search engine. Either way I don't think it will be fast with 3 dimensions unless you want to fake a third dimension using a shader (you'll need a shader either way unless you uv map the 3D mesh with a fit for size checker texture),

  • IgnatzIgnatz Mod
    edited July 2014 Posts: 5,396

    @Invad3rZIM - The secret is to fake it. Try this.

    function setup()
        setupSphere()
    end
    
    function setupSphere()
        --set up checkerboard pattern
        w,n=400,5 --size of image and number of squares
        img=image(w,w)
        setContext(img)
        background(0)
        fill(255)
        a=w/n
        for i=1,n do
            for j=1,n do
                if (i+j)%2==0 then rect((i-1)*a,(j-1)*a,a,a) end
            end
        end
        setContext()
        --create shader to draw sphere
        m=mesh()
        m:addRect(WIDTH/2,HEIGHT/2,w,w)   --add a rectangle to our mesh, the size you want (square)
        m.texture=img
        m.shader=shader(sphereShader.vertexShader,sphereShader.fragmentShader)
        m.shader.fraction=0.5
        m:setColors(color(255))
    end
    
    function draw()
        background(40, 40, 50)
        m.shader.time=ElapsedTime/20 --vary the time speed to change the rotation speed of the sphere
        m:draw()
    end
    
    sphereShader = {
    vertexShader = [[
    
    uniform mat4 modelViewProjection;
    
    attribute vec4 position;
    attribute vec4 color;
    attribute vec2 texCoord;
    
    varying lowp vec4 vColor;
    varying highp vec2 vTexCoord;
    varying vec4 vPosition;
    
    void main()
    {
        vColor=color;
        vTexCoord = texCoord;
        vPosition=position;
        gl_Position = modelViewProjection * position;
    }
    
    ]],
    fragmentShader = [[
    
    precision highp float;
    
    uniform lowp sampler2D texture;
    uniform float time;
    uniform float fraction;
    
    varying lowp vec4 vColor;
    varying highp vec2 vTexCoord;
    varying vec4 vPosition;
    
    void main()
    {
      //vec2 tc = vTexCoord.xy;
      vec2 p = -1.0 + 2.0 * vTexCoord;
      float r = dot(p,p);
      if (r > 1.0) discard; 
      float f = 0.5*(1.0-sqrt(1.0-r))/r;
      vec2 uv;
      uv.x = fraction+p.x*f + time;
      uv.y = fraction+p.y*f + time;
      vec4 c = texture2D( texture, vec2(mod(uv.x,1.0), mod(uv.y,1.0)));
      gl_FragColor = vec4(c.xyz, 1.0);
    }
    
    ]]}
    
  • dave1707dave1707 Mod
    Posts: 7,553

    @Ignatz Nice job. I made this change, looks great.

        w,n=WIDTH,50 --size of image and number of squares
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    @dave1707 - I found the code a while back on the net, it's very neat

  • Posts: 134

    So I need to learn shaders and meshes

  • Jmv38Jmv38 Mod
    Posts: 3,295

    very nice.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Invad3rZIM - eventually, yes.

    After starting with learning the basic Lua commands, tables etc, and how to draw stuff on the screen, the logical next step is meshes (a more advanced form of sprites). If you want special effects, shaders are worth learning.

    While I would leave shaders until last because they are more specialised, you shouldn't be afraid of them. The code looks scary at first, but you will find that most of the time, you only need a couple of lines of code for what you need.

    I've written ebooks on Lua, Codea, 3D graphics and shaders, which you can find here:
    https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa

Sign In or Register to comment.