Click here to Skip to main content
14,979,508 members
Articles / Multimedia / OpenGL
Article
Posted 16 Jun 2015

Stats

51.1K views
5.7K downloads
19 bookmarked

My first OpenGL Project: A 3D House

Rate me:
Please Sign up or sign in to vote.
4.57/5 (6 votes)
16 Jun 2015CPOL3 min read
The project that inspired me to start 3D programming
In this article, we will take a look at project that is one of the most complete projects I have seen to start 3D programming from scratch. We will see the project overview, interesting objects and list the openGL topics covered by this project.

Image 1

Introduction

Eight years ago, I was trying to enter the 3D programming world. I was studying NEHE tutorials but due to its complexity, I quit like 3 times. The tutorials were written in C++ and were sometimes very hard to understand. Then I came across this project. I have to say this is one of the most complete projects I have seen to start 3D programming from scratch.

In the project, you will see a house with everything inside. But the amazing part of it is that everything is done by code. That means that the project doesn't load any 3D object. It just creates them at runtime. There is no 3D engine used so you get to see how most of the code looks that is used to make cameras, object interaction, collisions, among others. The programmers made all the objects just by combining simple OpenGL primitives.

Project Overview

The project language is coded in C# using TAO namespace. TAO is a library that maps all OpenGL functions to .NET. In this way, you will be using OpenGL from the .NET sandbox. The project is very modular. That means that every object is almost standalone. Each object actually contains all its own working logic so you won't get lost with too many dependencies across the project. The main class is main.cs so you can start discovering the project from there.

This is a code example of the main.cs. You can see how objects are added to the scene during runtime.

C#
public MainClass(string[] args)
        {
            GlControl ViewPort = new GlControl(Width,Height);
            GlObjectList world = new GlObjectList();
            world.Add(new LightSource());
#if !testingObjects
            world.Add(new TranslatedObject(new Point3D(0,-10,0),new SkyBox()));
            GlObjectList casa = new GlObjectList();
            casa.Add(new Casa2());

            casa.Add(new Librero(new Point3D(210,0,-318),0));
            casa.Add(new Librero(new Point3D(210,Librero.Height+.3,-316),-3));
            casa.Add(new Lamp(new Point3D(60,270,190),70));
            casa.Add(new Refrigerador(new Point3D(-145,0,120),180));
            Plantilla obj = new Mesita(new Point3D(80,0,40),90);
            casa.Add(obj);
            casa.Add(new TV(new Point3D(80,obj.Height+.2,60),0));
            casa.Add(new Cama(new Point3D(370,0,-250),0,100,70));
            casa.Add(new Cama(new Point3D(370,0,-480),0,100,60));
            casa.Add(new Cama(new Point3D(-210,0,-420),0,90,40));
            casa.Add(new Estante(new Point3D(-210,170,-108)));
            obj = new Mesa(new Point3D(380,0,200));
            casa.Add(obj);
            casa.Add(new Silla(new Point3D(440,0,160)));
            casa.Add(new Silla(new Point3D(440,0,240)));
            casa.Add(new Silla(new Point3D(330,0,160),180));
            casa.Add(new Silla(new Point3D(330,0,240),180));
            casa.Add(new Silla(new Point3D(385,0,110),90));
            casa.Add(new Silla(new Point3D(385,0,290),-90));
            casa.Add(new Plato(new Point3D(380,obj.Height+.2,200)));
            casa.Add(new Vaso(new Point3D(380,obj.Height+.2,230)));
            casa.Add(new MesitaDeNoche(new Point3D(455,0,-140),-90));
            casa.Add(new MesitaDeNoche(new Point3D(455,0,-380),-90));
            casa.Add(new MesitaDeNoche(new Point3D(455,0,-580),-90));
            casa.Add(new MesitaDeNoche(new Point3D(-280,0,-340),90));
            obj = new EstanteHorizontal(new Point3D(330,0,-70),180);
            casa.Add(obj);

            casa.Add(new Butaca(new Point3D(-30,0,200),90,2));
            casa.Add(new Butaca(new Point3D(200,0,270),250,1));
            casa.Add(new Butaca(new Point3D(200,0,110),-70,1));

            casa.Add(new MesetaConFregadero(new Point3D(-307,0,-64),0));            

            casa.Add(new Clock(new Point3D(485,200,325),270));
            GlObject c = new TranslatedObject(new Point3D(0,0,-70),casa);
            world.Add(c);
#endif

            observer=new Avatar(ViewPort, world);
            Glut.glutDisplayFunc(new Glut.DisplayCallback(observer.Look));
            Glut.glutIdleFunc(new Glut.IdleCallback(observer.Look));
            Glut.glutMainLoop();
        }

