Friday, September 21, 2012

My first visualization of math

Following code rotates a triangle:

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
 
class Renderer implements GLEventListener 
{
    private GLU glu = new GLU();
 
    float  i = 0;
    
    public void display(GLAutoDrawable gLDrawable) 
    {
        i += 0.05f;
        
        final GL2 gl = gLDrawable.getGL().getGL2();
        
        
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        
        
        gl.glTranslatef(0f, 0.0f, -6.0f);
        
        
        double rotCos = Math.cos(i);
        double rotSine = Math.sin(i);
          
          
          
        gl.glBegin(GL2.GL_TRIANGLES);       

        {
            double pointX = 0;
            double pointY = 1.0f; 
            double pointZ = 0f;
            
            double pointNewX = pointX * rotCos + pointY * rotSine;
            double pointNewY = -(pointX * rotSine) + pointY * rotCos;
            double pointNewZ = 0;
            
                    
            gl.glVertex3d(pointNewX , pointNewY , pointNewZ);   
        }
        
 
        
        {
            double pointX = -1f;
            double pointY = -1f;
            double pointZ = 0f;
            
            double pointNewX = pointX * rotCos + pointY * rotSine;
            double pointNewY = -(pointX * rotSine) + pointY * rotCos;
            double pointNewZ = 0;
            
            gl.glVertex3d(pointNewX , pointNewY , pointNewZ);       
        }
       
        
        
        
        {
            double pointX = 1f;
            double pointY = -1f;
            double pointZ = 0f;
            
            double pointNewX = pointX * rotCos + pointY * rotSine;
            double pointNewY = -(pointX * rotSine) + pointY * rotCos;
            double pointNewZ = 0;
            
            gl.glVertex3d(pointNewX , pointNewY , pointNewZ);       
        }
       
        
        
        gl.glEnd();
        
        

    }
 
 
    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) 
    {
        System.out.println("displayChanged called");
    }
 
    public void init(GLAutoDrawable gLDrawable) 
    {
        System.out.println("init() called");
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_FLAT);
    }
 
    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) 
    {
        
        System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height);
        final GL2 gl = gLDrawable.getGL().getGL2();
 
        if (height <= 0) // avoid a divide by zero error!
        {
            height = 1;
        }
 
        final float h = (float) width / (float) height;
 
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
 
 
    public void dispose(GLAutoDrawable arg0) 
    {
        System.out.println("dispose() called");
    }
}


Finally made sense of those bracketed expressions, thanks Wikipedia!

http://en.wikipedia.org/wiki/Rotation_(mathematics)

No comments:

Post a Comment