Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Saturday, May 25, 2013

Sky simulation for dome theater




Wow, long time since I've wrote something here, sorry for that. University is keeping me busy :)
One of things I've been working on is this sky simulation for a dome theater at Visualiseringscenter in Norrköping, along with my classmates Aron Tornberg and Sebastian Piwell. It's done with C++, OpenGL and the library SGCT.

SGCT distributes the workload to 6 different nodes, one for every projector. You can read more about it in the link above. I wrote an external GUI in C# for easy tweaking of some parameters.

All the viewports for the dome projection

Fisheye projection
As the screenshots shows, performance is kind of slow on my laptop, but this is work in progress without any optimizations. Besides, the method we use for the clouds is kinda brute force. This probably won't be a problem in the dome theater, since every node renders a relatively small part of the sky.

I'll update/create a new post with more details once we have finished this. I'll try and get some footage from the dome theater as well :)

Additional credit:
Miroslav Andel, for all the support concerning SGCT

Thursday, May 23, 2013

Summer Of Code @ Linköping University

During April, a mail was sent out to all student at our university talking about a new project the university had started: "Summer of Code"(SOC for short). SOC gives students the possibility to develop one of their own projects during the summer and getting payed by the university. It would be  5 projects would be accepted for this opportunity. A total of 70 projects applied, we were one of them and...


Our projects was accepted!


Fredrik and I had discussed an idea for an smartphone app earlier this year that we wanted to create, and decided to apply to SOC with this idea. The final idea, we will keep a secret for now, but I thought we could still discuss parts of it.

None of us have really worked with app-development before, but you have to start somewhere. Our intentions are to develop for the android platform first and perhap, later for iOS since our target audience is students, and a lot of students use iPads. But we also want to keep developing costs down at a start.

The project consists of 3 major parts: Drawing, Detecting, and Evaluating. The biggest challenge for us right now is the second one, Detecting. None of use have worked with image recognition before. We've been looking into using OpenCV as a framework to help us in this stage. The detection should also be "on the fly" while the user is still drawing to give direct feedback to the user.

We are both looking forward to this July when we will start working and we will do some more updates on the blog for each part during development!

Sunday, January 20, 2013

Debugging follow-up: Eclipse

So as a follow up to my previous post, this one is more specific to where you'll find some of the tools of debugging in the IDE Eclipse. Both this and the previous post is targeted to my classmates to help them get started with debugging, but anyone that uses eclipse can check it out!

Lets start off the same place as the previous post, Breakpoints.

In Eclipse there are two simple ways of setting breakpoints. First one is to hit "Ctrl+shift+b". This will create a breakpoint on the current line your typing cursor is.
The other way is to double-click in the white space just left of the line numbers.

You can see in the picture below how there is an indication now of a blue ball on the line you've set your breakpoint on. If you right click this ball and pick breakpoint Properties... you'll get access to edit the conditional breakpoint options I mentioned in the advanced techniques on previous post.

on row 32 we can see the indication that we have a breakpoint set.

So how do we start debugging? Easy, on your toolbar you have these 3 buttons:


The middle one is the regular "build and run", the one to the right is just "run without rebuild".
But it's the one to the left we want to click to start debugging.
When we do this Eclipse tells us that it wants to change "Perspective". When we get this option, we want to press "yes". Now, DON'T PANIC! Your eclipse will change its layout.
So first, I'll tell you how to revert back. In the upper right corner you have buttons for changing the perspective or layout. the one to the very right is our debug perspective, and the one next to it is our regular coding perspective. So a perspective is just another word for "layout to fit what your current doing".

from left to right: open list of all perspective, Java EE persp(never used). Java persp., debugging persp.


We will now have some new windows to look at. One of these is the Variables window, this will show us the variables being changed in the current scope. We can also edit these values here.


If you right click a variable in the editor window of this perspective and pick "watch" you'll get another tab in this window that contains  your "watch list" of variables you want to keep watch on.

So now that we know where to look for our values and we're in debug perspective, we need to control the execution of our code. That's what these buttons in the toolbar are for:
From left to right we have: Step return (same as Step out), Step Over, and Step Into. Read my previous post to figure out what these do. The next one is the Resume button. and the last one to the right is terminate, it will shut down our program and stop debugging.