The project uses a FPS camera. You will change the camera direction with the mouse and move forward and backwards with left click/right click. To interact with objects like the house door, press space.

Also, you can press G key and activate ghost mode. In this mode, collisions won't work and you will be able to go through the house walls.

Interesting Objects

Clock

You will find a clock hanging in the wall when you enter the house. If you notice, the clock will show your computer time.

OpenGL Teapot

Entering the kitchen, you will find the classic OpenGL Teapot. This object was hardcoded into the GLUT library and is like the symbol of OpenGL.

Opening Doors

You will have to interact with doors. The will open and close. The good part is that there is no 3D engine behind it so you get to see how it is actually done.

House Furniture

Tables, chairs, beds, among other house objects can also be seen. If you look into the code, you will realize how they are built combining simple OpenGL primitives.

Skybox

If you walk around the house, you will see the sky and the ground. Form this project , I learned the basics for rendering the environment in 3D programming. Which is a textured cube (skybox) and a plane for the ground.

Ceiling Lamp

This ceiling lamp has a chain and the chain is made by code using OpenGL primitives. It is very interesting to have a look at how it is done.

Image 2Image 3Image 4

OpenGL Topics Covered by this Project

This is a list of all the basics that this project involves. The explanation of each one goes beyond the logic of this article. I recommend you to start looking over the internet.

  • FPS Camera
  • CSG (Constructive Solid Geometry)
  • Transparency
  • Collisions
  • Skybox Technique
  • Vector Handling
  • Texture Loading and mapping.
  • OpenGL primitives
  • Object interaction

Points of Interest

You can learn a lot from this project. You can start by commenting the objects added to the scene and see the outcome. If you get a little confident, you can start adding your own objects. The project is coded in a very modular way enabling you to add complexity without compromising the entire structure design.
I hope this will be your inspiration to start in this amazing world.

History

  • 16th June, 2015: First version

License

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

Share

About the Author

Vasily Tserekh
Software Developer
United States United States
Born on 86, had my first computer at the age of 8, wrote my first code at the age of 15(pascal), began to study software engineering at 2005 and graduated in 2010. I have a dev blog www.vasilydev.blogspot.com. My real passion is 3D game programming and playing guitar. I've programmed stuff in C#, python, Delphi, PHP, C++, JS, QT and others...

Comments and Discussions

 
QuestionRun Pin
Ashutosh Agarwal from Delhi8-May-21 19:22
MemberAshutosh Agarwal from Delhi8-May-21 19:22 
GeneralThis is excellent Program!! Pin
Member 1246905429-Dec-16 20:02
MemberMember 1246905429-Dec-16 20:02 
QuestionA minor correction Pin
Rob Grainger17-Jun-15 2:27
MemberRob Grainger17-Jun-15 2:27 
AnswerRe: A minor correction Pin
Vasily Tserekh17-Jun-15 3:47
MemberVasily Tserekh17-Jun-15 3:47 
GeneralLooks good, but... Pin
Brisingr Aerowing16-Jun-15 17:16
professionalBrisingr Aerowing16-Jun-15 17:16 
GeneralRe: Looks good, but... Pin
Vasily Tserekh16-Jun-15 17:38
MemberVasily Tserekh16-Jun-15 17:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.