IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenGL Discussion :

JOGL et double buffering


Sujet :

OpenGL

  1. #1
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 896
    Par défaut JOGL et double buffering
    Bonjour

    J'essaie d'adapter le code C++ de lighthouse sur le Color Picking en Java avec l'API JOGL.
    Voici mes sources:
    Main.java:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
     
    import com.sun.opengl.util.Animator;
    import java.awt.Dimension;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import javax.media.opengl.GLCanvas;
    import javax.media.opengl.GLCapabilities;
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
    public class Main implements WindowListener{
     
        private Animator anim, anim2;
        private JTabbedPane jTabbedPane;
        private GLCanvas canvas;
     
        public Main(){
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            GLCapabilities glc = new GLCapabilities();
            glc.setDoubleBuffered(true);
     
            canvas = new GLCanvas(glc);
            Renderer r = new Renderer(this);
            canvas.addGLEventListener(r);
     
            frame.getContentPane().add(canvas);
            frame.setMinimumSize(new Dimension(640, 480));
            frame.setVisible(true);
            canvas.requestFocus();
            canvas.requestFocusInWindow();
        }
     
        public static void main(String[]args){
            new Main();
        }
     
        public void windowOpened(WindowEvent arg0) {
        }
     
        public void windowClosing(WindowEvent arg0) {
            if(anim.isAnimating())
                anim.stop();
            if(anim2.isAnimating())
                anim2.stop();
        }
     
        public void windowClosed(WindowEvent arg0) {
        }
     
        public void windowIconified(WindowEvent arg0) {
        }
     
        public void windowDeiconified(WindowEvent arg0) {
        }
     
        public void windowActivated(WindowEvent arg0) {
        }
     
        public void windowDeactivated(WindowEvent arg0) {
        }
     
        public void refresh(){
            canvas.display();
        }
     
    }
    Et Renderer.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
     
    import com.sun.opengl.util.BufferUtil;
    import com.sun.opengl.util.GLUT;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.nio.ByteBuffer;
    import javax.media.opengl.GL;
    import javax.media.opengl.GLAutoDrawable;
    import javax.media.opengl.GLEventListener;
    import javax.media.opengl.glu.GLU;
     
    public class Renderer implements GLEventListener, MouseListener{
     
        private final int RENDER = 1;
        private final int SELECT = 2;
        private final int SelBufferSize = 512;
        private int snowman_display_list;
        private float[]cam = new float[9];
        private int mode = RENDER;
        private int cursorX, cursorY;
        private Main main;
        private GLAutoDrawable drawable;
     
        public Renderer(Main main){
            this.main = main;
        }
     
        private void drawSnowMan(GL gl) {
            GLUT glut = new GLUT();
            // Draw Body	
            gl.glTranslatef(0.0f, 0.75f, 0.0f);
            glut.glutSolidSphere(0.75f, 20, 20);
     
     
    // Draw Head
            gl.glTranslatef(0.0f, 1.0f, 0.0f);
            glut.glutSolidSphere(0.25f, 20, 20);
     
    // Draw Eyes
            gl.glPushMatrix();
            gl.glColor3f(0.0f, 0.0f, 0.0f);
            gl.glTranslatef(0.05f, 0.10f, 0.18f);
            glut.glutSolidSphere(0.05f, 10, 10);
            gl.glTranslatef(-0.1f, 0.0f, 0.0f);
            glut.glutSolidSphere(0.05f, 10, 10);
            gl.glPopMatrix();
     
    // Draw Nose
            gl.glColor3f(1.0f, 0.5f, 0.5f);
            gl.glRotatef(0.0f, 1.0f, 0.0f, 0.0f);
            glut.glutSolidCone(0.08f, 0.5f, 10, 2);
        }
     
        private int createDL(GL gl){
            int snowManDL;
     
    	// Create the id for the list
    	snowManDL = gl.glGenLists(1);
     
    	gl.glNewList(snowManDL,gl.GL_COMPILE);
    		drawSnowMan(gl);
    	gl.glEndList();
     
    	return(snowManDL);
        }
     
        public void init(GLAutoDrawable arg0) {
            final GL gl = arg0.getGL();
     
            this.drawable = arg0;
     
            gl.glEnable(gl.GL_DEPTH_TEST);
    	gl.glEnable(gl.GL_CULL_FACE);
    	snowman_display_list = createDL(gl);
            cam[0] = 1.5f;
    	cam[1] = 3.75f;
    	cam[2] = 3f;
     
    	cam[3] = 1.5f;
    	cam[4] = 1.75f;
    	cam[5] = 0f;
     
    	cam[6] = 0f;
    	cam[7] = 1f;
    	cam[8] = 0f;
     
            arg0.addMouseListener(this);
        }
     
        private void draw(GL gl) {
            // Draw ground
            gl.glColor3f(0.9f, 0.9f, 0.9f);
            gl.glBegin(gl.GL_QUADS);
            gl.glVertex3f(-100.0f, 0.0f, -100.0f);
            gl.glVertex3f(-100.0f, 0.0f, 100.0f);
            gl.glVertex3f(100.0f, 0.0f, 100.0f);
            gl.glVertex3f(100.0f, 0.0f, -100.0f);
            gl.glEnd();
     
    // Draw 4 SnowMen
     
            gl.glColor3f(1.0f, 1.0f, 1.0f);
     
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    gl.glPushMatrix();
                    gl.glPushName(i * 2 + j);
                    gl.glTranslatef((float)(i * 3.0), (float)0, (float)(-j * 3.0));
                    gl.glColor3f(1.0f, 1.0f, 1.0f);
                    gl.glCallList(snowman_display_list);
                    gl.glPopName();
                    gl.glPopMatrix();
                }
            }
        }
     
        private void drawPickingMode(GL gl) {
            // Draw 4 SnowMen
            gl.glDisable(gl.GL_DITHER);
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    gl.glPushMatrix();
     
                    switch (i * 2 + j) {
                        case 0:
                            gl.glColor3ub((byte)255, (byte)0, (byte)0);
                            break;
                        case 1:
                            gl.glColor3ub((byte)0, (byte)255, (byte)0);
                            break;
                        case 2:
                            gl.glColor3ub((byte)0, (byte)0, (byte)255);
                            break;
                        case 3:
                            gl.glColor3ub((byte)130, (byte)0, (byte)130);
                            break;
                    }
     
                    gl.glTranslatef((float)(i * 3.0), (float)0, (float)(-j * 3.0));
                    gl.glCallList(snowman_display_list);
                    gl.glPopMatrix();
                }
            }
            gl.glEnable(gl.GL_DITHER);
        }
     
        private void processPick(GL gl) {
            int[]viewport = new int[4];
    	ByteBuffer pixel = BufferUtil.newByteBuffer(3);
     
    	gl.glGetIntegerv(gl.GL_VIEWPORT, viewport, 0);
     
    	gl.glReadPixels(cursorX, viewport[3] - cursorY, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, pixel);
     
            System.out.println(pixel.get(0)+" "+pixel.get(1)+" "+pixel.get(2));
            if (pixel.get(0) == 255) {
                System.out.println("You picked the 1st snowman on the 1st row");
            } else if (pixel.get(1) == 255) {
                System.out.println("You picked the 1st snowman on the 2nd row");
            } else if (pixel.get(2) == 255) {
                System.out.println("You picked the 2nd snowman on the 1st row");
            } else if (pixel.get(0) == 250) {
                System.out.println("You picked the 2nd snowman on the 2nd row");
            } else {
                System.out.println("You didn't click a snowman!");
            }
            System.out.println("\n");
        }
     
        public void display(GLAutoDrawable arg0) {
            final GL gl = arg0.getGL();
     
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
     
    	gl.glLoadIdentity();
     
     
    	new GLU().gluLookAt(cam[0], cam[1], cam[2], cam[3], cam[4], cam[5], cam[6], cam[7], cam[8]);
     
            if (mode == SELECT)
    		drawPickingMode(gl);
    	else
    		draw(gl);
     
    	if (mode == SELECT) {
    		processPick(gl);
    		mode = RENDER;
    	}
    	else
                arg0.swapBuffers();
        }
     
        public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int w, int h) {
            final GL gl = arg0.getGL();
            float ratio;
    	// Prevent a divide by zero, when window is too short
    	// (you cant make a window of zero width).
     
    	if(h == 0)
    		h = 1;
     
    	ratio = 1.0f * w / h;
    	// Reset the coordinate system before modifying
    	gl.glMatrixMode(gl.GL_PROJECTION);
    	gl.glLoadIdentity();
     
    	// Set the viewport to be the entire window
        	gl.glViewport(0, 0, w, h);
     
    	// Set the clipping volume
    	new GLU().gluPerspective(45,ratio,0.1,1000);
     
    	// setting the camera now
    	gl.glMatrixMode(gl.GL_MODELVIEW);
        }
     
        public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {}
     
        public void mouseClicked(MouseEvent e) {
            if(e.getButton()==MouseEvent.BUTTON1){
                cursorX = e.getPoint().x;
                cursorY = e.getPoint().y;
                mode = SELECT;
                this.main.refresh();
            }
        }
     
        public void mousePressed(MouseEvent e) {
        }
     
        public void mouseReleased(MouseEvent e) {
        }
     
        public void mouseEntered(MouseEvent e) {
        }
     
        public void mouseExited(MouseEvent e) {
        }
     
    }
    Mais il y a un problème: en effet, au clic, je tombe sur le rendering de sélection OR, ceci ne devrait pas être visible à l'utilisation.
    Aurais-je oublié quelque chose ?
    Merci d'avance de votre aide sur cette adaptation.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  2. #2
    Membre Expert
    Avatar de shenron666
    Homme Profil pro
    avancé
    Inscrit en
    Avril 2005
    Messages
    2 582
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : avancé

    Informations forums :
    Inscription : Avril 2005
    Messages : 2 582
    Par défaut
    j'aimgine que c'est juste au clic où tu n'as qu'une seule et unique frame qui affiche la sélection ?

    j'ai remarqué un truc dans le code suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
        public void mouseClicked(MouseEvent e) {
            if(e.getButton()==MouseEvent.BUTTON1){
                cursorX = e.getPoint().x;
                cursorY = e.getPoint().y;
                mode = SELECT;
                this.main.refresh();
            }
        }
    le main.refresh est réellement nécessaire ?
    ce n'est pas juste le "mode = SELECT" qui active la sélection à la prochaine frame ?
    j'ai dans l'idée que c'est lui qui perturbe ton affichage
    Tutoriels OpenGL
    Je ne répondrai à aucune question en MP
    - Si c'est simple tu dis que c'est compliqué et tu le fait
    - Si c'est compliqué tu dis que c'est simple et tu le sous-traite ou le fait faire par un stagiaire.

  3. #3
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 896
    Par défaut
    Hello shenron

    Le problème est que je n'ai pas de rafraîchissement si je ne fais pas de "refresh".
    D'où cette implémentation. M
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  4. #4
    Membre Expert
    Avatar de shenron666
    Homme Profil pro
    avancé
    Inscrit en
    Avril 2005
    Messages
    2 582
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : avancé

    Informations forums :
    Inscription : Avril 2005
    Messages : 2 582
    Par défaut
    ok, en fait ton affichage n'est mis à jour que lors d'un clic de souris ?
    tu es obligé de faire un clic dans le vide pour voir ton rendu au début ?
    Tutoriels OpenGL
    Je ne répondrai à aucune question en MP
    - Si c'est simple tu dis que c'est compliqué et tu le fait
    - Si c'est compliqué tu dis que c'est simple et tu le sous-traite ou le fait faire par un stagiaire.

  5. #5
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 896
    Par défaut
    Citation Envoyé par shenron666 Voir le message
    ok, en fait ton affichage n'est mis à jour que lors d'un clic de souris ?
    tu es obligé de faire un clic dans le vide pour voir ton rendu au début ?
    Exactement
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  6. #6
    Membre Expert
    Avatar de shenron666
    Homme Profil pro
    avancé
    Inscrit en
    Avril 2005
    Messages
    2 582
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : avancé

    Informations forums :
    Inscription : Avril 2005
    Messages : 2 582
    Par défaut
    j'ai remarqué ce qui me semble être une anomalie, puisque tu fais du color picking, le rendu de ton snowman ne devrait pas modifier la couleur hors il comporte 2 glcolor

    bon ça n'apporte rien à ton problème
    je ne vois pas trop où ça peux se situer, par contre je ne comprend pas comment ça peux fonctionner

    lors d'un clic, tu passes en mode sélection (mode = SELECT) et rafraichit l'affichage (dessin en mode color picking)
    à la fin de l'affichage, tu fais
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    processPick(gl);
    mode = RENDER;
    mais tu ne rafraichis pas ton affichage ?
    Tutoriels OpenGL
    Je ne répondrai à aucune question en MP
    - Si c'est simple tu dis que c'est compliqué et tu le fait
    - Si c'est compliqué tu dis que c'est simple et tu le sous-traite ou le fait faire par un stagiaire.

  7. #7
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 896
    Par défaut
    Bonjour Shenron

    Très juste, il faudrait que je provoque un rafraîchissement.
    Mais il y a une chose que je ne comprend pas trop. J'utilise un objet nommé Animator (http://download.java.net/media/jogl/.../Animator.html). Cet objet doit rafraîchir régulièrement le GLCanvas. Donc, si je pige bien, je ne devrais pas m'occuper du rafraîchissement. Mais dans les faits.....

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  8. #8
    Membre éprouvé
    Inscrit en
    Juin 2008
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 162
    Par défaut
    Je ne suis pas sur mais j'ai l'impression que tes objets anim et anim2 tu les crée pas ...
    Puis il ne faut qu'un seul objet Animator par GLCanvas ...
    Il ne faut pas aussi oublier de lancer l'animator avec anim.start();

    ++

  9. #9
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 896
    Par défaut
    Mince, quelle busard je suis !
    Ok, goast merci pour la remarque, il manquait cela en effet.
    Toutefois, je remarque encore le clignotement lors du clic. Donc, est-ce encore une méconnaissance de ma part ou réellement une limitation (de Java ou de JOGL) ?
    Voici le code nettoyé:
    Main.java:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
     
    import com.sun.opengl.util.Animator;
    import java.awt.Dimension;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import javax.media.opengl.GLCanvas;
    import javax.media.opengl.GLCapabilities;
    import javax.swing.JFrame;
     
    public class Main implements WindowListener{
     
        private Animator anim;
        private GLCanvas canvas;
        private Renderer r;
     
        public Main(){
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            GLCapabilities glc = new GLCapabilities();
            glc.setDoubleBuffered(true);
     
            canvas = new GLCanvas(glc);
            r = new Renderer(this);
            canvas.addGLEventListener(r);
     
            anim = new Animator(canvas);
            anim.setRunAsFastAsPossible(true);
            anim.start();
     
            frame.getContentPane().add(canvas);
            frame.setMinimumSize(new Dimension(640, 480));
            frame.setVisible(true);
            canvas.requestFocus();
            canvas.requestFocusInWindow();
        }
     
        public static void main(String[]args){
            new Main();
        }
     
        public void windowOpened(WindowEvent arg0) {
        }
     
        public void windowClosing(WindowEvent arg0) {
            if(anim.isAnimating())
                anim.stop();
        }
     
        public void windowClosed(WindowEvent arg0) {
        }
     
        public void windowIconified(WindowEvent arg0) {
        }
     
        public void windowDeiconified(WindowEvent arg0) {
        }
     
        public void windowActivated(WindowEvent arg0) {
        }
     
        public void windowDeactivated(WindowEvent arg0) {
        }
     
        public void refresh(){
            canvas.display();
        }
     
    }
    Et Renderer.java:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
     
    import com.sun.opengl.util.BufferUtil;
    import com.sun.opengl.util.GLUT;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.nio.ByteBuffer;
    import javax.media.opengl.GL;
    import javax.media.opengl.GLAutoDrawable;
    import javax.media.opengl.GLEventListener;
    import javax.media.opengl.glu.GLU;
     
    public class Renderer implements GLEventListener, MouseListener{
     
        private final int RENDER = 1;
        private final int SELECT = 2;
        private final int SelBufferSize = 512;
        private int snowman_display_list;
        private float[]cam = new float[9];
        public int mode = RENDER;
        public int cursorX, cursorY;
        private Main main;
        public GLAutoDrawable drawable;
     
        public Renderer(Main main){
            this.main = main;
        }
     
        private void drawSnowMan(GL gl) {
            GLUT glut = new GLUT();
            // Draw Body	
            gl.glTranslatef(0.0f, 0.75f, 0.0f);
            glut.glutSolidSphere(0.75f, 20, 20);
     
     
    // Draw Head
            gl.glTranslatef(0.0f, 1.0f, 0.0f);
            glut.glutSolidSphere(0.25f, 20, 20);
     
    // Draw Eyes
            gl.glPushMatrix();
            gl.glColor3f(0.0f, 0.0f, 0.0f);
            gl.glTranslatef(0.05f, 0.10f, 0.18f);
            glut.glutSolidSphere(0.05f, 10, 10);
            gl.glTranslatef(-0.1f, 0.0f, 0.0f);
            glut.glutSolidSphere(0.05f, 10, 10);
            gl.glPopMatrix();
     
    // Draw Nose
            gl.glColor3f(1.0f, 0.5f, 0.5f);
            gl.glRotatef(0.0f, 1.0f, 0.0f, 0.0f);
            glut.glutSolidCone(0.08f, 0.5f, 10, 2);
        }
     
        private int createDL(GL gl){
            int snowManDL;
     
    	// Create the id for the list
    	snowManDL = gl.glGenLists(1);
     
    	gl.glNewList(snowManDL,gl.GL_COMPILE);
    		drawSnowMan(gl);
    	gl.glEndList();
     
    	return(snowManDL);
        }
     
        public void init(GLAutoDrawable arg0) {
            final GL gl = arg0.getGL();
     
            this.drawable = arg0;
     
            gl.glEnable(gl.GL_DEPTH_TEST);
    	gl.glEnable(gl.GL_CULL_FACE);
    	snowman_display_list = createDL(gl);
            cam[0] = 1.5f;
    	cam[1] = 3.75f;
    	cam[2] = 3f;
     
    	cam[3] = 1.5f;
    	cam[4] = 1.75f;
    	cam[5] = 0f;
     
    	cam[6] = 0f;
    	cam[7] = 1f;
    	cam[8] = 0f;
     
            arg0.addMouseListener(this);
        }
     
        private void draw(GL gl) {
            // Draw ground
            gl.glColor3f(0.9f, 0.9f, 0.9f);
            gl.glBegin(gl.GL_QUADS);
            gl.glVertex3f(-100.0f, 0.0f, -100.0f);
            gl.glVertex3f(-100.0f, 0.0f, 100.0f);
            gl.glVertex3f(100.0f, 0.0f, 100.0f);
            gl.glVertex3f(100.0f, 0.0f, -100.0f);
            gl.glEnd();
     
    // Draw 4 SnowMen
     
            gl.glColor3f(1.0f, 1.0f, 1.0f);
     
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    gl.glPushMatrix();
                    gl.glPushName(i * 2 + j);
                    gl.glTranslatef((float)(i * 3.0), (float)0, (float)(-j * 3.0));
                    gl.glColor3f(1.0f, 1.0f, 1.0f);
                    gl.glCallList(snowman_display_list);
                    gl.glPopName();
                    gl.glPopMatrix();
                }
            }
        }
     
        private void drawPickingMode(GL gl) {
            // Draw 4 SnowMen
            gl.glDisable(gl.GL_DITHER);
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    gl.glPushMatrix();
     
                    switch (i * 2 + j) {
                        case 0:
                            gl.glColor3ub((byte)255, (byte)0, (byte)0);
                            break;
                        case 1:
                            gl.glColor3ub((byte)0, (byte)255, (byte)0);
                            break;
                        case 2:
                            gl.glColor3ub((byte)0, (byte)0, (byte)255);
                            break;
                        case 3:
                            gl.glColor3ub((byte)130, (byte)0, (byte)130);
                            break;
                    }
     
                    gl.glTranslatef((float)(i * 3.0), (float)0, (float)(-j * 3.0));
                    gl.glCallList(snowman_display_list);
                    gl.glPopMatrix();
                }
            }
            gl.glEnable(gl.GL_DITHER);
        }
     
        private void processPick(GL gl) {
            int[]viewport = new int[4];
    	ByteBuffer pixel = BufferUtil.newByteBuffer(3);
     
    	gl.glGetIntegerv(gl.GL_VIEWPORT, viewport, 0);
     
    	gl.glReadPixels(cursorX, viewport[3] - cursorY, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, pixel);
     
            System.out.println(pixel.get(0)+" "+pixel.get(1)+" "+pixel.get(2));
            if (pixel.get(0) == 255) {
                System.out.println("You picked the 1st snowman on the 1st row");
            } else if (pixel.get(1) == 255) {
                System.out.println("You picked the 1st snowman on the 2nd row");
            } else if (pixel.get(2) == 255) {
                System.out.println("You picked the 2nd snowman on the 1st row");
            } else if (pixel.get(0) == 250) {
                System.out.println("You picked the 2nd snowman on the 2nd row");
            } else {
                System.out.println("You didn't click a snowman!");
            }
            System.out.println("\n");
        }
     
        public void display(GLAutoDrawable arg0) {
            final GL gl = arg0.getGL();
     
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
     
    	gl.glLoadIdentity();
     
     
    	new GLU().gluLookAt(cam[0], cam[1], cam[2], cam[3], cam[4], cam[5], cam[6], cam[7], cam[8]);
     
            if (mode == SELECT)
    		drawPickingMode(gl);
    	else
    		draw(gl);
     
    	if (mode == SELECT) {
    		processPick(gl);
    		mode = RENDER;
    	}
        }
     
        public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int w, int h) {
            final GL gl = arg0.getGL();
            float ratio;
    	// Prevent a divide by zero, when window is too short
    	// (you cant make a window of zero width).
     
    	if(h == 0)
    		h = 1;
     
    	ratio = 1.0f * w / h;
    	// Reset the coordinate system before modifying
    	gl.glMatrixMode(gl.GL_PROJECTION);
    	gl.glLoadIdentity();
     
    	// Set the viewport to be the entire window
        	gl.glViewport(0, 0, w, h);
     
    	// Set the clipping volume
    	new GLU().gluPerspective(45,ratio,0.1,1000);
     
    	// setting the camera now
    	gl.glMatrixMode(gl.GL_MODELVIEW);
        }
     
        public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {}
     
        public void mouseClicked(MouseEvent e) {
            if(e.getButton()==MouseEvent.BUTTON1){
                System.err.println("clic !");
                cursorX = e.getPoint().x;
                cursorY = e.getPoint().y;
                mode = SELECT;
            }
        }
     
        public void mousePressed(MouseEvent e) {
        }
     
        public void mouseReleased(MouseEvent e) {
        }
     
        public void mouseEntered(MouseEvent e) {
        }
     
        public void mouseExited(MouseEvent e) {
        }
     
    }
    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  10. #10
    Membre éprouvé
    Inscrit en
    Juin 2008
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 162
    Par défaut
    Ce qui m'etonne unpeu c'est que tu ne fais meme plus le canvas.swapBuffers(); !!!
    Ce qui signifi que t'es pas en doubleBuffer ...
    Je vien de retester sans swap ca affiche rien du tout en mode doubleBuffer..

  11. #11
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 896
    Par défaut
    Goast

    On est bien d'accord quand même que pour se mettre en double buffering, c'est ces lignes qui le définissent:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    GLCapabilities glc = new GLCapabilities();
    glc.setDoubleBuffered(true);
    canvas = new GLCanvas(glc);
    Parce que si ce n'est pas ça, alors l'erreur peut venir de là.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  12. #12
    Membre éprouvé
    Inscrit en
    Juin 2008
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 162
    Par défaut
    Ah oui je vien de trouver ... en fait le swapBuffers se fait par defaut meme si tu ne l'appel pas. pour desactiver il faut appeler la fonction setNoAutoRedrawMode() sur ton canevas...

    je l'ai trouvé ici : http://www.sm.luth.se/~david/classes...ide/index.html

    In some situations, typically when an application is using pbuffers to compute intermediate results, it is required that automatic redraws be suspended for a particular drawable so that the application can completely control when and where the display() method is called. For this reason the GLDrawable.setNoAutoRedrawMode() method was added; it is used not only by the Jogl implementation but also by utility libraries such as gleem (included in the jogl-demos distribution). We consider it unfortunate that it was necessary to expose two APIs to express basically the same idea and hope that if the JAWT implementation in the 1.5 platform has better locking behavior that GLDrawable.setNoAutoRedrawMode() may be able to be removed.

    Dans mon cas j'avait pas à le faire car je suis en SWT et non AWT et c'est geré unpeu differement

    ++

  13. #13
    Membre Expert
    Avatar de shenron666
    Homme Profil pro
    avancé
    Inscrit en
    Avril 2005
    Messages
    2 582
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : avancé

    Informations forums :
    Inscription : Avril 2005
    Messages : 2 582
    Par défaut
    vu que je ne suis pas calé en java, je ne pige pas tout au fonctionnement
    ce qui est sûr, comme l'a fait remarqué goast, il manque un swapbuffer caractéristique à un affichage en double buffering
    j'ai regardé un tuto nehe pour comparer et leur main se résume à ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
      public static void main(String[] args)
      {
        Frame frame = new Frame("Lesson 7: Texture Filters, Lighting & Keyboard Control");
        GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
        canvas.addGLEventListener(new Renderer());
        frame.add(canvas);
        frame.setSize(640, 480);
        animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            animator.stop();
            System.exit(0);
          }
        });
        frame.show();
        animator.start();
        canvas.requestFocus();
      }
    j'ai pris le tuto 7 pour tester, en bas de page il y a les portages dont un JoGL

    pour ton affichage, perso je ferai plutot ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
        private void init_display(GL gl) {
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
     
            gl.glLoadIdentity();
     
            new GLU().gluLookAt(cam[0], cam[1], cam[2], cam[3], cam[4], cam[5], cam[6], cam[7], cam[8]);
        }
     
        public void display(GLAutoDrawable arg0) {
            final GL gl = arg0.getGL();
     
            if (mode == SELECT) {
                init_display(gl);
                drawPickingMode(gl);
                processPick(gl);
                mode = RENDER;
            }
     
            init_display(gl);
            draw(gl);
        }
    en fait, je pense que ton affichage est raffraichit après un (et un seul) appel à display, donc il faut que dans la même boucle tu fasses ton picking suivi d'un nettoyage et du dessin normal (alors que dans ton cas tu fais un display en mode SELECT et tu attends un autre display en mode RENDER)
    Tutoriels OpenGL
    Je ne répondrai à aucune question en MP
    - Si c'est simple tu dis que c'est compliqué et tu le fait
    - Si c'est compliqué tu dis que c'est simple et tu le sous-traite ou le fait faire par un stagiaire.

  14. #14
    Membre éprouvé
    Inscrit en
    Juin 2008
    Messages
    162
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 162
    Par défaut
    Juste pour signaler que les tutos NeHe sont trop anciens et ne sont plus compatibles avec la version actuelle de JOGL

  15. #15
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 896
    Par défaut
    Bonjour

    Merci à vous Goast et Shenron de votre aide.
    @Goast, j'ai regardé ton lien mais je ne trouve pas dans la Javadoc de JOGL 1.1.1 la méthode que tu indiques (setNoAutoRedrawMode()) et ce pour les classes GLCanvas et GLAutoDrawable.
    @Shenron, j'ai implémenté ton code mais, hélas, le picking me donne les couleurs pour l'image vu par l'utilisateur, pas celles attendues
    Je continue de chercher mais je vous remercie encore de votre aide, ça fait plaisir

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

  16. #16
    Membre expérimenté
    Avatar de GLDavid
    Homme Profil pro
    Head of Service Delivery
    Inscrit en
    Janvier 2003
    Messages
    2 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Head of Service Delivery
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 896
    Par défaut
    Hello

    C'est bon, j'ai résolu le problème.
    J'ai lu ce papier: http://download.java.net/media/jogl/...Userguide.html
    Donc, mon implémentation a dû ajouter ces lignes. Dans Main.java:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    GLCapabilities glc = new GLCapabilities();
    glc.setDoubleBuffered(true);
     
    canvas = new GLCanvas(glc);
    canvas.addGLEventListener(new Renderer());
    canvas.setAutoSwapBufferMode(false);
    Puis, à l'invocation de mon programme, il faut ajouter ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    -Dopengl.1thread=false
    Depuis, ça fonctionne super

    Merci encore à vous pour vos indications.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code :tagcode: ni le tag :resolu:

    Je ne répond à aucune question technique par MP.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [GDI+] Double buffer
    Par sebbb dans le forum MFC
    Réponses: 3
    Dernier message: 24/05/2005, 15h19
  2. [MFC] Scinttillement vs Double buffering
    Par DamessS dans le forum MFC
    Réponses: 9
    Dernier message: 07/04/2005, 09h01
  3. Réponses: 1
    Dernier message: 04/04/2005, 11h19
  4. Réponses: 7
    Dernier message: 03/08/2004, 16h33
  5. [Exception]Double buffering & NullPointerException
    Par Seiya dans le forum API standards et tierces
    Réponses: 25
    Dernier message: 09/07/2004, 18h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo