Showing posts with label computer graphics. Show all posts
Showing posts with label computer graphics. Show all posts

Monday, May 24, 2010

OpenGL minimalist program

Sometime I need a minimalist program to start with for doing some experiment in OpenGL using c++. I always loose time searching or writing such minimalist program. So I post one here.



#include <GL/glut.h>

int win_w = 800;
int win_h = 600;

void resize(int w, int h)
{
  win_w = w; win_h = h;
  glViewport(0,0, win_w, win_h);
}

void draw(void)
{
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(80, win_w / (float)win_h, 1.0, 100.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0,0,-2);
  glutSolidTeapot(1.0f);
  glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
  glutPostRedisplay ();
}

int main(int argc, char **argv)
{
  /* Set window size and location */
  glutInit(&argc, argv);
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(0, 0);

  /* Select type of Display mode:
  double buffer & RGBA color */
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

  /*Initialize GLUT state */
  glutCreateWindow("OpenGL Tea Pot");
  glutDisplayFunc( draw );
  glutReshapeFunc( resize );
  glutKeyboardFunc ( keyboard );
  glutMainLoop();

  return 0;
}

And to compile it:

g++ main.cc -lGL -lGLU -lglut -o main

Thursday, May 28, 2009

Trackball view rotation

trackball illustration

Introduction

Often we would like to visualize a 3d model from different view angles. Trackball is rotation technique that allow the user to intuitively change the view angle by using the mouse. The mouse behaves like a virtual trackball. Let's try to understand how trackball view rotation work exactly. This could be useful, as a programmer, if you want to implement this rotation technique in your application. Or if your are an artist it could be interesting to know what's happen under the stage.

Principle

The mouse moves on the screen which is a 2D plane. However a trackball could rotate in any direction in a 3D space. The problem here is to match the planar mouse movement to a 3D rotation. The idea to solve this problem is to project the 2D coordinate of the mouse cursor on a virtual sphere behind the screen, thus we obtained 3D point (blue on the picture). The difference between this 3D point and the center of the virtual sphere (red on the picture) is a 3D vector. The movement of this 3D vector while dragging the mouse is a rotation around the sphere center. Here we are! We use that rotation to rotate the view.

Transformation matrices

"Unfortunately, no one can be told what the Matrix is. You have to see it for yourself." Morpheus, The Matrix - 1999

Introduction

Matrices in 3D applications are very useful tools. Translation, scaling and rotation in any combination are simplified by usage of matrices. Let's expose the most important statements:

  1. Any transformation in space can be describe by a matrix
  2. Transforming a point in space is done by multiplying a transform matrix by the column vector describing the coordinates of the point
  3. Several successive transformations can be resumed as one matrix

The maths

So if you want to perform a transformation on a point, all you have to do is to select the appropriate matrix (see below) plug in the parameter values (position, scaling factor, angle, or rotation axis) and multiply it with the column vector of the point coordinate.

Let's detail this, mathematically. The transformation can be written as:

v' = Mv

Where:
v a column vector describing the coordinate of the point (x, y, z, w) w = 1 most of the time
M a 4-by-4 matrix describing the transformation
v' a column vector describing the coordinate of the point after the transformation (x', y', z', w')

According the definition of matrix multiplication, we obtain

x'       a  b  c  d   x      ax + by + cz + dw
y'       e  f  g  h   y      ex + fy + gz + hw
z'   =   i  j  k  l . z   =  ix + jy + kz + lw
w'       m  n  o  p   w      mx + ny + oz + pw

Now, about the statement 3, it is again related to matrix multiplication. The result of 4-by-4 matrices multiplication is also a 4-by-4 matrix and it still a transformation.

And matrix multiplication is not commutative (AB is not BA). That is a good for us, it means that we can also describe the order of the transformations. Let's say we have point v and we would to transform it by a rotation R followed by a translation T.

v' = RTv

One remark here, we noticed that if we read from left to right, the rotation appear first. But that doesn't means that the rotation is perform first. The order of transformation is readed from right to left.

Another magical thing, suppose we have a complete 3D object. Transforming a 3D object is transforming all of its points. Instead of performing the rotation, follow by the translation to each point. We compute, once for all, the matrix that describe the combination of these two transformations, let's call it M. And then, multiply M by each point.

M = RT
for each point: v' = Mv

It does not matter how many transformations is performing, M will always describe them all. Magic! isn't it ?

Here is a list of of basic transformation matrices: translation, scaling, and rotation.

Translation matrix

X, Y, Z are the components of the translation vector.
1  0  0  X
0  1  0  Y
0  0  1  Z
0  0  0  1

Scaling matrix

X, Y, Z are scales factors along their respective axis.
X  0  0  0
0  Y  0  0
0  0  Z  0
0  0  0  1

Rotation around the x axis by an angle a

1   0   0   0
0   c  -s   0
0   s   c   0
0   0   0   1

Where c = cos(a) and s = sin(a)

Rotation around the y axis by an angle a

 c   0   s   0
 0   1   0   0
-s   0   c   0
 0   0   0   1

Where c = cos(a) and s = sin(a)

Rotation around the z axis by an angle a

c  -s   0   0
s   c   0   0
0   0   1   0
0   0   0   1

Where c = cos(a) and s = sin(a)

Rotation around an arbitrary axis by an angle a

x, y, z are the components of a unit vector that represent the rotation axis.

t*x*x + c        t*x*y - z*s     t*x*z + y*s     0
t*x*y + z*s      t*y*y + c       t*y*z - x*s     0
t*x*z - y*s      t*y*z + x*s     t*z*z + c       0
0                0               0               1

Where: c = cos(a), s = sin(a), and t = 1 - c