Archive for the ‘problems’ category

Using glClipPlanes and plane equations

5.10.2010

About a year ago, I searched for an OpenGL way of clipping a rectangle in 2D/3D space. To explain further: I’m making a GUI library with OpenGL, and I wanted a parent rectangle to be able to clip it’s child rectangles even if they were textured. I found glClipPlane, and tried it briefly. There was almost no documentation found, or it was very mathematical talking about plane equations without properly explaining how it should be transferred to code. I also could not find any coding examples of using them.

So, I spent about three months writing a system that would do this clipping in software. For flat rectangles this was easy, but for textured stuff it was very complex, as it was manipulating the texturecoords to get the clipping done. I somehow managed to write it, but the system was incomprehensible, added some software overhead, didn’t properly support my per widget zooming system, and I had to put all my OpenGL drawing inside this clipping subsystem. For about a year I was semi-satisfied with it, but as I changed to another way of drawing fonts, I needed either a new clipping system, or I’d have to adapt the external font code to my awful clipping system.

Then I did another search and found this page: http://local.wasp.uwa.edu.au/~pbourke/geometry/planeeq/

It’s dated 1989, and it’s another one of those mathematicians only explanations (I’m a pragmatic self-taught trial-by-error coder, with very poor mathematical knowledge, sorry). But for the first time it had something that looked a bit like pseudo-code and translated well into programming. So, I tested it and with a few first misses I was able to get the following code of it, that works for me. I’m not sure if this is the correct or best way of using plane equations, but it’s the only one that I’ve gotten working.

double[] planeEquation( float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)
{
double[] eq = new double[4];
eq[0] = (y1*(z2 – z3)) + (y2*(z3 – z1)) + (y3*(z1 – z2));
eq[1] = (z1*(x2 – x3)) + (z2*(x3 – x1)) + (z3*(x1 – x2));
eq[2] = (x1*(y2 – y3)) + (x2*(y3 – y1)) + (x3*(y1 – y2));
eq[3] = -((x1*((y2*z3) – (y3*z2))) + (x2*((y3*z1) – (y1*z3))) + (x3*((y1*z2) – (y2*z1))));
return eq;
}

Horrible formatting there. I don’t know how to use wordpress to get the code looking correct…

Then I’m doing the following on each parent rectangle, just before starting to translate or draw the children:

double[] left_eq = planeEquation(ix1,iy1,iz, ix1,iy2,iz, ix1,iy2,iz+1.0f);

glClipPlane(GL_CLIP_PLANE0, left_eq.ptr);

glEnable(GL_CLIP_PLANE0);

delete left_eq;

double[] right_eq = planeEquation(ix2,iy1,iz, ix2,iy2,iz, ix2,iy2,iz-1.0f);

glClipPlane(GL_CLIP_PLANE1, right_eq.ptr);

glEnable(GL_CLIP_PLANE1);

delete right_eq;

double[] top_eq = planeEquation(ix1,iy1,iz, ix2,iy1,iz, ix2,iy1,iz-1.0f);

glClipPlane(GL_CLIP_PLANE2, top_eq.ptr);

glEnable(GL_CLIP_PLANE2);

delete top_eq;

double[] bottom_eq = planeEquation(ix1,iy2,iz, ix2,iy2,iz, ix2,iy2,iz+1.0f);

glClipPlane(GL_CLIP_PLANE3, bottom_eq.ptr);

glEnable(GL_CLIP_PLANE3);

delete bottom_eq;

So all you have to give to this planeEquation function is three points on the clipping plane. It seems that the order of them somehow dictates which side of it will be clipped and which side will be visible. In this example just changing the direction of the last Z from plus to minus, changes which side gets clipped.

I hope this is useful to someone trying to get some sense into glClipPlane and plane equations. This might all be just wrong, but it works for me! Any corrections and suggestions in the comments, please.

Advertisements

I’m back

25.07.2007

I was freed from prison on sunday morning. I’m happy that I’m not there anymore. Twenty days is a long time, when you’re inside.

