OpenGL Demos
OpenGL Misc
MSG Board
Megabyte Softworks
C++, OpenGL, Algorithms

Current series: OpenGL 3.3
(Return to list of OpenGL 3.3 tutorials)

Download (120 KB)
6683 downloads. 49 comments
2.) First Triangle


7.3.2013 - Now compiled with glew version 1.9.0 and the executable is in bin folder now. Also all function names begin with uppercase letter.


This is the second OpenGL 3.3 (and later) tutorial. Here you can learn how to render a triangle the new way. It also explains what VBOs and VAOs are.

- Vertex Buffer Object (VBO):

You've probably already heard of it. It's one of the most important thing when it comes to learning new OpenGL (even though they were introduced in OpenGL 1.5, which is pretty old). VBOs are nothing but an arbitrary data stored on GPU Ram, so they can be accessed quickly and thus they speed up rendering. They can be whatever, but it is up to you to tell OpenGL how to interpret them. For example, you can have one VBO that stores vertices, three floats per each vertex, x, y and z coordinates, respectively. Then you can have another VBO, that stores colors of vertices. Another VBO can store texture coordinates and so on. You simply set the vertex attributes with each VBO (and vertex attributes can be really whatever, but generally, you need vertex position, texture coordinates and maybe color of vertex). If you have many VBOs, you can combine them into VAOs (Vertex Array Object), and then you can quickly switch between whole objects (more on VAOs in later tutorials).

The problem with older OpenGL was, that rendering using glBegin() and glEnd() was just creating a bottleneck on CPU, that had to pass every parameter to GPU. But now, we just load data of our object (or our scene) into GPU, tell OpenGL how to interpret them, and then we can finally start rendering. In this example, we render one triangle and one quad (using triangle strips). We won't use colors, they will come in next tutorial, along with shaders. So in initScene, we setup our vertex data (now it is only vertex positions, nothing more), and then we create two VBOs from them:

float fTriangle[9]; // Data to render triangle (3 vertices, each has 3 floats)
float fQuad[12]; // Data to render quad using triangle strips (4 vertices, each has 3 floats)

UINT uiVBO[2];

void initScene(LPVOID lpParam)
   glClearColor(0.0f, 0.5f, 1.0f, 1.0f);

   // Setup triangle vertices
   fTriangle[0] = -0.4f; fTriangle[1] = 0.1f; fTriangle[2] = 0.0f;
   fTriangle[3] = 0.4f; fTriangle[4] = 0.1f; fTriangle[5] = 0.0f;
   fTriangle[6] = 0.0f; fTriangle[7] = 0.7f; fTriangle[8] = 0.0f;
   // Setup quad vertices
   fQuad[0] = -0.2f; fQuad[1] = -0.1f; fQuad[2] = 0.0f;
   fQuad[3] = -0.2f; fQuad[4] = -0.6f; fQuad[5] = 0.0f;
   fQuad[6] = 0.2f; fQuad[7] = -0.1f; fQuad[8] = 0.0f;
   fQuad[9] = 0.2f; fQuad[10] = -0.6f; fQuad[11] = 0.0f;

   // Now we create two VBOs
   glGenBuffers(2, uiVBO);
   glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
   glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), fTriangle, GL_STATIC_DRAW);

   glBindBuffer(GL_ARRAY_BUFFER, uiVBO[1]);
   glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), fQuad, GL_STATIC_DRAW);

