We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 77,982 times

Contents

Related Categories

OpenGL and C# - Part 1 - Our Cube Class

Trialution

Our Cube Class

What we going to do now is making a class which will draw a cube. This cube can rotate, change color and even shrink or grow.I will just show you the code and I’m going to explain it to you later on.

using System;
using System.Drawing;
using CsGL.OpenGL;
namespace SimpleOpenGL
{
    public class Cube
    {
        protected float[] fColor = new float[3];
        /// <summary>
        /// How much Red between 0.00 and 1.00
        /// </summary>
        public float CubeColorRed
        {
            get { return fColor[0]; }
            set { fColor[0] = value ; }
        }
        /// <summary>
        /// How much Green between 0.00 and 1.00
        /// </summary>
        public float CubeColorGreen
        {
            get { return fColor[1]; }
            set { fColor[1] = value ; }
        }
        /// <summary>
        /// How much Blue between 0.00 and 1.00
        /// </summary>
        public float CubeColorBlue
        {
            get { return fColor[2]; }
            set { fColor[2] = value ; }
        }
        protected float fPlusPosition;
        protected float fMinusPosition;
        /// <summary>
        /// How big ??
        /// </summary>
        public float Scale
        {
            set
            {
                fPlusPosition = value;
                fMinusPosition = (0 - value);
            }
        }
        public float fRotate;
        protected bool bRotateCheck;
        /// <summary>
        /// Wanna rotate???
        /// default is false
        /// </summary>
        public bool Rotate
        {
            get { return bRotateCheck; }
            set { bRotateCheck = value; }
        }
        protected float fViewPort;
        /// <summary>
        /// With this you can zoom
        /// </summary>
        public float ViewPort
        {
            get { return fViewPort; }
            set { fViewPort = (-6 + value); }
        }
        /// <summary>
        /// Default Contructor
        /// </summary>
        public Cube()
        {
            for (int i = 0; i < 3; i++)
            {
                fColor[i] = 1.0f;
            }
            bRotateCheck = false;
            this.ViewPort = 0;
            this.fMinusPosition = 0f;
            this.Position = 2f;
        }
        /// <summary>
        /// Do the Draws
        /// </summary>
        public void glDrawCube()// To be in OpenGL style it I gave it a gl prefix
        {
            GL.glTranslatef(0.0f,0.0f, fViewPort);     // viewport = 0 0 0 and this is 6 deep
            if (bRotateCheck == true)
            {
                GL.glRotatef(fRotate, 1.0f, 1.0f, 1.0f);
            }
            GL.glBegin(GL.GL_QUADS);// start drawin the cube
                    GL.glColor3fv(fColor); // Color with an array of floats
                // Draw Top of the Cube
                GL.glVertex3f( fPlusPosition, fPlusPosition, fMinusPosition);                    
                GL.glVertex3f(fMinusPosition, fPlusPosition, fPlusPosition);                
                GL.glVertex3f(fMinusPosition, fPlusPosition, fPlusPosition);                
                GL.glVertex3f( fPlusPosition, fPlusPosition, fPlusPosition);                    
                // Draw Bottom of the Cube
                GL.glVertex3f( fPlusPosition, fMinusPosition, fPlusPosition);                    
                GL.glVertex3f(fMinusPosition,fMinusPosition, fPlusPosition);                    
                GL.glVertex3f(fMinusPosition,fMinusPosition, fMinusPosition);                    
                GL.glVertex3f( fPlusPosition,fMinusPosition, fMinusPosition);                
                // Draw Front of the Cube
                GL.glVertex3f( fPlusPosition, fPlusPosition, fPlusPosition);                
                GL.glVertex3f(fMinusPosition, fPlusPosition, fPlusPosition);                
                GL.glVertex3f(fMinusPosition,fMinusPosition, fPlusPosition);                
                GL.glVertex3f( fPlusPosition,fMinusPosition, fPlusPosition);        
                // Draw Back of the Cube
                GL.glVertex3f( fPlusPosition,fMinusPosition, fMinusPosition);    
                GL.glVertex3f(fMinusPosition,fMinusPosition, fMinusPosition);                
                GL.glVertex3f(fMinusPosition, fPlusPosition, fMinusPosition);                    
                GL.glVertex3f( fPlusPosition, fPlusPosition, fMinusPosition);                    
                // Draw Left of the Cube
                GL.glVertex3f(fMinusPosition, fPlusPosition, fPlusPosition);                
                GL.glVertex3f(fMinusPosition, fPlusPosition, fMinusPosition);                    
                GL.glVertex3f(fMinusPosition,fMinusPosition, fMinusPosition);            
                GL.glVertex3f(fMinusPosition,fMinusPosition, fPlusPosition);            
                // Draw Right of the Cube
                GL.glVertex3f( fPlusPosition, fPlusPosition, fMinusPosition);            
                GL.glVertex3f( fPlusPosition, fPlusPosition, fPlusPosition);                    
                GL.glVertex3f( fPlusPosition,fMinusPosition, fPlusPosition);                
                GL.glVertex3f( fPlusPosition,fMinusPosition, fMinusPosition);    
            GL.glEnd();// end drawing the cube    
           
            if (bRotateCheck == true)
            {
                fRotate += 2.5f;
            }
        }
    }
}

