Connecting outer angles to polygon shape

Hi there,

I was doing a second part the project I did with my wireframes and this time, instead of using GL.GL_LINE_STRIP, I’m using GL.GL_POLYGON.

So this makes it a little different. With lines I only needed 4 points to create my shape but now I need 5 points to make 4 angles using the polygon function.

Now my problem is this…I can’t connect the outer and inner points.

Here is the code.

import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer; 
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;


public class Project
{
  static Animator animator = null;
  
  public static void main(String[] args)
  {

    Frame frame = new Frame("Project");

    GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

    canvas.addGLEventListener(new Renderer());

    frame.add(canvas);

    frame.setSize(500, 500);

    animator = new Animator(canvas);

    frame.addWindowListener(new WindowAdapter()
    {

      public void windowClosing(WindowEvent e)
      {

        animator.stop();

        System.exit(0);
      }

    });

    frame.show();

    animator.start();

    canvas.requestFocus();   
  }//End of main
  
   

  static class Renderer implements GLEventListener, KeyListener
  {
    private float rtri = 0.5f; // rotation factor  
    
      
    
    int number= 20;
    
    
    
    float [][] xprime=new float[number] [5]; 
	float [][] yprime=new float[number] [5];
	float [][] zprime=new float[number] [5];
       
                      
    float [] x = {40.00f,40.00f,40.00f,10.00f,10.00f};//5 x points
    float [] y = {-100.00f,100.00f,100.00f,170.00f,-100.00f};//5 y points
    float [] z = {0.00f,0.00f,0.00f,0.00f,0.00f};//5 z points*/                
                    
    
      
    
    
    public void display(GLDrawable gLDrawable)
    {

     final GL gl = gLDrawable.getGL();
     final GLU glu = gLDrawable.getGLU();
      
     gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);  // Set display window to color.
     gl.glMatrixMode (GL.GL_MODELVIEW);
     gl.glLoadIdentity();
     gl.glRotatef(rtri, 1.0f, 1.0f, 1.0f); // to rotate the crayon
     gl.glFlush();
     
     drawCrayon(gl);//new method instantiated
       
     float radians=(float)(Math.PI/180);
     float fullCircle = 360;
     float angle = fullCircle/number;
     //Code
     for(int wrap = 0; wrap<number;wrap++)
	 {
	   for(int points =0; points<5;points++)
	   {
	  	//equation   
	    
	    
		xprime[wrap][points] =(float)(x[points]*(Math.cos(radians*angle))+ z[points]*(Math.sin(radians*angle)));
		yprime[wrap][points] = (float)(y[points]); 
		zprime[wrap][points] =(float)((-x[points])*(Math.sin(radians*angle))+ z[points]*(Math.cos(radians*angle)));

	   }//inside for-loop
	 
	   angle+=20;
	 }//outside for-loop
	  
 	 
    }//End display	 
    
         
    //Method drawCrayon     
    public void drawCrayon (GL gl)
    {
       
		 	    //Loop that connects lines vertically and horizontallly
		 	    							
		  //Does Vertical Lines		  			   
		  for(int count = 0; count < number; count++)
		    {
            	
               
               
                 	gl.glBegin(GL.GL_POLYGON);	 
				
				
				for(int i=0; i<5; i++)
				{
				        
			        //gl.glColor3f(CLUT[(int) count/2]);
			        //gl.glColor3f(1.0f, 1.0f, 0.0f);
			        gl.glColor3f((1.00f)*count/20, (1.00f)*count/20, (1.00f)*count/20);
					
								
			        gl.glVertex3f(xprime[count][i], yprime[count][i], zprime[count][i]);
			        
			        /*gl.glVertex3f(xprime[count+1][i], yprime[count+1][i], zprime[count+1][i]);
			        gl.glVertex3f(xprime[count+1][i+1], yprime[count+1][i+1], zprime[count+1][i+1]);
			        gl.glVertex3f(xprime[count][i+1], yprime[count][i+1], zprime[count][i+1]);*/
			        
			       			         
				 }             
                 gl.glEnd(); 
                
            }//End of outer for loop  
            
            //Does Horizontal Lines
           for(int count = 0; count <5 ; count++)
		    {
            	
               
                gl.glBegin(GL.GL_POLYGON);
                			
			
				for(int i=0; i<number; i++)
				{
			
				    gl.glColor3f((1.00f)*count/number, (1.00f)*count/number, (1.00f)*count/number);
				    //gl.glColor3f(CLUT[count/2]);
				      gl.glColor3f(1.0f, 1.0f, 0.0f);
				   
			        gl.glVertex3f(xprime[i][count], yprime[i][count], zprime[i][count]);
			        
						
					
				 }             
                gl.glEnd();
                
            }//End of outer for loop */ 
            
              
		   
			
			gl.glFlush();
	        rtri += 0.2f;	 
	                
	       				
    }// drawCrayon
     

           
      
