Results 1 to 5 of 5

Thread: Problem drawing a 3d dimand shaped object

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2009
    Posts
    3

    Problem drawing a 3d dimand shaped object

    Hi,

    I'm very new to Open GL. I'm attempting to draw a diamond shape that is missing a side. I have verifed that the points are drawn clockwise.

    I believe it is an issue with the way I'm using the buffer but I can't figure out what I'm doing wrong. I have a feeling my eyes are glazing over a typo.

    Code :
    public void onDrawFrame(GL10 gl) {
        // Back color
        gl.glClearColor(0f, 0.8f, 0f, 1.0f);				
     
        // reset the matrix
        gl.glLoadIdentity();
     
        //gl.glClearColor(red, green, blue, 1f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
     
        // which part of the triangle should be visible?
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glFrontFace(GL10.GL_CCW);
        gl.glCullFace(GL10.GL_FRONT);
     
        //TRIANGLE CODE START
        // prepare to use vertex arrays we want to draw
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
     
        float[] coords = {
            0f, 0f, 1f,			//0 
            0f, 0.25f, 0f,		//1 
            0f, 0f, -0.25f,		//2
            0.25f, -0.25f, 0f,	//3
            -0.25f, -0.25f, 0f,	//4
        };
        int vertexCount = coords.length;
     
        float[] colors = {
                1f, 0f, 0f, 1f, // red
                1f, 1f, 0f, 1f, // yellow
                1f, 1f, 1f, 1f, // white
                1f, 1f, 0f, 1f, // yellow
                1f, 1f, 0f, 1f, // yellow
        };
     
        short[] indices = new short[] {
            0, 1, 4, 
            0, 4, 3,
            0, 3, 1,
            2, 1, 3,
            2, 4, 1,
            2, 3, 4,
        };
     
        // float has 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        vertexBuffer = vbb.asFloatBuffer();
     
        // short has 2 bytes
        ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
        ibb.order(ByteOrder.nativeOrder());
        indexBuffer = ibb.asShortBuffer();
     
     
        // float has 4 bytes, 4 colors (RGBA) * number of vertices * 4 bytes
        ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
        cbb.order(ByteOrder.nativeOrder());
        colorBuffer = cbb.asFloatBuffer();
     
        vertexBuffer.put(coords); //all the points
        indexBuffer.put(indices); //draw order
        colorBuffer.put(colors);
     
        vertexBuffer.position(0);
        indexBuffer.position(0);	
        colorBuffer.position(0);	
     
        // define the vertices we want to draw. 3 points in a triangle
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);     
     
        //4 points in the color def
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
     
        // finally draw the vertices
        gl.glDrawElements(GL10.GL_TRIANGLES, vertexCount, GL10.GL_UNSIGNED_SHORT, indexBuffer);
    }

    The code above produces the following:
    [img]http://yfrog.com/elcroppercapture8p[/img]
    [img]http://yfrog.com/27missingfrontp[/img]
    [img]http://yfrog.com/04trianglekj[/img]


    Any help is greatly appreciated. Thanks in advance.

  2. #2
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Problem drawing a 3d dimand shaped object

    Have you tried with culling disabled? Do you see the same thing with culling enabled/disabled?

    I don't understand what I see in the screenshot, sorry perhaps it is too late for me.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2009
    Posts
    3

    Re: Problem drawing a 3d dimand shaped object

    Quote Originally Posted by dletozeun
    Have you tried with culling disabled? Do you see the same thing with culling enabled/disabled?

    I don't understand what I see in the screenshot, sorry perhaps it is too late for me.
    Thanks for the response. I disabled culling however the triangle is still not drawn.

    The last triangle identified in the indices array isn't being drawn:
    Code :
    short[] indices = new short[] {
        0, 1, 4, 
        0, 4, 3,
        0, 3, 1,
        2, 1, 3,
        2, 4, 1,
        2, 3, 4,
    };

    The triangle made from the points 2, 3, 4 are not being drawn. I've changed the draw order in the array. Its always the last triangle specified in the indices array that isn't drawn which makes me believe I'm doing something wrong with the buffer.

  4. #4
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Problem drawing a 3d dimand shaped object

    You have to give the indices count to glDrawElements not the vertices count. What does "coord.length" returns exactly? Ithink it is the number of float elements in the array of float and in this case the name "vertexCount" is not suitable.

    Try with 18 (6x3) as the number of elements in DrawElements since you have 18 elements if I am correct.

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2009
    Posts
    3

    Re: Problem drawing a 3d dimand shaped object

    Thanks for your help. Removing the vetrtexCount and replacing it with indices.length worked.

    Code :
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

Similar Threads

  1. Drawing on an object.
    By Waleed in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-28-2011, 08:29 AM
  2. Creating a cursor shaped image as a 2D layer
    By Rakefet in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 11-01-2011, 12:59 PM
  3. Donut Shaped Object
    By cnone in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-28-2009, 09:23 AM
  4. Possible approaches to cross-platrm shaped window?
    By mallyone in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-30-2009, 12:25 PM
  5. drawing 3D object
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-15-2004, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean