Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » Learn OpenGL

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Learn OpenGL
Neil Roy
Member #2,229
April 2002
avatar

I just found the best OpenGL tutorials I have seen on the net in years!

https://learnopengl.com/

...it teaches modern OpenGL, the basics and advanced topics in a really nice way. I managed to achieve a lot and he teaches how to do it properly! No immediate mode garbage, but properly coded shaders etc... it's been a lot of fun following this and I just had to share it here.

He used GLFW for this, but the way he teaches it, you could easily pick your library to use it, just swap the various GLFW functions to whatever. Probably Allegro, or SDL etc... wouldn't be difficult.

{"name":"611195","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/5\/c56411813895b429c3e52ce278753562.jpg","w":791,"h":594,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/5\/c56411813895b429c3e52ce278753562"}611195

Samuel Henderson
Member #3,757
August 2003
avatar

Yeah these tutorials are very well done and certainly the best I've been seen at explaining a lot of the 3D concepts (transformations, matrices, etc).

I wanted to re-do his 'In Practice: 2D Breakout Game' with Allegro 5 but alas, I haven't had the time.

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

Arvidsson
Member #4,603
May 2004

Very nice! Even goes through PBR! I'm tempted to do some C++ again hehe.

Neil Roy
Member #2,229
April 2002
avatar

Yeah, it's been pretty kewl to learn the right way to do things for a change.

He uses GLFW, but there's no reason why someone couldn't use Allegro, SDL2 or whatever you prefer.

I was used to using GLEW in the past, but I he uses something called GLAD and I love it as it is a header file only which I like. No precompiled needed etc... his image loader is the same. I have been doing it his way, but I may rewrite some of what I have learned using Code::Blocks and something else, Allegro or SDL2.

Samuel Henderson
Member #3,757
August 2003
avatar

Neil Roy said:

I was used to using GLEW in the past, but I he uses something called GLAD and I love it as it is a header file only which I like. No precompiled needed etc... his image loader is the same.

Yeah. When I was looking at his stuff a couple of years ago he was using SOIL (Simple Opengl Image Library)... I had to jump through all sorts of hoops to get that library working in Windows, Linux and macOS. At some point he switched to stb_image ... which makes things SO MUCH SIMPLER. Just add stb_image.h to your project and call stbi_load(...) and bam: here's an unsigned char * to the image data.

Also, Neil Roy, is this you?:
https://youtu.be/ZxMx2NUT5sw?t=112

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

bamccaig
Member #7,536
July 2006
avatar

and bam: here's an unsigned char * to the image data.

You lied, where is my unsigned char * to this mystery image data? ??? ;)

I started these when Neil posted them a week or so ago on another thread (or maybe here, I don't know). It's still hard to find the energy to learn this stuff after work, but it's nice to dabble in it at least.

Chris Katko
Member #1,881
January 2002
avatar

A long time ago I was trying to get into OpenGL and I was making a "space" version of Minecraft where you could build ships and fly them around.

{"name":"O8DGS9c.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/5\/4566f393bc13fff1dae8ab95cce0d64e.jpg","w":812,"h":638,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/5\/4566f393bc13fff1dae8ab95cce0d64e"}O8DGS9c.jpg

Years later, Space Engineers came out. They totally ripped me off. ;)

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Samuel Henderson
Member #3,757
August 2003
avatar

bamccaig said:

I started these when Neil posted them a week or so ago on another thread (or maybe here, I don't know). It's still hard to find the energy to learn this stuff after work, but it's nice to dabble in it at least.

I used to dabble with these tutorials too. Then my employer decided to pay me to do a bunch of OpenGL Core profile work. That tremendously accelerates the learning process ... being able to focus on learning and using OpenGL for 8+ hours a day.

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

Ariesnl
Member #2,902
November 2002
avatar

@ Chris Katko
Finish it anyway !
You might be able to make it better 8-)

- If Earth were flat, I would be studying at "Unseen University"

Neil Roy
Member #2,229
April 2002
avatar

Also, Neil Roy, is this you?:

Yeah, kewl, I didn't know he mentioned my little program. Thanks for letting me know.

Years later, Space Engineers came out. They totally ripped me off. ;)

I agree, you should continue it. You started with your own idea, keep going your own way, you may end up with something far superior.

I want to convert all his examples to SDL2 + C, no C++, no classes. Could be fun as I feel much more at ease with pure C.

Rodolfo Lam
Member #16,045
August 2015

I think I have somewhere around a wrapper I made at least for the initial tutorials that used Allegro instead of GLFW, I recall it almost mapped 1:1 with ease. Bumping this to see if I find it and post it on GitHub or something.

Neil Roy
Member #2,229
April 2002
avatar

I just read through the advanced tutorial on instancing, wow, that one will be REALLY valuable! He renders 100,000 asteroids on screen without much of a performance hit. I could have used this in my old terrain program I wrote years ago. Using immediate mode OpenGL (fixed pipeline), and with my tree numbers maxxed out, I got 1 FPS! LMAO With instancing I shouldn't see much of a hit at all which is uber kewl.

{"name":"611208","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/6\/d60249d72a7cd912fe7b359e69db05df.jpg","w":1280,"h":720,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/6\/d60249d72a7cd912fe7b359e69db05df"}611208

He also had some great tips on skybox's, which I already understood (screenshot above used one I created back then). One of his tips should help increase efficiency with them too, and that was to render the skybox LAST, which I would never do normally, but with the right settings, it would be more efficient as the fragments of the skybox that are not visible would simply not be drawn at all using a depth buffer trick he mentioned. Each fragment would be checked against the depth buffer and would not be drawn if it wasn't visible due to something else already being there.

Contrast that with how you normally do it. Turn depth testing off, draw the skybox first, then the objects closer, then turn depth testing on. The result is you draw the entire visible skybox whether it is blocked or not, where as using his trick, only the parts not blocked by closer objects are seen = more efficient.

I think I have somewhere around a wrapper I made at least for the initial tutorials that used Allegro instead of GLFW, I recall it almost mapped 1:1 with ease. Bumping this to see if I find it and post it on GitHub or something.

That would be interesting! I would love to see some high quality 3D stuff done with Allegro.

Arvidsson
Member #4,603
May 2004

Hmm I can't make glad work with allegro for some reason.

al_set_new_display_flags(ALLEGRO_OPENGL_3_0);
al_set_new_display_option(ALLEGRO_OPENGL_MAJOR_VERSION, 3, ALLEGRO_REQUIRE);
al_set_new_display_option(ALLEGRO_OPENGL_MINOR_VERSION, 3, ALLEGRO_REQUIRE);
display = al_create_display(width, height);
if (!display) return -1;
al_set_current_opengl_context(display);

if (!gladLoadGLLoader((GLADloadproc)al_get_opengl_proc_address)) return -2;

I tried the above but the call to gladLoadGLLoader fails. Any idea what might be wrong?

Neil Roy
Member #2,229
April 2002
avatar

Wish I could help, never used Allegro with anything 3D before. :/ You could try using GLEW instead, see if that works. It shouldn't matter which one you use to be honest.

bamccaig
Member #7,536
July 2006
avatar

My only guess would be that the glad that you have doesn't match the OpenGL that Allegro is using? Try to regenerate GLAD to match I guess?

Arvidsson
Member #4,603
May 2004

Good idea. I tried it out and something is not right. I get the version 0 from al_get_opengl_version. I printed it out as hex and it was 0.

I link against opengl32.lib.

This is the code:

#SelectExpand
1#include <glad/glad.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_opengl.h> 4#include <iostream> 5 6int main(int argc, char **argv) 7{ 8 al_init(); 9 al_set_new_display_flags(ALLEGRO_OPENGL_3_0); 10 al_set_new_display_option(ALLEGRO_OPENGL_MAJOR_VERSION, 3, ALLEGRO_REQUIRE); 11 al_set_new_display_option(ALLEGRO_OPENGL_MINOR_VERSION, 3, ALLEGRO_REQUIRE); 12 ALLEGRO_DISPLAY *display = al_create_display(800, 600); 13 if (!display) { 14 std::cout << "Failed to create display"; 15 return -1; 16 } 17 al_set_current_opengl_context(display); 18 std::cout << "OpenGL Version: " << std::hex << al_get_opengl_version() << std::endl; 19 20 ALLEGRO_TIMER *timer = al_create_timer(1.0 / 60); 21 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 22 al_register_event_source(event_queue, al_get_display_event_source(display)); 23 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 24 25 if (!gladLoadGLLoader((GLADloadproc)al_get_opengl_proc_address)) { 26 std::cout << "Failed to load opengl"; 27 return -1; 28 } 29 30 al_clear_to_color(al_map_rgb(0, 0, 0)); 31 al_flip_display(); 32 al_start_timer(timer); 33 bool redraw = true; 34 35 while (1) { 36 ALLEGRO_EVENT ev; 37 al_wait_for_event(event_queue, &ev); 38 39 if (ev.type == ALLEGRO_EVENT_TIMER) { 40 redraw = true; 41 } 42 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 43 break; 44 } 45 46 if (redraw && al_event_queue_is_empty(event_queue)) { 47 redraw = false; 48 glClearColor(1.0f, 0.3f, 0.3f, 1.0f); 49 glClear(GL_COLOR_BUFFER_BIT); 50 al_flip_display(); 51 } 52 } 53 54 al_destroy_timer(timer); 55 al_destroy_display(display); 56 al_destroy_event_queue(event_queue); 57 58 return 0; 59}

Any idea what else might be wrong here?

Edgar Reynaldo
Member #8,592
May 2007
avatar

Chris Katko
Member #1,881
January 2002
avatar

I've had tons of problems with mismatched libraries that were compiled at different times, or from ever-so-slightly different sub-versions between include files and binaries.

They may even not break ALL functions! Sometimes they only break SOME! :o :o :o Until you realize what's going on, it will give you a headache for why only one function will blow up ("how can 'half a library' break!?")

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Edgar Reynaldo
Member #8,592
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

Somehow I doubt you don't know this stuff Edgar, but...

How do you handle the poles with a skybox?

I'm not sure what you mean? You talking about rotations?

Quote:

How do you render a quad in modern OpenGL?

You create two triangles...

{"name":"611214","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/f\/af696c4f2afee676b9b97743c2b3f98e.jpg","w":500,"h":500,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/f\/af696c4f2afee676b9b97743c2b3f98e"}611214

The first triangle would be something like vertices 0, 2 and 1, and the second would be 1, 2, 3. Or you could create a triangle strip with 0, 2, 1, 3. OR you could just specify GL_QUADS, but that may or may not work as they have been depreciated, though personally, I doubt very much that they will disappear anytime soon as that would wreck backwards compatibility, and who wants to sell a card that does that, especially with the growing nostalgia for older games these days.

Personally, I stick with triangles for everything as you can easily store lists of vertices and lists of indexes for polygons (triangles) and you don't have to worry about the different types etc. Plus you want them all triangles anyhow so you can do the various occlusion tests, collision tests, lighting etc.

In other news, I done some of the Advanced OpenGL - Instancing part of the tutorials and I am blown away at what that can do!!! In this screenshot I am rendering 10000 asteroids which all have 570+ polygons each plus the planet which has over 4000 and getting 91FPS (bottom right corner) without any culling or other optimizations! This is really kewl stuff.

{"name":"611215","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/6\/965fbb75d6b8fac26aba174b69d7b5fa.jpg","w":1280,"h":720,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/6\/965fbb75d6b8fac26aba174b69d7b5fa"}611215

Chris Katko
Member #1,881
January 2002
avatar

Neil Roy said:

n other news, I done some of the Advanced OpenGL - Instancing part of the tutorials and I am blown away at what that can do!!! In this screenshot I am rendering 10000 asteroids which all have 570+ polygons each plus the planet which has over 4000 and getting 91FPS (bottom right corner) without any culling or other optimizations! This is really kewl stuff.

Hell yeah! That's what I was saying in one of my previous posts. Modern cards (even 7-10 year old cards!) are unfathomably faster than you think and if they're slow, it's 99% of the time you doing something wrong.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Neil Roy
Member #2,229
April 2002
avatar

Yeah, my system is actually starting to get a little dated. My CPU is only a triple core CPU (AMD) and my video is an NVidia GTX650, which isn't too bad, but also getting dated.

Still, runs well! I had 100,000 asteroids being rendered and it did it, was choppy though, around 9-10 FPS, but considering the polygons that were being pushed, something like 57million, that wasn't too shabby. With optimizations like proper culling etc... the frame rate could go up pretty dramatically.

I'm taking the asteroids program and adding on a space skybox with some lighting etc... I want to see what I can make with it. ;)

Edit: I also created a simple, cube based 3D City program using older OpenGL programming (immediate or fixed pipeline mode) and I managed to get around 600FPS with culling with it so I have high hopes for this modern stuff.

Edit2: I have a dropbox link for the older City3D program if you're curious.
https://www.dropbox.com/s/mfcyhj5q0rdjfd9/City3D.zip?dl=0

Edgar Reynaldo
Member #8,592
May 2007
avatar

Chris Katko
Member #1,881
January 2002
avatar

How do you handle looking up and down with a skybox?

It's just a box with textures on the inside...

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Edgar Reynaldo
Member #8,592
May 2007
avatar

 1   2   3 


Go to: