Tips on some code ....

Started by
14 comments, last by Zahlman 15 years, 10 months ago
This code is not yet completed. The purpose of this post is to aid in the development in a larger project. The aim of this code is to design a generic OpenGL Shapes using polygons, quads, spheres, etc. The next posing will probably be dealing with multi-threading & or scripting. Common_objects.h -> namespace _objects{ /* --------------------------------------- ---------- Rendering Object ----------- --------------------------------------- */ class _robject{ public: void SetName( char *Name ){ name = Name; } virtual void Render(); private: char *name; // Object Name }; } Common_objects.cpp -> #include "common_objects.h" void _objects::_robject::Render(){} Common_geom.h -> #include "common_objects.h" #include <iostream> namespace _cGeom{ enum Side{ _top, _bottom, _front, _back, _left, _right }; class _color3f { public: float r, g, b; }; class _vert3f { public: _vert3f(){} // _vert3f( float x1, float y1, float z1 ) : x(x1), y(y1), z(z1) {} ~_vert3f(){}; void Set( float x1, float y1, float z1 ){x = x1; y = y1; z = z1; } float GetX() const { return x; } float GetY() const { return y; } float GetZ() const { return z; } private: float x, y, z; _color3f color; }; // template<class T> class _shape : public _objects::_robject { public: _shape(){} ~_shape(){} virtual void SetPos(){}; virtual void SetSize(){}; }; class _quad : public _shape { public: void SetPos( float x, float y, float z ); void SetSize( float sizef ) { size = sizef; } virtual void Render(); void InitQuad( Side side); // If it is part of a box, then set a side. // void Init( ); private: float size; // Size of the quad; _vert3f vertex[4]; // 4 Sets of vertexes }; class _box { public: _box( float bSize ); ~_box(); virtual void Render(); private: _quad sides[6]; }; } Common_geom.cpp -> #include <windows.h> #include <gl\gl.h> #include "common_geom.h" void _cGeom::_quad::InitQuad(_cGeom::Side side) { switch( side ) { case _top: this->vertex[0].Set( size, size,-size ); this->vertex[1].Set(-size, size,-size ); this->vertex[2].Set(-size, size, size ); this->vertex[3].Set( size, size, size ); break; case _bottom: this->vertex[0].Set( size,-size, size ); this->vertex[1].Set(-size,-size, size ); this->vertex[2].Set(-size,-size,-size ); this->vertex[3].Set( size,-size,-size ); break; case _front: this->vertex[0].Set( size, size, size ); this->vertex[1].Set(-size, size, size ); this->vertex[2].Set(-size,-size, size ); this->vertex[3].Set( size,-size, size ); break; case _back: this->vertex[0].Set( size,-size,-size ); this->vertex[1].Set(-size,-size,-size ); this->vertex[2].Set(-size, size,-size ); this->vertex[3].Set( size, size,-size ); break; case _left: this->vertex[0].Set(-size, size, size ); this->vertex[1].Set(-size, size,-size ); this->vertex[2].Set(-size,-size,-size ); this->vertex[3].Set(-size,-size, size ); break; case _right: this->vertex[0].Set( size, size,-size ); this->vertex[1].Set( size, size, size ); this->vertex[2].Set( size,-size, size ); this->vertex[3].Set( size,-size,-size ); break; } } void _cGeom::_quad::Render() { glBegin(GL_QUADS); for(int i = 0; i < 4; i++ ) { glVertex3f( this->vertex.GetX(), this->vertex.GetY(), this->vertex.GetZ() ); } glEnd(); } _cGeom::_box::_box( float bSize ) { for( int i = 0; i < 6; i++ ) { sides.SetSize( bSize ); if(i == 0)sides.InitQuad(_top); if(i == 1)sides.InitQuad(_bottom); if(i == 2)sides.InitQuad(_front); if(i == 3)sides.InitQuad(_back); if(i == 4)sides.InitQuad(_left); if(i == 5)sides.InitQuad(_right); } } _cGeom::_box::~_box() { } void _cGeom::_box::Render() { for( int i = 0; i < 6; i++ ) this->sides.Render(); } ///////////////////////////////////////// ////////////////// EoF ////////////////// ///////////////////////////////////////// Please post any suggestions or if you would like a copy of the lib.
~ Travis
Advertisement
Why the hell is every identifier name starting with an underscore?

Your "name" member variable has lousy ownership semantics (and to make things worse, it's a pointer). Also, I suspect you wanted to make _robject a structure, and give it a virtual destructor.

Your vector "set" function is useless when you have a constructor and assignment operator. Also, your "color" member is useless.

Your quad needs constructors.

Your box constructor could be written in terms of an array of enumeration values, instead of the dreaded for-switch "idiom".
Quote:Original post by ToohrVyk
Why the hell is every identifier name starting with an underscore?


It's a thing I've been seeing more and more lately. People are finally stopping to use hungarian notation, and for some reason, they're switching to something like this. I've seen a lot of C# / Jiva code where the member variables have an underscore in front of them.

As a note to the original author: why are you doing this? There is no actual reason to do so, because most IDE's will inform you about the scope of a variable. It's also a hell to type and has no added goodness.

Toolmaker

I noticed it, also. Mainly some code I was reading in the Apple docs. It's disgusting.
Quote:Original post by Toolmaker
It's a thing I've been seeing more and more lately. People are finally stopping to use hungarian notation, and for some reason, they're switching to something like this. I've seen a lot of C# / Jiva code where the member variables have an underscore in front of them.
It's actually in the C# coding standards. I would quit a job where I had to use it.
[opinion]Does make it horrible to read, I think almost worse than the 'classic' hungarian, which at least had a meaning, even if it was pointless and confusing meaning...[/opinion]

I have only done stuff in DX, but I am not sure that having each object handle it's own rendering is exactly performant, although it is nice from an encapsulation POV. As you have written it, every quad has its own personal glBegin and glEnd calls, which I thought were pretty costly - maybe it would be smarter to batch these somehow, maybe using a QuadRenderer object or something which each quad renders itself through, ensuring only one glBegin and glEnd per frame, or at least per geometry type.

maybe each quad you create could add a pointer to itself to the drawing queue, and then only one call to the rendering class could render everything?

Hope this helps
Mathmo
I sometimes use an underscore for member variables when I have properties of the same name, as in

Private _score as Integer

Public Property Score as Integer
Get
return _score
End Get
End Property
-----------------------------------------------“The best, most affordable way to save the most lives and improve overall health is to increase the number of trained local, primary healthcare workers.”Learn how you can help at www.ghets.org
imho:

- another vote get rid of the underscores.

- name should be a std:string not char*. If your C++ code has "char *" in it, you have a bug (that's in someone's sig around here...)
It says if you post in the Beginner's Forum and you have char* in your code, you generally have a bug, but they way you worded is definitely not true.
Just a tip about underscores:

if C++ switch to c99 standard you might get into trouble. C99 reserves underscore for std library and double underscore for compiler writers :).

Otherwise I myself writes more code in C then in C++, and I tend to go against reason and use udnerscore in a certain case: for private API functions (only those that HAS to be shared between different files, but in a library itself). Underscore reminds me that those are not seen from the user application (but I could use some other char indeed).

For user exposed functions I really can't see any use of underscore. It is awkward to type and if you constantly underscore every name, then there is no point of having underscore :D.

Beside what is point of your lib, there are already "prmiitives" for opengl, check out glu and glut libraries.

It is also awkward to have basic objects like vertex or vector or color with setters for their x,y,z types. A vertex is a position and as such x,y,z coordinates belongs to its interface which probably will never change, so there is no point to hide it ... some basic of 3d programming. It is just awkward to do something like

v1.setX(v2.getX());

when you can do this: v1.x = v2.x;

Check also glut and glu libraries, there are already primitives there:

http://resumbrae.com/ub/dms423_f06/09/

And why do you need windows.h in such basic and generic code??? It is just poluting your code.

This topic is closed to new replies.

Advertisement