What I’m going to explain the following functions.

glTranslatef

Which is defined like this glTranslatef(float x, float y, float z) or like glTranslated(double x, double y, double z) which uses doubles instead of floats.

This function will set the drawing point. In this application we want to draw the cube right in the center of the screen so we set x and y to zero. So to make the cube visible we have to draw it a bit deeper then that we are looking so to go deeper we set the z variable in a minus position like mine apps default -6.

glRotatef

Which is defined like this glRotatef(float angle, float x, float y, float z) or like glRotated(double angle, double x, double y, double z).

To let it rotate we have to make the angle every time a bit larger like we did. For the “x y z” you just have to play with it to really get to know it.

glBegin & glEnd

Which is defined like this glBegin(uint mode) and the glEnd( void ). Those functions always come in pairs.

This function is to say against OpenGL start drawing mode or something like that. The glBegin function we used has GL_QUADS but can be much more than only quads. I will go on more detail for glBegin in the next article.

glColor3fv

This function has many others and here they are.

glColor3b(sbyte red, sbyte green, sbyte blue)
glColor3bv(sbyte[] v)
glColor3d(double red, double green, double blue)
glColor3dv(double[] v)
glColor3f(float red, float green, float blue)
glColor3fv(float[] v)
glColor3i(int red, int green, int blue)
glColor3iv(int[] v)
glColor3s(short red, short green, short blue)
glColor3sv(short[] v)

And the unsigned versions of them which are defined with a U before the type prefix. There’s also an alpha version of all this functions and have a 4 instead of a 3 where the last type variable is the alpha. This will give the quad a color where red, green and blue can be between 0.0 and 1.0

gllVertex3f

this function has also many others and here they are.

glVertex3d(double x, double y, double z)
glVertex3dv(double[] v)
glVertex3f(float x, float y, float z)
glVertex3fv(float[] v)
glVertex3i(int x, int y, int z)
glVertex3iv(int[] v)
glVertex3s(short x, short y, short z)
glVertex3sv(short[] v)

And then you can change 3 to 2 or 4. At the 2nd version of this row of functions there is no z axis. And with the 4th version of these functions there is also a w axis. This function will point a place where to draw. I will go deeper into the OpenGL coordinate at the next article so try to play with it for now.

I'm doing first year Informatica at academy of Amsterdam

Comments

  • Re: [3930] OpenGL and C# - Part 1

    Posted by mpuma on 01 Feb 2008

    I also get this error.  One way of circunventing is by setting the boolean


    CheckForIllegalCrossThreadCalls = false...

  • Re: [3930] OpenGL and C# - Part 1

    Posted by James2432 on 10 Feb 2007

    I get the fallowing exception:  Cross-Thread operation not valid: Control "accessed from a thread other than it was created on.

    in this:

    for (; ; ) // infinity loop for rendering

  • Re: [3930] OpenGL and C# - Part 1

    Posted by dreamlabs on 02 Jun 2006

    when i download the zip file for Csgl one of the files is missing, what should I do? I have looked over the web for other places that have the file, no luck.   

  • Cube doesn't appear...

    Posted by specode on 24 Nov 2005

    I am not sure but I think there is a misstyped variable name "Position", I wonder if its "fPlusPostion".

    Cube.cs Line 76
    ----
    this.ViewPort = 0;
    this.fMinusPosition = 0f...

  • Posted by echoSwe on 10 Jul 2005

    I have the same problem, but I do actually have that line in place in the constructor of OurView.

    I tried this:

    [code]
       Cube cb = new Cube();
       cb.CubeColorBlue = 0.7f;
       cb.CubeColorGreen = 0....