Wednesday, 19 September 2012

OpenGL - Gaps between Triangles


A strange problem posed itself to me in Linux last night, infact in OpenGL in general - this problem happened on OSX, Ubuntu, Fedora and Windows Vista for me... Here's the low down...

I've created a simple 3D view contained area in OpenGL



Into this I want to place items, buildings, people whatever... So, I start to define a rectangle, which is going to be a wall...



As you can see, on it I place my text texture, so I can tell left (red) from right (blue) and top (yellow) from bottom (white) to make sure my texture loading and texture coords are correct... But I can see that damn line between the two triangles... Yet I'm using the same vertices for the corners, there should be no gap.

Switching from glBegin(GL_TRIANGLES) to glBegin(GL_QUADS) I can still see a single line between the triangles, so its not my coordinates or triangles... What the heck can it be?

Now, I'm using pretty old OpenGL here, really really old stuff, so for 2012 the information available on the ground is pretty slim, there's alsorts of suggestions out there and no real documentation as to what is causing this...

In desparation I started to play about with the blending settings, to no avail, then I started to play with the smoothing mode:

glEnable(GL_SMOOTH);

glHint(GL_POLYGON_SMOOTH, GL_NICEST);

glEnable(GL_POLYGON_SMOOTH);

glShadeModel(GL_SMOOTH);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

None of this seemed to work... So I started to play with the multi-sampling...

glEnable(GL_MULTISAMPLE_ARB);

Still no joy....

But.. Disable the smoothing AND enable the multisampling and everything starts to work:



// Set Rendering smoothing
//glEnable(GL_SMOOTH);
//glHint(GL_POLYGON_SMOOTH, GL_NICEST);
//glEnable(GL_POLYGON_SMOOTH);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_MULTISAMPLE_ARB);

I can now start to add brick textures and make my boxes into buildings...


RAII - Please for the love of god....


I'm going to just vent here folks... In programming, if you have a class which HAS TO HAVE all its members initialised, DO NOT just expose an empty constructor...

Here's what I mean...

#include <string>
using namespace std;

class A
{
   public:
   
      int _id;
      string _name;      
      
      A (const int& id, const string& name)
        :
        _id(id),
        _name(name)
      {
      }          

};

There's a class right, one assumes there's only one way to initialise a copy of this class?  The constructor giving implicite values, yes?... Good.

So when you want this pattern, make sure it works just how you expect, hide the other constructors... thus:

#include <string>
using namespace std;

class A
{
   private:
   
      A() {}

   public:
   
      int _id;
      string _name;      
      
      A (const int& id, const string& name)
        :
        _id(id),
        _name(name)
      {
      }          

};

This stops blank copies being generated, on compilers which automatically create the defautl constructor public!

Likewise in C#, do the same... If you have a data class:

class B
{
    public int _id { get; set; }
    public string _name { get; set; }
    
    public B ()
    {
    }
    
    public B (int id, string name)    
    {
    _id = id;
    _name = name;    
    }
    
    public string NewToString ()
    {
    return string.Format("{0}-{1}", _id, _name);
    }
    
}

That class is no big deal, the int and string are implicit values, they have inplicit values upon creation.

B b = new B();
Console.writeline (b.NewToString());

All works fine, but if we add a user class in there, or any other class which can be null... then we have to be more careful...

class StudentRecord
{
    public string _name { get; set; }
    public DateTime _dob { get; set; }
}

class B
{
    public int _id { get; set; }
    private StudentRecord _student;
    public string _name 
    {
    get { _student._name; }
    set { _student._name = value; }
    }
    
    public B ()
    {
    }
    
    public B (int id, string name)    
    {
    _id = id;
    _student = new StudentRecord();
    _name = name;    
    }
    
    public string NewToString ()
    {
    return string.Format("{0}-{1}", _id, _name);
    }
    
}

What happens now?.... Well the code will crash:

B b = new B();
Console.writeline (b.NewToString());

Will throw a null reference exception in the call _name against _student, as it is not implicitely defined during creation when using the default constructor.

What we should really do is utilise the "Resource Allocation Is Initialisation" (RAII) concept and only allow a new B to be created with the defined values.  And we should do this to all classes, whether they have implicit values or user defined classes within.

