summaryrefslogtreecommitdiffstats
path: root/src/redbook/src/glredbook1314/mvarray.java
blob: fe96532f79f6d6a63b333a5940fc1b991c5832c0 (plain)
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
/**
 * 
 */
package glredbook1314;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import javax.media.opengl.*;
import javax.swing.JFrame;
import com.sun.opengl.util.BufferUtil;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.glu.GLU;

/**
 * This program demonstrates multiple vertex arrays, specifically the OpenGL
 * routine glMultiDrawElements(), but it's a bitch to setup--so I use
 * DrawElements in a loop instead.
 * 
 * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes (JOGL port)
 */
public class mvarray //
        implements GLEventListener //
        , KeyListener {
    private GLU glu;

    private int vertices[] = { 25, 25,//
            75, 75,//
            100, 125,//
            150, 75,//
            200, 175,//
            250, 150,//
            300, 125,//
            100, 200,//
            150, 250,//
            200, 225,//
            250, 300,//
            300, 250 };

    private IntBuffer vertexBuf = //
    BufferUtil.newIntBuffer(vertices.length);

    private byte oneIndices[] = { 0, 1, 2, 3, 4, 5, 6 };

    private byte twoIndices[] = { 1, 7, 8, 9, 10, 11 };

    private int count[] = { 7, 6 };

    private ByteBuffer indices[] = {//
    BufferUtil.newByteBuffer(oneIndices.length),
            BufferUtil.newByteBuffer(twoIndices.length) };

    // static GLvoid * indices[2] = {oneIndices, twoIndices};
    {
        vertexBuf.put(vertices);
        indices[0].put(oneIndices);
        indices[1].put(twoIndices);

        vertexBuf.rewind();
        indices[0].rewind();
        indices[1].rewind();
    }

    private boolean mde_bug;

    /**
     * 
     */
    public mvarray() {
    }

    private void setupPointer(GL2 gl) {
        gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
        gl.glVertexPointer(2, GL2.GL_INT, 0, vertexBuf);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.media.opengl.GLEventListener#display(javax.media.opengl.GLAutoDrawable)
     */
    public void display(GLAutoDrawable drawable) {
        final GL2 gl = drawable.getGL().getGL2();
        //
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glColor3f(1.0f, 1.0f, 1.0f);

        if (mde_bug)
            gl.glMultiDrawElements(GL.GL_LINE_STRIP, count, 0,//
                    GL.GL_UNSIGNED_BYTE, indices, 2);
        else {
            // workaround for glMultiDrawElem bug before July
            for (int i = 0; i < indices.length; i++)
                gl.glDrawElements(GL.GL_LINE_STRIP, count[i], //
                        GL.GL_UNSIGNED_BYTE, indices[i]);
        }
        gl.glFlush();
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.media.opengl.GLEventListener#displayChanged(javax.media.opengl.GLAutoDrawable,
     *      boolean, boolean)
     */
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
            boolean deviceChanged) {
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.media.opengl.GLEventListener#init(javax.media.opengl.GLAutoDrawable)
     */
    public void init(GLAutoDrawable drawable) {
        final GL2 gl = drawable.getGL().getGL2();
        glu = new GLU();
        //

        mde_bug = !gl.isFunctionAvailable("glMultiDrawElements");
        System.out.println("glMultiDrawElements bug: " + mde_bug);

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_SMOOTH);
        setupPointer(gl);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.media.opengl.GLEventListener#reshape(javax.media.opengl.GLAutoDrawable,
     *      int, int, int, int)
     */
    public void reshape(GLAutoDrawable drawable, int x, int y, //
            int width, int height) {
        final GL2 gl = drawable.getGL().getGL2();
        //
        gl.glViewport(0, 0, width, height);

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();

        glu.gluOrtho2D(0.0, (double) width, 0.0, (double) height);

        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        GLCapabilities caps = new GLCapabilities(null);
        caps.setSampleBuffers(true);
        GLJPanel canvas = new GLJPanel(caps);

        mvarray demo = new mvarray();
        canvas.addGLEventListener(demo);
        canvas.addKeyListener(demo);

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("mvarray");
        frame.setSize(350, 350);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(canvas);
        frame.setVisible(true);
        frame.setFocusable(false);
        canvas.requestFocusInWindow();
    }

    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_ESCAPE:
            System.exit(0);
            break;

        default:
            break;
        }
    }

    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    public void dispose(GLAutoDrawable arg0) {
         
    }

}