Anzeige:
Ergebnis 1 bis 3 von 3

Thema: Stencil Shadows

  1. #1
    Elvenone
    Gast

    Stencil Shadows

    hello

    iam trying to recode the "Stencil Shadow Volume" from C to java, but i dont know why, i cant get it to work, althrough its the same

    netbeans project : www.politea.sk/tmp/Shadows.zip

    the C++ code from which iam trying to get into java si on http://www.codesampler.com/source/ogl_shadow_volume.zip (http://www.codesampler.com/oglsrc/oglsrc_8.htm)

    PHP-Code:
    package org.yourorghere;

    import com.sun.opengl.impl.windows.PIXELFORMATDESCRIPTOR;
    import java.awt.*;
    import java.awt.event.*;

    import javax.media.opengl.*;
    import com.sun.opengl.util.*;
    import javax.media.opengl.glu.GLU;

    public class 
    Shadows implements GLEventListenerMouseListenerMouseMotionListenerMouseWheelListenerKeyListener {

            
    boolean g_bRenderShadowVolume true;
            
    float g_fAmountOfExtrusion  5.0f;
            
    float g_lightPosition[]     = { 2.0f6.0f0.0f1.0f }; // World position of light source

            
    static float view_scale 1.0f;

            public class 
    Vertex {
                public 
    float rgb;
                public 
    float xyz;
               
                private 
    Vertex(float irfloat igfloat ibfloat ix,float iyfloat iz) {
                    
    irigibixiyiz;
                }
               
                public  
    float getX() {return x;}
            };
           
            public 
    Vertex[] g_shadowCasterVerts =
            {
                new 
    Vertex(1.0f1.0f1.0f, -1.0f2.5f, -1.0f),
                new 
    Vertex(1.0f1.0f1.0f, -1.0f2.5f,  1.0f ),
                new 
    Vertex(1.0f1.0f1.0f,  1.0f2.5f,  1.0f ),
                new 
    Vertex(1.0f1.0f1.0f,  1.0f2.5f, -1.0f ),
            };     
           
            public 
    float[] g_shadowCasterNormal = { 0.0f1.0f0.0f };
           
            class 
    ShadowCaster {
                
    Vertex[] verts;// = new Vertex(0f,0f,0f,0f,0f,0f);        // Vertices of the actual shadow casting object
                
    float[]  normal;     
                
    int    numVerts;    
                
    int    shadowVolume;
            };

            
    ShadowCaster g_shadowCaster = new ShadowCaster();
       
            public static 
    void main(String[] args)
       {
                   
          
    Frame frame = new Frame("Simple JOGL Application");
          
    GLCanvas canvas = new GLCanvas();
         
          
    canvas.addGLEventListener(new Shadows());
          
    frame.add(canvas);
          
    frame.setSize(640480);
          final 
    Animator animator = new Animator(canvas);
          
    frame.addWindowListener(new WindowAdapter()
          {
             public 
    void windowClosing(WindowEvent e)
             {
                new 
    Thread(new Runnable()
                {
                   public 
    void run()
                   {
                      
    animator.stop();
                      
    System.exit(0);
                   }
                }).
    start();
             }
          });

          
    frame.setLocationRelativeTo(null);
          
    frame.setVisible(true);
          
    animator.start();
       }
           
            
    void extendVertex(float[] newVertfloat[] lightPositVertex vertfloat ext) {
                
    float[] lightDir = new float[3];

                
    // Create a vector that points from the light's position to the original vertex.
                
    lightDir[0] = vert.lightPosit[0];
                
    lightDir[1] = vert.lightPosit[1];
                
    lightDir[2] = vert.lightPosit[2];

                
    newVert[0] = lightPosit[0] + lightDir[0] * ext;
                
    newVert[1] = lightPosit[1] + lightDir[1] * ext;
                
    newVert[2] = lightPosit[2] + lightDir[2] * ext;
            }       
       
            
    void buildShadowVolume(GLAutoDrawable drawableShadowCaster casterfloat[] lightPositfloat ext ) {
                
    GL gl drawable.getGL();
               
                if ( 
    caster.shadowVolume != -)
                    
    gl.glDeleteListscaster.shadowVolume);

                
    caster.shadowVolume gl.glGenLists(1);

                
    gl.glNewListcaster.shadowVolumegl.GL_COMPILE );
                {
                    
    gl.glDisablegl.GL_LIGHTING );

                    
    gl.glBegingl.GL_QUADS );
                    {
                        
    gl.glColor3f0.2f0.8f0.4f );

                        
    float[] vExtended = new float[3];

                        for( 
    int i 0caster.numVerts; ++)
                        {
                            
                            
    int e0 i;
                            
    int e1 i+1;

                            if( 
    e1 >= caster.numVerts )
                                
    e1 0;

     
                            
    gl.glVertex3f(caster.verts[e0].xcaster.verts[e0].ycaster.verts[e0].z);

     
                            
    extendVertex(vExtendedlightPositcaster.verts[e0], ext);
                            
    gl.glVertex3f(vExtended[0], vExtended[1], vExtended[2]);

       
                            
    extendVertexvExtendedlightPositcaster.verts[e1], ext );
                            
    gl.glVertex3fvExtended[0], vExtended[1], vExtended[2] );


                            
    gl.glVertex3fcaster.verts[e1].xcaster.verts[e1].ycaster.verts[e1].);
                        }
                    }
                    
    gl.glEnd();

                    
    gl.glEnablegl.GL_LIGHTING );
                }
                
    gl.glEndList();
            }   
           
            
    void renderScene(GLAutoDrawable drawable) {

                
    GL gl drawable.getGL();
                
    GLUT glut = new GLUT();
               
                
    gl.glMatrixModegl.GL_MODELVIEW );
                
    gl.glLoadIdentity();
                
    gl.glTranslatef0.0f, -2.0f, -15.0f );

                if (
    mouseRButtonDown) {
                    
    view_scale += (prevMouseY nowMouseY) / 10000f;                 
                }
                 
                
    gl.glRotatef(view_rotx1.0f0.0f0.0f);
                
    gl.glRotatef(view_roty0.0f1.0f0.0f);
                
    gl.glRotatef(view_rotz0.0f0.0f1.0f);

                
    gl.glPushMatrix();
                {
                    
    gl.glBegingl.GL_QUADS );
                    {
                        
    gl.glNormal3f0.0f1.0f,  0.0f );
                        
    gl.glVertex3f(-5.0f0.0f, -5.0f );
                        
    gl.glVertex3f(-5.0f0.0f,  5.0f );
                        
    gl.glVertex3f5.0f0.0f,  5.0f );
                        
    gl.glVertex3f5.0f0.0f, -5.0f );
                    }
                    
    gl.glEnd();
                }
                
    gl.glPopMatrix();


                
    gl.glPushMatrix();
                {
                    
    gl.glTranslatef( -2.0f0.8f0.0f );
                    
    gl.glRotatef180.0f0.0f1.0f0.0f );
                    
    gl.glColor3f1.0f1.0f ,1.0f );
                    
    glut.glutSolidTeapot(1.0);
                }
                
    gl.glPopMatrix();

                
    gl.glDisablegl.GL_LIGHTING );

                
    gl.glPushMatrix();
                {

                    
    gl.glLightfvgl.GL_LIGHT0gl.GL_POSITIONg_lightPosition0);

                    
    gl.glTranslatefg_lightPosition[0], g_lightPosition[1], g_lightPosition[2] );

                    
    gl.glColor3f(1.0f1.0f0.5f);
                    
    glut.glutSolidSphere0.18);
                }
                
    gl.glPopMatrix();

                
    gl.glEnablegl.GL_LIGHTING );

                
    gl.glPushMatrix();
                {
                    
    gl.glBegingl.GL_POLYGON );
                    {
                        
    gl.glNormal3f(g_shadowCaster.normal[0],g_shadowCaster.normal[1], g_shadowCaster.normal[2]);

                        for( 
    int i 0g_shadowCaster.numVerts; ++)
                        {
                            
    gl.glVertex3fg_shadowCaster.verts[i].xg_shadowCaster.verts[i].yg_shadowCaster.verts[i].);
                        }
                    }
                    
    gl.glEnd();
                }
                
    gl.glPopMatrix();
            }               
           
       public 
    void init(GLAutoDrawable drawable)
       {

          
    GL gl drawable.getGL();               
          
    GLU glu = new GLU();
                  
          
    gl.glClearStencil(128);
        
          
    gl.setSwapInterval(1);
                   
                    
    gl.glClearColor0.35f0.53f0.7f1.0f );
                    
    gl.glEnable(gl.GL_LIGHTING);
                    
    gl.glEnable(gl.GL_LIGHT0);
                    
    gl.glEnable(gl.GL_DEPTH_TEST);

                    
    gl.glMatrixModegl.GL_PROJECTION );
                    
    gl.glLoadIdentity();
                    
    glu.gluPerspective45.0f640.0f 480.0f0.1f100.0f);

                    
    float[] lightAmbient  = {0.2f0.2f0.2f1.0f};
                    
    float[] lightDiffuse  = {1.0f1.0f1.0f1.0f};
                    
    float[] lightSpecular = {1.0f1.0f1.0f1.0f};
                    
    gl.glLightfv(gl.GL_LIGHT0gl.GL_DIFFUSE,  lightDiffuse0);
                    
    gl.glLightfv(gl.GL_LIGHT0gl.GL_SPECULARlightSpecular0);
                    
    gl.glLightfv(gl.GL_LIGHT0gl.GL_AMBIENT,  lightAmbient0);

                    
    g_shadowCaster.verts        g_shadowCasterVerts;
                    
    g_shadowCaster.normal       g_shadowCasterNormal;
                    
    g_shadowCaster.numVerts     = (4);
                    
    g_shadowCaster.shadowVolume = -1;
                   
                    
    drawable.addMouseListener(this);
                    
    drawable.addMouseMotionListener(this);
                    
    drawable.addMouseWheelListener(this);
                    
    drawable.addKeyListener(this);
       }
           

       
       public 
    void reshape(GLAutoDrawable drawableint xint yint widthint height)
       {
          
    GL gl drawable.getGL();
          
    GLU glu = new GLU();

          if (
    height <= 0)
                
    height 1;
          final 
    float h = (float) width / (float) height;
          
    gl.glViewport(00widthheight);
          
    gl.glMatrixMode(GL.GL_PROJECTION);
          
    gl.glLoadIdentity();
          
    glu.gluPerspective(45.0fh1.020.0);
          
    gl.glMatrixMode(GL.GL_MODELVIEW);
          
    gl.glLoadIdentity();
       }
       
       public 
    void display(GLAutoDrawable drawable)
       {
          
    GL gl drawable.getGL();
         
                    
    buildShadowVolume(drawableg_shadowCasterg_lightPositiong_fAmountOfExtrusion );

                    
    gl.glCleargl.GL_COLOR_BUFFER_BIT gl.GL_DEPTH_BUFFER_BIT gl.GL_STENCIL_BUFFER_BIT );

                    
    gl.glColorMaskfalsefalsefalsefalse );
                    
    renderScene(drawable);

                    
    gl.glEnablegl.GL_CULL_FACE );
                    
    gl.glEnablegl.GL_STENCIL_TEST );
                    
    gl.glDepthMask(false);
                    
    gl.glStencilFuncgl.GL_ALWAYS0);

                    
    gl.glStencilOpgl.GL_KEEPgl.GL_KEEPgl.GL_INCR );
                    
    gl.glCullFacegl.GL_BACK );
                    
    gl.glCallListg_shadowCaster.shadowVolume );

                    
    gl.glStencilOpgl.GL_KEEPgl.GL_KEEPgl.GL_DECR );
                    
    gl.glCullFacegl.GL_FRONT );
                    
    gl.glCallListg_shadowCaster.shadowVolume );

                    
    gl.glDepthMasktrue );               
                    
    gl.glDepthFuncgl.GL_LEQUAL );               
                    
    gl.glColorMasktruetruetruetrue );
                    
    gl.glStencilOpgl.GL_KEEPgl.GL_KEEPgl.GL_KEEP );             
                    
    gl.glCullFacegl.GL_BACK );
                    
    gl.glDisablegl.GL_CULL_FACE );

                    
    gl.glStencilFuncgl.GL_EQUAL1);
                                   
                    
    gl.glDisablegl.GL_LIGHT0 );
                    
    renderScene(drawable);
                   
                    
    gl.glStencilFuncgl.GL_EQUAL0);
                    
    gl.glEnablegl.GL_LIGHT0 );
                    
    renderScene(drawable);

                    
    gl.glDepthFuncgl.GL_LESS );
                    
    gl.glDisablegl.GL_STENCIL_TEST );

                    if( 
    g_bRenderShadowVolume )
                    {
                        
    gl.glPolygonModegl.GL_FRONT_AND_BACKgl.GL_LINE );
                        
    gl.glCallListg_shadowCaster.shadowVolume );
                        
    gl.glPolygonModegl.GL_FRONT_AND_BACKgl.GL_FILL );
                    }

       }
       
       public 
    void displayChanged(GLAutoDrawable drawableboolean modeChangedboolean deviceChanged)
       {}
           
            static 
    int prevMouseX 0prevMouseY 0nowMouseXnowMouseY;   
            static 
    boolean mouseRButtonDown false;
            private 
    float view_rotx 20.0fview_roty 30.0fview_rotz 0.0f;

            public 
    void mouseClicked(MouseEvent e) {

            }

            public 
    void mousePressed(MouseEvent e) {           
                if ((
    e.getModifiers() & e.BUTTON1_MASK) != 0) {
                    
    //view_scale += 0.5;   
                
    }
                if ((
    e.getModifiers() & e.BUTTON3_MASK) != 0) {
                    
    //view_scale -= 0.5;   
                
    }

                
    prevMouseX e.getX();
                
    prevMouseY e.getY();

                if ((
    e.getModifiers() & e.BUTTON3_MASK) != 0) {
                    
    mouseRButtonDown true;
                }
            }

            public 
    void mouseReleased(MouseEvent e) {   
                if ((
    e.getModifiers() & e.BUTTON3_MASK) != 0) {
                    
    mouseRButtonDown false;
                }
            }

            public 
    void mouseEntered(MouseEvent e) {
           }

            public 
    void mouseExited(MouseEvent e) {
           }

            public 
    void mouseDragged(MouseEvent e) {           
            
                if ((
    e.getModifiers() & e.BUTTON3_MASK) != 0) {
                    
    nowMouseX e.getX();
                    
    nowMouseY e.getY();

                }
                if ((
    e.getModifiers() & e.BUTTON1_MASK) != 0) {
                    
    int x e.getX();
                    
    int y e.getY();
                    
    Dimension size e.getComponent().getSize();

                    
    float thetaY 360.0f * ( (float)(x-prevMouseX)/(float)size.width);
                    
    float thetaX 360.0f * ( (float)(prevMouseY-y)/(float)size.height);

                    
    prevMouseX x;
                    
    prevMouseY y;

                    
    view_rotx += thetaX;
                    
    view_roty += thetaY;     
                }   
                
    //throw new UnsupportedOperationException("Not supported yet.");
            
    }

            public 
    void mouseMoved(MouseEvent e) {           

            }

            public 
    void mouseWheelMoved(MouseWheelEvent e) {
                if (
    e.getWheelRotation() < 0) {
                    
    view_scale += 0.1;           
                } else {
                    
    view_scale -= 0.1;                   
                }

            }

        public 
    void keyTyped(KeyEvent e) {
        }

        public 
    void keyPressed(KeyEvent e) {
            if (
    e.getKeyChar() == KeyEvent.CHAR_UNDEFINED) {
                
    int key e.getKeyCode();
             
                if (
    key == KeyEvent.VK_F5) {
                    
    g_lightPosition[0] += 0.1f;         
                }
                if (
    key == KeyEvent.VK_F6) {
                    
    g_lightPosition[0] -= 0.1f;             
                }
                if (
    key == KeyEvent.VK_F7) {
                    
    g_lightPosition[1] += 0.1f;       
                }
                if (
    key == KeyEvent.VK_F8) {
                    
    g_lightPosition[1] -= 0.1f;           
                }         
            }   
        }

        public 
    void keyReleased(KeyEvent e) {        
        }

    Geändert von Elvenone (07-11-2007 um 23:37 Uhr)

  2. #2
    Registrierter Benutzer
    Registriert seit
    02.12.2002
    Ort
    Darmstadt
    Beiträge
    615
    So what is the actual error you've got? Or did it compile but just didn't do what it's supposed to?
    Seine Rätselhaftigkeit wird nur durch seine Macht übertroffen!

  3. #3
    Elvenone
    Gast
    ou sorry, i want to look it like this :



    (on the left, the same code in C++)
    (on the right rewritten code to java)

    i cant figure out why it isnt working

    thank you

Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •