Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » GDI+ & Graphics » OpenGL Basics

OpenGL Basics

In this article I want to explain couple of more things. First of all, I would like to cover the OpenGL Coordinate System.

Page Views : 97738
Downloads : 636
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
RGBCube.zip
 
 
Mindcracker MVP Summit 2012
Become a Sponsor
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Hi and welcome back to one more article about OpenGL. Last time we created a Windows Forms application with an OpenGLControl on it so our OpenGL could draw a box. In this article I want to explain couple of more things. First of all, I would like to cover the OpenGL Coordinate System. Of course I cant explain everything about it but this is what you will need to get the point in the following articles. Second I want to explain how we can draw stuff on the screen. In the article OpenGL using csGL I already showed you how to draw simple graphics objects. 

OpenGL Coordinate System for Beginners

I wont go in much detail about the OpenGL coordinate system but I will tell you what you need to get the point in my following articles.

OpenGL doesnt use the same coordinate system as GDI+ uses. As we all know the GDI+ coordinate system has only an x and a y coordinate where x is the horizontal and y is the vertical. The value of x and y on the upper left corner is 0,0, that means the starting point of GDI+ coordinate system is upper-left corner. You probably know that OpenGL draws in 3D coordinate system, while GDI+ is a 2D coordinate system. For drawing in 3D you need at least 3 coordinates because you need to go in dept. The OpenGL coordinate system uses y x and z where 0,0,0 is the precise middle of the screen. At default youre looking at the negative z direction. That is why we used glTranslatef(0.0f, 0.0f, -6.0f) so we draw at a minus 6 coordinate to make it visible for us. The OpenGL supports another coordinate the so called w coordinate this is for dividing all the coordinates and it will be somewhat like this ( x/w, y/w, z/w ) but this isnt a very common used coordinate. So to make a 2d rectangle we have to specify 4 corners. If we want to make a rectangle with an overall length of 2 we do it like this.

glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( -1.0f, -1.0f, 0.0f);

We start at the upper left corner and we go clockwise to the upper right corner then to the botto right corner and at last the down left corner.

NOTE: that the OpenGL coordinate system is also called a left handed coordinate system because you can make the same coordinate system with your left hand try to figure it out.

Please Note: Every gl command is prefix with GL. this because all functions are in CsGL.OpenGL.GL if you have Visual Studio .Net its really easy press GL. and it will show all the functions in it.

OpenGL Basics

Now we got that covered we can start with drawing in OpenGL.
As you may saw in the previous article all drawing is done between glBegin(uint mode) and glEnd(). As you saw the uint mode was GL_QUADS this is for drawing quads but there are times when you want to draw a line you will do it like this.

GL.glBegin(GL.GL_LINES);
GL.glColor3f(0.5f,0.5f,0.0f);
GL.glVertex3f(-2.0f, 2.0f, 0.0f);
GL.glVertex3f(2.0f, -2.0f, 0.0f);
GL.glEnd();

You have to put it in the base code in the glDraw function where I commented
// TODO: Draw a box or something
Now compile and run your program (F5 Vs.Net ShortCut).
What you will see is a dark green line that goes from upper left corner to bottom right corner of the window.

Now were going to try to make a 2D box. Because we dont make it too big so we use ribs of 2. It will look like this.

GL.glBegin(GL.GL_LINES);
GL.glColor3f(0.5f, 1.0f, 0.5f);
GL.glVertex3f(-1.0f, 1.0f, 0.0f);
GL.glVertex3f(1.0f, 1.0f, 0.0f);
GL.glVertex3f(1.0f, -1.0f, 0.0f);
GL.glVertex3f(-1.0f, -1.0f, 0.0f);
GL.glEnd();

Run your program once more and notice that you only see 2 lines, one from (-1.0, 1.0) to (1.0, 1.0) and the other from (1.0, -1.0) to (-1.0, -1.0).
Ok to solve this we use GL_LINE_STRIP this will loop 1 line thru all points but dont end at the beginning point for this we use GL_LINE_LOOP.
One more thing to draw only points we use GL_POINTS.
I strongly suggest to try all these to see the difference.

Before we go any further I want to show you something I got from OpenGL Redbook.

Value Meaning
GL_POINTS individual points
GL_LINES pairs of vertices interpreted as individual line segments
GL_POLYGON boundary of a simple, convex polygon
GL_TRIANGLES triples of vertices interpreted as triangles
GL_QUADS quadruples of vertices interpreted as four-sided polygons
GL_LINE_STRIP series of connected line segments
GL_LINE_LOOP same as above, with a segment added between last and first vertices
GL_TRIANGLE_STRIP linked strip of triangles
GL_TRIANGLE_FAN linked fan of triangles
GL_QUAD_STRIP linked strip of quadrilaterals

 

 

 

After seeing this I think you will get a good idea how the other works. But I will give some codes for the GL_POLYGON and GL_TRIANGLE which I didnt covered in any of mine articles.

GL.glBegin(GL.GL_POLYGON);
GL.glColor3f(0.5f, 1.0f, 0.5f);
// light green
GL.glVertex3f(-1.0f, 1.0f, -1.0f);
GL.glVertex3f(-1.0f, -1.0f, -1.0f);
GL.glVertex3f(1.0f, -1.0f, -1.0f);
GL.glVertex3f(1.0f, 1.0f, -1.0f);
GL.glVertex3f(0.0f, 1.5f, -1.0f);
GL.glEnd();

And now the GL_TRIANGLE we will draw a 3d triangle.

GL.glBegin(GL.GL_TRIANGLES);
// front
GL.glColor3f(1.0f, 0.0f, 0.0f);
// red
GL.glVertex3f(-1.5f, -0.5f, -0.8f);
GL.glColor3f(0.0f, 1.0f, 0.0f);
// green
GL.glVertex3f(1.5f, -0.5f, -0.8f);
GL.glColor3f(1.0f, 1.0f, 1.0f);
// white
GL.glVertex3f(0.0f, 2.0f, 0.0f);
// right
GL.glColor3f(0.0f, 1.0f, 0.0f);
// green
GL.glVertex3f(1.5f, -0.5f, -0.8f);
GL.glColor3f(0.0f, 0.0f, 1.0f);
// blue
GL.glVertex3f(0.0f, -0.5f, 1.7f);
GL.glColor3f(1.0f, 1.0f, 1.0f);
// white
GL.glVertex3f(0.0f, 2.0f, 0.0f);
// left
GL.glColor3f(0.0f, 0.0f, 1.0f);
// blue
GL.glVertex3f(0.0f, -0.5f, 1.7f);
GL.glColor3f(1.0f, 0.0f, 0.0f);
// red
GL.glVertex3f(-1.5f, -0.5f, -0.8f);
GL.glColor3f(1.0f, 1.0f, 1.0f);
// white
GL.glVertex3f(0.0f, 2.0f, 0.0f);
GL.glEnd();

Ok know you know pretty well the basics of OpenGL drawing. I have a little exercise for you try to draw this cube.

Its the so called RGB cube. As you can see it has all the primary colors in it. The answer is in the source delivered with this article.

TIP: Try to draw your figures first before try to draw them in OpenGL. You can also set coordinates to corners.

Summary

I hope you get a good understanding of what I am trying to show you here. I guess to get a much better understanding, you have to try it yourself. Because thats the way we all learn practice practice and more practice. In my next article on OpenGL, I will cover Texture mapping, where you will see how to place a nice skin on the cube.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Jay Smith
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
thanks for opening csgl topic by esam On October 27, 2008
thanks alot it is a very good topic to continue in  thanks again....
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.