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

Aktuelle Zeit: So Jun 16, 2024 09:02

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



Ein neues Thema erstellen Auf das Thema antworten  [ 7 Beiträge ] 
Autor Nachricht
BeitragVerfasst: So Mai 13, 2007 23:27 
Offline
DGL Member
Benutzeravatar

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

I have been making good progress in OpenGL recently, but there are a couple of basic things I think I am still missing. I want to work in a world of three dimensions, but sometimes I find that I want to remove some perspective considerations. This may sound crazy... but let me describe a situation I am in at the moment...

I draw an object (in this case it is an H made up of vertices and normals) in the middle of the screen (0,0) and I get the representation that I want... I can then rotate that as I wish and I'm happy with what I see. If I move that object somewhere else on the scene and apply the same rotations to it, what I see is vastly different... i.e. a smooth swaying from side to side of that H in the middle of the scene becomes a very skewed sway (very much pronounced to the spin right) when I place that object to the right side of the scene.

This is a selection of Hs sitting still... there are no real surprises to me here... as I move an object away from 0,0 it starts to have a different perspective and you can see more of the sides of the Hs towards the extreme left and right.

Bild

What kind of did surprise me though... if in my Draw procedure I applied a couple of rotations to the objects (the same rotations for them all on the X and Y)... then I would get vastly different appearing movements in the objects further away from 0,0. In the screenshot below you can see that the furthest right H has almost spun around to show its back sides, when the movement I was hoping for was exactly like the green H is doing in the centre...

Bild

What does this mean for me? Well, it means that I can't depend on my rotations looking as I would expect if I then decide to move the objects around on the scene... and that is kinda bugging me. I want to find a way to, in effect, 'flatten' the perspective, so that I can still work in 3D, but the perspective remains much closer to what it is at 0,0 that at the far edges of the scene.

Is there a way of going about this? This is the entirety of my code... it's pretty simple. I would be very interested if someone was able to educate me a little bit on this beginner type question... is there a way to 'flatten' perspective projection so that it plays less of a part in the scene, but I still get my 3D..? Or... can I get to a situation where my rotations have much the same effect on all 9 Hs in the scene, i.e. they all more or less follow the green H in the middle?

If I'm even making sense of course.