Lastly, we have a window simply called "debug". This window works as our callstack. So in the picture below we can see that we're right now in the function SetDate from the class Date that's been called from "dateTestProgram.main", the main function of our program and all of this happens in the main thread.


So there we go, you now have everything you will need to start being a more efficient coder!


Friday, January 18, 2013

Debugging tutorial

There is no denying that a good way to learn is to explain to others. That's why I thought I would do this write up of practical debugging tips and techniques.

 What is debugging and why do I need it? 

So, what is debugging? Its the part of software development when you hit the "build" button and you get no errors in the code. Yes, there are no problems with how you've written the code for the computer to interpret it, but what about the logical errors? What about the while-loops that never ends and the if-statments that never returns true? This is what debugging is, resolving your logical errors that cause your software to misbehave or crash.

Easiest way to debug is just put a "print(x);" somewhere in the code to see the value of  x at that point. We've all started out debugging this way. But there comes a time when the logic of the programs we write gets too big, and using this "print debugging" won't be enough. Either your code will be filled with loads of  print-calls to check why your code doesn't enter a if-clause or why the values of our variables doesn't make any sense. This type of debugging is also insufficient since it will never actually tell you where your problem is, just that somewhere after this, something is wrong. That's why you should use debugging tools that many (if not all) better IDEs (Integrated Development Environment) ships with. I will have Visual studios 2008 as a base when talking about most of the parts in this tutorial, but your IDE should have something similar.

Below are some of the key features to use when debugging.

Breakpoints:
One of the main tools you'll be using while debugging is the breakpoints. The name explains its purpose very good; it's a point within your code that will break(or halt) the execution of code. While the code is halted you'll be able to inspect values of variables and, in some cases, even edit them.
Note: The line with the break point has NOT been executed yet.  
Lets look at the code below:
int apples;
apples = 5;
apples = a + 5;

If our breakpoint is set on:
  • Line 1, apples will still not be declared, most (smarter) editors will just move your breakpoint down to line 2 since nothing in your program changes during deceleration.
  • Line 2, apples has been declared but has not been given a value, it will have something like -5468165819 as its value.
  • Line 3, apples has the value of 5.
  • Line 4(not shown), apples will have the value of 10.

But since we don't want to have breakpoints for every line, we have some tools that allow us to "step" in our code.
There are three types of stepping, Step over, Step into, Step out.

Step Over:  The most used one. This basically say "execute the line we're currently on, but halt at the next line".
Step Into: This stepping method allows us to go into functions. If we look at the code below. if we were to have a breakpoint on line 5 and use Step over we would go to line 6 and be able to see the value of ourApples after the function sum has been run. However, if we press Step into, we will jump into the sum function and halt execution on the first line of the function (line 12). We can then continue using Step over to look at every line in this function.
int main()
{
    int myApples = 5;
    int johnsApples = 10;
    int ourApples = Sum(myApples, johnsApples);
    print ourApples;
}

int Sum(int a, int b)
{
    //no need for this, but for showing debugging, we do this.
    int returnSum = a;
    returnSum += b;
    return returnSum;
}
Step Out: If we realize, "The error we're looking for isn't caused in this function", then Step out is used to exit that function and go back to the line that called the function (read more about keeping track of how other functions call functions in call stack part further down).
Let say we have a breakpoint at line 12 in the code above, if we would press Step out we would end up on line 6 again, with no need of stepping over line 14 to go back.

We also have Resume which will continue the execution of code until we hit the next breakpoint.

To set a breakpoint you often press to the left of the editor and some sort of indicator should appear. You can often right-click on a line and pick "set breakpoint" there aswell. There are some other, more advanced topics about breakpoints that you can read further below.

One key feature is the fact that when you hit a breakpoint, you can edit the values of variables! So lets say that right now you're not intersted why your variable's value makes no sense, but you want to try your an if-loop thats within a if-case. If you variable's value is hogwash, the if-case will never be true and you'll never enter the loop - make a breakpoint at the if-case and just change the value of the variable and try out your look!

Breakpoint has been hit, indicated by the yellow arrow, 
By hovering over a we can see that it has a very small value, 
we could draw the conclusion that it has only be declared bu never given a value.

