Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Rendered 3D Cube in Java without using Java3D?

Options
  • 23-03-2009 11:49am
    #1
    Registered Users Posts: 18,272 ✭✭✭✭


    Have to create a rendered 3d cube in java for an assignment in college.

    Cant use any packages so have to do it using a scan line algorithm,
    I have created a cube using bresenhams line algorithm, so I have a wireframe view of a 3d cube.

    I need to texture map the cube so I need to learn how to create a scan line to do this.

    Notes we got were very vague so was wondering if anyone has any proper good tutorials or hints and tips on how to do it in java?

    Thanks in advance


Comments

  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    nobody?

    this is what i have so far

    75542.jpg

    I need a z_buffer or scan line algorithm to apply a texture map to the cube, can anyone point me in the right direction? please?


  • Registered Users Posts: 2,426 ✭✭✭ressem


    You're looking for details on perspective corrected texture mapping?

    http://chrishecker.com/Miscellaneous_Technical_Articles

    Or more simply
    http://www.geocities.com/siliconvalley/2151/tmap.html#perfect


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    ressem wrote: »
    You're looking for details on perspective corrected texture mapping?

    http://chrishecker.com/Miscellaneous_Technical_Articles

    Or more simply
    http://www.geocities.com/siliconvalley/2151/tmap.html#perfect

    Thanks,

    Need to learn how to render a static cube using the Normal-vector interpolation shading scan line rendering algorithm,

    I have googled it and a lot of conflicting stuff comes up, I cant seem to tie down what way the actual scan line for it works


  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    draffodx wrote: »
    Need to learn how to render a static cube using the Normal-vector interpolation shading scan line rendering algorithm,

    That's a lot of keywords mashed together that can be interpreted in different ways.

    In Java, I've done normal-vector solid shading of polygons in 3-d.
    Ie. all the polygons are shaded, but the colour in the each polygon is solid, not-interpolated. I done that using my own routines, not Java 3d.

    In C++/assembly, I've also done gouraud shading of polygons, where you calculate the surface normal of each polygon, then add those surface normal vectors up at each vertex to get the surface normal vector at the vertices. Then you can work out the shading at each vertex by using it's newly calculated surface normal and the lighting vector ( direction of your light source ). So then you have the shaded colour at each vertex. To gouraud shade it, you interpolate the colour along each pixel of the vertex edges, then interpolate the colour of each row from one vertex edge to the other. Result is a gradual smooth shading across the polygon.

    Now that was only with solid colours... ie. I only had one RGB value for the entire polygon to start with.

    Throwing texture mapping into the mix complicates things even further.
    So the question is, are you solid shading the texture, or do you have to gouraud shade it ?

    Solid shading would be much easier, eg. if you start by pretending each polygon had a dummy colour of 100, 100, 100 (nice figure to use to work out percentage of shading) then when you work out the shading you might find that it ends up at 75, 75, 75.
    So you need to reduce the RGB value of each of the polygons pixels by 75%.

    Bit I suggest you get the texture mapping working first.
    Later on you can work out the shading.

    Couple of old links to Dr Dobbs articles on texture mapping, mostly in C, but better than nothing.

    Texture Mapping

    More


  • Registered Users Posts: 62 ✭✭starman08


    Did you manage to get the texture mapping done.. im still struggling with mine


  • Advertisement
  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    starman08 wrote: »
    Did you manage to get the texture mapping done.. im still struggling with mine

    Heres what I have so far

    76237.JPG

    Although it leaks when the polygon is moved....

    76238.JPG

    Thanks for the links CreepingDeath, very helpful


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    ok so have it working as above and it is using a in/out boolean called inside, the problem seems to be that it hits the line and gets set inside but at some stage it doesn't get set back to false properly.

    here is the code, wondering if anyone knows a good way to make sure the in out Boolean is set properly, it only leaks at the top and bottom where it is sloped.
    for(scanY=0;scanY<bImg.getHeight();scanY++)
                {
                   inside =  false;
                    //double interpolation;
                   e1z2 += e1zIncrement;
                   e2z2 += e2zIncrement;
                  //  interpolation = (e2z2 - e1z2)
                    //    / ((e2x1 + inverse) - (e1x1 + inverse2));
              
                    for (int scanX = 0; scanX < bImg.getWidth(); scanX++)
                    {  
                        //int texturepix = ReadPixel(scanX, scanY);
                        //WritePixel(scanX, scanY,texturepix );
                  
                  
                        if(bImg.getRGB(scanX, scanY)==Color.WHITE.getRGB() )
                        {   
                            System.out.println("hit line *********************" );
                            System.out.println("boolean!&#8364;&#8364;&#8364;&#8364;&#8364;&#8364;&#8364;&#8364;&#8364;&#8364;&#8364; =  " + inside );
                            if (inside == false)
                            {
                                inside = true;
                            }
                            else
                            {
                                inside = false;
                            }
                        }
                            if(inside == true)
                            {
                            	int texturepix = 0;
                               
                               texturepix = readPixel(scanX, scanY);
                                	
                                	
                                
                                if(readPixelb(scanX,scanY)!= Color.WHITE.getRGB())
                                {
                                writePixel(scanX, scanY,texturepix );
                                
                                }
                               
                                if(readPixelb(scanX,scanY)== Color.WHITE.getRGB())
                                {
                                
                                
                                }
                            }
    
                  
                }
                //System.out.println("Interpolation = " + interpolation);
           }
                update(g);
          
        }
    


    76273.JPG


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    I haven't looked that closely, but my guess is that inside is flip-flopping at each white pixel. So for an even number of white pixels it's ending up as true, and for an odd number it's set to false (or vice versa).

    However you're going about it the wrong way - you shouldn't be relying on pixel colours to determine the edges, because then you face all sorts of issues that could easily break the algorithm. You should instead break down each face into triangles (triangles trump all other polygons because they are always coplanar and so are much easier to rasterise) and use some simple geometry to do the work for you. See here for more details: http://www.gamedev.net/reference/articles/article852.asp.

    The affine texture mapping described there isn't great and obviously perspective-correct is much better, but it'll show you the basics. And once you get perpective-correct interpolation of your texture coordinates, shading the object should be a cinch.


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    thanks for that satchmo, found it helpful but couldnt get it coded in time

    just out of interest would java 3d be a much easier and efficient way to do it?


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    draffodx wrote: »
    thanks for that satchmo, found it helpful but couldnt get it coded in time

    just out of interest would java 3d be a much easier and efficient way to do it?

    Yes, much more so, I'd reckon. I haven't worked with Java3D specifically, but in general, what you're doing here is called "software rendering" - doing everything by hand, on the CPU, without using hardware acceleration from a GPU. Realtime 3D isn't really done this way anymore (expect for platforms where you have no choice), it's done through APIs like OpenGL or Direct3D that work at a higher level of abstraction, and use the graphics hardware to render things - "hardware rendering". This is generally a lot easier for the developer to get going with, and far faster as it uses the GPU's dedicated hardware for rendering. Pretty much any modern 3D game engine for instance, will only use hardware rendering. (that's relatively recent, up to a few years back, a good few of them had fallback software renderers too, and before that again, it was all software).

    That said, software rendering is still something people do from time to time for the technical challenge, for educational purposes, or to achieve things you can't do easily with GPUs.


  • Advertisement
Advertisement