OpenGL: C and D same code - different results

Wolfgang Draxinger wdraxinger at darkstargames.de
Thu Feb 8 03:10:53 PST 2007


Lionello Lunesu wrote:

> dyh wrote:
>> recently I've tried few OpenGL examples ported from C to D (on
>> win32) and have run into strange problems.
>> 
>> Translated example was built against same C libs as original,
>> using same (translated from platform sdk) headers. Resulted
>> binary is using same dlls as original. But there are some
>> differences:
>> 
>> 1. Behavior
>> In original calls glIndexi() has *no* effect. In translation
>> has. In original calls glColor3f() has effect. In translation
>> has *not*. In original initial color is white. In translation
>> it is kind of brown.>> 
>> 2. Performance
>> original example performs noticeably faster than translated
>> one.
>> 
>> No matter what compiler switches I've tried (-O, -inline,
>> -release, etc). Example is extremely simple, and i do not see
>> any possibilities to have any difference in performance. There
>> is no GC used - there are no memory allocations array slice at
>> all. Actually there are no array usage. In fact there is
>> nothing at all except opengl api calls. And it is literally
>> same library in both example and translation.
>> 
>> Here is code (~300 lines)
>> original (C) http://paste.dprogramming.com/dpfwrsgw.php
>> translation (D) http://paste.dprogramming.com/dpu768pr.php.
>> 
>> Any tips from opengl experts?

Yes, me. The problem is that you're using indexed mode. For some
reason the non D-example does not get a index colour mode, but a
RGB mode.

The OpenGL bindings for D, that I've seen so far circumvent the
normally used linkage to the DLL, which is normally happening by
specifying the DLL in the Executable header. Instead the D
bindings use LoadLibrary and GetProcAddress. Maybe this makes
the D version to actually get the index mode.

That also explains, why glColor3f has effect in the C99 example:
In index mode glColor doesn't work - period. Instead you must
set a palette for your drawable, which is then accessed by the
index values. Since you don't set a palette, and glColor doesn't
work you will get only white shapes in index mode.

The bad performance is caused by the simple fact, that index mode
is no longer supported by modern hardware and must be emulated
by the OpenGL software renderer, which is by nature very slow.
In fact OpenGL2.0 no longer has indexed color mode. Just don't
use it and alwas do things in RGB(A). If you really need an
indexed image first render in RGB(A) and dither afterhand.

Wolfgang Draxinger
-- 
E-Mail address works, Jabber: hexarith at jabber.org, ICQ: 134682867




More information about the Digitalmars-d mailing list