Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / Win32

OpenGL One Window 6 Views With Tao and C# (Multiple Views)

Rate me:
Please Sign up or sign in to vote.
2.46/5 (8 votes)
25 Feb 2008CPOL3 min read 58.7K   3K   25  
An OpenGl window with 6 different views
using System;
using System.Collections.Generic;
using System.Text;
using Tao.FreeGlut;
using Tao.OpenGl;

namespace OpenGLOneWindow6Views
{
    partial class OpenGLOneWindow6Views
    {
        // This function is used for the navigation keys
        public static void keyboard(byte key, int x, int y)
        {
            switch (key)
            {
                // x,X,y,Y,z,Z uses the glRotatef() function
                case 120:	// x 			// Rotates screen on x axis 
                    rotX -= 2.0f;
                    break;
                case 88:	// X			// Opposite way 
                    rotX += 2.0f;
                    break;
                case 121:	// y		    // Rotates screen on y axis
                    rotY -= 2.0f;
                    break;
                case 89:	// Y			// Opposite way
                    rotY += 2.0f;
                    break;
                case 122:	// z			// Rotates screen on z axis
                    rotZ -= 2.0f;
                    break;
                case 90:	// Z			// Opposite way
                    rotZ += 2.0f;
                    break;

                // j,J,k,K,l,L uses the gluLookAt function for navigation
                case 106:   // j
                    rotLx -= 2.0f;
                    Gl.glMatrixMode(Gl.GL_MODELVIEW);
                    Gl.glLoadIdentity();
                    Glu.gluLookAt(rotLx, rotLy, 15.0 + rotLz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
                    break;
                case 74:    // J
                    rotLx += 2.0f;
                    Gl.glMatrixMode(Gl.GL_MODELVIEW);
                    Gl.glLoadIdentity();
                    Glu.gluLookAt(rotLx, rotLy, 15.0 + rotLz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
                    break;
                case 107:   // k
                    rotLy -= 2.0f;
                    Gl.glMatrixMode(Gl.GL_MODELVIEW);
                    Gl.glLoadIdentity();
                    Glu.gluLookAt(rotLx, rotLy, 15.0 + rotLz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
                    break;
                case 75:    // K
                    rotLy += 2.0f;
                    Gl.glMatrixMode(Gl.GL_MODELVIEW);
                    Gl.glLoadIdentity();
                    Glu.gluLookAt(rotLx, rotLy, 15.0 + rotLz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
                    break;
                case 108: // (l) It has a special case when the rotLZ becames less than -15 the screen is viewed from the opposite side
                    // therefore this if statement below does not allow rotLz be less than -15
                    if (rotLz + 14 >= 0)
                        rotLz -= 2.0f;
                    Gl.glMatrixMode(Gl.GL_MODELVIEW);
                    Gl.glLoadIdentity();
                    Glu.gluLookAt(rotLx, rotLy, 15.0 + rotLz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
                    break;
                case 76:    // L
                    rotLz += 2.0f;
                    Gl.glMatrixMode(Gl.GL_MODELVIEW);
                    Gl.glLoadIdentity();
                    Glu.gluLookAt(rotLx, rotLy, 15.0 + rotLz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
                    break;
                case 98:    // b		// Rotates on x axis by -90 degree
                    rotX -= 90.0f;
                    break;
                case 66:	// B		// Rotates on y axis by 90 degree
                    rotX += 90.0f;
                    break;
                case 110:	// n		// Rotates on y axis by -90 degree
                    rotY -= 90.0f;
                    break;
                case 78:	// N		// Rotates on y axis by 90 degree
                    rotY += 90.0f;
                    break;
                case 109:	// m		// Rotates on z axis by -90 degree
                    rotZ -= 90.0f;
                    break;
                case 77:	// M		// Rotates on z axis by 90 degree
                    rotZ += 90.0f;
                    break;
                case 111:	// o		// Resets all parameters
                case 80:    // O	    // Displays the cube in the starting position
                    rotation = false;
                    X = Y = 0.0f;
                    Z = 0.0f;
                    rotX = 0.0f;
                    rotY = 0.0f;
                    rotZ = 0.0f;
                    rotLx = 0.0f;
                    rotLy = 0.0f;
                    rotLz = 0.0f;
                    Gl.glMatrixMode(Gl.GL_MODELVIEW);
                    Gl.glLoadIdentity();
                    Glu.gluLookAt(rotLx, rotLy, 15.0f + rotLz, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
                    break;
            }
            Glut.glutPostRedisplay();	// Redraw the scene
        }

        // Called on special key pressed
        private static void specialKey(int key, int x, int y)
        {
            // Check which key is pressed
            switch (key)
            {
                case Glut.GLUT_KEY_LEFT:    // Rotate on x axis
                    X -= 2.0f;
                    break;
                case Glut.GLUT_KEY_RIGHT:	// Rotate on x axis (opposite)
                    X += 2.0f;
                    break;
                case Glut.GLUT_KEY_UP:		// Rotate on y axis 
                    Y += 2.0f;
                    break;
                case Glut.GLUT_KEY_DOWN:	// Rotate on y axis (opposite)
                    Y -= 2.0f;
                    break;
                case Glut.GLUT_KEY_PAGE_UP:  // Roatae on z axis
                    Z -= 2.0f;
                    break;
                case Glut.GLUT_KEY_PAGE_DOWN:// Roatae on z axis (opposite)
                    Z += 2.0f;
                    break;
                case Glut.GLUT_KEY_F1:      // Enable/Disable coordinate lines
                    lines = !lines;
                    break;
                case Glut.GLUT_KEY_F2:      // Enable/Disable automatic rotation
                    rotation = !rotation;
                    break;
                default:
                    break;
            }
            Glut.glutPostRedisplay();		// Redraw the scene
        }

        // Caputure the mouse click event 
        static void processMouseActiveMotion(int button, int state, int x, int y)
        {
            mousePressed = button;          // Capture which mouse button is down
            old_x = x;                      // Capture the x value
            old_y = y;                      // Capture the y value
        }

        // Translate the x,y windows coordinates to OpenGL coordinates
        static void processMouse(int x, int y)
        {
            if ((mousePressed == 0))    // If left mouse button is pressed
            {
                X = (x - old_x) / 15;       // I did divide by 15 to adjust fo a nice translation 
                Y = -(y - old_y) / 15;
            }

            Glut.glutPostRedisplay();
        }

        // Get the mouse wheel direction
        static void processMouseWheel(int wheel, int direction, int x, int y)
        {
            Z += direction;  // Adjust the Z value 

            Glut.glutPostRedisplay();
        }
        // Initiliaze the OpenGL window
        static void init()
        {
            Gl.glShadeModel(Gl.GL_SMOOTH);                  // Set the shading model to smooth 
            Gl.glClearColor(0, 0, 0, 1.0f);                 // Clear the Color
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);    // Clear the Color and Depth Buffer
            Gl.glClearDepth(1.0f);          // Set the Depth buffer value (ranges[0,1])
            Gl.glEnable(Gl.GL_DEPTH_TEST);  // Enable Depth test
            Gl.glDepthFunc(Gl.GL_LEQUAL);   // If two objects on the same coordinate show the first drawn
            Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
            Gl.glDisable(Gl.GL_CULL_FACE);
        }

        // This function is called whenever the window size is changed
        static void reshape(int w, int h)
        {
            Gl.glClearColor(0, 0, 0, 0.0f);
            Gl.glViewport(0, 0, w, h);				// Set the viewport
            Gl.glMatrixMode(Gl.GL_PROJECTION);		// Set the Matrix mode
            Gl.glLoadIdentity();
            Glu.gluPerspective(75f, (float)w / (float)h, 0.10f, 500.0f);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            Glu.gluLookAt(rotLx, rotLy, 15.0f + rotLz, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
None

Comments and Discussions