Main Page Namespace List Class Hierarchy Alphabetical List Data Structures File List Namespace Members Data Fields Globals

# GlView.cpp

Go to the documentation of this file.

    00001 // GlView.cpp : implementation file
    00002 //
    00003
    00004 #include "stdafx.h"
    00005 #include "SDKInterface.h"
    00006 #include "GlView.h"
    00007
    00008
    00009 // CGlView
    00010
    00011 IMPLEMENT_DYNAMIC(CGlView, CWnd)
    00012 CGlView::CGlView()
    00013 {
    00014 }
    00015
    00016 CGlView::CGlView(CWnd *pWnd)
    00017 {
    00018         m_pWnd  = pWnd;
    00019     m_hWnd      = pWnd->m_hWnd;
    00020     m_hDC   = ::GetDC(m_hWnd);
    00021         m_fZoom = -4.0f;
    00022         m_fAngleX = 0.0f;
    00023         m_fAngleY = 0.0f;
    00024         m_TextureCounter=0;
    00025 }
    00026
    00027 CGlView::~CGlView()
    00028 {
    00029 }
    00030
    00031 BOOL CGlView::SetPixelformat(HDC hdc)
    00032 {
    00033
    00034     PIXELFORMATDESCRIPTOR *ppfd;
    00035     int pixelformat;
    00036
    00037     PIXELFORMATDESCRIPTOR pfd = {
    00038     sizeof(PIXELFORMATDESCRIPTOR),  //  size of this pfd
    00039     1,                     // version number
    00040     PFD_DRAW_TO_WINDOW |   // support window
    00041     PFD_SUPPORT_OPENGL |   // support OpenGL
    00042     PFD_GENERIC_FORMAT |
    00043     PFD_DOUBLEBUFFER,      // double buffered
    00044     PFD_TYPE_RGBA,         // RGBA type
    00045     32,                    // 24-bit color depth
    00046     0, 0, 0, 0, 0, 0,      // color bits ignored
    00047     8,                     // no alpha buffer
    00048     0,                     // shift bit ignored
    00049     8,                     // no accumulation buffer
    00050     0, 0, 0, 0,            // accum bits ignored
    00051     64,                    // 32-bit z-buffer
    00052     8,                     // no stencil buffer
    00053     8,                     // no auxiliary buffer
    00054     PFD_MAIN_PLANE,        // main layer
    00055     0,                     // reserved
    00056     0, 0, 0                // layer masks ignored
    00057     };
    00058
    00059
    00060     ppfd = &pfd;
    00061
    00062
    00063     if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
    00064     {
    00065         ::MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
    00066         return FALSE;
    00067     }
    00068
    00069     if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
    00070     {
    00071         ::MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
    00072         return FALSE;
    00073     }
    00074
    00075     return TRUE;
    00076
    00077 }
    00078
    00079
    00080 GLvoid CGlView::ReSizeGLScene(GLsizei width, GLsizei height)    // Resize And Initialize The GL Window
    00081 {
    00082
    00083         if (height==0)                                                                          // Prevent A Divide By Zero By
    00084         {
    00085                 height=1;                                                                               // Making Height Equal One
    00086         }
    00087
    00088         glViewport(0,0,width,height);   // Reset The Current Viewport
    00089
    00090         glMatrixMode(GL_PROJECTION);            // Select The Projection Matrix
    00091         glLoadIdentity();                                       // Reset The Projection Matrix
    00092
    00093         // Calculate The Aspect Ratio Of The Window
    00094         gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    00095
    00096         glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
    00097         glLoadIdentity();                                                                       // Reset The Modelview Matrix
    00098
    00099 }
    00100
    00101
    00102 int CGlView::InitGL(GLvoid)                                                                             // All Setup For OpenGL Goes Here
    00103 {
    00104         m_hDC = ::GetDC(this->m_hWnd);
    00105
    00106     if(!SetPixelformat(m_hDC))
    00107     {
    00108         ::MessageBox(::GetFocus(),"SetPixelformat Failed!","Error",MB_OK);
    00109         return -1;
    00110     }
    00111
    00112     m_hRC = wglCreateContext(m_hDC);
    00113     int i= wglMakeCurrent(m_hDC,m_hRC);
    00114
    00115         glShadeModel(GL_SMOOTH);                                                        // Enable Smooth Shading
    00116         glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                           // Black Background
    00117         glClearDepth(1.0f);                                                                     // Depth Buffer Setup
    00118         glDepthFunc(GL_LEQUAL);                                                         // The Type Of Depth Testing To Do
    00119         glCullFace(GL_BACK);
    00120         glEnable(GL_CULL_FACE);
    00121         //glEnable(GL_LIGHTING);
    00122         glEnable(GL_COLOR_MATERIAL );
    00123         glEnable(GL_DEPTH_TEST);
    00124         glEnable(GL_AUTO_NORMAL);
    00125         glEnable(GL_NORMALIZE);
    00126         glEnable(GL_ALPHA_TEST);
    00127         glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);
    00128
    00129         m_SizeX=1;
    00130         m_SizeY=1;
    00131
    00132
    00133 //      LoadTexture("c:/cygwin/home/data/data/textures/car.bmp",0);
    00134 //      LoadTexture("c:/cygwin/home/data/data/textures/carmask.bmp",1);
    00135
    00136         return TRUE;                                                                            // Initialization Went OK
    00137 }
    00138
    00139
    00140 int CGlView::DrawGLScene(GLvoid)                                        // Here's Where We Do All The Drawing
    00141 {
    00142         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear Screen And Depth Buffer
    00143         glLoadIdentity();                                                                       // Reset The Current Modelview Matrix
    00144         glTranslatef(0.0f,0.0f,m_fZoom);                                                // Move Left 1.5 Units And Into The Screen 6.0
    00145         glRotatef(m_fAngleX,1.0,0.0,0.0);
    00146         glRotatef(m_fAngleY,0.0,1.0,0.0);
    00147         //---------------------------------
    00148         //Draw Axis
    00149         //---------------------------------
    00150         float i;
    00151         glDisable(GL_LIGHTING);
    00152         glBegin(GL_LINES);
    00153                 glColor3f(1.0f,0.0f,0.0f);
    00154                 for ( i=-100;i<101;i+=2)
    00155                 {
    00156                         glVertex3f(0.0f,      i, 100.0f);
    00157                         glVertex3f(0.0f,      i,-100.0f);
    00158                         glVertex3f(0.0f, 100.0f,      i);
    00159                         glVertex3f(0.0f,-100.0f,      i);
    00160                 }
    00161                 glColor3f(0.0,1.0f,0.0f);
    00162
    00163                 for ( i=-100;i<101;i+=2)
    00164                 {
    00165                         glVertex3f( 100.0f,     i,0.0f);
    00166                         glVertex3f(-100.0f,     i,0.0f);
    00167                         glVertex3f(     i, 100.0f,0.0f);
    00168                         glVertex3f(     i,-100.0f,0.0f);
    00169                 }
    00170                 glColor3f(0.0,0.0f,1.0f);
    00171                 for (i=-100;i<101;i+=2)
    00172                 {
    00173                         glVertex3f( 100.0f,0.0f,      i);
    00174                         glVertex3f(-100.0f,0.0f,      i);
    00175                         glVertex3f(      i,0.0f, 100.0f);
    00176                         glVertex3f(      i,0.0f,-100.0f);
    00177                 }
    00178         glEnd();
    00179         glEnable(GL_LIGHTING);
    00180         //---------------------------------
    00181         //Draw interface
    00182         //---------------------------------
    00183         //enter ortho mode
    00184         glMatrixMode(GL_PROJECTION);
    00185         glPushMatrix();
    00186         glLoadIdentity();
    00187         glOrtho( 0,m_SizeY,m_SizeX,0,0,1);
    00188         glMatrixMode(GL_MODELVIEW);
    00189         glLoadIdentity();
    00190         //-----------------
    00191         glEnable(GL_TEXTURE_2D);
    00192         glDisable(GL_LIGHTING);
    00193         glColor4f(1, 1, 1, 1);
    00194         glDisable(GL_DEPTH_TEST);
    00195         glBlendFunc(GL_DST_COLOR,GL_ZERO);
    00196         glEnable(GL_BLEND);
    00197
    00198         glBindTexture(GL_TEXTURE_2D, m_Texture[1]);
    00199
    00200         glBegin(GL_QUADS);
    00201                 glTexCoord2f(0.0f, 1.0f);       glVertex2f(0, 0);
    00202                 glTexCoord2f(0.0f, 0.0f);       glVertex2f(0, m_SizeY);
    00203                 glTexCoord2f(1.0f, 0.0f);       glVertex2f(m_SizeX, m_SizeY);
    00204                 glTexCoord2f(1.0f, 1.0f);       glVertex2f(m_SizeX, 0);
    00205         glEnd();
    00206
    00207         glBlendFunc(GL_ONE,GL_ONE);
    00208         glBindTexture(GL_TEXTURE_2D, m_Texture[0]);
    00209
    00210         glBegin(GL_QUADS);
    00211                 glTexCoord2f(0.0f, 1.0f);       glVertex2f(0, 0);
    00212                 glTexCoord2f(0.0f, 0.0f);       glVertex2f(0, m_SizeY);
    00213                 glTexCoord2f(1.0f, 0.0f);       glVertex2f(m_SizeX, m_SizeY);
    00214                 glTexCoord2f(1.0f, 1.0f);       glVertex2f(m_SizeX, 0);
    00215     glEnd();
    00216
    00217         glDisable(GL_BLEND);
    00218         glDisable(GL_TEXTURE_2D);
    00219         glEnable(GL_DEPTH_TEST);
    00220
    00221         //---------------------------------
    00222         //Draw interface region
    00223         //---------------------------------
    00224         glBegin(GL_QUADS);
    00225                 glVertex2f(topx,topy);
    00226                 glVertex2f(bottomx,topy);
    00227                 glVertex2f(bottomx,bottomy);
    00228                 glVertex2f(topx,bottomy);
    00229     glEnd();
    00230         //Leave orthomode
    00231         glMatrixMode( GL_PROJECTION );
    00232         glPopMatrix();
    00233         glMatrixMode( GL_MODELVIEW );
    00234         //-----------------
    00235         SwapBuffers(m_hDC);
    00236         return TRUE;                                                                            // Keep Going
    00237 }
    00238
    00239 void CGlView::IncreaseZoom(void)
    00240 {
    00241         m_fZoom=m_fZoom+1.0f;
    00242 }
    00243
    00244 void CGlView::DecreaseZoom(void)
    00245 {
    00246         m_fZoom=m_fZoom-1.0f;
    00247 }
    00248
    00249 bool CGlView::LoadTexture (CString FileName,int Number)
    00250 {
    00251         //Load Texture
    00252         AUX_RGBImageRec* m_texture;
    00253         m_texture = auxDIBImageLoad((const char*)FileName);
    00254
    00255         if(!m_texture)
    00256         {
    00257                 MessageBox("Picture could not be loaded");
    00258                 return false;
    00259         }
    00260         glGenTextures(1,&m_Texture[Number]);
    00261         glBindTexture(GL_TEXTURE_2D,m_Texture[Number]);
    00262         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    00263         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    00264         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    00265         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    00266
    00267         gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, m_texture[0].sizeX, m_texture[0].sizeY,
    00268         GL_RGB, GL_UNSIGNED_BYTE, m_texture[0].data);
    00269         return true;
    00270 }
    00271
    00272 bool CGlView::LoadImage (CString FileName)
    00273 {
    00274         return LoadTexture(FileName,0);
    00275 }
    00276 bool CGlView::LoadMask (CString FileName)
    00277 {
    00278         return LoadTexture(FileName,1);
    00279 }
    00280
    00281 BEGIN_MESSAGE_MAP(CGlView, CWnd)
    00282 END_MESSAGE_MAP()
    00283
    00284
    00285
    00286
    00287