summaryrefslogtreecommitdiffstats
path: root/src/redbook/src/glredbook12x/multitex.java
blob: 33b9f63a1bcda7691bbb22f83a561fdcfd5f8d63 (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
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
package glredbook12x;

import glredbook10.GLSkeleton;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.nio.ByteBuffer;

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;

/**
 *
 * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
 */

public class multitex //
        extends GLSkeleton<GLJPanel>
        implements GLEventListener, KeyListener {
    private GLU glu;
    // private byte[][][] texels0 = new byte[32][32][4];
    // private byte[][][] texels1 = new byte[16][16][4];
    private ByteBuffer texelsBuf0 = BufferUtil.newByteBuffer(32 * 32 * 4);
    private ByteBuffer texelsBuf1 = BufferUtil.newByteBuffer(16 * 16 * 4);

    @Override
    protected GLJPanel createDrawable() {
        GLCapabilities caps = new GLCapabilities(null);
        //
        GLJPanel panel = new GLJPanel(caps);
        panel.addGLEventListener(this);
        panel.addKeyListener(this);
        return panel;
    }

    public static void main(String[] args) {
        GLCapabilities caps = new GLCapabilities(null);
        GLJPanel canvas = new GLJPanel(caps);

        multitex demo = new multitex();
        canvas.addGLEventListener(demo);
        if (demo instanceof KeyListener)
            canvas.addKeyListener(demo);
        // explicit cast for class not impl'ing listeners
        // to make it compile,
        if (demo instanceof MouseListener)
            canvas.addMouseListener((MouseListener) demo);
        if (demo instanceof MouseMotionListener)
            canvas.addMouseMotionListener((MouseMotionListener) demo);
        if (demo instanceof MouseWheelListener)
            canvas.addMouseWheelListener((MouseWheelListener) demo);

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

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

    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        glu = new GLU();
        //
        int texNames[] = new int[2];

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_FLAT);
        gl.glEnable(GL.GL_DEPTH_TEST);

        makeCheckImages();
        gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);

        gl.glGenTextures(2, texNames, 0);
        gl.glBindTexture(GL2.GL_TEXTURE_2D, texNames[0]);
        gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, //
                32, 32, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE, texelsBuf0);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
                GL2.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
                GL2.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
        gl
                .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
                        GL2.GL_REPEAT);
        gl
                .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
                        GL2.GL_REPEAT);

        gl.glBindTexture(GL2.GL_TEXTURE_2D, texNames[1]);
        gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, //
                16, 16, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE, texelsBuf1);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
                GL2.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
                GL2.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
                GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP_TO_EDGE);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
                GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP_TO_EDGE);
        /*
         * Use the two texture objects to define two texture units for use in
         * multitexturing
         */

        // gl.glActiveTextureARB(GL2.GL_TEXTURE0_ARB);// deprecated
        gl.glActiveTexture(GL2.GL_TEXTURE0);
        gl.glEnable(GL2.GL_TEXTURE_2D);
        gl.glBindTexture(GL2.GL_TEXTURE_2D, texNames[0]);
        gl.glTexEnvi(GL2.GL_TEXTURE_ENV, //
                GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE);
        gl.glMatrixMode(GL2.GL_TEXTURE);
        gl.glLoadIdentity();
        gl.glTranslatef(0.5f, 0.5f, 0.0f);
        gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
        gl.glTranslatef(-0.5f, -0.5f, 0.0f);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        // gl.glActiveTextureARB (GL2.GL_TEXTURE1_ARB); deprecated
        gl.glActiveTexture(GL2.GL_TEXTURE1);
        gl.glEnable(GL2.GL_TEXTURE_2D);
        gl.glBindTexture(GL2.GL_TEXTURE_2D, texNames[1]);
        gl.glTexEnvi(GL2.GL_TEXTURE_ENV, //
                GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
    }

    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        gl.glBegin(GL2.GL_TRIANGLES);
        gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0.0f, 0.0f);
        gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 1.0f, 0.0f);
        gl.glVertex2f(0.0f, 0.0f);
        gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0.5f, 1.0f);
        gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 0.5f, 0.0f);
        gl.glVertex2f(50.0f, 100.0f);
        gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 1.0f, 0.0f);
        gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 1.0f, 1.0f);
        gl.glVertex2f(100.0f, 0.0f);
        gl.glEnd();

        gl.glFlush();
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
        GL2 gl = drawable.getGL().getGL2();
        //
        gl.glViewport(0, 0, w, h);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        if (w <= h)
            glu.gluOrtho2D(0.0, 100.0, //
                    0.0, 100.0 * (double) h / (double) w);
        else
            glu.gluOrtho2D(0.0, 100.0 * (double) w / (double) h, 0.0, 100.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
            boolean deviceChanged) {
    }

    void makeCheckImages() {
        for (int i = 0; i < 32; i++) {
            for (int j = 0; j < 32; j++) {
                // texels0[i][j][0] = (byte) i;
                // texels0[i][j][1] = (byte) j;
                // texels0[i][j][2] = (byte) ((i*j)/255);
                // texels0[i][j][3] = (byte) 255;
                // changed from above for more visible result
                texelsBuf0.put((byte) (i * i * i));
                texelsBuf0.put((byte) (j * j * j));
                texelsBuf0.put((byte) ((i * j) / 255));
                texelsBuf0.put((byte) 0xFF);
            }
        }

        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                // texels1[i][j][0] = (byte) 255;
                // texels1[i][j][1] = (byte) i;
                // texels1[i][j][2] = (byte) j;
                // texels1[i][j][3] = (byte) 255;
                // changed from above for more visible result
                texelsBuf1.put((byte) 0xFF);
                texelsBuf1.put((byte) (i * i));
                texelsBuf1.put((byte) (j * j));
                texelsBuf1.put((byte) 0xFF);
            }
        }
        texelsBuf0.rewind();
        texelsBuf1.rewind();
    }

    public void keyTyped(KeyEvent key) {
    }

    public void keyPressed(KeyEvent key) {
        switch (key.getKeyCode()) {

        case KeyEvent.VK_ESCAPE:
            System.exit(0);
            break;

        default:
            break;
        }
    }

    public void keyReleased(KeyEvent key) {
    }

    public void dispose(GLAutoDrawable arg0) {
         
    }

}