Jump to content
  • Advertisement
Sign in to follow this  
Icetigris

Drawing and culling issues

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone. I'm building a 3D graph with lots of little cubes, but right now my cubes aren't drawing properly. I know it's an issue with the drawing order, but I'm not sure if it's of the verts, triangles, faces, or all of the above. I tried a few different orders, but in all cases, there are missing triangles, even when culling is off. Can anyone see where my order is screwed up?
public Cube()
 {
     lowLeftFront = new Point3D(0, 0, 0);
     lowLeftBack =  new Point3D(0, 0, 1);
     upLeftFront = new Point3D(0, 1, 0);
     upLeftBack = new Point3D(0, 1, 1);
     lowRightFront = new Point3D(1, 0, 0);
     lowRightBack = new Point3D(1, 0, 1);
     upRightFront =  new Point3D(1, 1, 0);
     upRightBack =  new Point3D(1, 1, 1);
     
     color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
     location = new Point3D(0, 0, 0);
     name = 0;
 }

.
.
.

public FloatBuffer makeVertexBuffer()
 {
     //2 triangles per face * 6 faces * 3 verts per triangle * 3 coords per vert
     vertexBuffer = BufferUtil.newFloatBuffer(108);
     
     float[] verts = {//front
                      upLeftFront.getX(), upLeftFront.getY(), upLeftFront.getZ(),
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      upRightFront.getX(), upRightFront.getY(), upRightFront.getZ(),
                      
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      upRightFront.getX(), upRightFront.getY(), upRightFront.getZ(),
                      
                      //top
                      upLeftBack.getX(), upLeftBack.getY(), upLeftBack.getZ(),
                      upLeftFront.getX(), upLeftFront.getY(), upLeftFront.getZ(),
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      upLeftFront.getX(), upLeftFront.getY(),upLeftFront.getZ(),
                      upRightFront.getX(),upRightFront.getY(),upRightFront.getZ(),
                      
                      //left
                      upLeftFront.getX(), upLeftFront.getY(), upLeftFront.getZ(),
                      upLeftBack.getX(), upLeftBack.getY(), upLeftBack.getZ(),
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      upLeftBack.getX(), upLeftBack.getY(), upLeftBack.getZ(),
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      
                      //bottom
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      lowRightBack.getX(), lowRightBack.getY(), lowRightBack.getZ(),
                      
                      //right
                      upRightFront.getX(), upRightFront.getY(), upRightFront.getZ(),
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      lowRightBack.getX(), lowRightBack.getY(), lowRightBack.getZ(),
                      
                      //back
                      upLeftBack.getX(), upLeftBack.getY(), upLeftBack.getZ(),
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      lowRightBack.getX(), lowRightBack.getY(), lowRightBack.getZ()
     };
     
     vertexBuffer.put(verts, 0, verts.length);
     vertexBuffer.rewind();
     return vertexBuffer;
 }
