Archived

This topic is now archived and is closed to further replies.

ShawMishrak

OpenGL OpenGL and C vs C++

Recommended Posts

ShawMishrak    146
I remember reading a thread a month back or so on this subject, but I cannot find it as the search function is not available. I have been using OpenGL inside C++ classes for a couple years now, but a question has been bothering me recently: Should I use C++ or C to write the graphics engine? I originally thought C++ classes were the way to go to keep everything together and pretty. However, I read an article about Doom3, and in it was an interview with Carmack in which he stated that the game was written in C++, with the exception of the graphics engine, which was written in C. This made me think; is C faster or better than C++ when it comes to OpenGL? If so, which parts should be written in C and which in C++? Should the whole graphics engine be written in C, or just the functions that interface directly with OpenGL? I''m just looking for any advice, since this question has been bothering me lately and I haven''t been able to find anything online beside that thread which I now cannot find.

Share this post


Link to post
Share on other sites
Dean Harding    546
If John Carmark jumped off a building, would do that, too?

If C++ works for you, stick with C++ - let John C. do it however the hell he wants.

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
DC_Tsunami    122
C is slightly faster than C++ because it doesn''t have virtual functions (with virtual functions, the program has to figure out which methods to call at run-time rather than compile-time). That''s probably why he used it. However, most compilers do enough optimization that the difference is negligible. It''s perfectly possible to create a great graphics engine in C++. Doom 3 isn''t the only game in the world with great graphics, you know.

Share this post


Link to post
Share on other sites
Beer Hunter    712
quote:
Original post by DC_Tsunami
C is slightly faster than C++ because it doesn''t have virtual functions...
You only get the overhead of virtual functions if you declare them as virtual!

And getting the same effect in C requires the exact same overhead.

Share this post


Link to post
Share on other sites
simbobx    122

Possibly C just suits the style of engine iD/JC want to write.

Also .. Quake and Q2 were definitely written in C .. possibly Q3 (dunno, haven''t seen the code) .. in which case they may just be building on the (known good) technology they already have.

tbh, what everyone else says above: use what works for you

Share this post


Link to post
Share on other sites
_the_phantom_    11250
as I side note, from what I read about Doom3 the only hold overs from Q3 to start with was stuff like loading code, audio code etc, the graphics engine was apprently gutted and started again from the beginning.

As for C vs C++ in the case of Doom3, its probably a case of JC just likes/knows how to use C more than C++ to get better results, so if you prefer to go with C++ over C then do so, basical at the end of the day its whatever works for you

Share this post


Link to post
Share on other sites
jitspoe    122
I personally think C is the way to go. C++ might be good for GUI''s and whatnot, but when it comes down to doing efficient graphics code, you want to make as few function calls as possible. You want to avoid setting the same things over again if they''ve already been set (textures, colors, etc). I find it difficult to impliment that well with classes since you basically want to sort and render things on a polygon (or strip or whatever) basis rather than on an "object" basis. When you try to do all that with C++, you might end up basically writing C code in a .cpp file, and I prefer direct access to data in structs and arrays over accessor methods any day. If you can optimize it with classes, more power to you, but I''m sticking to C when it comes to rendering.


Share this post


Link to post
Share on other sites
_the_phantom_    11250
I think the keyword in all of that was the second one ''personaly''... you like to do it that way, I think its more logical to render per object (grouping object types to cut down on state swtiching) but thats just my personal choice

Share this post


Link to post
Share on other sites
Oluseyi    2116
quote:
Original post by jitspoe
C++ might be good for GUI''s and whatnot, but when it comes down to doing efficient graphics code, you want to make as few function calls as possible. You want to avoid setting the same things over again if they''ve already been set (textures, colors, etc).

So group your data by texture, color, etc. Write a data manager that does this for you.

quote:

I find it difficult to impliment that well with classes since you basically want to sort and render things on a polygon (or strip or whatever) basis rather than on an "object" basis.

No offense, but your ineptness at a technique is not a valid argument against it.

Not that I''m advocating C++. This is a pointless argument/discussion/thread. Yeah, Doom3''s graphics engine is written in C. UT2003/Unreal Championship''s is written in C++. If you''re asking a question like this, your code is unlikely to be efficient or well-structured enough for it to make a difference.

So get back to work. Real work.

Share this post


Link to post
Share on other sites
zedzeek    529
personally i use c++ but more as ''a better c''(tm)

in the old days c used to be quicker cause the c compilers used to be better than there c++ counterparts (mainly cause c is a simpler language IIRC only 21 keywords), nowadays c++ compilers will produce code around the same speed a c compilers.

what language u choose is not gonna make any difference to the final speed of your game all speed is in made in the hands of the programmeur/s (usually by choosing the most efficent algorithms)

personally i feel sorely tempted when i read posts ''java is slow'' of rewriting my KEA game in java, the only thing stopping me is, i dont know java + are unwilling to spend a month learning it.



http://uk.geocities.com/sloppyturds/kea/kea.html
http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites
benjamin bunny    838
Yes, there is a performance issue with C and C++. You should be coding everything in ASM.

More seriously though, as has already been stated, you can get the same speed from C++ as from C (C being a subset of C++, this is to be expected).

If you care about performance, do some profiling, and find where the slowest parts of your code are. Most likely there''ll be one or two inner loops which could be optimised, and nothing else will make any difference. If you''re concerned about the overhead of virtual functions, bear in mind that all functions have a calling overhead, and in speed-critical inner loops you''re not going to be calling functions anyway (unless they''re inlined).

____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now