I’ve noticed that Pihlaja stops crashing doesn’t crash as often when I use it from a console, and have most of the debug output enabled. It seems that if seeks and some other GStreamer calls happen too fast, it will crash. But when it does debug output on the console on every call, it slows it down, and the crashes disappear. Hmm. I’m not sure how to deal with this problem… How to limit the amount of gstreamer calls being made…? I can just leave the debug output on, but if the user doesn’t start Pihlaja from the console, there will be crashes, so it’s not a complete solution…

Pihlaja 003 alpha released

1.07.2007

I tried to embed this video, but it didn’t work out, but you can click the link. It’s a small three minute example showing the Pihlaja user interface and even a crash in the middle.

Release notes for Pihlaja 003 alpha (”Well, it’s a start of some sort”)

Six months (or four years if you count the earlier versions) of work has culminated into this very buggy and crashy piece of software that is called Pihlaja. You can get the binary for Ubuntu Feisty Fawn from the sourceforge page. I really hope I haven’t forgotten anything in this busy process. The source is there too, and in the svn over http://www.dsource.org. Hope it works! Some more release notes will follow in the end of this post. But first for a small update on my life, and a small appeal:

This alpha release comes just before a weird period starts in my life: The Finnish state is putting me to prison for three weeks. I’m not kidding.

The reason for this is that in Finland there is an obligatory military service for all men (excluding Jehova’s witnesses), it’s duration is either 6 months, 8 months or 12 months. I want to emphasize that a large majority of the people who go to military service get the 6 months. Only some special areas demand the longer 8 or 12 months. Then there’s also the alternative service called ”civilian service” for conscientious objectors (like me). It’s duration is 13 months, which is considered a punishment for not doing military service, and to keep people from going to civilian service. Most people choose the (propably) 6 months military service instead of the 13 months alternative. Yet there are some thousands of people who still go to civilian service each year..

I started my civilian service some three years ago, and stopped it after 11 and a half months as a protest to the punishmental duration of civilian service, and the fact that conscientious objectors are released from the military only during peace time. I had 41 days before my service would have ended.

A long juridical process started and I eventually had my trial a couple of months ago. I was convicted quilty of ”civilian service crime”, and (as is the standard) the court ordered me to jail for half of my remaining service, which (rounded to full days) was 20 days. I complained to the upper court level, but the result didn’t change. And now I’m starting at the Kerava prison on 3th of July. It is a normal closed prison. (In Finland there are also so called ”open prisons”, where non-violent prisoners who have done less serious crimes might be able to get to.) So for the next three weeks I will not be able to code on Pihlaja and I’ll only be able to see my two year old daughter during visiting hours on the weekends. I’ll propably spend my time mostly reading books.

Since 1999 Amnesty international has considered Finnish ”total objectors” as prisoners of conscience, because of the punitive length of the civilian service. Also in the recent years many international bodies (including UN’s Human Rights Committee) have demanded Finland to take measures to improve the legislation concerning conscientious objectors. There are about 50 people going to jail each year as ”total objectors”. Some of them have to spend 6 and a half months in a prison, if they don’t even start doing any civilian service at all. More info about these issues is available here.

If you too feel like that this punishment that I’m getting is a bit over the top, then I’d like to ask you to take up a little of your time to write one letter (or even a postcard) to either

A) a random member of the Finnish parliament, who could decide to end this

Pick a random parlamentarian from this list, and send the letter to this address:

firstname lastname
Eduskunta, Parliament of Finland
00102 HELSINKI
FINLAND

or

B) a random member of the European parliament, who could pressure Finnish parliamentarians to act on this issue. You can find contact information from: http://www.europarl.europa.eu

I don’t think it would be good that the letter’s would all be the same, so I won’t make any example texts, so you’d might have to write it yourself. But if you do take the time to write one, then me (and yearly about 50 young finnish men, who are put to jail as prisoners of conscience) would much appreciate it. Thank you!

 

But back to the Pihlaja release notes:

Here’s a list of issues and a guide to survival:

Installing (is that even possible?)

 

You can get Pihlaja binaries from the sourceforge site. I’ve put the compiling from source section to the end, as that’s kind of hard to do, really. But you can get the source from svn in http://www.dsource.org.

 

What you can do

 