OK, let's have a look at important parts. After setting color and initializing vertices data, we call function glGenBuffers. The first parameter is number of VBOs we want (in our case, 2). Second parameter is pointer where to store them (in our case, store it in uiVBO). Now in uiVBO, we have two IDs of buffers. With them we can access and manipulate them. After that, we tell OpenGL that we're gonna work with first buffer (that has name stored in uiVBO[0]) with function glBindBuffer. First parameter is target to which buffer is bound (in out case it is GL_ARRAY_BUFFER, other possible values are for pixel buffer objects for example, more on them in later tutorials). Second parameter is buffer ID. After that we call glBufferData. With that function, we load data into GPU. First parameter is buffer type, then it's number of bytes to be transferred to buffer, third parameter is source of data in client memory and with last parameter we tell OpenGL how is the buffer intended to be used. In our case, we will not change data (they're static), so we set it to GL_STATIC_DRAW. Other possible values are for example GL_DYNAMIC_DRAW, or GL_STREAM_DRAW for example. OpenGL somehow optimizes performance using this hint.

We call these two functions also for our quad. Now we have data set, let's look and analyze rendering function:

void renderScene(LPVOID lpParam)
   // Typecast lpParam to COpenGLControl pointer
   COpenGLControl* oglControl = (COpenGLControl*)lpParam;

   // We just clear color

   // Triangle render
   glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
   glDrawArrays(GL_TRIANGLES, 0, 3);

   // Quad render using triangle strip
   glBindBuffer(GL_ARRAY_BUFFER, uiVBO[1]);
   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


We are interested in two functions. First is glVertexAttribPointer(). With it, we set attributes of vertices. As I mentioned in previous tutorial, vertices can have multiple attributes, like position, color and so on (in shaders, we can tell OpenGL how to treat attributes). The first parameter is index of vertex attributes. In our case, it's 0 and it will be a vertex position. Since we are not using shaders yet, vertices go through rendering pipeline without any change, and are output with same values as they came in (notice that we don't tell OpenGL that attribute with index 0 is position, but it seems that somehow it's the default behavior that it will treat it as position). So parameters of this functions are in order: index of attribute set (in our case 0), number of components per vertex attribute (in our case it's 3, as one position consists of X, Y and Z values), then there is data type (we have floats, so GL_FLOAT is used), fourth parameter is whether data should be normalized (no, they shouldn't, that's why GL_FALSE), fifth parameter (stride) is byte offset between two attributes (but we have data tightly packed in memory, so it's 0, and in most cases, this parameter will be 0), and the last parameter is a pointer to the first component (we begin where the array starts, so it's 0).

Now we have set up what to render, and we can call glDrawArrays to actually render something. First parameter is render mode (ol' good GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_LINES... but some have been deprecated, like GL_QUADS, now you cannot render quads this way, you must replace it with for example triangle strips). Second parameter is starting index (again, we want to render all data, in our case send down 3 vertices, so it's 0). Third is number of indices to be rendered (and in our case one index consists of 3 floats [X,Y,Z] position, as we told it with glVertexAttribPointer). We call the same set of functions for quad as well (and we render it using GL_TRIANGLE_STRIP, if you don't know how triangle strips work, it can be found anywhere on Internet). And hurray, the result:

Edit on 21.01.2012

It's been reported that some cards draw only blank blue screen. That's because of different implementations of OpenGL on them probably. I tested this tutorial on ATI Radeon HD 5870 (triangles were drawn), GTX 550 (triangles were drawn) and GTX 220 (nothing was drawn, only blue screen). Problem is that we're not using shaders and not telling OpenGL how to treat incoming data. Some implementations treat them as vertices, and some don't know what to do with them, so they rather do nothing. If this doesn't work for you, move on to third tutorial with shaders, and all later tutorials should work (learn just important stuff from here).

So that's it for today. Hope this tutorial helped you a little. In next tutorial, we will start using shaders and will add colors to our triangle and quad, to make the scene nicer.

Download (120 KB)
6683 downloads. 49 comments


Enter the text from image:


Angelendug (lvmu14209@first.baburn.com) on 06.12.2017 03:59:37


[url=http://www.gebrauchte-boote-motorboote-segelboote.de/684-parajumpers-damen-leder.htm]Parajumpers Damen Leder[/url]
[url=http://www.domar.nu/443-parajumpers-juliet-takki.htm]Parajumpers Juliet Takki[/url]
[url=http://www.digifone.co.uk/572-duvetica-grey.html]Duvetica Grey[/url]
[url=http://www.la-jetee.it/992-ugg-australia-dakota-shearling-lined-moccasins.html]Ugg Australia Dakota Shearling-lined Moccasins[/url]
[url=http://www.cygnethouse.co.uk/308-woolrich-gtx-mountain-parka.php]Woolrich Gtx Mountain Parka[/url]
WilliamRus (oztk28877@first.baburn.com) on 30.11.2017 02:30:30
[url=http://www.liefdedelennaverlies.nl/oakley-frogskin-bestellen-933.php]Oakley Frogskin Bestellen[/url]
When attempting to lose weight you ought to include plenty of species of fish and fish in your daily diet. Swapping all the red meat consume with species of fish is amongst the very best actions to take. Species of fish has just as much proteins as red meat, nevertheless it has much less calories and much less saturated fats.


This article has taught you what it really takes to simply build muscle mass and boost your way of life. Now it is perfectly up to one to manage your physique and start to exercise it within a healthy, powerful approach. All you have to do is get going nowadays!Being a parent Is Not Really For Your Faint Of Heart: Tips To Help You Become successful!

Angelfum (ldop99918@first.baburn.com) on 07.11.2017 18:57:34
[url=http://www.assuwish.nl/adidas-superstar-rood-wit-blauw-534.html]Adidas Superstar Rood Wit Blauw[/url]
Use photos intelligently. Bitmap images tend not to often fare nicely for online use, and several GIFs do not work well with plenty of color. Image dimensions are significant as properly, as larger sized photos could make your visitors ought to wait to allow them to acquire. Select small pictures, and utilize them sparingly to make your site a lot more achievable.


Attempt to make a list of reasons why you are stopping. Keep it convenient. This list will function as a reminder of all the stuff that you will be getting by giving up in the long term. Every time you sense the urge to cigarette smoke, try to have a great long take a look at collection and this will assist you to adhere to your prepare.

Thomasagoms (xcbp81840@catch@first.baburn.com) on 07.11.2017 17:48:11


[url=http://www.pedicure-podologue-bourgeoismontrouge.fr/adidas-chaussures-stan-smith-050.php]Adidas Chaussures Stan Smith[/url]
[url=http://www.gressoneywalserfestival.it/568-hollister-outlet-orlando.html]Hollister Outlet Orlando[/url]
[url=http://www.stadium-mk.co.uk/michael-kors-crossbody-with-zippers-328.html]Michael Kors Crossbody With Zippers[/url]
[url=http://www.smithland.co.uk/nike-cortez-shoes-buy-595.html]Nike Cortez Shoes Buy[/url]
[url=http://www.gpbiancheria-palombella.it/rivenditore-vibram-fivefingers-823.htm]Rivenditore Vibram Fivefingers[/url]
JamesCex (lgiv20224@first.baburn.com) on 21.10.2017 19:49:37
<a href=http://www.buchhandlung-lubig.de/under-armour-basketballschuhe-kinder-584.php>Under Armour Basketballschuhe Kinder</a>
In order to avoid candidiasis, you ought to be cautious of your products which enter in to experience of your genitals. Feminine aerosols, bubble bathing, and scented cleansers can all inflame the area inside the vaginal area and increase the potential risk of a yeast infection. Anything that is fragrant or dyed should never come into exposure to the vagina.


Enhance your blog site regularly. Article usually and you will definitely make your visitors returning for far more. There exists absolutely nothing much more disappointing than going to a beloved weblog, only to discover it hasn't been updated in days. Operating a profitable blog site is undoubtedly an continuing responsibility, so make sure you are completely ready for the long haul.

BryceAsper (czke72494@first.baburn.com) on 21.10.2017 15:24:32
<a href=http://www.cao-verblijfsrecreatie.nl/ray-ban-new-wayfarer-matte-610.php>Ray Ban New Wayfarer Matte</a>
Stay inside your home while in great plant pollen matter times. Pollen and other air-borne contaminants take into account numerous symptoms of asthma signs. When pollen matters attain greater amounts, the volume of individuals from the ER with extreme asthma attack assaults boosts. Prevent an assault, by keeping yourself in the house with the air conditioning unit or another atmosphere filtering method.


Offer your prospects with the chance to sell your product or service with an affiliate marketing program. Giving it has the possible to turn a single sale into many sales. Even if you just obtain a number of customers to join up, the income which you make off their operate may help with the advertising and marketing as well as other expenses.

BrentDOb (pqjj63737@first.baburn.com) on 04.10.2017 21:30:42
<a href=http://www.deharmonie.nu/ray-ban-Γυαλια-Τιμες-308.php>Ray Ban Γυαλια Τιμες</a>
Whilst looking after your organic garden entails a lot of huge, working day-very long duties, additionally, it calls for smaller work that should be performed more frequently. Keep a great take care of in the minor requirements of your backyard garden to enable you to make very good use of quick periods of leisure time. Once you have a few momemts to free, why not utilize them weeding, trimming or carrying out other backyard routine maintenance tasks?


One thing many of us have to worry about every day is the way you take care of our oral hygiene. In the event you don't deal with your mouth, you could find yourself paying out a ton of money to get your troubles repaired. Make use of the subsequent information to make certain you're taking care of yourself.

Donaldbycle (ntnb64920@first.baburn.com) on 03.09.2017 21:11:01


<a href=http://www.starlightmusic.it/797-jordan-spizike-retro.php>Jordan Spizike Retro</a>
<a href=http://www.angelozzisrl.it/air-force-one-bianche-basse-985.htm>Air Force One Bianche Basse</a>
<a href=http://www.intercircoli.it/scarpe-caterpillar-uomo-vendita-online-870.htm>Scarpe Caterpillar Uomo Vendita Online</a>
<a href=http://www.3in1concepts.it/034-adidas-kanadia-7-trail.php>Adidas Kanadia 7 Trail</a>
<a href=http://www.historiography.it/scarpe-gucci-bianche-prezzo-365.html>Scarpe Gucci Bianche Prezzo</a>
Richarddync (ebaj77485@first.baburn.com) on 02.09.2017 05:25:06


<a href=http://www.convergenc.es/559-nike-air-max-thea-grises.html>Nike Air Max Thea Grises</a>
<a href=http://www.pasionescort.es/puma-x-veil-325.php>Puma X Veil</a>
<a href=http://www.ajedrezlinares.es/adidas-zx-colores-852.html>Adidas Zx Colores</a>
<a href=http://www.itcolorsesteelauder.es/asics-verdes-mujer-103.asp>Asics Verdes Mujer</a>
<a href=http://www.conelguaposubidoterracotta.es/adidas-x15.1-azules-294.html>Adidas X15.1 Azules</a>
Stephenwimew (kqon98766@catch@first.baburn.com) on 02.09.2017 05:25:05


<a href=http://www.clinicadeldolorneuromuscular.es/superstar-rosas-adidas-791.html>Superstar Rosas Adidas</a>
<a href=http://www.gigaphotoproject.es/adidas-sl-78-082.php>Adidas Sl 78</a>
<a href=http://www.livingincomfort.es/zapatos-cat-mujer-con-plataforma-359.html>Zapatos Cat Mujer Con Plataforma</a>
<a href=http://www.spainlacrosse.es/nike-janoski-grises-con-azul-541.html>Nike Janoski Grises Con Azul</a>
<a href=http://www.spainlacrosse.es/nike-sb-janoski-37-729.html>Nike Sb Janoski 37</a>
GreggBoade (ymjo8403@first.baburn.com) on 26.08.2017 20:09:29


<a href=http://www.agriturlasabbionara.it/640-air-jordan-3-rosse.htm>Air Jordan 3 Rosse</a>
<a href=http://www.sancolombanocalcio.it/adidas-zx-700-prezzo-735.htm>Adidas Zx 700 Prezzo</a>
<a href=http://www.agriturismo-a-firenze.it/039-converse-bianche-suola-alta.php>Converse Bianche Suola Alta</a>
<a href=http://www.agriturismo-a-firenze.it/027-converse-alte-personalizzate.php>Converse Alte Personalizzate</a>
<a href=http://www.garim.it/nike-flyknit-chukka-colors-421.asp>Nike Flyknit Chukka Colors</a>
Michaelmup (cbti61745@catch@first.baburn.com) on 23.08.2017 20:01:11


<a href=http://www.gigaphotoproject.es/precio-adidas-tubular-417.php>Precio Adidas Tubular</a>
<a href=http://www.itcolorsesteelauder.es/zapatillas-asics-volleyball-mujer-749.asp>Zapatillas Asics Volleyball Mujer</a>
<a href=http://www.tv-gratuite.fr/stan-smith-1990-249.htm>Stan Smith 1990</a>
<a href=http://www.probaiedumontsaintmichel.fr/186-new-balance-996-homme-bordeaux.php>New Balance 996 Homme Bordeaux</a>
<a href=http://www.probaiedumontsaintmichel.fr/338-new-balance-ml373-noir.php>New Balance Ml373 Noir</a>
Richarddync (ihnj52103@catch@first.baburn.com) on 13.08.2017 08:12:58
<a href=http://www.xavier-massonnaud.fr/ray-ban-aviator-mirror-silver-985.php>Ray Ban Aviator Mirror Silver</a>
Don't neglect to pack a meal for an extended fishing vacation. There exists practically nothing far more frustrating than ending an excellent sportfishing getaway earlier simply because you are receiving hungry. Pack some snacks during the day as well. Recall, don't litter and keep every one of the product packaging in a handbag to dispose of later!


Whenever you can become the first man or woman to go over a topic, that's the best choice for articles. Speak about items you truly are an authority in and give recommendations which can't be located in other places. When people observe that what you're offering isn't available on other sites, they'll think about your blog to become the expert in the area.

MerlinBot (zwyn42708@first.baburn.com) on 02.08.2017 18:05:09
<a href=http://www.campesatosrl.it/occhiali-da-sole-oakley-2015-790.php>Occhiali Da Sole Oakley 2015</a>
To assist you to stop smoking throughout the giving up procedure, you must stay away from spending time with people who light up. Hanging out with smokers from the initial levels of the stop will in all probability cause you to relapse into cigarette smoking. Just, the scent of tobacco on the clothing of such folks will give you the impulse to illuminate a tobacco cigarette.


Just before using any kind of eye lash glue about the eyes, analyze it within your left arm fifteen-a number of time prior to you are wanting to use it for your eyeballs. This can be the easiest way to analyze for allergy symptoms and may help you prevent getting your eyes swell close from a hypersensitive reaction.

Richarddync (nald92518@first.baburn.com) on 20.07.2017 09:23:06


<a href=http://www.rifugioparcodeltadelpo.it/scarpe-nike-scontate-milano-476.htm>Scarpe Nike Scontate Milano</a>
<a href=http://www.romars.it/air-max-maschili-ebay-867.html>Air Max Maschili Ebay</a>
<a href=http://www.amadoriscavi.it/nike-huarache-military-green-311.html>Nike Huarache Military Green</a>
<a href=http://www.pixelgenic.it/vans-verdi-alte-059.html>Vans Verdi Alte</a>
<a href=http://www.montevarchicalcio.it/longchamp-backpacks-932.html>Longchamp Backpacks</a>
Richarddync (styw45944@first.baburn.com) on 10.07.2017 06:07:20


<a href=http://www.tr-online.nl/188-adidas-zx-flux-white.php>Adidas Flux</a>
<a href=http://www.rwpieters.nl/814-vans-pro.html>Vans Pro</a>
<a href=http://www.decoraciondeinterioresweb.es/gafas-ray-ban-clubmaster-precio-441.php>Gafas Ban</a>
<a href=http://www.adidasneoblanche.fr/chaussure-neo-piona-850.php>Chaussure Neo Piona</a>
<a href=http://www.demetz.co.uk/adidas-zx-flux-black-womens-534.html>Adidas Zx Flux Black Womens</a>
Scottawant (mwvb32118@first.baburn.com) on 09.07.2017 07:59:55


<a href=http://www.hotel-katerstuben.de/507-adidas-nmd-runner-original.htm>Adidas Nmd Runner Original</a>
<a href=http://www.b-photo.fr/polo-lacoste-homme-bleu-541.php>Polo Lacoste Homme Bleu</a>
<a href=http://www.vacu-step.fr/851-louis-vuitton-sacoche-pour-homme.php>Louis Vuitton Sacoche Pour Homme</a>
<a href=http://www.istitutocomprens1giorgione.it/864-longchamp-backpack-online-shop.htm>Longchamp Backpack Online Shop</a>
<a href=http://www.s-pruesse.de/183-nike-roshe-one-herren-grau.htm>Nike Roshe One Herren Grau</a>
Curtisst (ecor54892@first.baburn.com) on 03.07.2017 04:13:02


<a href=http://www.professionalplan.es/salomon-zapatillas-aliexpress-282.php>Salomon Aliexpress</a>
<a href=http://www.wallbank-lfc.co.uk/849-adidas-tubular-radial-blackburgundy.htm>Adidas Tubular Radial Black/Burgundy</a>
<a href=http://www.pcbodelft.nl/280-nike-sb-stefan-janoski-max-grijs.html>Nike Sb Stefan Janoski Max Grijs</a>
<a href=http://www.wallbank-lfc.co.uk/114-adidas-shoes-for-girls-black-and-gold.htm>Adidas Shoes For Girls Black And Gold</a>
<a href=http://www.conijn-partyservice.nl/468-welke-salomon-schoenen.php>Welke Salomon Schoenen</a>
TylerDed (apvy29527@catch@first.baburn.com) on 27.05.2017 00:17:02


<a href=http://www.hotel4alle.de/nike-jordan-herren-schwarz-116.aspx>Nike Jordan Herren Schwarz</a>
<a href=http://www.jetzt-lastminute-pauschalreise.de/564-adidas-neo-rot.php>Adidas Neo Rot</a>
<a href=http://www.hotel-katerstuben.de/500-adidas-superstar-colorways.htm>Adidas Superstar Colorways</a>
<a href=http://www.counter-fuchs.de/164-reebok-classic-khaki-oliv-limitiert.php>Reebok Classic Khaki Oliv Limitiert</a>
<a href=http://www.cafedart-neuulm.de/635-ray-ban-schwarz-damen.html>Ray Ban Schwarz Damen</a>
Donaldwhels (iloc51165@catch@rng.marvsz.com) on 13.05.2017 07:49:46


<a href=http://www.danimation.se/869-adidas-superstar-black-gold.html>Adidas Superstar Black Gold</a>
<a href=http://www.autoankauf-wesel.de/ralph-lauren-trainingsanzug-rot-805.html>Ralph Lauren Trainingsanzug Rot</a>
<a href=http://www.b-photo.fr/pull-ralph-lauren-soldes-893.php>Pull Ralph Lauren Soldes</a>
<a href=http://www.cvpsab.se/new-balance-890-orange-162.php>New Balance 890 Orange</a>
<a href=http://www.getraenke-schnellkauf.de/converse-weiß-leder-412.php>Converse Weiß Leder</a>
Jump to page:
1 2 3