aboutsummaryrefslogtreecommitdiffstats
path: root/demos/RonsDemos/texobj.java
blob: 49923a0efc6b0c029194eb8da3498401c11ea6cd (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
228
229
230
231
232
233
/**
 * @(#) texobj.java
 * @(#) author: Brian Paul (converted to Java by Ron Cemer)
 */

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.util.*;

import gl4java.GLContext;
import gl4java.awt.GLAnimCanvas;
import gl4java.applet.SimpleGLAnimApplet1;

public class texobj extends SimpleGLAnimApplet1 
{

        /* Initialize the applet */


	public void init()
	{
	super.init();
        Dimension d = getSize();
        canvas = new texobjCanvas(d.width, d.height);
        add("Center", canvas);
	}


        /* Local GLAnimCanvas extension class */


    private class texobjCanvas extends GLAnimCanvas
    {
        private int TexObj[];
        float Angle = 0.0f;

        public texobjCanvas(int w, int h)
        {
            super(w, h);
            GLContext.gljNativeDebug = false;
            GLContext.gljClassDebug = false;
            setAnimateFps(30.0);

            TexObj = new int[2];
        }
    
        public void preInit()
        {
            doubleBuffer = true;
            stereoView = false;
        }
    
        public void init()
        {
            System.out.println("init(): " + this);
            reshape(getSize().width, getSize().height);
            
            int width = 8, height = 8;
            byte tex1[] =
            {
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 1, 0, 0, 0,
                0, 0, 0, 1, 1, 0, 0, 0,
                0, 0, 0, 0, 1, 0, 0, 0,
                0, 0, 0, 0, 1, 0, 0, 0,
                0, 0, 0, 0, 1, 0, 0, 0,
                0, 0, 0, 1, 1, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0
            };
            byte tex2[] =
            {
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 2, 2, 0, 0, 0,
                0, 0, 2, 0, 0, 2, 0, 0,
                0, 0, 0, 0, 0, 2, 0, 0,
                0, 0, 0, 0, 2, 0, 0, 0,
                0, 0, 0, 2, 0, 0, 0, 0,
                0, 0, 2, 2, 2, 2, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0
            };
            byte tex[] = new byte[64*3];
            int i, j;

            gl.glDisable(GL_DITHER);

                /* Setup texturing */
            gl.glEnable(GL_TEXTURE_2D);
            gl.glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
            gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);

                /* generate texture object IDs */
            gl.glGenTextures(2, TexObj);

                /* setup first texture object */
            gl.glBindTexture(GL_TEXTURE_2D, TexObj[0]);
                /* red on white */
            for (i = 0; i < height; i++)
            {
                for (j = 0; j < width; j++)
                {
                    int p = i*width+j;
                    if ((tex1[(height-i-1)*width+j]) != (byte)0)
                    {
                        tex[p*3] = (byte)255;
                        tex[(p*3)+1] = (byte)0;
                        tex[(p*3)+2] = (byte)0;
                    }
                    else
                    {
                        tex[p*3] = (byte)255;
                        tex[(p*3)+1] = (byte)255;
                        tex[(p*3)+2] = (byte)255;
                    }
                }
            }
            gl.glTexImage2D
                (GL_TEXTURE_2D,
                 0,
                 3,
                 width,
                 height,
                 0,
                 GL_RGB,
                 GL_UNSIGNED_BYTE,
                 tex);
            gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
            gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
            gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
            gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
                /* end of texture object */

                /* setup second texture object */
            gl.glBindTexture(GL_TEXTURE_2D,TexObj[1]);
                /* green on blue */
            for (i = 0; i < height; i++)
            {
                for (j = 0; j < width; j++)
                {
                    int p = i*width+j;
                    if ((tex2[(height-i-1)*width+j]) != (byte)0)
                    {
                        tex[p*3] = (byte)0;
                        tex[(p*3)+1] = (byte)255;
                        tex[(p*3)+2] = (byte)0;
                    }
                    else
                    {
                        tex[p*3] = (byte)0;
                        tex[(p*3)+1] = (byte)0;
                        tex[(p*3)+2] = (byte)255;
                    }
                }
            }
            gl.glTexImage2D
                (GL_TEXTURE_2D,
                 0,
                 3,
                 width,
                 height,
                 0,
                 GL_RGB,
                 GL_UNSIGNED_BYTE,
                 tex);
            gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
            gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
            gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
            gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
                /* end texture object */

            glj.gljCheckGL();
        }
    
        public void reshape(int width, int height)
        {
            gl.glViewport(0,0,width,height);

            gl.glMatrixMode(GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glFrustum(-2.0, 2.0, -2.0, 2.0, 6.0, 20.0);
            gl.glMatrixMode(GL_MODELVIEW);
            gl.glLoadIdentity();
            gl.glTranslatef(0.0f, 0.0f, -8.0f);
        }

        public void display()
        {
            if (glj.gljMakeCurrent() == false) return;
    
            Angle += 0.2f;
            
            gl.glDepthFunc(GL_EQUAL);
            gl.glClear(GL_COLOR_BUFFER_BIT);
            
            gl.glColor3f(1.0f, 1.0f, 1.0f);
            
            /* draw first polygon */
            gl.glPushMatrix();
            gl.glTranslatef(-1.0f, 0.0f, 0.0f);
            gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f);
            gl.glBindTexture(GL_TEXTURE_2D, TexObj[0]);
            gl.glBegin(GL_POLYGON);
            gl.glTexCoord2f(0.0f, 0.0f);   gl.glVertex2f(-1.0f, -1.0f);
            gl.glTexCoord2f(1.0f, 0.0f);   gl.glVertex2f( 1.0f, -1.0f);
            gl.glTexCoord2f(1.0f, 1.0f);   gl.glVertex2f( 1.0f,  1.0f);
            gl.glTexCoord2f(0.0f, 1.0f);   gl.glVertex2f(-1.0f,  1.0f);
            gl.glEnd();
            gl.glPopMatrix();
            
            /* draw second polygon */
            gl.glPushMatrix();
            gl.glTranslatef(1.0f, 0.0f, 0.0f);
            gl.glRotatef(Angle-90.0f, 0.0f, 1.0f, 0.0f);
            gl.glBindTexture(GL_TEXTURE_2D, TexObj[1]);
            gl.glBegin(GL_POLYGON);
            gl.glTexCoord2f(0.0f, 0.0f);   gl.glVertex2f(-1.0f, -1.0f);
            gl.glTexCoord2f(1.0f, 0.0f);   gl.glVertex2f( 1.0f, -1.0f);
            gl.glTexCoord2f(1.0f, 1.0f);   gl.glVertex2f( 1.0f,  1.0f);
            gl.glTexCoord2f(0.0f, 1.0f);   gl.glVertex2f(-1.0f,  1.0f);
            gl.glEnd();
            gl.glPopMatrix();
        
            glj.gljSwap();
            glj.gljCheckGL();
            glj.gljFree();
    
            //if (!isSuspended()) repaint();  // Animate at full speed.
        }
    }
}