Learning OpenGL. Log 4

This post is part of a series of logs of my journey in learning OpenGL (or as some say, modern OpenGL. OpenGL 3.1+).

—————————————————————————————————————————————–

Since the last log. I’ve done a lot of code exercises, basically involving implementing the render system in my engine. The main two focus were moving away from the homogenous/normalized device coordinate system and using VAOs(vertex array objects) properly.

Projection, projections

I spent  quite a bit of time over the last couple days learning and implementing perspective and orthogonal projects (and wrapping them into a simple camera class). It’s been quite a few years since I’ve done calculus back in high school. Where I’ve learnt the basics of matrix maths. I know how matrix multiplication works and I do have some basic ideas on what the maths are doing when I looked at the necessary steps to create a perspective project matrix, but I didn’t really understand the higher level concepts and theories in the transformations. Mostly because my brain was in denial and did not want to learn them, plus I just couldn’t care less at the time. So I did what any lazy person would and went the premixed cake flour route. I read a bunch of tutorials online and books and stitch together bits and pieces of codes that will work for now. Leaving the mess to my future self to worry later. At the end of the day. The minimal you need to know when comes to learning new stuff is knowing what things do, but not how they do it. At least that’s how I look at it.

Vertex Buffer Objects

I only just picked up what vertex buffer objects really do yesterday. Reading any tutorial or guide on modern OpenGL. One of the first things you do when you want to draw something (e.g. a triangle). You have to setup a vertex buffer object by calling glGenVertexArray(), and bind it by calling glBindVertexArray(). They then move on to do other stuff only to never return to the generated vertex array object (aka: VAO).

After spending much time on Google-ing, reading and cursing. I’ve finally came to the understanding that the VAO is conceptually just an object that records all the configure and setup calls such as glBindBuffer(), glEnableVertexAttriArray()m gkVertexAttribPointer() when it is binded. So instead of calling those functions everytime I made a draw call. I can just create and bind a VAO  and make those calls during object setup, and then bind the VAO each time I want to draw the object and unbind right after. When drawing multiple objects (e.g. multiple triangle shapes), each object should have their own vertex buffer object and just bind it when it needs to be drawn. Very cool stuff.

One thing to take note here: The usual order to setup things up is the following:

  • create and bind a VAO
  • create and bind a vertex buffer object (VBO)
  • buffer data.
  • call your gl*Pointer functions to enable and set attributes. (This automatically associates the attributes with the VBO. Don’t call glDisableAttribPointer() here.
  • you can unbind your VBO now. (as far as I can tell it doesn’t effect anything).
  • unbind VAO
  • call glDisableVertexAttribPointer(). (seems to be optional. I am not sure if I am suppose to).

Next

The next thing I will learn and work on is textures and simple lights.

Learning OpenGL. Log 3

This post is part of a series of logs of my journey in learning OpenGL (or as some say, modern OpenGL).

—————————————————————————————————————————————–

I’ve started an other log, dedicated for the engine project, so I won’t talk about it any more here unless it’s very cool or related.

Today I worked with the glm library a bit more and wrote a TransformationMatrix class.

A very simple class. The name is a bit deceiving. The entire class is actually compose of 4 glm::mat4 objects. 3 for the basic transformations, and the final one being the product of the other 3. The comments are a bit incomplete but you can probably tell what the member functions do already. There are 3 setter methods, for scaling, translation and rotation. There are also two getter methods for getting the final transformation matrix.

What interesting here is that, at the moment the final transformation matrix is only evaluated when it needs to be re-evaluated. Calling any of the setter methods will not trigger the evaluation, but they will m_Evaluated to false. When the getter methods are called. They will re-evaluate the final transformation matrix only if it’s been previous modified.

There are no methods such as TranslateFromCurrentPos or RotateFromCurrentAngle at the moment because I believed those should be implemented in a entity class which will compose one or multiple TransformationMatrix in the render component. This will allow me to implement higher level movement stuff on top while keeping the matrix class lightweight and encapsulated.

Here’s the cpp part of the class:

Of course. The class is incomplete. I will overload some operators for matrix multiplication and what not. At the moment I have to call “GetGLMMatrix()” to pass the matrix to my ShaderProgram class. I will need to add support for this new class into my shader code.

I will begin work on a camera class next, once I’m up to date with this week’s lab work.

Learning OpenGL. Log 2

This post is part of a series of logs of my journey in learning OpenGL (or as some say, modern OpenGL).

—————————————————————————————————————————————–

So far

Quite a bit of work has been done on the engine (briefly mentioned in my last log) over the last week. I did get permission from my coordinator so I will be using the engine for the unit projects. It was mostly bug fixes and getting things to play nice with raw OpenGL. I can’t seem to remember the specifics. My short term memory is seems to be much worse than I remembered… Jokes aside. I will discuss a few things today that might not be directly related to OpenGL.

Windows on Windows

All of the OpenGL development so far, including my engine, has been done using Visual Studio 2012 on a Windows 8 machine. This might be too much information, but on Windows. Interesting things happen when you drag/move and/or re-size a window widget around.

While an user is moving or re-sizing an application’s window, the application’s Win32 message pump enters an other loop that blocks until the user let go of the mouse button. If you’re developing a game or anything alike, it means your game loop would be blocked as well. This blocks your render update, your physics update, and your logic update etc etc. Depending on how things are implemented, and the duration of the window action. Bad things could happen (game is paused when it shouldn’t be, logic break down, network de-sync etc). Of course. Being a perfectionist means that a resolution to this problem is needed if I want a good night sleep.

If you’re creating your window using WinMain, you can hack around it by intercepting and handling the events from the window yourself, instead of passing it to DefWindowProc. If you’re on XNA. You can also hack around it via custom classes instead of using the default XNA provided classes for the related stuff. It’s a bit trickier if you’re using a library such as GLFW and SFML, which provides abstraction on top of the platform specific windowing stuff. I think you could use the refresh callback to resolve the issue in GLFW (I could well be wrong). On SFML is a bit more complicated.

On SFML. sf::Window.pollEvent() also blocks until the window manipulation action is returned. If I guessed correctly, it is probably just a simple wrapper over the windows message pump. I could put some hack into the windows specific code and recompile SFML, but I did not want to do that. Anyway. After some more Googling. I stumbled upon a SFML forum post where someone proposed a solution to do the rendering on a separate thread, leaving all  the event handling and everything else on the main thread. I thought to myself. You might as well do everything on the second thread, and just leave the event handling to the main thread. This was implemented into my engine and now I have the option of running the game in a ‘mainthread’ mode and a ‘dualthread’ mode.

The only thing relevant to OpenGL in this is that the context has to be active on the thread that you’re doing the rendering.

For more information on the Windows message pump issue. Youcan go to this gamedev post where a guy has done some digging and reported his findings. (link: http://www.gamedev.net/topic/488074-win32-message-pump-and-opengl—rendering-pauses-while-draggingresizing/)

God Functions and OpenGL Tutorials

I’ve been encountering an issue that might not actually be an issue. It’s god functions in tutorials and/or minimal examples. It’s the sole reason I hate about reading code. Besides poor code format and bracketing. The tutorials that I’ve been following are full of them. Majority of the typical draw a triangle (equivalent of printing “hello world”)  example tries to stuff everything into as few function as possible, if not into the entire main function. It teaches nothing about the lifetime of any of the OpenGL ‘objects’.

How am I suppose to know that vertex array can be deleted after the data is buffered. How am I suppose to know that the shader source and the shader can be detached and deleted after the shader program is compiled. Wrapping all of these in nice simple classes such a ShaderProgram class, or a Triangle class would have been much more clearer and cleaner.

SFGUI

On the GUI side of things, I’ve discovered and gave SFGUI a try over the last few days and was able to get it semi integrated into my engine.

I did not mention SFGUI in my last log. It’s a GUI library just like the ones mentioned before (AntTweakBar, CEGUI etc). It is built on top of SFML, but it’s on a slow transition to become SFML independent. The widget looks very nice, and is very similar to Valve’s older in game GUI’s. It written in C++ so you don’t get retarded C interfaces that has a gezzalion variants for a single function. The support is awesome and the developers are very active on the forums. It is a new library though.

One issue that I do have with SFGUI is it’s sfg::SFGUI object. At the moment I have the aspect ratio fixed by calling glViewport with the appropriate parameters when a re-size happens. When the GUI is displayed by calling sfg::SFGUI::Display(). It calls glViewport internally which breaks my aspect ratio. I reported the issue on the forum and got a response from one of the developers very quickly. We are currently in the process on finding a workaround before the issue is fixed for the next version.

Qubicle Constructor

I bought a license for Qubicle Constructor last night. It’s a voxel model creator. I plan to use it to create some models for the unit projects.  I have to say. It is quite pricey.. (link)

For those who don’t know what a voxel is. Here’s a wiki link (http://en.wikipedia.org/wiki/Voxel).

In conclusion

I haven’t done much studies on OpenGL lately, so I need to get on with that. Gotta go to sleep a lot more earlier as well..

Note to myself

It might be better to start a different log specifically for my engine development. Instead of contaminating this log with things that are unrelated to OpenGL. Since having a general log seem like a worst idea, but that’s a lot of writing for 2 logs….

According to Murphy’s law: your professor will find your blog, and haunt you in your sleep if you say bad things about them.