Sign in to follow this  
streamer

OpenGL OpenGL & DirectX

Recommended Posts

streamer    415
Hi. I know much of DirectX and when MS released DX9 I found very nice stuff in it. After googling the net I found almost none tutorial for DirectX for some advance programing, like shaders, HDR and stuff. Meanwhile I noticed that there are lot of examples for OpenGL but also I didn't found lot of shader examples. Few years ago almost every noticable FPS game was writen in OpenGL, and almost none in DirectX. And that games runned like hell on my Pentium II. Well I buyed new PC, and not so long ago a new graphic card (ATI9800Pro) and tried Half-Life2 (DX9), FarCry(DX9), and Doom3(OpenGL). Not to mention that DX games runned on max details with decent fps, and doom3 with medium details on 20fps (I was unable to set it in higher setting). My curiosity for OpenGL raised, I dl lot of examples for OpenGL and every example was detailed so people can learn from it much. And now I don't know what to do...I need BSP, octrees, lightmaps, and that stuff I can find for OpenGL and for DX nothing!!! I was thinking to port my whole engine into OpenGL, but then I didn't find lot of shader examples for OpenGL, nor HDR lighting... But on the other hand new engines runs poor on OpenGL, and fast on DirectX... I don't know what to do! I thought that two APIs are similar and that both are good. But what is the truth? Please someone give me a guideline. Thanks in advance

Share this post


Link to post
Share on other sites
Roboguy    794
Quote:
Original post by streamer
I thought that two APIs are similar and that both are good. But what is the truth?


If you are talking about Direct3D and OpenGL, then that is the truth (depending on what you mean by "similar").

Share this post


Link to post
Share on other sites
streamer    415
Quote:
Original post by Roboguy
Quote:
Original post by streamer
I thought that two APIs are similar and that both are good. But what is the truth?


If you are talking about Direct3D and OpenGL, then that is the truth (depending on what you mean by "similar").


Yes I was thinking about Direct3D (DX9 droped support for DirectDraw), but if that is the truth why Doom3 is so "hardware eating engine". I don't think that Id software have some newbie programers in team, they proved a lot in past. But for example Half-life 2 looks beautifull! and two engines take best of hardaware they can.
For example I looked at code that MS use for skinning meshes. It was awfull!! I didn't understand a thing at first look. But f.e. when I looked at code for rendering md3 format for OpenGL everything looked understandable even if I don't know OpenGL!

Share this post


Link to post
Share on other sites
mikeman    2942
1)Both APIs are just as good and powerful. There are some issues with NVidia having better OpenGL support, and ATI better D3D, but IMO it's not a big deal. Doom3 runs slower because it uses a much more complex shading model(100% dynamic, while HL2 uses for the most part precalculated radiosity maps). If you're looking for a reason to choose between GL and D3D, you won't find it in performance. Make your pick based on your personal preferences and coding style.
That's why noone can tell you what are the "weak spots" of each one; it's up on you. For me, OpenGL being procedural is a pro, for others is a con.

2)BSPs,octrees,lightmaps are all techniques irrelevant of the API. If you know the API syntax well enough you can implement them easily.

3)There exist plenty Shader examples, but shaders(and especially HDR) are relatively new, so it's logical that they're fewer shader examples than those that use fixed-function pipeline. Again, for someone that knows the syntax, translating from D3D to OpenGL is trivial.

In sort, stop dwelling on this thought that one API is better than the other, and it's important that you find out which one is it, so it will magically make you a better programmer and your games run faster. Just pick one(or both) and learn. They're both deserve the time you'll spend on them.

Share this post


Link to post
Share on other sites
Roboguy    794
Quote:
Original post by mikeman
Make your pick based on your personal preferences and coding style.


Portability might be a factor in your choice also.

Share this post


Link to post
Share on other sites
Ravyne    14300
First of all, you really can't compare HL2 on D3D to Doom3 on OGL, and expect the comparison to mean anything about the performance. Both games look great, granted, but Doom3 does a LOT of pixel shading effects, where this is generally more limited in HL2.

Both APIs in their current version (and with appropriate extensions in GL's case) expose identical, or nearly identical features in the very least. The bottom Line is that an API is only that, choosing one or the other when they're so similar is not going to make any application better or worse, its about how you use it.

GL has some advantages, such as cross-platform compatibility, that DirectX doesn't even attempt to touch. Moving to the future, the PS3 used OpenGL|ES as its graphics API, and Nintendo reportedly uses a custom GL-like api, Macs and any non-Microsoft OS also use GL. Microsoft and its Xboxen are the only players promoting/using DX, so basically its MS and DirectX versus everyone else and OpenGL. Granted that due the massive windows user base, one could argue that the two are neck-and-neck, but GL has more supporters, especially in console games which are what really drive the industry.

Also, any GOOD engine should abstract the 3D API away from the game code, and developing an equivilant renderer for the other API is possible (and IMHO, should always be done.)


The bottom line though, is that you should use whatever one you are more comfortable with, and that is purely personal preference and up to you.

Share this post


Link to post
Share on other sites
Optus    422
I'll start with the easy ones.

HDR in OpenGL courtesy of humus.ca
Lots of shaders in Cg, GLSL, vp/fp, HLSL, etc

These people are right, the only reason that Doom3 runs slower then the other games is the full per-pixel realtime lighting. The performance difference between OpenGL and Direct3D is virtually zero.

I would argue, however, that OpenGL can be easier to learn for a beginner. It allows you to easily render geometry to at least get something up on the screen (I'm referring to glVertex3f and friends). Direct3D doesn't have support for so-called "immediate" mode rendering like this. While a production game or 3d application will probably never use immediate mode rendering because of the inherent slowness, it is helpful to get the concepts down.

Share this post


Link to post
Share on other sites
streamer    415
Thanx guys [smile] you help a lot, but now I'm even more messed up.
I know D3D very well, and OpenGL not. I understand difference between engines (now). I have thoughts (for about week or two) that OpenGL is better in some way, and thats why I posted here. I have found articles on net of chaos in OpenGL extensions for newest stuff support. Is that true? Or things are better? And is there OpenGL 2.0?

Share this post


Link to post
Share on other sites
Promit    13246
Well, here.
OpenGL:

#include <windows.h>
#include <GL/gl.h>

HWND hWindow;
HDC hDC;
HGLRC hRC;

struct Vertex
{
float x, y, z;
float r, g, b;
};

LRESULT WINAPI WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
ValidateRect( hWnd, NULL );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}

int main()
{
HINSTANCE hInstance = GetModuleHandle( NULL );

//create a window
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0, 0, hInstance, NULL, NULL, NULL, NULL, "MiniOGL", NULL };
RegisterClassEx( &wc );
hWindow = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, "MiniOGL", "MiniOGL", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
300, 300, NULL, NULL, hInstance, NULL );
ShowWindow( hWindow, SW_SHOW );
UpdateWindow( hWindow );

//Set up pixel format and context
hDC = GetDC( hWindow );
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 16;
pfd.cStencilBits = 0;
int pixelFormat = ChoosePixelFormat( hDC, &pfd );
SetPixelFormat( hDC, pixelFormat, &pfd );

hRC = wglCreateContext( hDC );
wglMakeCurrent( hDC, hRC );

//now create our triangle
Vertex Triangle[3] =
{
{ 0.0f, 0.9f, 0.5f, 1.0f, 0.0f, 0.0f },
{ -0.9f, -0.9f, 0.5f, 0.0f, 1.0f, 0.0f },
{ 0.9f, -0.9f, 0.5f, 0.0f, 0.0f, 1.0f }
};

MSG msg;
bool RunApp = true;
while( RunApp )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
RunApp = false;

TranslateMessage( &msg );
DispatchMessage( &msg );
}

//render stuff
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glBegin( GL_TRIANGLES );
for( int v = 0; v < 3; ++v )
{
glColor3f( Triangle[v].r, Triangle[v].g, Triangle[v].b );
glVertex3f( Triangle[v].x, Triangle[v].y, Triangle[v].z );
}
glEnd();

SwapBuffers( hDC );
}

wglMakeCurrent( 0, 0 );
wglDeleteContext( hRC );
ReleaseDC( hWindow, hDC );
DestroyWindow( hWindow );

return 0;
}





Direct3D 9:

#include <windows.h>
#include <d3d9.h>

HWND hWindow;
IDirect3D9* D3D;
IDirect3DDevice9* Device;

struct Vertex
{
float x, y, z, rhw;
D3DCOLOR Color;
};
#define FVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

LRESULT WINAPI WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
ValidateRect( hWnd, NULL );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}

int main()
{
HINSTANCE hInstance = GetModuleHandle( NULL );

//create a window
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0, 0, hInstance, NULL, NULL, NULL, NULL, "MiniD3D", NULL };
RegisterClassEx( &wc );
hWindow = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, "MiniD3D", "MiniD3D", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
300, 300, NULL, NULL, hInstance, NULL );
ShowWindow( hWindow, SW_SHOW );
UpdateWindow( hWindow );

//Set up d3d
D3D = Direct3DCreate9( D3D_SDK_VERSION );
D3DPRESENT_PARAMETERS d3dpp = { 0 };
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
D3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &Device );

//now create our triangle
Vertex Triangle[3] =
{
{ 150.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 255, 0, 0 ) },
{ 250.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 0, 255, 0 ) },
{ 50.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 0, 0, 255 ) }
};

MSG msg;
bool RunApp = true;
while( RunApp )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
RunApp = false;

TranslateMessage( &msg );
DispatchMessage( &msg );
}

//render stuff
Device->Clear( 0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );

Device->BeginScene();
Device->SetFVF( FVF_VERTEX );
Device->DrawPrimitiveUP( D3DPT_TRIANGLELIST, 1, Triangle, sizeof(Vertex) );
Device->EndScene();

Device->Present( 0, 0, 0, 0 );
}

Device->Release();
D3D->Release();

DestroyWindow( hWindow );
return 0;
}





These two programs do almost exactly the same thing, draw a single triangle on screen with a different color at each vertex. Take a look at the code, and decide for yourself which one you prefer. Functionally, Direct3D and OpenGL are basically equivalent. It's a matter of taste.

Share this post


Link to post
Share on other sites
DrewGreen    370
Ah, you've picked a great topic to start a flame war with here. Personally I've seen enough of such arguments so respect to all so far for not taking the bait.

At the end of the day it's really down to personal preference, or the requirements of your project. Try both & decide which is right for you.

DirectX/3D provides a lot of useful helper functions that aren't present in the OpenGL standard - if you need part or all of this functionality in OpenGL you have to write it yourself (or use existing code from elsewhere). So in this respect development times can be cut substantially by using D3D.
On the other hand the usual argument given supporting OGL is that its power lies in the simplicity of its design, portability, etc etc.

DX also provides other features such as audio and input, which can be used in conjunction with either API.

Personally I use OpenGL for rendering alongside DirectInput & Sound, but that's not to say my way is the best.

Share this post


Link to post
Share on other sites
streamer    415
Well thanx a lot to everybody [smile] I made decission and I'll start to learn OpenGl. I'll post some screenshoots after few weeks of my work in progress.
Thank you again.

Share this post


Link to post
Share on other sites
LilBudyWizer    491
I see little reason to not learn both. There are a lot of good resources out there for both. Why exclude some book because you code in DirectX and it's written using OpenGL? You might code primarily in one, but you should be able to read both. A third of my books are DirectX, another third OpenGL and the other third is just the math behind both. If it seems a good book I buy it regardless of the API it's examples use.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now