     public void init(GLDrawable gLDrawable)
     {            
                
      final GL gl = gLDrawable.getGL();

      final GLU glu = gLDrawable.getGLU();

     

      gl.glMatrixMode (GL.GL_PROJECTION); 

      gl.glClearColor (0.0f, 0.0f,0.0f, 0.0f);   //set background to black

      gl.glClearDepth(1.0f);                 // Depth Buffer Setup

      gl.glEnable(GL.GL_DEPTH_TEST);            // Enables Depth Testing

      gl.glDepthFunc(GL.GL_LEQUAL);                   // The Type Of Depth Testing To Do

      gl.glOrtho (-250.0, 250.0, -250, 250.0, -250.00, 250.0);  // define drawing area

      gLDrawable.addKeyListener(this);

    }

   

     public void displayChanged(GLDrawable gLDrawable, boolean 
modeChanged, boolean deviceChanged)

    {

    }


       public void reshape(GLDrawable gLDrawable, int x, int y, int 
width, int height)
    {

    }

    public void keyPressed(KeyEvent e)
    {

      if (e.getKeyCode() == KeyEvent.VK_ESCAPE)

      animator.stop();

        System.exit(0);
    }

      

    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

  }//Static class Renderer

}//End of Crayon 


  

I just posted all this so if anyone can copy and compile it using Java, they can see what’s going on.

It looks pretty funny, with 20 of those polygons just lining up one right next to the other, but like I said, notice that the outer points aren’t connected to create a solid shape.

I’ve tried, but I don’t know how to alter my for-loops in order to get the outer angles to connect.

Any suggestions?

If anyone could just copy and compile the code in Java and see what’s really going on and give me some help, that would be great.

Thank You!

There are some confusing things going on here, so I’m not quite sure what you’re actually trying to do.

Wild guess: You actually want gl.glBegin(GL.GL_TRIANGLES); instead of gl.glBegin(GL.GL_POLYGON);

And this:

float angle = fullCircle/number;
//...
angle+=20;

looks like you actually wanted this:

float angle = 0.0f;
float delta_angle = fullCircle/number;
//...
angle+=delta_angle;

I also do not understand the bit about 5 instead of 4 points, especially since the 2. and 3. point are identically…

Hope this helps…

No, that has nothing to do with anything that I have there. That particular part of the code doesn’t need changing. I know that for a fact.

It’s what’s inside theloops themeselves that need altering (for-loops).

That’s why it would be better if you could compile the code so you could better see what I’m talking about. It’s supposed to be a solid crayon shape with actual surfaces, but the way I did it, it came out funny.

I’m still trying to tweak the code but nothing seems to be working so far…

Again, if you can, please compile the code and you can see what the problem is. Obviously my explaining it hasn’t made it clear.

Never mind, I had someone look at it for me and it was the foor loop, just like I said.

Thanks for the help, people.