When you start Pihlaja, you are greeted with a project chooser dialog. Pihlaja is one of those nasty programs that manage your files for you. Currently you can’t even override that. So there’s no ”save as”. There is an autosave every 5 minutes. Ignore the users section, as it doesn’t affect anything yet, and focus on the projects section. You can alternatively edit the name of the ”New untitled project” into something meaningfull, or you can just select it and click OK, in which case it will ask the name in a dialog. If Pihlaja doesn’t crash before it closes you will find your project here the next time you start Pihlaja.

 

Now the project window opens up. On the left you have a list of all the movies, materials and bins in your project. On the right there’s some room for the monitor. And the lower part of the window is reserved for the editing canvas. On the editing canvas you can see two rectangles right next to each other, the ”Start” and the ”End” rectangle. Below the list of the movies, materials and bins you have a big button labeled ”Import material…”. Click on that and you’re presented with a file chooser dialog letting you to choose one or more files to import. Be sure to only import files with DV content (.dv, .mov and .avi should be fine as long as their contents are DV. I haven’t tested .avi myself though. I just tested some Quicktime files, and found out that when exporting from Final Cut Pro, you’ll have to ”export using quicktime conversion” and make the sound linear PCM with little endian. The default seems to be big endian and that doesn’t seem to work with Pihlaja). You can double click on files you want to import. When you’re done, you must click the close button (this way you can import many files easily).

 

If you’re lucky, your files will get recognized correctly and they’ll end up in the list. Now you should drag a file (or material in Pihlaja terminology) into the editing canvas. Bing. It’s there. And it hopefully didn’t crash, as it has never crashed for me at this stage! But you never know. Drag two more files somewhere else on the canvas. The objects that you just created that look like windows of an operating system are called scenes in Pihlaja. The scenes you created only contain two tracks, one video, one audio (if all went well). The tracks contain only one edit or clip each at this stage (haven’t yet decided on that terminology, as I’m not sure what those should be called in english. Feedback is welcome). The scenes could contain more edits, but let’s forget that, as that side of Pihlaja is not really working reliably yet (what part of Pihlaja is working reliably – one might ask).

 

The scene has a white ruler on it’s middle, separating the video and audio tracks. You can click on the ruler to see how buggy Pihlaja is this time. If you’re really really lucky, you’ll see a monitor appear on the right (Oh, it might have appeared there before too). Press space on the keyboard to make the scene play and to pause again. You can use the arrow keys to move around. Left and right for 1 frame and up and down for 10 frames movement.

 

There’s two ways to edit in Pihlaja.

1. You can grab the scene ”windows” from their headers, and put them one after each other (they’ll snap, and then you just release) to form chains of scenes.

2. Or you can move your mouse a little left from the ruler of a scene, to the outside of the ”scene window”, and from there start dragging a link to another scene. This way you can connect scenes, as if they we’re nodes.

 

Much of this doesn’t work the way it’s supposed to yet, so you can’t really reorder them or relink them, after you’ve done it the first time. So, you can just put them together, and watch the result. Trimming the scenes doesn’t work yet. And I didn’t get the export working yet, so that put’s Pihlaja in the “not really usable for anything” -category.

 

A list of things not to do (feel free to do them if you want to test the limits)

 

  • Holding your mouse button pressed and moving it on the Ruler might sometimes result in Pihlaja crashing while it has grabbed the mouse. This will make your system look like it’s crashed, but it’s not. You should avoid dragging on the ruler to avoid this, but if you happen to come to crash while dragging, then do the following (you should write this down on a piece of paper): CTRL-ALT-F4 to create a new terminal session(?). Log in with your username. Write: ”ps -x”. This will list the running processes. Locate Pihlaja on the list and look it’s process id – it should be the first number in the front. Then write: ”kill 8712” – and replace the 8712 with the number of the Pihlaja process you just checked with ps -x. CTRL-ALT-F7 to get back to your X session. Great! Now it’s working again.

  • Don’t do anything that you’d think should work. It will propably not work, or it will crash.

  • Saving works, but loading can be a little unreliable. So don’t do any real work with this version.

  • Sometimes your monitor might disappear, because of screensaver or some window blocking it. You usually can move the pane handles(?) to resize the monitor so it will be redrawn. (This is a bug with me not getting the expose event handled correctly with the xoverlay.)

  • You should propably avoid using multiple movies, or selecting the materials. So, just use that one movie, and test editing the scenes and scenechains with that. Selecting other movies will eventually lead to crashes, and before that you’ll propably not be able to select scenes and see them in the monitor anymore. (So, sometimes the gstreamer pads don’t get connected correctly. This is propably my fault, because of not fully understanding how to do stuff correctly.)

  • Changing the scenes order doesn’t work yet.

  • Editing the edits/clips inside the scene doesn’t work that great yet, so don’t do it.

  • Trimming the edits isn’t good, so forget about that too, for now.

 

