OpenGL

  • もう一度OpenGLについての演習を行ってから課題を出すことにします.
  • 注意:本ページからソースをコピーするとき,ソースのmethod名にpukiwikiの”?”が入るので注視しなさい.
  • OpenGLリンク集

課題

  • 上記のリンクなどのOpenGLの解説を読み,下記のサンプルファイルをベースとして,JOGLを用いたオリジナルの3Dグラフィクスオブジェクトを表示するJavaプログラムを作りなさい.
  • 締切:1月31日(月)
  • 形式:e-mailによりソースファイルを送ること
  • サンプルファイル1
fileTest.java
Expand allFold all
  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
 
 
 
 
 
-
|
-
-
|
|
|
|
|
|
|
|
|
|
-
-
|
|
!
!
|
|
!
-
|
!
!
|
|
-
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
-
|
|
|
|
!
|
|
|
|
!
|
|
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
|
|
-
|
|
-
!
|
-
-
!
-
-
-
|
|
|
|
!
|
!
|
|
|
|
|
!
|
|
-
!
|
-
!
|
|
-
|
|
-
|
!
!
|
-
-
|
!
!
|
|
|
-
-
|
|
|
|
|
|
|
|
|
|
|
|
!
|
|
!
!
import java.awt.*; 
import java.awt.event.*;
 
import net.java.games.jogl.*; 
public class Test 
{ 
    public static void main( String[] args ) 
    { 
        try { 
            Frame testFrame = new Frame("TestFrame"); 
            
            GLCapabilities glCaps = new GLCapabilities(); 
            GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas( glCaps ); 
            
            canvas.addGLEventListener(new TestRenderer()); 
            testFrame.add(canvas ); 
            testFrame.setSize( 500, 500 ); 
                       
            final Animator animator = new Animator( canvas); 
            testFrame.addWindowListener(new WindowAdapter() { 
                public void windowClosing(WindowEvent e) { 
                    animator.stop(); 
                    System.exit(0); 
                }
             }); 
            testFrame.show(); 
            animator.start(); 
        } 
        catch( Exception e ) { 
          e.printStackTrace(); 
        } 
    }
    
    static class TestRenderer implements GLEventListener, MouseListener, MouseMotionListener 
    { 
        private float view_rotx = 0.0f, view_roty = 0.0f, view_rotz = 0.0f;
        private float angle = 0.0f;
        private int prevMouseX, prevMouseY;
        private boolean mouseRButtonDown = false;
 
        private GL       gl; 
        private GLU       glu; 
        private GLDrawable   glDrawable; 
    
        public void init(GLDrawable drawable) 
        { 
            gl = drawable.getGL(); 
            this.glDrawable = drawable; 
            System.out.println("Init GL is " + gl.getClass().getName()); 
 
       float pos[] = { 5.0f, 5.0f, 10.0f, 0.0f };
 
 //    gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, pos);
 //     gl.glEnable(GL.GL_CULL_FACE);
 //     gl.glEnable(GL.GL_LIGHTING);
 //     gl.glEnable(GL.GL_LIGHT0);
 //     gl.glEnable(GL.GL_DEPTH_TEST);
 
            gl.glEnable(GL.GL_NORMALIZE);
                
            drawable.addMouseListener(this);
            drawable.addMouseMotionListener(this);
       } 
    
        public void display(GLDrawable drawable) 
        { 
            float red[] = { 0.8f, 0.1f, 0.0f, 1.0f };
            float green[] = { 0.0f, 0.8f, 0.2f, 1.0f };
            float blue[] = { 0.2f, 0.2f, 1.0f, 1.0f };
            float yellow[] = { 0.8f, 0.8f, 0.0f, 1.0f };
            float orenge[] = { 0.8f, 0.4f, 0.0f, 1.0f };
            float white[] = { 0.8f, 0.8f, 0.8f, 1.0f };
 
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT ); 
 
 
        gl.glPushMatrix();
        gl.glRotatef(-view_rotx, 1.0f, 0.0f, 0.0f);
        gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
        gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);
 
        gl.glColor3f(1.0f, 1.0f, 0.0f ); 
        float ss2 = 5.0f;
        gl.glBegin(GL.GL_LINES);
        gl.glColor3f(1.0f, 0.0f, 0.0f ); 
        gl.glVertex3f( 0.0f, 0.0f, 0.0f );
        gl.glVertex3f( ss2, 0.0f, 0.0f );            
        gl.glColor3f(0.0f, 1.0f, 0.0f ); 
        gl.glVertex3f( 0.0f, 0.0f, 0.0f );
        gl.glVertex3f( 0.0f, ss2, 0.0f );            
        gl.glColor3f(0.0f, 0.0f, 1.0f ); 
        gl.glVertex3f( 0.0f, 0.0f, 0.0f );
        gl.glVertex3f( 0.0f, 0.0f, ss2 );            
        gl.glEnd();
          
