Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Di Mai 14, 2024 22:49

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 7 Beiträge ] 
Autor Nachricht
BeitragVerfasst: Fr Jan 26, 2007 18:15 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jan 24, 2007 00:44
Beiträge: 144
Hi all,

This is one of those 'get me started please' questions I'm afraid. I'm a relatively experienced Delphi coder but just starting getting into OpenGL - I want to signficantly imrprove the user interface of my Media Centre. Up to now I have been using my spare time to bring myself up to speed with the 3D side of things... all my stuff is on a Delphi form, rendered onto a TPanel... by coding all the vertexes and normals and default lighting for a 3D "H" that I have put into the title bar of my custom forms - it adds a bit of eye-candy to my program.

Now I want to move on (surely going backwards?) to 2D stuff - Orthographic Projection - as that's simply all I need for the time being, I'm not interested in perspective, I just want to use OpenGL to provide my application with some very slick and polished graphics that the users can manipulate easily - my program's screen has CD covers all over it (see the following URL for an idea of what I'm referring to - http://www.hmusiccentre.org.uk) and I would like to imagine these CDs covers could quickly slide left, right, up and down (according to the position of the mouse) bringing new CD covers into view.

I've started a small project to set up Orthographic Projection... in the startup, I do:

Code:
  1. glClearColor (0,0,0,0);
  2. glMatrixMode (GL_PROJECTION);
  3. glLoadIdentity;
  4. glOrtho (0,10,0,10,-1,1);
Then, in my Draw, I do the following:

Code:
  1. glClear (GL_COLOR_BUFFER_BIT);
  2.  
  3. Square (4,4,2,2);
  4. Square (1,1,2,2);
  5. Square (7,7,2,2);
  6.  
  7. SwapBuffers (wglGetCurrentDC);
...and I get three white squares on screen as expected... the Square procedure just does:

Code:
  1. procedure TfrmMain.Square ( X, Y, Width, Height : GLFloat
  2.                           );
  3. begin
  4.   glBegin (GL_TRIANGLE_STRIP);
  5.     glVertex2f (X,Y);
  6.     glVertex2f (X,Y + Height);
  7.     glVertex2f (X + Width,Y);
  8.     glVertex2f (X + Width,Y + Height);
  9.   glEnd;
  10. end;
Nothing special at all... so I then wanted to start transforming things... I figured I would do a simple rotation of one of the squares. I figured that I would need to push and pop the matrix as I didn't want the other shapes to be affected by the transformations. I thought I would need a glLoadIdentity but that seems to cause things to not work at all. As I'm starting out with this stuff, it's pretty obvious I'm missing something obvious...

Code:
  1. procedure TfrmMain.Draw;
  2. begin
  3.   glClear (GL_COLOR_BUFFER_BIT);
  4.  
  5.   { Without push and pop matrix around this square is the same as having it. }
  6.   glPushMatrix;
  7.     { Removing both the next two lines keeps everything as normal - three squares.
  8.       Having just glLoadIdentity or both lines makes the middle square not be there.
  9.       Having just glRotate makes the middle square decrease and disappear. }
  10.     glLoadIdentity;
  11.     glRotate (Rotation,1,0,0);
  12.     Square (4,4,2,2);
  13.   glPopMatrix;
  14.  
  15.   Square (1,1,2,2);
  16.   Square (7,7,2,2);
  17.  
  18.   SwapBuffers (wglGetCurrentDC);
  19. end;
So I started hacking around - not knowing what I was doing... I eventually ended up changing the startup code to the following:

Code:
  1. glClearColor (0,0,0,0);
  2. glMatrixMode (GL_PROJECTION);
  3. glLoadIdentity;
  4. glOrtho (0,10,0,10,-1,1);
  5. glMatrixMode (GL_MODELVIEW); // Added!
And I also changed my Draw to:

Code:
  1. procedure TfrmMain.Draw;
  2. begin
  3.   glClear (GL_COLOR_BUFFER_BIT);
  4.  
  5.   glPushMatrix;
  6.     glLoadIdentity;
  7.     glRotate (Rotation,0,0,1);
  8.     Square (4,4,2,2);
  9.   glPopMatrix;
  10.  
  11.   Square (1,1,2,2);
  12.   Square (7,7,2,2);
  13.  
  14.   SwapBuffers (wglGetCurrentDC);
  15. end;
...so I'd changed by Rotate to use the Z and kept in the glLoadIdentity call and I get my middle square kind of doing what I want... in that it flies off in an arc and eventually comes back to where it started... So, now I've finally got a transformation working on just one of my drawn squares (leaving the others alone as intended)... but, I'll be honest and say that I don't fully understand why. This is so basic I feel quite bad asking this - but could someone possibly explain why it works now with the MODELVIEW and LoadIdentity, but didn't before..?

In addition, knowing how I can get my middle square to rotate around its centre - so it's spinning on a point, rather than spinning around a point - would be good to know...

Also... my next question... I'm working in 2D... I'm not interested in perspective (yet!) and I much prefer to work in units of pixels. How do I go about setting up OpenGL so that I can reference all my drawing on a TPanel (of any dimensions) by saying that I want to draw something at 100,100 pixels? Is that an easy situation to get to?

Sorry that my questions are not that interesting or 3D related... I'll get there. I can post the .dpr, .pas and .dfm is anyone needs it... nothing special at all.

Cheers, DpM
http://www.hmusiccentre.org.uk


Nach oben
 Profil  
Mit Zitat antworten  
BeitragVerfasst: Fr Jan 26, 2007 18:56 
Offline
Ernährungsberater
Benutzeravatar

