Click here to Skip to main content
15,867,141 members
Articles / Desktop Programming / MFC

Transformations Using OpenGL

Rate me:
Please Sign up or sign in to vote.
3.25/5 (8 votes)
1 May 2002CPOL2 min read 111K   3.6K   32   7
This article explains the viewing and modeling transformations.

Sample Image - Trans.jpg

Introduction

This article explains the two transformations - Viewing and Modeling.

The viewing transformation

The viewing transformation is analogous to positioning and aiming a camera. The viewing transformation is specified with gluLookAt(). The arguments for this command indicate where the camera (or eye position) is placed, where it is aimed, and which way is up. The arguments used here place the camera at (0, 0, 5), aim the camera lens towards (0, 0, 0), and specify the up-vector as (0, 1, 0). The up-vector defines a unique orientation for the camera.

In this code example, before the viewing transformation can be specified, the current matrix is set to the identity matrix with glLoadIdentity(). This step is necessary since most of the transformation commands multiply the current matrix by the specified matrix and then set the result to be the current matrix. If you don't clear the current matrix by loading it with the identity matrix, you continue to combine previous transformation matrices with the new one you supply. In some cases, you do want to perform such combinations, but you also need to clear the matrix sometimes.

The modeling transformation

You use the modeling transformation to position and orient the model. For example, you can rotate, translate, or scale the model - or perform some combination of these operations. glScalef() is the modeling transformation that is used. The arguments for this command specify how scaling should occur along the three axes. If all the arguments are 1.0, this command has no effect. In the demo application the polygon is drawn twice as large in the y direction. Thus, if one corner of the polygon had originally been at (3.0, 3.0, 3.0), that corner would wind up being drawn at (3.0, 6.0, 3.0).

My previous article on OpenGL - Getting started with OpenGL, showed the basic framework for drawing in a window.

The OnPaint() for the demonstration project of this article looks like this:

void OnPaint()
{
  //do this if u want to clear the area before showing an image
  this->Invalidate(TRUE);

  // Draw the scene.

  // Get a DC, then make the RC current and
  // associate with this DC
  hdc = ::BeginPaint(hwnd,&ps);

  wglMakeCurrent( hdc, hRC );

  //********Drawing Start****************

  // Define the modelview transformation.

  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();

  // move the viewpoint out to where we can see everything
  glTranslatef( 0.0f, 0.0f, -5.0f );

  //Specify the polygon vertices in an array
  static GLfloat vertices[] =  {
                 1.0,-0.5,0.5,
                 1.0,0.5,-1.0,
                 0.5,1.0,-1.0,
                 0.5,-1.0,1.0
                           };
  glEnableClientState(GL_VERTEX_ARRAY);//Enable use of array of vertices
  glColor3f(0.0,0.0,0.0);//black color
  glVertexPointer(3,GL_FLOAT,0,vertices);

  glPolygonMode(GL_FRONT,GL_LINE);//Front face as a lined plolygon
  glPolygonMode(GL_BACK,GL_FILL);//back face as a filled polygon
  glLoadIdentity ();/* clear the matrix */

    /* viewing transformation  */
     //camera position , where its pointed ,up direction    
     gluLookAt (vx1,vy1,vz1,vx2,vy2,vz2,vx3,vy3,vz3);

     /*modeling transformation Front Face*/
     glScalef((float)mx,(float)my,(float)mz);
     glBegin(GL_POLYGON);
     for(int i=0;i<4;i++)
       glArrayElement (i);//Plotting using array
     glEnd();

     glDisableClientState(GL_VERTEX_ARRAY);

     glFlush();

//********END**************************

    // we're done with the RC, so
    // deselect it
    // (note: This technique is not recommended!)
    wglMakeCurrent( NULL, NULL );

   ::EndPaint(hwnd,&ps);

}

About the demo application

The demo application allows you to specify the co-ordinates for the transformations and accordingly the image can be viewed. Just change the x co-ordinate to -1 in the modeling transformation dialog box to view the back face of the polygon.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
henry369525-Jul-10 21:42
henry369525-Jul-10 21:42 
QuestionHow to run 2 different(or same) OpenGL objects in one DialogBox? Pin
werter13-May-04 21:21
werter13-May-04 21:21 
QuestionHow do i convert the windows client coordinate to Opengl coordinates? Pin
User 21559717-Apr-03 5:57
User 21559717-Apr-03 5:57 
AnswerRe: How do i convert the windows client coordinate to Opengl coordinates? Pin
black tiger31-Oct-05 11:51
black tiger31-Oct-05 11:51 
void main()
{
auxInitDisplayMode(AUX_SINGLE||AUX_RGBA);
auxInitPosition(0,0,500,500);
auxInitWindow("Mouse");
auxMouseFunc(AUX_LEFTBUTTON,AUX_MOUSEDOWN,(AUXMOUSEPROC)Draw);//
auxMainLoop((AUXMAINPROC)Display);
}

void Draw(AUX_EVENTREC* event)//
{
GLint viewport[4];
int x = event->data[AUX_MOUSEX];
int y = event->data[AUX_MOUSEY];
glGetIntegerv(GL_VIEWPORT,viewport);
DrawPoint(x,viewport[3]-y);
}

awady
GeneralRe: How do i convert the windows client coordinate to Opengl coordinates? Pin
User 2155971-Nov-05 8:00
User 2155971-Nov-05 8:00 
QuestionWhy Win32 SDK? Pin
7-May-02 17:53
suss7-May-02 17:53 
AnswerRe: Why Win32 SDK? Pin
post2man14-Nov-02 1:55
post2man14-Nov-02 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.