PDA

View Full Version : The most difficut problem in OPENGL! (Try it)



Kinicky
06-17-2002, 02:08 AM
To make a triangle move on the middle of a window horizontally (not back and forth) and the triangle should slowly disappear on one side and appear on other side slowly. (My prof claim no one can slove this problem, but who knows, maybe u guys are genius, try to solve this problem for me, thanks in advance!)

Bob
06-17-2002, 03:10 AM
Is this some kind of trick question, or did i misunderstand it?



#include <GL/glut.h>

// --------------------------------------------------------------------------------
//
// --------------------------------------------------------------------------------
int viewportHeight;
int viewportWidth;

int trianglePos = 0;
int triangleSize = 120;

// --------------------------------------------------------------------------------
//
// --------------------------------------------------------------------------------
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();
glTranslatef(trianglePos, 0, 0);
glBegin(GL_TRIANGLES);
{
glVertex2i(0, viewportHeight / 2);
glVertex2i(triangleSize / 2, viewportHeight / 2 + triangleSize);
glVertex2i(triangleSize, viewportHeight / 2);
}
glEnd();

glTranslatef(-viewportWidth, 0, 0);
glBegin(GL_TRIANGLES);
{
glVertex2i(0, viewportHeight / 2);
glVertex2i(triangleSize / 2, viewportHeight / 2 + triangleSize);
glVertex2i(triangleSize, viewportHeight / 2);
}
glEnd();

glutSwapBuffers();
}

void reshape(int w, int h)
{
viewportWidth = w;
viewportHeight = h;

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void idle(void)
{
++trianglePos;
if(trianglePos > viewportWidth) trianglePos = 0;

glutPostRedisplay();
}

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutCreateWindow("scrolling triangle");

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);

glutMainLoop();

return 0;
}

coredump
06-17-2002, 04:44 AM
yea...that doesn't seem too hard. unless you want something like this:



+----------------------+
| |
| /\ |
|/ \ |
| \ /|
| \ / |
|______\ /__|
| |
+----------------------+


in this case, you'd need to check the position of each vertex of the triangle, and draw the lines separately.

b

dorbie
06-17-2002, 04:55 AM
Or draw two triangles when the bounds approaches the edge.

Either the question isn't phrased correctly or your professor is a moron.

I doubt you'd find a professor quite this dumb so maybe you should try and solve your exercise questions before posting and making your prof. sound like an idiot.

Gavin
06-17-2002, 06:56 AM
Or are you insinuating some sort of circular viewing plane?

Kinicky
06-17-2002, 08:30 AM
coredump, u got the point!exactly same as what i mean! the diffculty is how to do. what u siad si correct in theory, but how to put in code is big problem since i am still blur in OpenGL. Anyway, thanks for ur reply!

coredump
06-17-2002, 09:51 AM
the easiest way would be to do what dorbie suggested...when the triangle passes the right edge of the window, draw a second triangle coming in from the left side. when the triangle on the right is all the way past the window's edge, then just draw the second triangle. simple!

b

Gavin
06-17-2002, 02:37 PM
That is more of an emulation of a simple triangle case. The algrithm for n cases is a bit different, but by no means impossible. The idea of wrapping is the way to go rather than trying t odo a 'real' circular viewing plane. Althought thinking about it a circular plane should nt be too difficult.

dorbie
06-17-2002, 04:24 PM
An emulation? Give me a break. Now you're changing the rules. Talk about contriving to define a problem that doesn't exist. Any problem is tough if you tie someone's hands and exclude options after a trivial solution is presented. It's not an emulation. I could say most clipping hardware built is an emulation based on this kind of definition.