Registriert: Sa Jan 01, 2005 17:11
Beiträge: 2067
Programmiersprache: C++
What do you meen with "decrease" when you talking about the middle square?
It will rotate around the x-axis which should look like its getting a rectancle and at last a line.
You have to use the z-axis to rotate the square like a paper on a table.

glLoadIdentity is not needed at all at this point. It is just resetting the matrix and is undoing all your rotations and transations.

For your second question:
Just type:
Code:
  1. glOrtho (0,Panel.Width,0,Panel.Height,-1,1);

_________________
Steppity,steppity,step,step,step! :twisted:
❆ ❄ ❄ ❄ ❅ ❄ ❆ ❄ ❅ ❄ ❅ ❄ ❅ ❄ ❄
❄ ❄ ❄ ❅ ❄ ❄ ❄ ❅ ❄ ❄ ❆ ❄ ❄


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Fr Jan 26, 2007 19:10 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jan 24, 2007 00:44
Beiträge: 144
Thank-you.

Yes, you're right - glLoadIdentity is not required. Having...

Code:
  1. procedure TfrmMain.Draw;
  2. begin
  3.   glClear (GL_COLOR_BUFFER_BIT);
  4.  
  5.   glPushMatrix;
  6.     glRotate (Rotation,0,0,1);
  7.     Square (4,4,2,2);
  8.   glPopMatrix;
  9.  
  10.   Square (1,1,2,2);
  11.   Square (7,7,2,2);
  12.  
  13.   SwapBuffers (wglGetCurrentDC);
  14. end;
...works fine - it all seems to be down to the call to glMatrixModel (GL_MODELVIEW).

Also, I changed the code to:

Code:
  1. glOrtho (0,pOGL.Width,0,pOGL.Height,-1,1);
...and that works very well - everything got a lot smaller - I guess I am now working in pixels without perspective, which is what I wanted. With my rotation on the Z axis I am still getting the square rotating in a wide arc around a point that I believe might be at the bottom-left of my panel, as opposed to rotating around its own centre point and not really moving - which is what I would prefer... what do I need to do to address that?

P.S. Answering the question of "decrease", I believe it was disappearing to a line, yes - but, once again, the rotation wasn't around the centre axis - it seemed to fall over... i.e. 'cos it wasn't 3D and there's no lighting, it appeared to decrease in height until it was 0 pixels high - but not equally moving in from the top and bottom - just moving down from the top.

_________________
Cheers, DpM
http://www.hmusiccentre.org.uk


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Fr Jan 26, 2007 19:25 
Offline
Ernährungsberater
Benutzeravatar

Registriert: Sa Jan 01, 2005 17:11
Beiträge: 2067
Programmiersprache: C++
glrotate is rotating around the current position.
If you don't use glTranslate the current position will be (0,0,0), the bottom left of the panel.
For Square (4,4,2,2) you have do the following:
Code:
  1.  
  2. glTranslatef(5,5,0); //move to the center
  3. glRotate (Rotation,1,0,0); //rotate
  4. Square(-1,-1,2,2); //draw the square, including modified position
  5. //alternativ:
  6. (*
  7. glTanslatef(-5,-5,0); //move back to origin
  8. Square(4,4,2,2);
  9. *)
  10.  


If you don't translate back to the old position you have to adjust the coordinates of the square. This is because the OpenGL functions are working with relativ coordinates, just like if you draw on canvas.

_________________
Steppity,steppity,step,step,step! :twisted:
❆ ❄ ❄ ❄ ❅ ❄ ❆ ❄ ❅ ❄ ❅ ❄ ❅ ❄ ❄
❄ ❄ ❄ ❅ ❄ ❄ ❄ ❅ ❄ ❄ ❆ ❄ ❄


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Nice one!
BeitragVerfasst: Fr Jan 26, 2007 19:29 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jan 24, 2007 00:44
Beiträge: 144
Sweet.

Code:
  1. procedure TfrmMain.Draw;
  2. begin
  3.   glClear (GL_COLOR_BUFFER_BIT);
  4.  
  5.   glPushMatrix;
  6.     glTranslate (5,5,0);
  7.     glRotate (Rotation,0,0,1);
  8.     Square (-1,-1,2,2);
  9.   glPopMatrix;
  10.  
  11.   Square (1,1,2,2);
  12.   Square (7,7,2,2);
  13.  
  14.   SwapBuffers (wglGetCurrentDC);
  15. end;
...works a treat - and I understand why. Thank you very much.

_________________
Cheers, DpM
http://www.hmusiccentre.org.uk


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Di Jan 30, 2007 23:26 
Offline
Guitar Hero
Benutzeravatar

Registriert: Do Sep 25, 2003 15:56
Beiträge: 7804
Wohnort: Sachsen - ERZ / C
Programmiersprache: Java (, Pascal)
You can simplify your code by leave out the position of the quad in your draw function.
You can set the position of the quad with glTranslate. Just make the draw funktion drawing the quad relativ to (0,0), than you can do something like that:

glTranslate(middleX, middleY, 0);
drawQuad(width, height);

Its nothing big, but with less parameters your code is maybe easier to interprete if you look on it in a few days. ;)

_________________
Blog: kevin-fleischer.de und fbaingermany.com


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Agreed.
BeitragVerfasst: Sa Feb 03, 2007 17:22 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jan 24, 2007 00:44
Beiträge: 144
Yes, this makes sense. Thank you.

_________________
Cheers, DpM
http://www.hmusiccentre.org.uk


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 7 Beiträge ] 
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 6 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.032s | 17 Queries | GZIP : On ]