Watch/Local Window:
These two windows help us keep track of our variables without having to hover over them in our editor. In Visual studios, we have both "watch window" and "local window". (some IDEs might not have the "local" one)

The Watch window is at first empty. You have to add variables to it by right clicking them in your editor and click "add watch". Then you'll be able to see the value of that variable as long as it remains in the memory or you remove them, which could be nice if that variable, for some reason, is not close to your current breakpoint.

The Local window shows all variables active in the current scope and therefore changes quite radical when changing scope (when you exit a loop or function).
The local window, the value of the calculate array is indicated by red, 
that means that the last row edited this variable

This way you can see how multiple variables are modified quickly.

Call Stack:
When your programs get more and more complicated and class-functions are called from multiple places, you sometimes what to know "From where is this function called?". This is what the window Call stack can do for you. In the picture below, 
  • The top row shows where our current breakpoint is, its in the function called MoreCalculations on line 39.
  • The next row shows us that MoreCalculations was called from another function, CalculateSum and it was called on line 24 of that function.
  • The rows after that get more complicated but shows us that CalculateSum was called from our windows main and after that we get to even more complicated entries.



But what to take away from this is that we can use this window to keep track of, when and where was this function called, and we can use the call stack to retrace those steps.
Advanced Techniques
Conditional Breakpoints
Sometimes when working with breakpoints within loops, it could be a pain in the butt to having to click resume 45 times, since the error we're looking for occurs on the 45th time the loop runs. That's why we want to use conditional breakpoints. There are often two methods for this.

  • The first one is to tell your editor "I only want to break when i've passed this breakpoint 45 times or more" (could be useful in a while(true) loop)
  • Second one is "I want to halt execution when this variable has the value of 45" (useful in for loops e.g i == 45).
This can be useful to speed our debugging processes up.

Disassembly Code
Just before your code is turned into binary, its first turned into Assembly Code. These are CPU specific instructions that are incredibly cryptic and quite difficult to read if you don't know how. This is a very advanced topic that related more to optimization debugging, but in certain cases, the assembler code can help you find the problem to why your software crashes.
I won't talk about it here, because at the time my knowledge for this is quite limited. But if you're intersted, I'd recommend Alex Darby's Low-level Curriculum series over at #AltDevBlogADay

Sunday, November 18, 2012

Stay Focusd!

Both me and Fredrik been busy for a while now with things happening in school and been quite bad at updating the blog.

But we have a recommendation for everyone out there: "Stay Focusd!"
It's an extension for chrome that helps you avoid procrastinating in different ways.
- Set a fixed amount of minutes to surf on specific websites
- "Nuke" the internet and stop you from checking twitter/facebook every second.
- A good system to make sure you don't disable the system.

It's a great system for making sure you don't keep watching youtube videos all day instead of actually doing work.

Check it out: https://chrome.google.com/webstore/detail/stayfocusd/laankejkbhbdhmipfmgcngdelahlfoji

Wednesday, September 26, 2012

Venturing into the world of PyQt and Model-View design

This semester I'm taking a course called "Communication and UIs" (alongside multivariable calculus and "Design"). I've taken this opportunity to venture into the world of Python, PyQt and Model-View design by making a little asset viewer/mini-manager for Maya.

Huge thanks to Yasin Uludag for making the PyQt4 Model View tutorial series (available on his youtube channel).




Asset Viewer

  • Reads/writes to an XML file to store the asset metadata
  • Asset status
  • Asset preview image (with viewport snap option)
  • Filter (semi-broken atm)

The reasoning for making this little tool was to demonstrate the benefits of having the tools that permits the user to deal with the important stuff and not manual file labor, because everyone knows how messy that can get even if it's just a little personal project. I bet if I take a look around some old stuff I've worked on I will find wonderful examples like "robot_v212_final_reallyFinal_shitisbroken.ma".
A simple structure from the beginning goes a long way, especially when it comes to eventually move to a more complex and feature filled management.


There is still some things that needs working on, but since the focus of the course is really just UI design I won't be adding any more features that takes it beyond a "concept of asset management". I'll upload the full source etc when it's "done".

PS. Any comments concerning the UI design is greatly appreciated! I will eventually be graded on this, and my professor will not be taking all that beautiful Model-View design into consideration. Thanks!

