JOGL-003 Draw Triangle with Animation
Post

JOGL-003 Draw Triangle with Animation

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
import javax.media.opengl.*;

import com.sun.opengl.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame {
    public Test() {
        this.setSize(400, 300);
        this.setTitle("Test1");

        GLCapabilities glCaps = new GLCapabilities();
        glCaps.setRedBits(8);
        glCaps.setBlueBits(8);
        glCaps.setGreenBits(8);
        glCaps.setAlphaBits(8);

        GLCanvas canvas = new GLCanvas(glCaps);

        canvas.addGLEventListener(new TestRenderer());

        this.add(canvas);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

        Animator anim = new Animator(canvas);
        anim.start();
    }

    public static void main(String args[]) {
        Test test = new Test();
        test.setVisible(true);
    }
};

class TestRenderer implements GLEventListener {
    private GL gl;
    private GLDrawable glDrawable;

    private int rotZ;

    public void display(GLAutoDrawable drawable) {
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();

        if (rotZ++ >= 360) rotZ = 0;        //degree 각을 이용한다.

        gl.glRotatef(rotZ, 0.0f, 0.0f, 1.0f);   //매 프레임마다 증가된 회전값 적용.

        gl.glBegin(GL.GL_TRIANGLES);
        gl.glColor3f(1.0f, 0.0f, 0.0f);     //red
        gl.glVertex3f(0.0f, 0.0f, -10.0f);

        gl.glColor3f(0.0f, 1.0f, 0.0f);     //green
        gl.glVertex3f(1.0f, 0.0f, -10.0f);

        gl.glColor3f(0.0f, 0.0f, 1.0f);     //blue
        gl.glVertex3f(1.0f, 1.0f, -10.0f);
        gl.glEnd();
    }

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

    }

    public void init(GLAutoDrawable drawable) {
        gl = drawable.getGL();
        glDrawable = drawable;
        drawable.setGL(new DebugGL(gl));

        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
        GL gl = drawable.getGL();

        float ratio = (float) h / (float) w;

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

        gl.glFrustum(-1.0f, 1.0f, -ratio, ratio, 1.0f, 60.0f);

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

img