How do we do this?  By stopping the default constructor being used:

class B
{
    public int _id { get; set; }
    private StudentRecord _student;
    public string _name 
    {
    get { _student._name; }
    set { _student._name = value; }
    }
    
    private B ()
    {
    }
    
    public B (int id, string name)    
    {
    _id = id;
    _student = new StudentRecord();
    _name = name;    
    }
    
    public string NewToString ()
    {
    return string.Format("{0}-{1}", _id, _name);
    }
    
}

Now, the only way for the programmer to get using this wrong is to mess about with the code!

It stops all those annoying "What the Hell just happened" during runtime, and some poor other schmuck doesn't have to do running around working out where your code went wrong because it missed an assignment, such as this, very poor code:

class B
{
    public int _id { get; set; }
    public StudentRecord _student { get; set; }
    public string _name 
    {
    get { _student._name; }
    set { _student._name = value; }
    }
    
    private B ()
    {
    }      
    
    public string NewToString ()
    {
    return string.Format("{0}-{1}", _id, _name);
    }
    
}

B b = new B();
b._id = 0;
b._name = "Tom";

Which would need to be:

B b = new B();
b._id = 0;
b._student = new StudentRecord();
b._name = "Tom";

Horrid horrid, its bad code, bad coding practice, and using excuses like "This class will be removed later" doesn't really wash with me, because code has a half life you can never predict, you don't know how long your code, or your class, will be around and used and release it in a bad form today, it'll bite you again and again... Release it nicely, with rigourous defensive programming and you'll find yourself not being hassled by it.

Tuesday, 18 September 2012

BBC iPlayer Volume


What the fuck is it with the BBC iPlayer not keeping the volume level how you've set it between videos?...

I mean you visit their site and are instantly blasted out your seat by a volume level of 7... So you scurry to turn it down how you like it (or up if you like) then swap show and BOOM its back up to 7 again.

So very very annoying.

Monday, 17 September 2012

Sweets for Programming - I


Over the next few weeks, I'm going to be setting up my desk a little better and working more code time at home, in the evenings... Consequently I've decided I need to do something constructive towards my coding productivity at home... I need to pick a sweet to suck on, something to let my mouth idle whilst I mull over complex coding problems.

In America, as I understand it, chewing gum is the vogue, however, gum results in the need to spit, stick or in emergencies swallow its tacky remains.  I prefer a sweet, so that I can suck it to oblivion without worrying.

So, the first sweet I'm picking is the Polo... I love Polo's... The hole, the fact some crumble, whilst others are hard as diamond, they're minty, and great... They also come in a variety of flavours, and the first I've picked is the Spearmint Polo...



I'll let you know how I get along.

DropBox Coffee Script Conversion


I was just reading coverage of DropBox switching to using some CoffeeScript rather than JavaScript in some of their code base, I was insenced enough with confusion over the coverage to post a comment on the thread... Let me give yout he gist...

The folks over at DropBox have changed to this CoffeeScript code in an effort to "simplify" the code... Least this is one of the major reported reasons for their switching out to JavaScript, they also mention removing bugs... and less size/RAM being used.

Analysing this waffer thing argument however and there's a glaring problem... First of all, CoffeeScript "compiles"* down to JavaScript, so there's nothing CoffeeScript does which you can't yourself achieve in JavaScript... If you end up with smaller code from the output of CoffeeScript then you could simply have written your JavaScript better yourself...

Then there's the argument about CoffeeScript being simpler, and easier, to maintain... I argue that CoffeeScript is just someone deciding that they don't like the C style language formatting of JavaScript and opting for a different style, something which looks rather like HASKEL to me... gah... or Occam... Or a myriad of other languages which don't use braces and semi-colons, you can check it out on their site.

Now consider that most programmers worth their salt know a C style language, its more common and arguably more comprehensible, given JavaScript code they could get by... Given CoffeeScript... its rather alien, take for example the JavaScript for a function:

square = function(x)
{
  return x * x;
};

Now, look at the CoffeeScript...

square = (x) -> x * x

I find the second to be too abstract, square equals an x in brackets (which indicate parameters input) pointing at x times x... What the hell does that actually mean?

