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
| 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 {
private GL gl;
private GLU glu = new GLU();
private GLUT glut = new GLUT();
//########################################################################################
//# GLEventListener 클래스의 추상 메소드
//########################################################################################
public void display(GLAutoDrawable drawable) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glRasterPos2f(0.0f, 0.0f); // (0, 0)은 좌측 상단
glut.glutBitmapString(GLUT.BITMAP_8_BY_13, "Hello~~ ");
}
//########################################################################################
//# GLEventListener 클래스의 추상 메소드
//########################################################################################
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
//########################################################################################
//# GLEventListener 클래스의 추상 메소드
//########################################################################################
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
//########################################################################################
//# GLEventListener 클래스의 추상 메소드
//########################################################################################
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glViewport(0, -10, w, h); //-10은 제목표시줄 두께
glu.gluOrtho2D(0, drawable.getWidth(), drawable.getHeight(), 0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
};
//########################################################################################
//# com.sun.opengl.util.Animator 클래스는 최상위 클래스로 Object클래스를 둔다.
//# GLEventListener 클래스의 display(...) 함수를 지속적으로 호출한다.
//#
//# gluOrtho2D(...)를 사용하여, World를 2차원으로 설정했다.
//# 그러면 glRasterPos2f(...)을 이용하여 화면의 정확한 위치(2차원의 위치)에 출력할 수 있다.
//# ,
//# 만약, gluPerspective(...)등을 사용한다면, 화면의 중앙이 원점이 된다.
//# 그리고 glRasterPos3f(...)을 이용하여 z축 값도 추가해 준다.
//# 이 경우, 출력 위치는 화면 좌표계가 아니라, OpenGL 좌표계를 따르게 된다.
//########################################################################################
|