Click here to Skip to main content
15,896,606 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
// OpenGLOneWindow6Views
// Ercan Polat 02/22/2008
// Language  : C#
// Platform  : Vista
// IDE       : Microsoft Visual Studion 2005
// Technology: Tao( Tao.OpenGl,Tao.FreeGlut), win32 Console
//******************************************************************************************
// This project is based on my previous project OpenGL 3D Navigation2 With Tao and CSharp. 
// It shows how to create multiple views on a single window. I did create 6 different views  
// of the same object. 
// *****************************************************************************************
// OpenGL 3D Navigation2 WIth Tao and CSharp
// I think one of the most important knowledge for beginners in OpenGL is to 
// learn how to create 3D space and navigate in this space.
// It can be very confiusing if you don't know where are you actually looking at.
// This program shows how to navigate in 3D space.  
// This is a beginner level program to show how to navigate in 3D space. It uses 
// glRotatef(),	glTranslatef()and glLookAt() function for navigation. It has
// example of key events, mouse events. It has example of 3D cube and Color.
// In order to make it easy I divided the 3D space with lines. The intersection of the x,y,z axis is the
// location (0,0,0). The doted part of the line is the negative side for each axis. Notice that the z-axis 
// is not viewable, because we look at the space from (0,0,15)coordinates. 
// Green for x axis
// Red for y axis
// Blue for z axis  
// x rotates on x axis       		// uses glRotatef() funxtion
// X rotates to opposite direction               "
// y rotates on y axis                           "
// Y rotates to opposite direction        		 "
// z rotates on z axis                           "
// Z rotates to opposite direction				 "
//
// left_key - translates to left (x axis)               // uses glTranslatef()
// right_key - translates to right (x axis)				       	  "
// up_key - translates up	(y axis)						      "
// down_key - translates down	(y axis)					      "
// page_up  - translates on z axis (zoom in)				      "
// page_down - translates on z axis (zoom out)				      "
//
// b,B rotates (+/-)90 degrees on x axis
// n,N rotates (+/-)90 degrees on y axis
// m,M rotates (+/-)90 degrees on z axis
//
// o,O brings everything to starting position
//
// F1 removes/shows the lines
// F2 rotates/stops the cube in x,y,z direction
// LeftMouseDown translates cube in x and y direction (up/down,left/right)
// Mouse wheel translates cube in +/- z direction (zoom in / zoom out)

using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
using Tao.FreeGlut;

namespace OpenGLOneWindow6Views
{
    sealed partial class OpenGLOneWindow6Views
    {
        // Declared static (no need for object reference)
        static float X = 0.0f;		// Translate screen to x direction (left or right)
        static float Y = 0.0f;		// Translate screen to y direction (up or down)
        static float Z = 0.0f;		// Translate screen to z direction (zoom in or out)
        static float rotX = 0.0f;	// Rotate screen on x axis 
        static float rotY = 0.0f;	// Rotate screen on y axis
        static float rotZ = 0.0f;	// Rotate screen on z axis

        static float rotLx = 0.0f;   // Translate screen by using  the glulookAt function (left or right)
        static float rotLy = 0.0f;   // Translate screen by using  the glulookAt function (up or down)
        static float rotLz = 0.0f;   // Translate screen by using  the glulookAt function (zoom in or out)

        static bool lines = true;       // Display x,y,z lines (coordinate lines)
        static bool rotation = false;   // Rotate if F2 is pressed   
        static int old_x, old_y;        // Used for mouse event
        static int mousePressed;        // Check which botton is pressed

        // Used for the font that will be displayed
        static void text(string c)
        {
            for (int i = 0; i < c.Length; i++)
            {
                // Render bitmap character
                Glut.glutBitmapCharacter(Glut.GLUT_BITMAP_TIMES_ROMAN_24, c[i]);
            }
        }

        // Draw the lines (x,y,z)
        static void draw()
        {
            int width = Glut.glutGet(Glut.GLUT_WINDOW_WIDTH);       // Get the windows width
            int height = Glut.glutGet(Glut.GLUT_WINDOW_HEIGHT);     // Get the windows height
            width = (width + 1) / 3;                                // 3 OpenGL windows side by side
            height = (height + 1) / 2;                              // 2 OpenGl windows up and down

            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); // Clear Color and Depth Buffer
           
            Gl.glEnable(Gl.GL_SCISSOR_TEST);                             // Enable Scissor Test for multiple view
           
            // First window (Front view)
            Gl.glViewport(0, height, width, height);                  // Set the viewPort
            Gl.glScissor(0, height, width, height);                   // Divide the window
            setWindow(width, height);
            Glu.gluLookAt(0.0f , 0.0f , 15.0f , 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);     // Look at the new window at this position  
            drawCube();        // Draw the cube
            Gl.glRasterPos3f(-15, 19, -15);    // Set the position for the string (text)
            text("Front");  // Display the text "Front"
           