Where as the first bit... the square equals a function that takes an x... and that function returns x times x... its a bit less alient, a bit less "haahoo we know sucks to you" in the coding community parlance.

The article says it all, my comment, if it gets published says more...

Thursday, 13 September 2012

Wii U - Will Fail


Taking a long look at the images released of the new Nintendo Wii U games console, I can only see it being a huge and obvious flop.



Its too bulky, has no cool feel, its performance and specifications are way down on the ladder, there is in fact nothing the device offers that a smart phone already does and smart phones are cool.

This thing may possibly sell in Japan, but for the rest of the world, its going to sink like a stone.

Personally, I wish companies would realise that devices like this don't seem to catch on.  The original GameBoy caught on big time, it filled a void not just a niche.  There was no rival anywhere near the GameBoy, and to some extent its carried that legacy into the newer DS devices range.  But this Wii U is going to go the same way as the Atari Lynx, which it seems to ape in so many ways.



And its to the Lynx I now turn, the Lynx for its part was designed by a creative team, one of its leaders R. J. Mical is a great, and fun, guy; whos fame spreads back to the Amiga 1000 development.  But no-one turned around to that team and really scrutinised the product, sure it had a nobel and dedicated fan base, some people "loved that giant machine". But it was too big and bulky for the main stream.

And here we are 23 years later with Nintendo, the company whom (lets face it) sank the Lynx, stepping themselves over those exact same mistake sink holes full of lava... They'll sink, but they don't seem to see it.

Tuesday, 11 September 2012

DayZ Bullet Physics versus Trees


Well, after a week without internet and a very busy day having new carpets fitted, and sorting out a whole bunch of my old junk, I sat down; with the wife - she's so cool; to play some DayZ.

Now, my internet (wireless) signal has been really really screwy, with all the stuff moved about in the house we're getting echo's and shadows in the signal all over the house, so my connection has been so flakey to servers.  I got disconnected from like three servers after a few minutes and then finally got connected to a stable server.

I was on an UK server, that had myself, one other person and then a group of three guys all with the same clan icons "[A2]" I think... Anyway, I hit a military camp and then start to make my way north, rain is bucketing it down and as I enter a small clump of tree's, seemingly in the middle of nowhere, I hear the zip of a shot and then another zip of a shot...

Someone is shooting at me, from an unknown, very distant location... But I can't figure out where from... I can't hear the gun, just the shots hitting the tree's around me... And I can't see any flash of the gun in the dark...

I was tempted to squeeze off a whole mag of M14A1 fire into the blackness, to try to freak them out into moving away, but they don't... I can still hear lots of rounds hitting the tree's, but no direction...

I moved out of cover, and got hit in the legs... luckily I have bandages, morphine and plenty of meat to make up the blood loss and crawl under cover...

And this is where the problems kicked off... I started to lag again, I can hear shots ripping up the foliage above me, and I can't move with a yellow lag out... Suddenly my character glitches onto a small wall and stands up... FUCK FUCK... and I lay down again, but too late, I get hit again, and have to seek refuge to kneel and bandage again.

I think the bandage went on, and then red connection... GAH!!!

But, this was fine, I waited in the bush, hoping it would come back, but it never did so I let the thing fall over and tried to get back on.

It was thrilling, my stupid connection broke the situation, which was intense... But in the end I cowardly left it... And didn't dare go back, or try, with my connection.

Now, this brought me to thinking, what was wrong with the situation?... I could not get a bead on a return shot... I could not hear the shots coming in, just their landing, and I could not hear the source weapon... This was problematic at best...

At worst it was a right royal pain in the back side, I wanted to turn and loose some covering fire in the general direction and then set about running out of there... but which direction?

Now, the shots coming in were hitting the bushes and making a specific sound... so there is a trigger so the game knows that the shots hit the foliage... All it needed was an effect, where leaves and such came out of the tree's in the trail of the bullets, then I'd have known the direction the fire was coming from...

My awesome MSPaint SkillZ

I'm going to post this on the forum, as a suggestion, I think... It'd help those of us (like me) who like to hang out in the bush, rather than spend time in the concrete.