1

I am a beginner in openGL. I am currently working on a program which take in inputs the width and the length of a board. Given those inputs i want to dynamically position my camera so that i can have a view on the whole board. Let' s say that my window size is 1024x768.

Are there any mathematical formula to compute the different parameters of the opengl function glookat to make it possible ?

the view i want to have on the board should look like this.

board image

It doesn't matter if a board too big will make things look tiny. What matters the most here is to position the camera in a way that the view on the whole board is made possible

So far i am hopelessly randomly changing the parameters of my glookat function till i ran into something decent for a X size width and and Y size Height.

my gluperpective function : gluPerspective(70 ,1024 / 768,1,1000)

my glooatfunction for a 40 * 40 board

gluLookAt(20, 20, 60, 20, -4, -20, 0, 1, 0);

how i draw my board (plane):

         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  glMatrixMode( GL_MODELVIEW );

  glLoadIdentity();
  gluLookAt(20, 20, 60, 20, -4, -20, 0, 1, 0);

  glBindTexture(GL_TEXTURE_2D, texture_sol);
  glBegin(GL_QUADS);
  glTexCoord2i(0, 0);  glVertex3i(width, 0, height);
  glTexCoord2i(10, 0);  glVertex3i(0, 0, height)
  glTexCoord2i(10, 10);  glVertex3i(0, 0, 0);
  glTexCoord2i(0, 10);  glVertex3i(width, 0, 0);
  glEnd();

the output looks as follow :

my board output image

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • What have you tried? Any specific problem that you face in trying something? – legends2k Apr 01 '14 at 21:52
  • So far I have been able to draw something and put a camera above. It kind of look decent. Problem is to make the rendering on the whole board i am hopelessly randomly changing the glookat parameters till i ran into something decent. Let me update my question with some snippet of my work. – user1040986 Apr 01 '14 at 22:03
  • [manual](http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml) shows each parameter do. To determine values try to visualize (eg on a sheet of paper) where the 'board' lies, and where the camera should be. eg pass the middle of the board to the `LookAt` as `center*` and an edge as `eye*` – Valerij Apr 01 '14 at 22:32

2 Answers2

0

Though this is written as an XNA tutorial, the basic technique and math behind it should carry over to OpenGL and your project:

Positioning the Camera to View All Scene Objects

Also see

OpenGL FAQ

8.070 How can I automatically calculate a view that displays my entire model? (I know the bounding sphere and up vector.)

Edit in response to the comment question

A bounding sphere is simply a sphere that completely encloses your model. It can be described as:

A bounding sphere, S, of a point set P with n points is described by a center point, c, and a radius, r.

So,

P = the vertices of your model (the board in this case)

c = origin of your model

r = distance from origin of the vertex, in P, farthest from the origin

So the Bounding Sphere for your board would be composed of the origin location (c) and the distance from one corner to the origin (r) assuming the board is a square and all points are equidistant.

For more complicated models, you may employ pre-created solutions [1] or implement your own calculations [2] [3]

Community
  • 1
  • 1
ssell
  • 6,429
  • 2
  • 34
  • 49
  • Do you (or anyone) have any suggestions on how can i compute a bounding sphere for the plane i have (with y constant, equals zero) ? – user1040986 Apr 01 '14 at 23:26
0

gluLookAt takes 2 points and a vector; the eye and centre positions and the up vector. There's no issue with the last parameter. The first two are relevant to your question.

I see that your board in the world space is extending on the positive X and Y axes with some arbitrary width and height values. Lets take width = height = 1.0 for instance. So the board spans from (0, 0), (1, 0), (1, 1), (0, 1); the Y value is ignored here since the board lies on the Y = 0 plane and have the same value for all vertices; these are just (X, Z) values.

Now coming to gluLookAt, eye is where the camera is in world space and centre is the point where you want the camera to be looking at (in world space)

Say you want the camera to look at centre of the board I presume, so

eye = (width / 2.0f, 0, height/2.0f);

Now you've to position the camera at its vantage point. Say somewhere above the board but towards the positive Z direction since there's where the user is (assuming your world space is right handed and positive Z direction is towards the viewer), so

centre = (width / 2.0f, 5.0f, 1.0f);

Since the farthest point on Z is 0, I just added one more to be slightly father than that. Y is how much above you want to see the board from, I just chose 5.0 as an example. These are just arbitrary values I can come up with, you'll still have to experiment with these values. But I hope you got the essence of how gluLookAt works.

legends2k
  • 31,634
  • 25
  • 118
  • 222