            // Second Window (Back View)
            Gl.glViewport(0, 0, width, height);
            Gl.glScissor(0, 0, width, height);
            setWindow(width, height);
            Glu.gluLookAt(0.0f, 0.0f , -15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
            drawCube();
            Gl.glRasterPos3f(15.0f , 19.0f , 15.0f);
            text("Back");       

            // Third window (Right View)
            Gl.glViewport(width, height, width, height);
            Gl.glScissor(width, height, width, height);
            setWindow(width, height);
            Glu.gluLookAt(15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
            drawCube();
            Gl.glRasterPos3f(-15.0f, 19.0f, 15.0f);
            text("Right");

            // Forth Window (Left View)
            Gl.glViewport(width, 0, width, height);
            Gl.glScissor(width, 0, width, height);
            setWindow(width, height);
            Glu.gluLookAt(-15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
            drawCube();
            Gl.glRasterPos3f(15.0f, 19.0f, -15.0f);
            text("Left");

            // Fifth window (Upside down view)
            Gl.glViewport(2 * width, height, width, height);
            Gl.glScissor(2 * width, height, width, height);
            setWindow(width, height);
            Glu.gluLookAt(0.0f , 15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f);
            drawCube();
            Gl.glRasterPos3f(-15.0f, -15.0f, -19.0f);
            text("Up");

            // Sixth window bottom up view
            Gl.glViewport(2 * width, 0, width, height);
            Gl.glScissor(2 * width, 0, width, height);
            setWindow(width, height);
            Glu.gluLookAt(0.0f, -15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f);
            drawCube();
            Gl.glRasterPos3f(15.0f, 15.0f, -19.0f);
            text("Bottom");
         
            Gl.glDisable(Gl.GL_SCISSOR_TEST);
            Glut.glutSwapBuffers();
        }

        static void setWindow(float width, float height)
        {
            Gl.glMatrixMode(Gl.GL_PROJECTION);		                  // Set the Matrix mode
            Gl.glLoadIdentity();
            Glu.gluPerspective(75.0f, (float)width / (float)height, 0.10f, 500.0f);   // Set the perspective
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }
        // Main Starts
        static void Main(string[] args)
        {
            Glut.glutInit();                                                        // Initialize glut
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_RGB);		        // Setup display mode to double buffer and RGB color
            Glut.glutInitWindowSize(600, 600);						                // Set the screen size
            Glut.glutCreateWindow("OpenGL Multiple View");                          // Windows capture
            init();     // Call init()
            Glut.glutReshapeFunc(reshape);
            Glut.glutDisplayFunc(draw);
            Glut.glutKeyboardFunc(new Glut.KeyboardCallback(keyboard));             // Set window's key callback 	
            Glut.glutSpecialFunc(new Glut.SpecialCallback(specialKey));	            // Set window's to specialKey callback
            Glut.glutMouseFunc(new Glut.MouseCallback(processMouseActiveMotion));   // Set window's to Mouse callback
            Glut.glutMotionFunc(new Glut.MotionCallback(processMouse));             // Set window's to motion callback
            Glut.glutMouseWheelFunc(new Glut.MouseWheelCallback(processMouseWheel));// Set window's to mouse motion callback
          
            Glut.glutMainLoop();
        }

        static void drawCube()
        {
            // Clear the Color Buffer and Depth Buffer
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            Gl.glPushMatrix();								// It is important to push the Matrix before calling glRotatef and glTranslatef
            Gl.glRotatef(rotX, 1.0f, 0.0f, 0.0f);			// Rotate on x
            Gl.glRotatef(rotY, 0.0f, 1.0f, 0.0f);			// Rotate on y
            Gl.glRotatef(rotZ, 0.0f, 0.0f, 1.0f);			// Rotate on z

            if (rotation) // If F2 is pressed update x,y,z for rotation of the cube
            {
                rotX += 0.2f;
                rotY += 0.2f;
                rotZ += 0.2f;
            }

            Gl.glTranslatef(X, Y, Z);		// Translates the screen left or right, up or down or zoom in zoom out

            if (lines)  // If F1 is pressed don't draw the lines
            {
                // Draw the positive side of the lines x,y,z
                Gl.glBegin(Gl.GL_LINES);
                Gl.glColor3f(0.0f, 1.0f, 0.0f);			    // Green for x axis
                Gl.glVertex3f(0f, 0f, 0f);
                Gl.glVertex3f(10f, 0f, 0f);
                Gl.glColor3f(1.0f, 0.0f, 0.0f);				// Red for y axis
                Gl.glVertex3f(0f, 0f, 0f);
                Gl.glVertex3f(0f, 10f, 0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);				// Blue for z axis
                Gl.glVertex3f(0f, 0f, 0f);
                Gl.glVertex3f(0f, 0f, 10f);
                Gl.glEnd();

                // Dotted lines for the negative sides of x,y,z coordinates
                Gl.glEnable(Gl.GL_LINE_STIPPLE);				// Enable line stipple to use a dotted pattern for the lines
                Gl.glLineStipple(1, 0x0101);		    		// Dotted stipple pattern for the lines
                Gl.glBegin(Gl.GL_LINES);
                Gl.glColor3f(0.0f, 1.0f, 0.0f);			        // Green for x axis
                Gl.glVertex3f(-10f, 0f, 0f);
                Gl.glVertex3f(0f, 0f, 0f);
                Gl.glColor3f(1.0f, 0.0f, 0.0f);				    // Red for y axis
                Gl.glVertex3f(0f, 0f, 0f);
                Gl.glVertex3f(0f, -10f, 0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);				    // Blue for z axis
                Gl.glVertex3f(0f, 0f, 0f);
                Gl.glVertex3f(0f, 0f, -10f);
                Gl.glEnd();
                Gl.glDisable(Gl.GL_LINE_STIPPLE);				// Disable the line stipple
            }

                // Drawing 3D cube
                // Front side 
                Gl.glBegin(Gl.GL_POLYGON);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);             // Set color to blue
                Gl.glVertex3f(3.0f, 3.0f, 3.0f);
                Gl.glColor3f(1.0f, 0.0f, 0.0f);             // Set color to red
                Gl.glVertex3f(3.0f, -3.0f, 3.0f);
                Gl.glColor3f(0.0f,0.0f, 1.0f);           
                Gl.glVertex3f(-3.0f, -3.0f, 3.0f);
                Gl.glColor3f(1.0f, 0.0f, 0.0f);             
                Gl.glVertex3f(-3.0f, 3.0f, 3.0f);
                Gl.glEnd();

                // Back side
                Gl.glBegin(Gl.GL_POLYGON);
                Gl.glColor3f(0.5f, 0.0f, .0f);            
                Gl.glVertex3f(3.0f, 3.0f, -3.0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(3.0f, -3.0f, -3.0f);
                Gl.glColor3f(0.5f, 0.0f, 0.0f);
                Gl.glVertex3f(-3.0f, -3.0f, -3.0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(-3.0f, 3.0f, -3.0f);
                Gl.glEnd();

                // Right side
                Gl.glBegin(Gl.GL_POLYGON);
                Gl.glColor3f(0.0f, 1.0f, 0.0f);        
                Gl.glVertex3f(3.0f, 3.0f, 3.0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f); 
                Gl.glVertex3f(3.0f, 3.0f, -3.0f);
                Gl.glColor3f(0.0f, 1.0f, 0.0f);
                Gl.glVertex3f(3.0f, -3.0f, -3.0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(3.0f, -3.0f, 3.0f);
                Gl.glEnd();
                
                // Left Side
                Gl.glBegin(Gl.GL_POLYGON);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(-3.0f, 3.0f, 3.0f);
                Gl.glColor3f(0.0f, 1.0f, 0.0f);
                Gl.glVertex3f(-3.0f, -3.0f, 3.0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(-3.0f, -3.0f, -3.0f);
                Gl.glColor3f(0.0f, 1.0f, 0.0f);
                Gl.glVertex3f(-3.0f, 3.0f, -3.0f);
                Gl.glEnd();
                
                // Upside
                Gl.glBegin(Gl.GL_POLYGON);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(3.0f, 3.0f, 3.0f);
                Gl.glColor3f(1.0f, 1.0f, 0.0f);
                Gl.glVertex3f(3.0f, 3.0f, -3.0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(-3.0f, 3.0f, -3.0f);
                Gl.glColor3f(1.0f, 1.0f, 0.0f);
                Gl.glVertex3f(-3.0f, 3.0f, 3.0f);
                Gl.glEnd();

                // Bottom
                Gl.glBegin(Gl.GL_POLYGON);
                Gl.glColor3f(1.0f, 1.0f, 0.0f);
                Gl.glVertex3f(3.0f, -3.0f, 3.0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(3.0f, -3.0f, -3.0f);
                Gl.glColor3f(1.0f, 1.0f, 0.0f);
                Gl.glVertex3f(-3.0f, -3.0f, -3.0f);
                Gl.glColor3f(0.0f, 0.0f, 1.0f);
                Gl.glVertex3f(-3.0f, -3.0f, 3.0f);
                Gl.glEnd();
                Gl.glColor3f(1.0f, 0.5f, 0.5f);	
           
                Glut.glutPostRedisplay();	                    // Redraw the scene
                Gl.glPopMatrix();							    // Don't forget to pop the Matrix
        }
    }
}

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