        gl.glPopMatrix();
      } 
    
        public void reshape(GLDrawable drawable, int x, int y, int width, int height) 
        { 
              float h = (float)height / (float)width;
              
//              gl.glViewport(0,0,500,500);
              gl.glMatrixMode(GL.GL_PROJECTION);
        
              if (!gl.isFunctionAvailable("glLoadTransposeMatrixfARB")) {
                // --- not using extensions
                gl.glLoadIdentity();
              } else {
                // --- using extensions
                final float[] identityTranspose = new float[] {
                  1, 0, 0, 0,
                  0, 1, 0, 0,
                  0, 0, 1, 0,
                  0, 0, 0, 1
                };
                gl.glLoadTransposeMatrixfARB(identityTranspose);
              }
 
          gl.glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -400.0f, 10000.0f);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();
            gl.glTranslatef(0.0f, 0.0f, -80.0f);
        } 
 
        public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) 
        { 
        } 
 
 // Methods required for the implementation of MouseListener
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
 
    public void mousePressed(MouseEvent e) {
      prevMouseX = e.getX();
      prevMouseY = e.getY();
      if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
        mouseRButtonDown = true;
      }
    }
    
    public void mouseReleased(MouseEvent e) {
      if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
        mouseRButtonDown = false;
      }
    }
    
    public void mouseClicked(MouseEvent e) {}
    
    // Methods required for the implementation of MouseMotionListener
    public void mouseDragged(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();
      Dimension size = e.getComponent().getSize();
 
      float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)size.width);
      float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)size.height);
    
      prevMouseX = x;
      prevMouseY = y;
 
      view_rotx += thetaX;
      view_roty += thetaY;
    }
    public void mouseMoved(MouseEvent e) {}
    
    } 
}

三角錐

File not found: "java" at page "ソフトウェア基礎演習2/04/第7回";

           float ss= 1.0f;
          gl.glColor3f(1.0f, 0.0f, 0.0f );
           gl.glBegin( GL.GL_TRIANGLES );
           gl.glVertex3f( ss, 0.0f, -ss );
           gl.glVertex3f( 0.0f, ss, 0.0f );
           gl.glVertex3f( 0.0f, 0.0f, ss );
           gl.glEnd();
           gl.glColor3f(0.0f, 1.0f, 0.0f );
           gl.glBegin( GL.GL_TRIANGLES );
           gl.glVertex3f( -ss, 0.0f, -ss );
           gl.glVertex3f( 0.0f, 0.0f, ss );
          gl.glVertex3f( 0.0f, ss, 0.0f );
            gl.glEnd();
          gl.glColor3f(0.0f, 0.0f, 1.0f );
           gl.glBegin( GL.GL_TRIANGLES );
           gl.glVertex3f( 0.0f, ss, 0.0f );
           gl.glVertex3f( ss, 0.0f, -ss );
           gl.glVertex3f( -ss, 0.0f, -ss );
          gl.glEnd();
          gl.glColor3f(1.0f, 1.0f, 0.0f );
           gl.glBegin( GL.GL_TRIANGLES );
           gl.glVertex3f( ss, 0.0f, -ss );
           gl.glVertex3f( 0.0f, 0.0f, ss );
           gl.glVertex3f( -ss, 0.0f, -ss );
          gl.glEnd();

EOF

立方体

File not found: "Java" at page "ソフトウェア基礎演習2/04/第7回";

           float ss2= 2.0f;
		gl.glShadeModel(GL.GL_FLAT);
		
		gl.glNormal3f(0.0f, 0.0f, 1.0f);
		
		gl.glColor3f(1.0f, 0.0f, 0.0f ); 
           gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, white);
			gl.glBegin(GL.GL_LINES);
           gl.glVertex3f( 0.0f, 0.0f, 0.0f );
           gl.glVertex3f( ss2, 0.0f, 0.0f );			
           gl.glVertex3f( 0.0f, 0.0f, 0.0f );
           gl.glVertex3f( 0.0f, ss2, 0.0f );			
           gl.glVertex3f( 0.0f, 0.0f, 0.0f );
           gl.glVertex3f( 0.0f, 0.0f, ss2 );			
			gl.glEnd();
           gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, orenge);
           gl.glBegin( GL.GL_POLYGON );
           gl.glVertex3f( ss, ss, -ss );
           gl.glVertex3f( ss, ss, ss );
           gl.glVertex3f( ss, -ss, ss );
           gl.glVertex3f( ss, -ss, -ss );
            gl.glEnd();
           gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, red);
           gl.glBegin( GL.GL_POLYGON );
           gl.glVertex3f( -ss, ss, -ss );
           gl.glVertex3f( -ss, -ss, -ss );
           gl.glVertex3f( -ss, -ss, ss );
           gl.glVertex3f( -ss, ss, ss );
           gl.glEnd();
           gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, green);
           gl.glBegin( GL.GL_POLYGON );
           gl.glVertex3f( -ss, ss, -ss );
           gl.glVertex3f( -ss, ss, ss );
           gl.glVertex3f( ss, ss, ss );
           gl.glVertex3f( ss, ss, -ss );
           gl.glEnd();
           gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, green);
           gl.glBegin( GL.GL_POLYGON );
           gl.glVertex3f( -ss, -ss, -ss );
          gl.glVertex3f( ss, -ss, -ss );
           gl.glVertex3f( ss, -ss, ss );
           gl.glVertex3f( -ss, -ss, ss );
            gl.glEnd();
           gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, blue);
           gl.glBegin( GL.GL_POLYGON );
           gl.glVertex3f( ss, ss, ss );
           gl.glVertex3f( -ss, ss, ss );
           gl.glVertex3f( -ss, -ss, ss );
           gl.glVertex3f( ss, -ss, ss );
           gl.glEnd();
           gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, blue);
           gl.glBegin( GL.GL_POLYGON );
           gl.glVertex3f( -ss, ss, -ss );
           gl.glVertex3f( ss, ss, -ss );
          gl.glVertex3f( ss, -ss, -ss );
            gl.glVertex3f( -ss, -ss, -ss );
           gl.glEnd();

EOF



添付ファイル: fileTest.java 384件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2013-06-20 (木) 14:45:27 (2280d)