본문 바로가기
Computer/JOGL

4. Color Cube

by DogBull 2007. 7. 30.
사용자 삽입 이미지
 
< 컬러 큐브 >



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;

    //########################################################################################
    //# GLEventListener 클래스의 추상 메소드
    //########################################################################################
    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);
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    }

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

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

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

    //########################################################################################
    //# GLEventListener 클래스의 추상 메소드
    //########################################################################################
    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();
    }

    //########################################################################################
    //# Cube의 하나의 사각형을 그리기 위한 메소드
    //########################################################################################
    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 }
    };
};

//########################################################################################
//# 간단한 마우스 회전 기능 추가. 회전으로 인해 좌표축이 변경되는 경우를 고려하지 않음.
//# gluLookAt(...)를 사용 하지 않고, World를 Z축으로 -5 만큼 이동시킴.
//# 이는 glu.gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0)과 비슷한 효과를 냄.

//########################################################################################

'Computer > JOGL' 카테고리의 다른 글

6. GLSL -- HelloWorld  (0) 2007.08.03
5. Text  (0) 2007.07.31
3. Draw Triangle & Animation  (0) 2007.07.29
2. Draw Triangle  (0) 2007.07.29
1. 셋팅  (0) 2007.07.29