Results 1 to 7 of 7
  1. #1
    aknbad23 is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Question Please help. Trying to display a sphere.

    I have two classes. One where I read in a file(sphere.obj), do some calculations with the data and the second where I spit it back out as x,y values connected by lines on the screen. I read in the file and multiply its vector info by this matrix
    1 0 0 0
    0 1 0 0
    0 0 0 0
    0 0 -1/d 0
    And then do another calculation

    The file I read in consists of vertices and faces and looks like this:
    v
    0.0000000
    0.0000000
    1.0000000
    v
    9.0988444
    1.0098484
    3.9948844
    f
    1
    2
    3
    f
    2
    5
    7

    etc...

    The point is that its supposed to produce a wire frame sphere but I get a small quarter of a circle in the top left corner of my window. help please.
    I need to see the entire thing and make it a lot bigger(to see lines connecting points).

    Here is the code:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package renderer;

    import javax.swing.JComponent;
    import java.io.*;
    import java.util.Scanner;
    import java.awt.geom.Line2D;
    import java.awt.Graphics;
    import java.awt.Graphics2D;



    Java Code:
    /**
     *
     * @author 
     */
    public class Main extends JComponent{
        
        
        /**
         * @param args the command line arguments
         */
        @Override
        public void paintComponent(Graphics g) {
            
            Float[] value = new Float[3];
            int[] val = new int[3];
            float value1;
            float value2;
            float value3;
            int val1,val2,val3;
            int num=0;
            int num2=0;
            float one=1;
            int a,b,c;
            
            //Try all basic arrays
            Float basArray1[][] = new Float[994][4];
            int basArray2[][] = new int[1984][3];
            Float points[][] = new Float[994][4];
            Float points2[][] = new Float[994][2];
            
            try{
                File file = new File("C:\\Gamemovies\\sphere.obj");
                Scanner scanner = new Scanner(file);
                
                while (scanner.hasNext()){
                    String test = scanner.next();
                    if (test.equals("v")){
                        value1 = Float.parseFloat(scanner.next());
                        value2 = Float.parseFloat(scanner.next());
                        value3 = Float.parseFloat(scanner.next());
                        basArray1[num][0]=value1; 
                        basArray1[num][1]=value2;
                        basArray1[num][2]=value3;
                        basArray1[num][3]=one;
                        num++;
                    }
                    if (test.equals("f")){
                        val1 = Integer.parseInt(scanner.next());
                        val2 = Integer.parseInt(scanner.next());
                        val3 = Integer.parseInt(scanner.next());
                        basArray2[num2][0]=val1;
                        basArray2[num2][1]=val2;
                        basArray2[num2][2]=val3;
                        num2++;
                    }
                                 
                    
                }
            
            scanner.close();
            }
            catch (FileNotFoundException e){
                e.printStackTrace();
            }
            
            for (int i=0;i<994;i++){
                        //System.out.print(basArray1.length);
                        //System.out.println(basArray1[i][0] + " " + basArray1[i][1] + " " + basArray1[i][2] + " " + basArray1[i][3]);
            }   
            int d =1;
            int mat[][] = new int[4][4];
            mat[0][0]=1; mat[1][0]=0; mat[2][0]=0; mat[3][0]=0;
            mat[0][1]=0; mat[1][1]=1; mat[2][1]=0; mat[3][1]=0;
            mat[0][2]=0; mat[1][2]=0; mat[2][2]=0; mat[3][2]=0;
            mat[0][3]=0; mat[1][3]=0; mat[2][3]=(-1/d); mat[3][3]=0;
            
            for(int i=0; i<994; i++){
                points[i][0] = mat[0][0]*basArray1[i][0]+ mat[1][0]* basArray1[i][1] + mat[2][0]*basArray1[i][2]+ mat[3][0]*basArray1[i][3];
                points[i][1] = mat[0][1]*basArray1[i][0]+ mat[1][1]* basArray1[i][1] + mat[2][1]*basArray1[i][2]+ mat[3][1]*basArray1[i][3];
                points[i][2] = mat[0][2]*basArray1[i][0]+ mat[1][2]* basArray1[i][1] + mat[2][2]*basArray1[i][2]+ mat[3][2]*basArray1[i][3];
                points[i][3] = mat[0][3]*basArray1[i][0]+ mat[1][3]* basArray1[i][1] + mat[2][3]*basArray1[i][2]+ mat[3][3]*basArray1[i][3];
                
            }
            
            for(int i=0; i<994; i++){
                points2[i][0]=points[i][0]/(1-(0/d)); //x value
                points2[i][1]=points[i][1]/(1-(0/d)); //y value
            }
                    
            for(int i=0; i<994; i++){
                //System.out.println(points[i][0] + " || " + points[i][1] + " || " + points[i][2] + " || " + points[i][3]);
                //System.out.println(points2[i][0] + " || " + points2[i][1]);
            }
            //Java2D
            Graphics2D g2 = (Graphics2D) g;
            
            for(int i=0; i<1984; i++){
                a = (basArray2[i][0]) - 1;
                b = (basArray2[i][1]) - 1;
                c = (basArray2[i][2]) - 1;
                //g.drawLine(basArray1[a][0].intValue(), i, i, i); 
                Line2D lin = new Line2D.Float(points2[a][0], points2[a][1], points2[b][0], points2[b][1]);
                Line2D lin2 = new Line2D.Float(points2[b][0], points2[b][1], points2[c][0], points2[c][1]);
                Line2D lin3 = new Line2D.Float(points2[c][0], points2[c][1], points2[a][0], points2[a][1]);
                g2.draw(lin); 
                g2.draw(lin2);
                g2.draw(lin3); 
                
                            
            }
            
        }
     
    }






    Java Code:
    * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package renderer;
     
    import javax.swing.JFrame;
    /**
     *
     * @author Degrion
     */
    public class Sphere {
        public static void main(String[] args){
            //JFrame frame = new JFrame();
            JFrame frame = new JFrame();
            frame.setSize(700,600);
            frame.setTitle("Render");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            
                  
            Main component = new Main();
            frame.add(component);
            
            frame.setVisible(true);
        }
     
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    Can you change your program NOT to need an external file for ease of testing?

    The paintComponent method should only do the drawing. Do the computations and file reading outside of it and call repaint when ready to draw.

    Where are the method calls that are to draw the sphere?

    There are a lot of undocumented calcuations in the program. Are the results of those calcs correct?
    Add enough println() statements to show what they are and how they are arrived at.
    Last edited by Norm; 10-06-2008 at 11:30 PM.

  3. #3
    aknbad23 is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by aknbad23 View Post
    Java Code:
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package renderer;
     
    import javax.swing.JComponent;
    import java.io.*;
    import java.util.Scanner;
    import java.awt.geom.Line2D;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
     
     
    /**
     *
     * @author 
     */
    public class Main extends JComponent{
        
        
        /**
         * @param args the command line arguments
         */
        @Override
        public void paintComponent(Graphics g) {
            
            Float[] value = new Float[3];
            int[] val = new int[3];
            float value1;
            float value2;
            float value3;
            int val1,val2,val3;
            int num=0;
            int num2=0;
            float one=1;
            int a,b,c;
            
            //Try all basic arrays
            Float basArray1[][] = new Float[994][4];
            int basArray2[][] = new int[1984][3];
            Float points[][] = new Float[994][4];
            Float points2[][] = new Float[994][2];
            
            try{
                File file = new File("C:\\Gamemovies\\sphere.obj");
                Scanner scanner = new Scanner(file);
                
                while (scanner.hasNext()){
                    String test = scanner.next();
                    if (test.equals("v")){
                        value1 = Float.parseFloat(scanner.next());
                        value2 = Float.parseFloat(scanner.next());
                        value3 = Float.parseFloat(scanner.next());
                        basArray1[num][0]=value1; 
                        basArray1[num][1]=value2;
                        basArray1[num][2]=value3;
                        basArray1[num][3]=one;
                        num++;
                    }
                    if (test.equals("f")){
                        val1 = Integer.parseInt(scanner.next());
                        val2 = Integer.parseInt(scanner.next());
                        val3 = Integer.parseInt(scanner.next());
                        basArray2[num2][0]=val1;
                        basArray2[num2][1]=val2;
                        basArray2[num2][2]=val3;
                        num2++;
                    }
                                 
                    
                }
            
            scanner.close();
            }
            catch (FileNotFoundException e){
                e.printStackTrace();
            }
            
            for (int i=0;i<994;i++){
                        //System.out.print(basArray1.length);
                        //System.out.println(basArray1[i][0] + " " + basArray1[i][1] + " " + basArray1[i][2] + " " + basArray1[i][3]);
            }
            //This is the 3d to 2d perspective projection matrix
            int d =1;
            int mat[][] = new int[4][4];
            mat[0][0]=1; mat[1][0]=0; mat[2][0]=0; mat[3][0]=0;
            mat[0][1]=0; mat[1][1]=1; mat[2][1]=0; mat[3][1]=0;
            mat[0][2]=0; mat[1][2]=0; mat[2][2]=0; mat[3][2]=0;
            mat[0][3]=0; mat[1][3]=0; mat[2][3]=(-1/d); mat[3][3]=0;
            
                //Here I mult all vertices by the matrix above
                for(int i=0; i<994; i++){
                points[i][0] = mat[0][0]*basArray1[i][0]+ mat[1][0]* basArray1[i][1] + mat[2][0]*basArray1[i][2]+ mat[3][0]*basArray1[i][3];
                points[i][1] = mat[0][1]*basArray1[i][0]+ mat[1][1]* basArray1[i][1] + mat[2][1]*basArray1[i][2]+ mat[3][1]*basArray1[i][3];
                points[i][2] = mat[0][2]*basArray1[i][0]+ mat[1][2]* basArray1[i][1] + mat[2][2]*basArray1[i][2]+ mat[3][2]*basArray1[i][3];
                points[i][3] = mat[0][3]*basArray1[i][0]+ mat[1][3]* basArray1[i][1] + mat[2][3]*basArray1[i][2]+ mat[3][3]*basArray1[i][3];
                
            }
            
                //Another calculation to get the correct x,y values
                for(int i=0; i<994; i++){
                points2[i][0]=points[i][0]/(1-(0/d)); //x value
                points2[i][1]=points[i][1]/(1-(0/d)); //y value
            }
                    
            for(int i=0; i<994; i++){
                //System.out.println(points[i][0] + " || " + points[i][1] + " || " + points[i][2] + " || " + points[i][3]);
                //System.out.println(points2[i][0] + " || " + points2[i][1]);
            }
            //Java2D (This section connects the points by lines to form triangles
            Graphics2D g2 = (Graphics2D) g;
            
            for(int i=0; i<1984; i++){
                a = (basArray2[i][0]) - 1;
                b = (basArray2[i][1]) - 1;
                c = (basArray2[i][2]) - 1;
                //g.drawLine(basArray1[a][0].intValue(), i, i, i); 
                Line2D lin = new Line2D.Float(points2[a][0], points2[a][1], points2[b][0], points2[b][1]);
                Line2D lin2 = new Line2D.Float(points2[b][0], points2[b][1], points2[c][0], points2[c][1]);
                Line2D lin3 = new Line2D.Float(points2[c][0], points2[c][1], points2[a][0], points2[a][1]);
                g2.draw(lin); 
                g2.draw(lin2);
                g2.draw(lin3); 
                
                            
            }
            
        }
     
    }






    Java Code:
    * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package renderer;
     
    import javax.swing.JFrame;
    /**
     *
     * @author Degrion
     */
    public class Sphere {
        public static void main(String[] args){
            //JFrame frame = new JFrame();
            JFrame frame = new JFrame();
            frame.setSize(700,600);
            frame.setTitle("Render");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            
                  
            Main component = new Main();
            frame.add(component);
            
            frame.setVisible(true);
        }
     
    }
    I added a few comments

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    You still need to move the computation code outside of the paint method.
    You need to debug your computations to see if they are generating the values you need. One aid is to add println statements to show the values.
    What is the relationship between the points in the arrays? Can you add some debug code to search the arrays for max and min values to see if the comps are correct?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Dratted cross poster.

    db

  7. #7
    aknbad23 is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You still need to move the computation code outside of the paint method.
    You need to debug your computations to see if they are generating the values you need. One aid is to add println statements to show the values.
    What is the relationship between the points in the arrays? Can you add some debug code to search the arrays for max and min values to see if the comps are correct?
    I see what you're saying. I'm going to do that now.

Similar Threads

  1. Replies: 0
    Last Post: 06-28-2008, 09:26 PM
  2. To display as a list
    By yuvi461 in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 12:06 PM
  3. SWT Display class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 12:19 PM
  4. Display program
    By nhlfan in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •