aboutsummaryrefslogtreecommitdiffstats
path: root/demos/MiscDemos/TriangleRotate.java
blob: 1775b68710c2b2fcda62d511084d2fc02bcb9b8c (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
/**
 * @(#) TriangleRotate.java
 * @(#) author: Joe Zimmerman (converted to Java by Sven Goethel)
 */

/* This program is free software under the license of LGPL */

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

import gl4java.*;
import gl4java.drawable.*;
import gl4java.awt.*;
import gl4java.applet.*;

public class TriangleRotate extends SimpleGLAnimApplet1 
{

    public void init()
    {
	GLContext.gljNativeDebug = true;
	GLContext.gljThreadDebug = false;
	GLContext.gljClassDebug = true;

	super.init();
        Dimension d = getSize();
	System.out.println("applet size: "+d);
        GLCapabilities caps = new GLCapabilities();

        canvas =
            GLDrawableFactory.getFactory().createGLAnimCanvas(caps, d.width, d.height);

        gldemo demo = new gldemo();
        canvas.addGLEventListener(demo);

        add("Center", canvas);
    }

	public static void main( String args[] ) {
                GLContext.gljNativeDebug = true;
                GLContext.gljClassDebug = true;
		TriangleRotate applet = 
		         new TriangleRotate();

		Frame f = new Frame("TriangleRotate");

		f.addWindowListener( new WindowAdapter()
				{
					public void windowClosed(WindowEvent e)
					{
						System.exit(0);
					}
					public void windowClosing(WindowEvent e)
					{
						windowClosed(e);
					}
				}
			);

		f.setLayout(new BorderLayout());
		f.add("Center", applet);
		applet.setSize(500,300);
		applet.init();
		applet.start();
		Dimension ps = applet.getPreferredSize();
		f.setBounds(-100,-100,99,99);
		f.setVisible(true);
		f.setVisible(false);
		f.setVisible(true);
		Insets i = f.getInsets();
		f.setBounds(0,0,
			    ps.width+i.left+i.right, 
		            ps.height+i.top+i.bottom);
		f.setVisible(true);
	}

    private class gldemo
    	implements GLEventListener
    {
	float rotate;

	float LightAmbient[]  = { 0.75f, 0.75f, 0.75f, 1.5f};
	float LightDiffuse[]  = { 1.0f, 1.0f, 1.0f, 0.9f};
	float LightSpecular[] = { 0.8f, 0.8f, 0.8f, 1.0f};

        private GLFunc gl;
        private GLUFunc glu;
	private GLContext glj;

        public gldemo()
        {
        }
    
        public void cleanup(GLDrawable drawable)
	{
	}

        public void init(GLDrawable drawable)
        {
            gl = drawable.getGL();
            glu = drawable.getGLU();
            glj = drawable.getGLContext();

		//drawable.reshape(getSize().width, getSize().height);

		gl.glEnable(GL_LIGHT0);
		gl.glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
		gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
		gl.glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);

		glj.gljCheckGL();
        }
    
        public void reshape(gl4java.drawable.GLDrawable d,int width,int height)
        {
		gl.glMatrixMode(GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(45, (float)width/(float)height, 1, 700);
		gl.glMatrixMode(GL_MODELVIEW);
		gl.glLoadIdentity();
            	gl.glViewport(0,0,width,height);
        }

        public void display(GLDrawable drawable)
        {
		gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

		gl.glColor4f(1, 1, 1, 1);

		rotate++;

		gl.glTranslatef(0, 0, -10);
		gl.glRotatef(rotate, 0, 1, 0);
		gl.glBegin(GL_POLYGON);
			gl.glNormal3f( 1, 0, 0);
			gl.glVertex3f( 0,  1, 0);
			gl.glVertex3f(-1, -1, 0);
			gl.glVertex3f( 1, -1, 0);
		gl.glEnd();

		glj.gljCheckGL();
        }

        public void preDisplay(GLDrawable drawable)
	{
	}

        public void postDisplay(GLDrawable drawable)
	{
	}
   }
}