JOGL-004 Color Cube
Post

JOGL-004 Color Cube

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

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();                           //에니메이션 시작(GLEventListener 의 display(...) 지속적인 호출)
    }

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

class TestRenderer implements GLEventListener, MouseListener, MouseMotionListener {
    private GL gl;
    private GLU glu = new GLU();
    private GLDrawable glDrawable;

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

        //~ 간단한 회전을 구현 하기 위해 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        //~ 월드의 모든 물체는 Z축으로 -5 만큼 이동된후 그려진다.
        gl.glTranslatef(0.0f, 0.0f, -5.0f);     //모든 물체 -5만큼 이동.
        gl.glRotatef(worldRotY, 0, 1, 0);       //마우스 움직임에 따른 Y축 회전
        gl.glRotatef(worldRotX, 1, 0, 0);       //마우스 움직임에 따른 X축 회전
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        //~ 육면체의 각 면을 렌더링~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        drawQuad(0, 3, 2, 1);
        drawQuad(1, 2, 6, 5);
        drawQuad(2, 3, 7, 6);
        drawQuad(3, 0, 4, 7);
        drawQuad(4, 5, 6, 7);
        drawQuad(5, 4, 0, 1);
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    }

    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.glEnable(GL.GL_CULL_FACE);

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

        drawable.addMouseMotionListener(this);
        drawable.addMouseListener(this);
    }

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

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

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

        glu.gluPerspective(60.0f, ratio, 1.0f, 60.0f);  //gluPerspective

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

    public void drawQuad(int a, int b, int c, int d) {
        gl.glBegin(GL.GL_QUADS);
        gl.glColor3fv(colors[a], 0);
        gl.glVertex3fv(vertices[a], 0);
        gl.glColor3fv(colors[b], 0);
        gl.glVertex3fv(vertices[b], 0);
        gl.glColor3fv(colors[c], 0);
        gl.glVertex3fv(vertices[c], 0);
        gl.glColor3fv(colors[d], 0);
        gl.glVertex3fv(vertices[d], 0);
        gl.glEnd();
    }

    public void mousePressed(MouseEvent me) {
        if ((me.getModifiers() & me.BUTTON3_MASK) != 0) {
            prevMouseX = me.getX();
            prevMouseY = me.getY();
            rBtn = true;
        }
    }

    public void mouseReleased(MouseEvent me) {
        if ((me.getModifiers() & me.BUTTON3_MASK) != 0) {
            rBtn = false;
        }
    }

    public void mouseClicked(MouseEvent me) {
    }

    public void mouseEntered(MouseEvent me) {
    }

    public void mouseExited(MouseEvent me) {
    }

    public void mouseDragged(MouseEvent me) {
        if (rBtn) {
            worldRotY -= prevMouseX - me.getX();    //마우스 x축 이동은 월드의 Y축 회전
            worldRotX -= prevMouseY - me.getY();    //마우스 y축 이동은 월드의 x축 회전

            prevMouseX = me.getX();
            prevMouseY = me.getY();

            if (worldRotY > 360) worldRotY = 0;
            if (worldRotY < 0) worldRotY = 360;
            if (worldRotX > 360) worldRotX = 0;
            if (worldRotX < 0) worldRotX = 360;
        }
    }

    public void mouseMoved(MouseEvent me) {
    }


    //==================================================================================
    //= 육면체 버텍스, 컬러 리스트와 월드 회전을 위한 변수
    //==================================================================================
    float worldRotX;
    float worldRotY;
    float prevMouseX;
    float prevMouseY;
    boolean rBtn;

    float vertices[][] = {
            {-1, -1, 1}, {-1, 1, 1},
            {1, 1, 1}, {1, -1, 1},
            {-1, -1, -1}, {-1, 1, -1},
            {1, 1, -1}, {1, -1, -1}
    };

    float colors[][] = {
            {0, 0, 1}, {0, 1, 1},
            {1, 1, 1}, {1, 0, 1},
            {0, 0, 0}, {0, 1, 0},
            {1, 1, 0}, {1, 0, 0}
    };
};

img