Go rasterize on a torroidal framebuffer with a guard band clip (that's the other simple solution) if you like. I'll stick with my first idea, it works on hardware. It's funny that this is posed as if it's a 'grand challenge' it's not, almost nobody wants to render like this, maybe someone writing an asteroids game, but that's about it.

Aha, you are writing an asteroids game aren't you. And you asked old prof a question he couldn't answer. This is almost funny now.


[This message has been edited by dorbie (edited 06-17-2002).]

tarantula
06-17-2002, 07:22 PM
you are looking for 360 degree fov baby!
render into four side by side viewports with x fov 90 degrees and rotating the scene 90 degree in each viewport http://www.opengl.org/discussion_boards/ubb/tongue.gif

dorbie
06-18-2002, 12:26 AM
Hmm... well if that's what you need, I didn't see it in the question but you've opened my eyes. It is possible. The polygon would actually be rotating.

It can be done, here's how:
http://patft.uspto .gov/netacgi/nph-Pars...orbie&RS=dorbie (http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=/netahtml/search-bool.html&r=1&f=G&l=50&co1=AND&d=ft00&s1=dorbie&OS=dorbie&RS=dorbie)

tarantula, your suggestion would have discontinuities, you need to distortion correct the individual projections.

[This message has been edited by dorbie (edited 06-18-2002).]

Kinicky
06-18-2002, 02:49 AM
Come on guys,Iím only able to scratch the surface of OpenGL.pls dun play the lip service for me, i have to submit it on firday, it's not a breezy tp put thise idea in code. My prof. is always yelling at me, "hi, when r u going ot pass up the assignment?"!

Gavin
06-18-2002, 02:58 AM
Yawn, yawn dorbie

rhmazza
06-18-2002, 05:17 AM
Sounds like a good problem to me. It's obviously not useful for 1 triangle, but for any set of primitives. And the beauty would be to solve the problem by having the same triangle, not two diff. triangles drawn. Tarantula's got the right idea with his suggestion. So rather than translating the triangle or whatever model, you'd rotate it around the camera.

Syslock
06-18-2002, 08:20 AM
What's so damn hard about it?

Render in ortho, use two triangles.

dorbie
06-18-2002, 08:29 AM
The question hasn't been completely expressed. It's still ambiguous. I have used the algorithm described on the link I gave for spherical dome displays but it is sufficiently general for a cylindrical mapping. As I have said the distortion correction must be solved for the general case (if that's even the question being posed). The simple double triangle solution also solves for the special case apparently posed in the question.


[This message has been edited by dorbie (edited 06-18-2002).]

dorbie
06-18-2002, 08:36 AM
Yea Syslock, I guess that's just too boring around here :-) Even in perspective it can be done by separating the copy as a function of depth. It seems clear rereading the posts that Kinicky is asking about the trivial case.

[This message has been edited by dorbie (edited 06-18-2002).]

Coconut
06-18-2002, 09:04 AM
BTW, is this one of those SGI patents bought by Microsoft few months ago, Angus?

dorbie
06-18-2002, 09:07 AM
Yes, I have never worked for Microsoft and I filed this patent when I worked at SGI. This patent is one of several assigned to Microsoft that I know of and I'm sure it's the tip of the iceberg. I couldn't mention it at the time but now the information is clearly in the public domain.

Be careful what you file, one day you may wake up and discover that your work is owned by the large predatory monopolist you filed to help your employer survive against.


[This message has been edited by dorbie (edited 06-18-2002).]

JML
06-18-2002, 10:47 PM
Another approach (don't know how useful it is because of possible texture size limitations) to fake/emulate/draw in the required way could be:
1. render the triangle to a (large) texture that approximates the screen size
2. set up the texture to be transparent in areas outside the triangle
3. set up the texture to repeat in s and t
4. draw a quad/tristrip to the screen with the texture, varying the texture coordinates in such a way that the triangle moves in the required direction.

Make sure the quad/tristrip covers the screen completely and also make sure sMax - sMin == 1 and tMax - tMin == 1.

HTH

Jean-Marc.

06-18-2002, 11:02 PM
Hi, I'm not an experienced programmer in OpenGL or even in the language, but couldn't you just clip the triangle and continue on the othere side of the screen but with the values mirrored?

rIO
06-19-2002, 01:48 AM
This question doesn't seem that easy to solve to me, it's obvious that rendering two triangle is a solution, but it isn't a generic solution, and even duplicating all the geometry isn't a good solution.

The point is this isn't really a 3d question (and must not be solved with 3d math), this behaviour is typical of software rendering, where exceeding the screen size doesn't mean exceeding the linear frame buffer, and the result is to see the SAME triangle showing on the other side of the screen, one pixel lower.
The ONLY way (IMHO) to simulate this behaviour of software rendering is to use a render to texture method.
And please, it isn't absolutely a dumb question, the idea to use two triangle is the worse way to solve this problem.

rIO.sK

harsman
06-19-2002, 01:55 AM
Why is using two triangles "the worst way to solve this problem"? It seems simple and elegant to me, especially compared to the complexity of rendering to texture. Just render two triangles spaced screenwidth units apart, translate gradually along x and when you've translated screenwidth units reset the scene. Continue ad infinitum. Or if you really want to see the pixels of a single triangle rendered, use a triangle texture and scroll that with a texture wrap mode of GL_REPEAT.

rIO
06-19-2002, 03:15 AM
Because with using the "two triangle method" u solve just this exact situation, that IMHO (maybe I'm wrong) it's just a sample of what the method needs to do.
Using the double geometry to solve a GENERIC "screen wrap" (?) it's obviously wrong.
I just interpreted the triangle question as an example, "How can we render a geometry that can wrap against screen borders ? Let's use a single triangle as an example."

Of course if the question was related ONLY to a single triangle implementing a render to texture is wrong, but I hope the professor just used the triangle as a sample.
Otherwise excuse me for the previous post, and freely say to the prof he is a dumb! http://www.opengl.org/discussion_boards/ubb/biggrin.gif

rIO.sK

rixed
06-19-2002, 04:23 AM
render in a bigger viewport then copy the buffer by hand to the result to screen :-)

(btw, I dont know what the result would be when you render in a viewport bigger than screen)

Batti
06-19-2002, 05:58 AM
I think he made fun of you and/or needed the code for himself.

dorbie
06-19-2002, 07:35 AM
Rio, if it is a 3D question then yes it is more difficult, but it is described about as badly as can be imagined in that case. The solution for the 3D problem (with a rotating triangle around the viewer) has also been posted.

Jeffry J Brickley
06-19-2002, 07:42 AM
I learned years ago not to accept challenges like this from someone with control over your grade. I accepted a similar challenge when I was caught goofing off after finishing my assignment, the prof said, "do the last assigment in the book to prove you are that fast, that should keep you busy for a WEEK." I told him I'd have it on his desk the next day, he laughed. I had it on his desk by the end of the day (worked through another class shhhhhhhhhhh), because he laughed at me. Turned out he got his job as instructer because he solved it in 2 full days work, about 24 man hours.... I did it in 6 man hours and had to walk carefully for the next 10 weeks till end of class....

Dorbie has the correct solution, though difficult to code. I could probably code up a hack that emulates cylindrical projection only, my mind hurts if I think spherical. http://www.opengl.org/discussion_boards/ubb/wink.gif

And dorbie is correct, almost any "feature" is an emulation. After all, are you really moving that texel in physical computer space? what is physical computer space and how did I fit inside both my tower system and my laptop? (ahhhh compression!! that is why my laptop is slower!!).

seriously though, most techniques are emulations, bump mapping doesn't "add" detail, it simulates detail, etc. Until we synthesize physical geometry from a computer program, it is all just pretty pictures, and if one implementation looks the same as the other, why is one wrong? I saw nowhere in the description that it demanded a spherical display buffer.

Though if I solved it, just to be annoying; I would probably render to a buffer larger than the screen area and wrap the overlapping portions of the buffer around the screen area. If the overlap area is the size of half the triangle's size no one would know. X movement would be based on a simulation of radial movement around a cylinder, at 359.0 it is mostly on the left side, part on the right; at 1.0 degrees it is mostly on the right partially on the left, doesn't matter which side of the buffer writes to the other with only one object, it masks easily.

you've emulated it based on changing your xy mapping, not on changing your view. This was the same way I wrote the old 2D sprite games. (egad!! I just admitted my age) Buffer the edges equal to half the size of your sprites, write your buffers back across the edge of the screen on each side. Your only error is when there are more than one object at the edges and you have to deal with which object is in front of the other.

The screen drawing is done by drawing the buffer to the screen not the geometry, either texture or draw pixels style (he didn't say it had to be optimized).

Just me...

[This message has been edited by Jeffry J Brickley (edited 06-19-2002).]

harsman
06-19-2002, 08:15 AM
Yeah, riO I agree, it isn't a general solution. But the more experience I get with programming, the more I value pragamtical solutions. From the Tao of programming 5.3:

A novice programmer was once assigned to code a simple financial package.

The novice worked furiously for many days, but when his Master reviewed his program, he discovered it contained a screen editor, a set of generalized graphics routines, and an
artificial intelligence interface, but not the slightest hint of anything financial.

When the Master asked about this, the novice became indignant.

"Don't be so impatient," he said, "I'll put in the financial stuff eventually."

http://www.opengl.org/discussion_boards/ubb/smile.gif Anyway I never thought I'de get to view patents filed by Angus on spherical projection from reading a thread with a homework question. The good stuff that comes from this forum never ceases to amaze me.

rhmazza
06-19-2002, 10:25 AM
Hey, Jeffry --
I'm wondering what the problem was you showed your professor up on...?

Jeffry J Brickley
06-19-2002, 10:35 AM
the problem? boring stuff, acounting software was where I started in '85. I don't recall the exact problem, I remember it involved multiple ledgers, reconciliations and some other stuff, for fun I added an investment predictor with some minor fuzzy logic. Really it was not that complicated design wise, just time consuming coding wise in Cobol or RPG II. I had already coded a 3.5GL pre-processor by 2/3 of the class (yup I am the perfect model for the example a few posts up). It really wasn't quite a 4GL but it meant I coded in a fraction of the time, that way I could goof off more or write games when no one was looking. Even wrote a full Visicalc compatable spreadsheet with RPG-II, which of course means I really do have brain damage. http://www.opengl.org/discussion_boards/ubb/wink.gif

Or the long and short of it. I had already changed the rules of the game, he still thought I was hand coding. http://www.opengl.org/discussion_boards/ubb/wink.gif http://www.opengl.org/discussion_boards/ubb/biggrin.gif

rhmazza
06-19-2002, 10:42 AM
Sweet! ... I love coding things professors say are impossible to do in such and such amount of time... heh.

Syslock
06-19-2002, 11:12 AM
JML,

Hey, that's a good one. http://www.opengl.org/discussion_boards/ubb/smile.gif

Throw up a large quad plane, and texture it with triangle.

Then animate the texture by scrolling the texture coordinates across.

Jeffry J Brickley
06-19-2002, 11:28 AM
I do like JML's solution best too. And since I regularly texture in 2Kx2K textures, I know it would work faster than mine. Depends on how tough the professor is about "drawing" the triangle.... It's the perfect cheat, you draw it once and texture it rapidly ever after. Knowing some of my professors, I probably would have done it my way if only because I am actually "drawing" it every frame; I always respect the letter of the law. Mine would probably be a 10th in frame rate from JML's. His method should easily pull 200fps on this machine I am on and that is assuming a 1Kx1K texture at 1024x768 rendering, it would still be lightning fast at 2Kx2K for rendering above 1024x768 mode on a Geforce3 or better.

john
06-19-2002, 05:26 PM
Hello,

the problem is ill-posed. Drawing more than one of anything _is_ a hack/emulation of the problem (and it won't give the correct answer, anyway).

I belive the original problem isn't "make the triangle appear on the other side of the screen", but rather, make a spherical projection.

c.f. http://www.cs.adelaide.edu.au/~john/proj.jpg

on the left is 'normal' projection. the proejction of a 3D point is the interesection of the vector through the point p1 and the optical centre of the camera c with the view-plane, v. There will always be such a point, provided c \E v. Since monitors/cameras/eyes are not infinitely large, then only points that project within a bounding rectangle are viewable. This is 'standard', planar projection.

The trick is to create a cylindrical or spherical projection. It's the same idea, but this time the view plane is actually a cylinder (or sphere). In this case, the projection of a point will generate TWO potential values on the view-cylinder; depending on what you want to do wtih the system, you might want to axe the value further away from the original point (p1' and p2' for p1 and p2, respectively). ALl that is required now is to construct a cylindrical to planar mapping to display on the montior, ie. a function that maps 0--2pi to 0--resolution. ALl of this can be mathematically expressed and is thus _not impossible_.

in conclusion;

- the problem is ill-posed
- drawing more than one copy of the geometry is a suitable answer for how the question was expressed (but I belive the original question was actually more complicated than described)
- it isn't difficult or mathematically impossible!

cheers,
John