Porting to OpenGL

Started by
15 comments, last by solenoidz 11 years, 3 months ago

Hello people.

I have a Direct3D 9.0 engine and I want to add an OpenGL renderer.

The engine uses deferred lighting with Shader model 3.0 and unfortunately isn't written in abstraction level in mind. I mean, it uses DirectX types, objects and D3DX calls all over the code.

My questions are :

1. What OpenGL version I should implement ? OpenGL 2.x , 3.x, 4.0 ? I need a smooth transition and easy port, that matches Direct3D 9.0 functionality well.

2. Should I use glew, glut, SDL , other stuff like that ? I will most probably want to port it to other platforms like Linux, Mac ? I don't want to mess with extensions so probably glew will be of help.

3. Should I use nVidia CG shader language in order to use my existing HLSL shader code directly, without rewriting them to GLSL ?

4. Is there a good sample source code, tutorials, articles that show basic deferred rendering with a modern OpenGL way ( version 3 ++) ?

5. I'm using D3DX library for common math stuff etc, is there a library for OpenGL that is to it, like D3DX is to Direct3D ?

I have a limited OpenGL experience from the past.

Thank you.

More questions to come.

Advertisement
Hello people.

I have a Direct3D 9.0 engine and I want to add an OpenGL renderer.

The engine uses deferred lighting with Shader model 3.0 and unfortunately isn't written in abstraction level in mind. I mean, it uses DirectX types, objects and D3DX calls all over the code.

My questions are :

1. What OpenGL version I should implement ? OpenGL 2.x , 3.x, 4.0 ? I need a smooth transition and easy port, that matches Direct3D 9.0 functionality well.

2. Should I use glew, glut, SDL , other stuff like that ? I will most probably want to port it to other platforms like Linux, Mac ? I don't want to mess with extensions so probably glew will be of help.

3. Should I use nVidia CG shader language in order to use my existing HLSL shader code directly, without rewriting them to GLSL ?

4. Is there a good sample source code, tutorials, articles that show basic deferred rendering with a modern OpenGL way ( version 3 ++) ?

5. I'm using D3DX library for common math stuff etc, is there a library for OpenGL that is to it, like D3DX is to Direct3D ?

I have a limited OpenGL experience from the past.

Thank you.

More questions to come.

hi,

opengl 2.1 matches dx9 functionality.

yes you should use glew and sdl (or sfml, if you prefer c++-ish solutions), but these are not mandatory, you can write your own.

well cg is another language right? so you would have to port to cg then... the question is, do you want to learn glsl or not?

see: http://ogldev.atspace.co.uk/

but there are tons of others. google is your friend.

well for mesh loading and stuff related to that, you have assimp

for texture loading, you can do this with sdl / sfml. Just load in an image using sdl / sfml, acquire the pointer to the raw data, and fill a texture using that data.