A huge thanks to the freenode channels #d, #d.tango and #gstreamer for much appreciated help on various issues. Pihlaja reuses some code from the Pitivi project and heavily relies on Gnonlin for it’s functionality, so a big thank you goes out to bilboed.

 

Compiling from source

 

Umm. It might be hard. But here’s a list of what you need:

Get the Pihlaja 003 alpha source package from sourceforge. It will also include my personal branch of gtkD and gstreamerD, because I didn’t have the time to merge it with the official version found from www.dsource.org. Pihlaja only works on Linux. Some other Unixes might work too. I’m currently using Ubuntu 6.10 Dapper Drake (never had the time to backup my files and update).

  • You should have the usual GCC stuff installed. You also need GStreamer (and the plugins) and Gnonlin installed.

  • Get the DMD D compiler from www.digitalmars.com/d. I’m currently using version 1.014. To be sure, get that version. Don’t get the experimental 2.0 beta version. (You might also try GDC, but it appears there’s not yet a version that matches DMD 1.014. But it might work anyway… Haven’t tested it yet.)

  • Get DSSS from www.dsource.org. Try to find some installation instructions.

  • Get the Tango library from www.dsource.org. There’s propably a way to install Tango with DSSS too, so if you find that, you can use it. It will be easier. It has something to do with dsss net install tango, but I’ve never used it yet.

  • You also need the Tangobos library. You can propably install that with dsss net install, somehow.

  • In the dir where you’ve got the patched version of gtkD and gstreamerD, run command: dsss build. If that finishes without errors, and you’ve got libraries called libSgtkD.a and libSgstreamerD.a, then you can run the command: sudo dsss install.

  • In the Pihlaja directory called pihlaja/src/ again run command: dsss build. You don’t have to run install. After you’ve made some changes to the code, you should run dsss clean and after that dsss build to build it again. Run ../pihlaja to try it out.

  • The build process will be much more simpler after I’ll find the time to merge with the official branch of gtkD, which doesn’t need Tangobos. Hopefully it will be as easy as dsss net install pihlaja.

  • To get the latest Pihlaja source after this release you can do:

    svn co http://svn.dsource.org/projects/pihlaja

Some Pihlaja related websites (ideally there should be only one):
http://pihlaja.wordpress.org The Pihlaja development blog is the number one source of information regarding Pihlaja development and news.
http://www.pihlaja.org This is going to be a promotional site for using Pihlaja. Currently it just links to the old sourceforge pages.
https://sourceforge.net/project/showfiles.php?group_id=85992 The sourceforge page is currently used for binary releases, but that might change in the future.
http://www.dsource.org/projects/pihlaja Development wiki and subversion repository.
http://www.dsource.org/forums/viewforum.php?f=132 Pihlaja forums for all feedback and questions.

 

Not yet

19.06.2007

As some might have noticed, there’s been no release yet. Next month might also come too soon. We’ll see. To make a release I’d like two things: export working and the dynamic pads working reliably. Getting the monitor to be an XOverlay in the main window is optional but would make a huge plus. Pihlaja is quite crashy (if there can be such a word).

I’ve been in the Midnight Sun film festival in Sodankylä for the last week, and so haven’t been able to do much on Pihlaja. Saw some great movies like Jia Zhang-Ke: Still Life, Ghobadi: Half Moon and Elia Suleiman: Chronicle of Disappearance and Chronicle of Love and Pain.

Although I’ve coded the basis for exporting. It still doesn’t work for some reason though. As I said earlier I have huge issues with some dynamic pads not being created. I’ll have to try to ask someone about that stuff, as there’s really little documentation on problems.