Here's what it looks like; I've colored all the triangles differently: http://s2.photobucket.com/albums/y5/Icetigris/colourinitial.png http://s2.photobucket.com/albums/y5/Icetigris/Screenshot3.png http://i2.photobucket.com/albums/y5/Icetigris/Screenshot4.png Here's the colour function:
public FloatBuffer makeColorBuffer()
 {
     colorBuffer = BufferUtil.newFloatBuffer(144);
//     float[] rgbaArray = new float[144];
     
     float[] rgbaArray = {//red
                                       1.0f, 0.0f, 0.0f, 1.0f,
                                       1.0f, 0.0f, 0.0f, 1.0f,
                                       1.0f, 0.0f, 0.0f, 1.0f,

                                       //yellow
                                       1.0f, 1.0f, 0.0f, 1.0f,
                                       1.0f, 1.0f, 0.0f, 1.0f,
                                       1.0f, 1.0f, 0.0f, 1.0f,
     
                                       //green
                                       0.0f, 1.0f, 0.0f, 1.0f,
                                       0.0f, 1.0f, 0.0f, 1.0f,
                                       0.0f, 1.0f, 0.0f, 1.0f,
     
                                       //cyan
                                       0.0f, 1.0f, 1.0f, 1.0f,
                                       0.0f, 1.0f, 1.0f, 1.0f,
                                       0.0f, 1.0f, 1.0f, 1.0f,
     
                                       //blue
                                       0.0f, 0.0f, 1.0f, 1.0f,
                                       0.0f, 0.0f, 1.0f, 1.0f,
                                       0.0f, 0.0f, 1.0f, 1.0f,
     
                                       //magenta
                                       1.0f, 0.0f, 1.0f, 1.0f,
                                       1.0f, 0.0f, 1.0f, 1.0f,
                                       1.0f, 0.0f, 1.0f, 1.0f,
     
                                       //purple
                                       0.5f, 0.2f, 1.0f, 1.0f,
                                       0.5f, 0.2f, 1.0f, 1.0f,
                                       0.5f, 0.2f, 1.0f, 1.0f,
     
                                       //grey
                                       0.5f, 0.5f, 0.5f, 1.0f,
                                       0.5f, 0.5f, 0.5f, 1.0f,
                                       0.5f, 0.5f, 0.5f, 1.0f,
     
                                       //orange
                                       1.0f, 0.2f, 0.2f, 1.0f,
                                       1.0f, 0.2f, 0.2f, 1.0f,
                                       1.0f, 0.2f, 0.2f, 1.0f,
     
                                       //maroon
                                       0.3f, 0.0f, 0.0f, 1.0f,
                                       0.3f, 0.0f, 0.0f, 1.0f,
                                       0.3f, 0.0f, 0.0f, 1.0f,
     
                                       //pink
                                       1.0f, 0.7f, 0.7f, 1.0f,
                                       1.0f, 0.7f, 0.7f, 1.0f,
                                       1.0f, 0.7f, 0.7f, 1.0f,
     
                                       //white
                                       1.0f, 1.0f, 1.0f, 1.0f,
                                       1.0f, 1.0f, 1.0f, 1.0f,
                                       1.0f, 1.0f, 1.0f, 1.0f}; //12 verts * 4 colour component values (i.e. rgba)

     
     for(int i = 0; i < 48; i+=4)
     {
         rgbaArray     = (color.getRed())/255;
         rgbaArray[i + 1] = (color.getGreen())/255;
         rgbaArray[i + 2] =  (color.getBlue())/255;
         rgbaArray[i + 3] =  (color.getAlpha())/255;
     }
     
     colorBuffer.put(rgbaArray, 0, rgbaArray.length);
     colorBuffer.rewind();
     return colorBuffer;
 }
EDIT: I changed my draw function a little bit, so it works a little better, but not a whole lot better.
public void draw(GL gl)
 {
     gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
     gl.glEnableClientState(gl.GL_COLOR_ARRAY);
     gl.glVertexPointer(3, gl.GL_FLOAT, 0, makeVertexBuffer());
     gl.glColorPointer(4, gl.GL_FLOAT, 0, makeColorBuffer());
     
     gl.glDrawArrays(gl.GL_TRIANGLES, 0, vertexBuffer.capacity() / 3);
     gl.glDrawArrays(gl.GL_COLOR, 0, colorBuffer.capacity()/ 4);
     
     gl.glDisableClientState(gl.GL_COLOR_ARRAY);
     gl.glDisableClientState(gl.GL_VERTEX_ARRAY);

 }
New pictures: http://i2.photobucket.com/albums/y5/Icetigris/Screenshot1.png http://i2.photobucket.com/albums/y5/Icetigris/Screenshot2.png EDIT 2: I fixed it a little more. I reversed all the z-coordinates so now it draws and culls properly, but doesn't colour properly. [Edited by - Icetigris on July 23, 2008 2:12:16 PM]

Share this post


Link to post
Share on other sites
Advertisement
Well, since the drawing issue has been solved, can anyone point out what I'm doing wrong with the colouring? Only 2 faces are the colour they're supposed to be while the rest of the cube is black.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!