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 "SDKParticules.h"
    00006 #include "..\sdkparticules\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 = -10.0f;
    00022         m_fAngleX = 45.0f;
    00023         m_fAngleY = 45.0f;
    00024         m_ShowGrid=false;
    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_DEPTH_TEST);
    00122
    00123         ExNihilo::InitBasicObjectList();
    00124         LoadTexture("../Data/Textures/twirl.bmp");
    00125         system.SetParticuleTextureName("twirl");
    00126         return TRUE;                                                                            // Initialization Went OK
    00127 }
    00128
    00129
    00130 int CGlView::DrawGLScene(GLvoid)                                        // Here's Where We Do All The Drawing
    00131 {
    00132         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear Screen And Depth Buffer
    00133         glLoadIdentity();                                                                       // Reset The Current Modelview Matrix
    00134         glTranslatef(0.0f,0.0f,m_fZoom);                                                // Move Left 1.5 Units And Into The Screen 6.0
    00135         glRotatef(m_fAngleX,1.0,0.0,0.0);
    00136         glRotatef(m_fAngleY,0.0,1.0,0.0);
    00137
    00138         glDisable(GL_LIGHTING);
    00139         glDisable(GL_TEXTURE_2D);
    00140
    00141         if(m_ShowGrid)glCallList(GRILLE);
    00142
    00143         glDisable(GL_LIGHTING);
    00144         glColor3f(0.2f,0.2f,0.2f);
    00145         glBegin(GL_QUADS);                                                                      // Draw A Quad
    00146                 glVertex3f(-10.0f,- 0.5f,10.0f);
    00147                 glVertex3f( 10.0f,- 0.5f,10.0f);
    00148                 glVertex3f( 10.0f,- 0.5f,-10.0f);
    00149                 glVertex3f(-10.0f,- 0.5f,-10.0f);
    00150         glEnd();
    00151
    00152         glDisable(GL_LIGHTING);
    00153         glDisable(GL_TEXTURE_2D);
    00154         system.Draw();
    00155
    00156         SwapBuffers(m_hDC);
    00157         return TRUE;                                                                            // Keep Going
    00158 }
    00159
    00160 void CGlView::IncreaseZoom(void)
    00161 {
    00162         m_fZoom=m_fZoom+1.0f;
    00163 }
    00164
    00165 void CGlView::DecreaseZoom(void)
    00166 {
    00167         m_fZoom=m_fZoom-1.0f;
    00168 }
    00169
    00170 bool CGlView::LoadTexture (CString fileName)
    00171 {
    00172         int Status=FALSE;                                                                       // Status Indicator
    00173         AUX_RGBImageRec *TextureImage[1];                                       // Create Storage Space For The Texture
    00174         memset(TextureImage,0,sizeof(void *)*1);                // Set The Pointer To NULL
    00175         // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    00176         if (TextureImage[0]=auxDIBImageLoad(fileName)   )
    00177         {
    00178                 Status=TRUE;                                                                    // Set The Status To TRUE
    00179                 glDeleteTextures(1, &texture[0]);
    00180                 glGenTextures(1, &texture[0]);                                  // Create The Texture
    00181                 glBindTexture(GL_TEXTURE_2D, texture[0]);
    00182                 glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    00183                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    00184                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    00185         }
    00186
    00187         if (TextureImage[0])                                                                    // If Texture Exists
    00188         {
    00189                 if (TextureImage[0]->data)                                                      // If Texture Image Exists
    00190                 {
    00191                         free(TextureImage[0]->data);                                    // Free The Texture Image Memory
    00192                 }
    00193
    00194                 free(TextureImage[0]);                                                          // Free The Image Structure
    00195         }
    00196         system.m_TextureID=texture[0];
    00197
    00198         return Status;
    00199 }
    00200
    00201 BEGIN_MESSAGE_MAP(CGlView, CWnd)
    00202 END_MESSAGE_MAP()
    00203
    00204
    00205
    00206
    00207
    00208
    00209