Author Topic: [C++][OpenGL] Object-Orientated GL project  (Read 6093 times)

0 Members and 1 Guest are viewing this topic.

Offline lucastar

  • C= 64
  • **
  • Posts: 50
  • Karma: 5
    • View Profile
Hi everyone!

Why most of the OpenGL example projects (i mean tutorials, etc) don't use classes?
I mean:
They are obviously coded in C++(not pure C), otherwise there won't be functions defined in structures, or variables declared in loops (for(int x...))... but there are no classes.

Example:
Why this isn't  right:

Code: [Select]
class Light
{
        public:
        Light();
        virtual ~Light();

        float * GetAmbient() { return &m_Ambient[0]; }
        void SetAmbient(float val[4]) { for(int index = 0; index < 4; index++)
{
m_ambient[index] = *(p+index);
.
.
.
}
}
I mean, this does work, but nobody does it.
Why use structures instead of classes? Why no-object-oriented programming?

Thanks!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17419
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: [C++][OpenGL] Object-Orientated GL project
« Reply #1 on: April 11, 2013 »
Hi CosmicColours, you're right of course and not everyone here writes their stuff like that but there are a lot of old farts here (including me) who grew up on procedural programming and somewhat stuck in our ways or very lazy!

Shockwave ^ Codigos
Challenge Trophies Won:

Offline lucastar

  • C= 64
  • **
  • Posts: 50
  • Karma: 5
    • View Profile
Re: [C++][OpenGL] Object-Orientated GL project
« Reply #2 on: April 11, 2013 »
So it's just habit?
I think i read once that accessing to structures is much quicker that accessing to objects.
So, that very frequently used items should be structures (Ej: Points(x,y)).
It's that right?

Thanks!

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++][OpenGL] Object-Orientated GL project
« Reply #3 on: April 11, 2013 »
I think i read once that accessing to structures is much quicker that accessing to objects.
So, that very frequently used items should be structures (Ej: Points(x,y)).
It's that right?
In C++ "class" and "struct" are basically the same, all the oop-features which you know from "class" are also available for "struct".
The only difference is that struct-members are public by default and class-members are private by default.
But as most C++ programmers started in C, they still use "struct" as it was used back then, namely a collection of public variables.
So there's absolutely no general performance-difference between struct and class.
However it can make quite a big difference whether you do an actual function call to access a member variable or have an inline getter implementation.
« Last Edit: April 11, 2013 by hellfire »
Challenge Trophies Won:

Offline lucastar

  • C= 64
  • **
  • Posts: 50
  • Karma: 5
    • View Profile
Re: [C++][OpenGL] Object-Orientated GL project
« Reply #4 on: April 11, 2013 »
However it can make quite a big difference whether you do an actual function call to access a member variable or have an inline getter implementation.

You mean it's quicker to have the Get and Set properties defined in the header than a function that references them in the implementation(class.cpp)??

Edit:
I have a question about turning or not Vsync on... should i start another thread? ???
« Last Edit: April 11, 2013 by CosmicColours »

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++][OpenGL] Object-Orientated GL project
« Reply #5 on: April 12, 2013 »
You mean it's quicker to have the Get and Set properties defined in the header than a function that references them in the implementation(class.cpp)??
If you tell the compiler to optimize for speed, it is usually clever enough to decide on its' own which function should be inlined and which not.
There are some cases where automatic inlining is impossible, though.
For example if the requested function is in a separate lib or dll or if a virtual function is overloaded.
If the compiler needs to do an actual function call for fundamental things, for example to get the individual x,y,z-values of a 3d-vector (which is probably called in thousands of situations), this will have a major speed impact.
So I'm not saying you should blindly inline any relevant function but instead just check the generated code from time to time to see what's actually happening.

Quote
I have a question about turning or not Vsync on... should i start another thread? ???
Yes because it's easier to find for others who are looking for the same information.
When you're looking for information about vsync, you don't expect it in the middle of an oop discussion...
« Last Edit: April 12, 2013 by hellfire »
Challenge Trophies Won:

Offline lucastar

  • C= 64
  • **
  • Posts: 50
  • Karma: 5
    • View Profile
Re: [C++][OpenGL] Object-Orientated GL project
« Reply #6 on: April 12, 2013 »
Perfect, thank you very much for your answers. ;D