< 에니메이션을 적용시킨 삼각형 >
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(); //에니메이션 시작(GLEventListener 의 display(...) 지속적인 호출)
}
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;
//########################################################################################
//# GLEventListener 클래스의 추상 메소드
//########################################################################################
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();
}
//########################################################################################
//# GLEventListener 클래스의 추상 메소드
//########################################################################################
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){
}
//########################################################################################
//# GLEventListener 클래스의 추상 메소드
//########################################################################################
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);
}
//########################################################################################
//# GLEventListener 클래스의 추상 메소드
//########################################################################################
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();
}
};
//########################################################################################
//# com.sun.opengl.util.Animator 클래스는 최상위 클래스로 Object클래스를 둔다.
//# GLEventListener 클래스의 display(...) 함수를 지속적으로 호출한다.
//########################################################################################
'Computer > JOGL' 카테고리의 다른 글
| 6. GLSL -- HelloWorld (0) | 2007.08.03 |
|---|---|
| 5. Text (0) | 2007.07.31 |
| 4. Color Cube (0) | 2007.07.30 |
| 2. Draw Triangle (0) | 2007.07.29 |
| 1. 셋팅 (0) | 2007.07.29 |