for maths, you have tons of choices. The most popular ones are CML (http://cmldev.net) and GLM (http://glm.g-truc.net/).

I have also developed a math library: https://github.com/Yours3lf/libmymath

if you need anythin else, just go to the topics (http://www.gamedev.net/forum/25-opengl/) look at the right side of the page -->

and there you have it, tons of links.

As OpenGL version a suggest 3.3+ because almoste all modern graphic card support it and is very similar to OpenGL 4.0.
As GUI library you can use the Qt library.
As math library you can use Eigen (very fast with extensive use of Expression Template and SIMD istruction (SSE, AVX, Neon)).
But you have to learn very well GLSL language. OpenGL is a C API and is not easy to write OOP code. Take a look to (http://oglplus.org/)
As OpenGL version a suggest 3.3+ because almoste all modern graphic card support it and is very similar to OpenGL 4.0.
Second this.

Although OpenGL 2.1 is the "equivalent" of DX9, there is no good reason not to use OpenGL 3.x for you (but there are good reasons against using OpenGL 2.x).

Since you come from DX9, you will have to learn a new API in any case, so best learn the "correct" version right away. Otherwise, you'll have to spend extra time at some point int the future on unlearning and re-learning. Going from 3.x to 4.x is a breeze, but there is a huge paradigm cut between 2.x and 3.x.

I wouldn't quite consider GL2.1 an equivalent of D3D9 - there are some D3D9 features (particularly at the SM3 level - e.g. instancing) that are not available in GL until 3.x, so that seems the most reasonable level to shoot for (particularly if you're using those features!) and I agree with those above recommending it.

Regarding choice of shading language, you're on the right track with Cg. Cg and HLSL are so similar (they were originally more or less the same language) and share so many concepts that it makes sense.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

As OpenGL version a suggest 3.3+ because almoste all modern graphic card support it and is very similar to OpenGL 4.0.
As GUI library you can use the Qt library.
As math library you can use Eigen (very fast with extensive use of Expression Template and SIMD istruction (SSE, AVX, Neon)).
But you have to learn very well GLSL language. OpenGL is a C API and is not easy to write OOP code. Take a look to (http://oglplus.org/)

GL 3.0+ is still not very widely supported. If you are targeting a casual market, you may want to stick with 2.0 as your target. In this thread it was listed that as few as 51% of Minecraft users supports GL 3.0+. It really comes down to what you need; GL 2 is a fine target for most purposes and can be used in almost the same way as the 3+ programmable pipeline (shaders, custom built-ins, etc). Many drivers will support geometry shaders, FBOs, float textures, etc. through extensions. For example, 93% of drivers support FBO EXT, and it's pretty much a staple in any driver in the last several years.

GL 2.0 and GLSL < 150 is also more compatible with OpenGL ES, so it will prepare you for iOS/Android/WebGL graphics programming.

GL 3.0+ is still not very widely supported. If you are targeting a casual market, you may want to stick with 2.0 as your target. In this thread it was listed that as few as 51% of Minecraft users supports GL 3.0+. It really comes down to what you need; GL 2 is a fine target for most purposes and can be used in almost the same way as the 3+ programmable pipeline (shaders, custom built-ins, etc). Many drivers will support geometry shaders, FBOs, float textures, etc. through extensions. For example, 93% of drivers support FBO EXT, and it's pretty much a staple in any driver in the last several years.

GL 2.0 and GLSL < 150 is also more compatible with OpenGL ES, so it will prepare you for iOS/Android/WebGL graphics programming.

The OP's engine uses deferred lighting with SM3 so I think we can safely assume that a casual market is not quite the target audience.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Thank you guys. (that doesn't mean I intend to put an end to the thread)

I'm using shader model 3.0 because I need vertex texture fetch, VPOS in pixel shaders, more complex programs etc.

I guess I can't find a simple tutorial for OpenGL 3.3 deferred lighting with sample source code.

The closest I found is one that deals with OpenGL 2.1 , GLSL 1.2

http://www.codinglabs.net/tutorial_simple_def_rendering.aspx

DirectX has SDK with samples, I guess OpenGL has one too and there is a deferred lighting sample. Or maybe nVidia developers zone or AMD samples are my friends.

One thing to note, if you want it to be sure all hardware that runs DX9 to run your game you need to limit yourself at most to OpenGL 2.0 :(

I have several older comps (DX9) that can run at most OpenGL 2.0 or even OpenGL 1.5 (althrough you can drop that ancient ones). It's especially true for laptops (infamous integrated Intel GPU chips).

Sadly, OpenGL (higher versions) is not that supported (drivers, and if the manufacturer never released drivers for OpenGL higher than 2.0 you can't do anything about it).

It's really annoying, these comps do have these capabilities, and are only artificially crippled because of lack of proper drivers. Fortunatelly it does not apply to relatively modern hardware, so if you are ready to abandon some of the older ones you should be fine with OpenGL 3+.

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

Thanks again everyone.
Porting is coming along very nice and I'm happy with the results so far, as the OpenGL version runs faster than D3D, probably due to badly written D3D code on my side or the simplicity of GLSL shaders I use for now. Anyway I have another question.
I have entities which transformation is a D3DXMATRIX and I want to use that matrix for my OpenGL version of the renderer.
I did this already with my D3D view matrix, which is a D3DXMATRIX by multiplying it with a (1,1,-1) scaled matrix



D3DXMatrixScaling(&sca,1,1,-1) ; 
 OGLMatrix = g_Camera->view * sca ;
I load this into OpenGL and it works as a MODEL_VIEW matrix which has the MODEL part identity, so all my meshes lie in origin with no scaling and rotation factors, but I can move the camera around correctly.

Now I need to involve the meshes transform matrices from D3D so they are placed, scaled and rotated in the world, but I'm missing something probably because I get incorrect results.
This doesn't seem to work as expected :




D3DXMatrixScaling(&sca,1,1,-1) ;
  for(int i = 0 ; i < pOGLMeshes.size() ; i++ )
   {       
OGLMatrix = pOGLMeshes[i]->d3dMatrix * g_Camera->view * sca ;
g_pOGLRenderer->RenderMesh(pOGLMeshes[i] , &OGLMatrix[0] ) ;
   }
If I have a D3D model and view matrices, how am I supposed to concatenate them correctly, so I can load them in OpenGL as MODELVIEW matrix ?

Thank you.

This topic is closed to new replies.

Advertisement