Thursday, September 13, 2012

”I can do it myself!”

avoiding copy/paste programming tutorials.

I Just recently started working on creating my platform for a 3D engine using DirectX to use during my education as a base for trying to implement techniques and ideas that are relevant and new within the game industry. I quickly found a website with a lot of information and code about the subject. I started using this website and the code that it had on the subject, but I quickly realized that I was more just copy/pasting code than really understanding the concepts and how DirectX actually work and in the end, I couldn't really explain the code to my debugging friend (a horse with rainbow mane and a disco ball around his neck, check pic below). This made me realize, that in the end I wouldn't really learn anything from this and instead went for a new source of information: Frank D. Luna's books about DirectX(Link to book). It has been a great source and I highly recommend it if you want to get your hands dirty with DirectX. Not only does it talk about the fundamental logic behind it, but also explains the code and function calls very thoroughly and my understanding of the code I write and DirectX is much improved. 

(My rubber ducking friend, Tony Mane-ro)

I've always tried to avoid doing copy/paste from websites to solve problems that I meet, mostly to make sure that I understand the logic to the solution and also to avoid diving into a subject just to find the solution and then swim back out without really looking around and seeing a bigger picture within the subject. And therefore I thought I would write some ideas and tips to share to help you avoid this, and also, when you write blog posts or reply on threads regarding a problem to help your reader not to do these kind of dives.

When reading about solutions to certain problems that might occur when programming, you can find times where you see a small text with no explanation or very little explanation of the code and huge block of code. In the heat of the moment you can often say “great that chunk of code solves my problem!” (Perhaps with some modification) and you end up just copy and pasting the code right in to your code and continue on. Sometimes this is acceptable if it's a trivial problem you just forgot the name of a function and not really something you have to spend much time thinking of how the solution will really work. But even then, instead of just copy/paste, write the code yourself! Muscle memory can perhaps help you remember that function next time you need it! I would say that you should always retype code instead of just copy/pasting to avoid missing to rename a variable or any other mistakes that could be easily avoided. This also helps you see the actual flow of the code that solves your problem and you might find new ways to write code that treats something within the problem that you haven't seen before, you find better solutions to situations that you perhaps never saw as a problem. This is perhaps something you should also think about when starting a new project where you want to reuse some of your old code. Take a look at that code and evaluate it, is it possible to re-write it so it's faster/smarter/less error prone? Then do that before you copy/paste it or reference it into your next project.
   I believe this is very important when working with production code, it could be something you'll perhaps will need to give to someone else at a later stage of production. If you can't fully understand it, how should your co-worker relate to this? I had an experience like this once when one of my co-workers was working on RnD stuff and was coding towards an SDK he'd never worked with before. I was asked to take a look and see if a could see anything that could solve his issue he had at the time and so he handed me the code he had “Written”. I quickly realized that a lot of this code was just copied and pasted from some website since some of the comments and variable were in spanish... And in the end to solution to his original problem was incredibly simple and he would have stumble upon it if he would have taken the time and read 4-5 pages in the SDK documentation.
   What actually happened was that I re-wrote everything because of the design of the copied code was very strict and I knew that it wouldn't work later when we later woul have to expand the system. So not only did he copy/pasting take more time because of the refactoring, he would have also spotted his problem and solved it with some reading and gained a greater understanding of what he was actually doing.
   Some might see that re-writing code that someone else has posted is re-inventing the wheel, but if you later have to either change the wheel or perhaps remodel the wheel, it could be good to know why it looks like it does when you start.

What can you do to help others avoid the pitfall of copy/pasting code? It depends on what the person is asking for or who is your intended audience.
   If someone on a forum asks for a function within Maya or any other DCC, instead of typing it, point them in a direction where they can find more information about it(like the Maya documentation). This might help them find the solution to another one of their problems in the future. It's like when your trying to solve a puzzle and ask for a hint, you don't want them to give the solution, just a way to find the solution for yourself.
And if you're writing a blog post where you want explain how you solved a certain issue, I would recommend you to talk more about your approach in text than in code. Post the code in segments rather than in a big chunk and talk about those segments individually. Many of the articles you find at  www.Codeproject.com , I believe, does this very well!

So start copying code from the internet,
but do it a letter at a time.