Code:
  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   OpenGL, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls, ComCtrls;
  8.  
  9. type
  10.   TfrmMain = class(TForm)
  11.     Timer1 : TTimer;
  12.     pOGL: TPanel;
  13.     procedure FormCreate (Sender : TObject);
  14.     procedure Timer1Timer (Sender : TObject);
  15.   private
  16.     procedure SetOrthographicProjection;
  17.     procedure SetPerspectiveProjection;
  18.     procedure BuildList;
  19.     procedure Draw;
  20.   public
  21.   end;
  22.  
  23. var
  24.   frmMain : TfrmMain;
  25.   HCallList : GLUInt;
  26.  
  27.  
  28. implementation
  29.  
  30.  
  31. {$R *.DFM}
  32.  
  33.  
  34. procedure TfrmMain.BuildList;
  35. var
  36.   Width, Height, Thick : GLFloat;
  37. begin
  38.   HCallList := glGenLists (1);
  39.  
  40.   Height := 1;
  41.   Width := 0.50;
  42.   Thick := 0.13;
  43.  
  44.   glNewList (HCallList,GL_COMPILE);
  45.     glBegin (GL_QUADS);
  46.       { 4 cross bars. }
  47.       { Front... }
  48.       glNormal3f (0,0,1);
  49.       glVertex3f (-Width,Thick,Thick);
  50.       glVertex3f (-Width,-Thick,Thick);
  51.       glVertex3f (Width,-Thick,Thick);
  52.       glVertex3f (Width,Thick,Thick);
  53.  
  54.       { Back... }
  55.       glNormal3f (0,0,-1);
  56.       glVertex3f (-Width,Thick,-Thick);
  57.       glVertex3f (-Width,-Thick,-Thick);
  58.       glVertex3f (Width,-Thick,-Thick);
  59.       glVertex3f (Width,Thick,-Thick);
  60.  
  61.       { Top... }
  62.       glNormal3f (0,1,0);
  63.       glVertex3f (-Width,Thick,Thick);
  64.       glVertex3f (-Width,Thick,-Thick);
  65.       glVertex3f (Width,Thick,-Thick);
  66.       glVertex3f (Width,Thick,Thick);
  67.  
  68.       { Bottom... }
  69.       glNormal3f (0,-1,0);
  70.       glVertex3f (-Width,-Thick,Thick);
  71.       glVertex3f (-Width,-Thick,-Thick);
  72.       glVertex3f (Width,-Thick,-Thick);
  73.       glVertex3f (Width,-Thick,Thick);
  74.  
  75.       { 4 left stand-ups. }
  76.       { Inner... }
  77.       glNormal3f (1,0,0);
  78.       glVertex3f (-Width,-Height,Thick);
  79.       glVertex3f (-Width,-Height,-Thick);
  80.       glVertex3f (-Width,Height,-Thick);
  81.       glVertex3f (-Width,Height,Thick);
  82.  
  83.       { Outer... }
  84.       glNormal3f (-1,0,0);
  85.       glVertex3f (-(Width + (Thick * 2)),-Height,Thick);
  86.       glVertex3f (-(Width + (Thick * 2)),-Height,-Thick);
  87.       glVertex3f (-(Width + (Thick * 2)),Height,-Thick);
  88.       glVertex3f (-(Width + (Thick * 2)),Height,Thick);
  89.  
  90.       { Back... }
  91.       glNormal3f (0,0,-1);
  92.       glVertex3f (-(Width + (Thick * 2)),-Height,-Thick);
  93.       glVertex3f (-(Width + (Thick * 2)),Height,-Thick);
  94.       glVertex3f (-Width,Height,-Thick);
  95.       glVertex3f (-Width,-Height,-Thick);
  96.  
  97.       { Front... }
  98.       glNormal3f (0,0,1);
  99.       glVertex3f (-(Width + (Thick * 2)),-Height,Thick);
  100.       glVertex3f (-(Width + (Thick * 2)),Height,Thick);
  101.       glVertex3f (-Width,Height,Thick);
  102.       glVertex3f (-Width,-Height,Thick);
  103.  
  104.       { 4 right stand-ups. }
  105.       { Inner... }
  106.       glNormal3f (-1,0,0);
  107.       glVertex3f (Width,-Height,Thick);
  108.       glVertex3f (Width,-Height,-Thick);
  109.       glVertex3f (Width,Height,-Thick);
  110.       glVertex3f (Width,Height,Thick);
  111.  
  112.       { Outer... }
  113.       glNormal3f (1,0,0);
  114.       glVertex3f (Width + (Thick * 2),-Height,Thick);
  115.       glVertex3f (Width + (Thick * 2),-Height,-Thick);
  116.       glVertex3f (Width + (Thick * 2),Height,-Thick);
  117.       glVertex3f (Width + (Thick * 2),Height,Thick);
  118.  
  119.       { Back... }
  120.       glNormal3f (0,0,-1);
  121.       glVertex3f (Width + (Thick * 2),-Height,-Thick);
  122.       glVertex3f (Width + (Thick * 2),Height,-Thick);
  123.       glVertex3f (Width,Height,-Thick);
  124.       glVertex3f (Width,-Height,-Thick);
  125.  
  126.       { Front... }
  127.       glNormal3f (0,0,1);
  128.       glVertex3f (Width + (Thick * 2),-Height,Thick);
  129.       glVertex3f (Width + (Thick * 2),Height,Thick);
  130.       glVertex3f (Width,Height,Thick);
  131.       glVertex3f (Width,-Height,Thick);
  132.  
  133.       { 4 ends... }
  134.       { Left-bottom... }
  135.       glNormal3f (0,-1,0);
  136.       glVertex3f (-(Width + (Thick * 2)),-Height,Thick);
  137.       glVertex3f (-(Width + (Thick * 2)),-Height,-Thick);
  138.       glVertex3f (-Width,-Height,-Thick);
  139.       glVertex3f (-Width,-Height,Thick);
  140.  
  141.       { Left-top... }
  142.       glNormal3f (0,1,0);
  143.       glVertex3f (-(Width + (Thick * 2)),Height,Thick);
  144.       glVertex3f (-(Width + (Thick * 2)),Height,-Thick);
  145.       glVertex3f (-Width,Height,-Thick);
  146.       glVertex3f (-Width,Height,Thick);
  147.  
  148.       { Right-bottom... }
  149.       glNormal3f (0,-1,0);
  150.       glVertex3f (Width + (Thick * 2),-Height,Thick);
  151.       glVertex3f (Width + (Thick * 2),-Height,-Thick);
  152.       glVertex3f (Width,-Height,-Thick);
  153.       glVertex3f (Width,-Height,Thick);
  154.  
  155.       { Right-top... }
  156.       glNormal3f (0,1,0);
  157.       glVertex3f (Width + (Thick * 2),Height,Thick);
  158.       glVertex3f (Width + (Thick * 2),Height,-Thick);
  159.       glVertex3f (Width,Height,-Thick);
  160.       glVertex3f (Width,Height,Thick);
  161.     glEnd;
  162.   glEndList;
  163. end;
  164.  
  165.  
  166. procedure SetupPixelFormat ( DC : HDC
  167.                            );
  168. const
  169.   PFD : TPIXELFORMATDESCRIPTOR = (
  170.         nSize : SizeOf (TPIXELFORMATDESCRIPTOR);
  171.         nVersion : 1; dwFlags : PFD_SUPPORT_OPENGL or PFD_DRAW_TO_WINDOW or PFD_DOUBLEBUFFER;
  172.         iPixelType : PFD_TYPE_RGBA; cColorBits : 24; cRedBits : 0; cRedShift : 0;
  173.         cGreenBits : 0; cGreenShift : 0; cBlueBits : 0; cBlueShift : 0; cAlphaBits : 0;
  174.         cAlphaShift : 0; cAccumBits : 0; cAccumRedBits : 0; cAccumGreenBits : 0;
  175.         cAccumBlueBits : 0; cAccumAlphaBits : 0; cDepthBits : 16; cStencilBits : 0;
  176.         cAuxBuffers : 0; iLayerType : PFD_MAIN_PLANE; bReserved : 0; dwLayerMask : 0;
  177.         dwVisibleMask : 0; dwDamageMask : 0);
  178. var
  179.   PixelFormat : Integer;
  180. begin
  181.   PixelFormat := ChoosePixelFormat (DC,@PFD);
  182.   if (PixelFormat = 0) then
  183.   begin
  184.     Exit;
  185.   end;
  186.  
  187.   if (SetPixelFormat (DC,PixelFormat,@PFD) <> True) then
  188.   begin
  189.     Exit;
  190.   end;
  191. end;
  192.  
  193.  
  194. procedure TfrmMain.FormCreate ( Sender : TObject
  195.                               );
  196. var
  197.   DC : HDC;
  198.   RC : HGLRC;
  199. begin
  200.   DC := GetDC (pOGL.Handle);
  201.   SetupPixelFormat (DC);
  202.   RC := wglCreateContext (DC);
  203.   wglMakeCurrent (DC,RC);
  204.  
  205.   BuildList;
  206.  
  207.   { Quick and dirty lighting... }
  208.   glEnable (GL_LIGHT0);
  209.     glEnable (GL_LIGHTING);
  210.   glEnable (GL_COLOR_MATERIAL);
  211.  
  212.   glDepthFunc (GL_LEQUAL);
  213.   glEnable (GL_DEPTH_TEST);
  214.  
  215.   SetPerspectiveProjection;
  216. end;
  217.  
  218.  
  219. procedure TfrmMain.Draw;
  220. var
  221.   Loop : Integer;
  222.   X, Y : GLFloat;
  223. begin
  224.   glClearColor (0.905,0.470,0.003,1);
  225.   glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  226.  
  227.   { Calculate rotations... }
  228.   X := Sin (GetTickCount / 500) * 60;
  229.   Y := Sin (GetTickCount / 600) * 30;
  230.  
  231.   Loop := -8;
  232.   while Loop < 10 do
  233.   begin
  234.     glLoadIdentity;
  235.     if (Loop = 0) then
  236.     begin
  237.       { This is the one we're happy with... }
  238.       glColor3f (0,1,0);
  239.     end else
  240.     begin
  241.       { These we are less happy with. }
  242.       glColor3f (1,1,0);
  243.     end;
  244.     glTranslatef (Loop,0,-10);
  245.     glRotatef (X,0,1,0);
  246.     glRotatef (Y,1,0,0);
  247.     { Draw the H... }
  248.     glCallList (HCallList);
  249.     Inc (Loop,2);
  250.   end;
  251.  
  252.   SwapBuffers (wglGetCurrentDC);
  253. end;
  254.  
  255.  
  256. procedure TfrmMain.Timer1Timer ( Sender : TObject
  257.                                );
  258. begin
  259.   Draw;
  260. end;
  261.  
  262.  
  263. procedure TfrmMain.SetOrthographicProjection;
  264. begin
  265.   glMatrixMode (GL_PROJECTION);
  266.   gluOrtho2D (0,ClientWidth,0,ClientHeight);
  267.   glMatrixMode (GL_MODELVIEW);
  268.   glLoadIdentity;
  269. end;
  270.  
  271.  
  272. procedure TfrmMain.SetPerspectiveProjection;
  273. begin
  274.   glMatrixMode (GL_PROJECTION);
  275.   gluPerspective (45,ClientWidth / ClientHeight,1,100);
  276.   glMatrixMode (GL_MODELVIEW);
  277.   glLoadIdentity;
  278. end;
  279.  
  280.  
  281. end.

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


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mo Mai 14, 2007 08:49 
Offline
DGL Member

Registriert: Mi Jan 31, 2007 09:02
Beiträge: 28
heyas dpm,
I am not 100% sure but I think what you are looking for is the orthogonal "mode" of OpenGL. With it I think you'll get the exact results you are looking for. SO there actually two possibilities here:
1. You decrease the FOV angle, which flattens the perspective. (Make sure to move the camera position away from your H's while decreasing the FOV), and
2. you use glOrtho to setup an orthogonal view of your 3D space. It is actually quite easy to setup, just check http://wiki.delphigl.com/index.php/glOrtho. If this doesnt help enough, there are plenty tutorials out there. I dunno if there is one in this wiki.
If you have problems implimenting the ortho mode post again and you will be helped ; )
Anyhow I hope this is what you want
regards Woltan


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mo Mai 14, 2007 09:09 
Offline
DGL Member
Benutzeravatar

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

In the code, I have both a SetPerspectiveProjection and SetOrthographicProjection procedure. I had tried replacing the single line in my FormCreate that set the perspective projection with a call to the procedure that set orthographic, but I got nothing then... maybe there is something awry with my procedure? If this is the best way of achieving what I want to achieve, I would appreciate being assisted in understanding how to get that working as well - I have used orthographic projection before... but always for 2D stuff.

However, then I had a good attempt with your second point... and I have had more success with this I think. I have changed the values quite drastically though and I'm hoping that this doesn't cause any future problems - not sure if it even could.

So, my original call to gluPerspective was: gluPerspective (45,ClientWidth / ClientHeight,1,100); and this has now changed to gluPerspective (1,ClientWidth / ClientHeight,1,400); - radically decreasing the FOV value and increasing the Far value.

Then, in my Draw procedure, I have changed the translation Z value from glTranslatef (Loop,0,-10); to glTranslatef (Loop,0,-399); and this has the following effect... which I am actually very happy with:

Bild

As you can see, all the Hs are now swaying in unison, dancing to the same song if you like! I will try to implement this in my main program and see if I have any problems. Thank you for the assistance.

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


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Success!
BeitragVerfasst: Mo Mai 14, 2007 09:45 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jan 24, 2007 00:44
Beiträge: 144
Integrated into my main project, this looks OK... I get the right sway on the objects even though they are at the far bottom and far right of the scene, as illustrated here, the H you see below is at the end of its swing and it is perfect - before, when the H was placed at the bottom-right of the scene, at the end of its swing, it was almost facing backwards.

Bild

And here, for Volume (you can see I'm not a graphics artist!):

Bild

I do think I'm seeing some object distortion though (might be my eyes!) - on the H especially, where the cross-bar meets the two vertical bars, I get a few different coloured pixels showing during the rotation sequence. I didn't think I had this when I was using my previous FOV (45) and Far (100) and Z translate (-16) figures... in my main project I am now using FOV = 1, Far = 600 and Z translate -599 respectively.

Is it possible that I am gaining something (perfect rotation motion) and losing something (detail?) by using this, or is it in my head? For this reason, I would certainly like to know more about the discussion on setting up an orthogonal view of my 3D space.

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


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mo Mai 14, 2007 10:52 
Offline
Guitar Hero
Benutzeravatar

Registriert: Do Sep 25, 2003 15:56
Beiträge: 7804
Wohnort: Sachsen - ERZ / C
Programmiersprache: Java (, Pascal)
If I understand your concept you try to use 3D symbols for your mediaplayer? If this works like discriped above its fine. If you face problems in the future you can try this:

1. Place the object whereever you want.
2. Calculate the start angle depending on the position
3. rotate the objekt till you are back at the start angle.

With this procedure you don't need to change the projection/FOV.

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


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Viewport...
BeitragVerfasst: Mo Mai 14, 2007 12:36 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jan 24, 2007 00:44
Beiträge: 144
I have approached this yet another way... without changing any of my translate or rotate lines, I have set up a viewport that is a quarter the size of the entire scene and located it down at the bottom right-hand corner... then I just called the same code and ended up with a swaying H that has the same perspective (seemingly to my eyes) as the one that is in the middle of the screen.

This means I can keep my rotations the same, and my translations.

Code:
  1.   glViewport (0,0,pOGL.Width,pOGL.Height);
  2.   glLoadIdentity;
  3.   glColor3f (0,1,0);
  4.   glTranslatef (0,0,-3.2);
  5.   glRotatef (X,0,1,0);
  6.   glRotatef (Y,1,0,0);
  7.   { Draw the H... }
  8.   glCallList (HCallList);
  9.  
  10.   glViewport (pOGL.Width - (pOGL.Width div 4),0,pOGL.Width div 4,pOGL.Height div 4);
  11.   glLoadIdentity;
  12.   glColor3f (0,1,0);
  13.   glTranslatef (0,0,-3.2);
  14.   glRotatef (X,0,1,0);
  15.   glRotatef (Y,1,0,0);
  16.   { Draw the H... }
  17.   glCallList (HCallList);
  18.  

As illustrated...

Bild

This looks good to me; I am going to try and take it out of the sandbox I am working in and integrate it into my main project.

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


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Finish!
BeitragVerfasst: Mo Mai 14, 2007 12:59 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jan 24, 2007 00:44
Beiträge: 144
To close this off - this viewport thing works very well for me, I defined a viewport 1/5th of the screen width and height dimensions and placed it at the bottom-right, then rendered my HMusicCentre indicators down there (did a bit of playing with the Z, but left both X and Y at 0) - they have the right sway that I want and have the right perspective too. After that, I just re-define the viewport to be the full area and everything works very nicely.

I've learned a bit doing this. Good fun and thanks for the pointers.

_________________
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 9 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.076s | 17 Queries | GZIP : On ]