Guest User

Untitled

a guest
Jan 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.73 KB | None | 0 0
  1. /*
  2.  *      This Code Was Created By Jeff Molofee 2000
  3.  *      A HUGE Thanks To Fredric Echols For Cleaning Up
  4.  *      And Optimizing This Code, Making It More Flexible!
  5.  *      If You've Found This Code Useful, Please Let Me Know.
  6.  *      Visit My Site At nehe.gamedev.net
  7.  */
  8. #define _USE_MATH_DEFINES
  9. #include <math.h>
  10. #include <windows.h>        // Header File For Windows
  11. //#include <gl\gl.h>            // Header File For The OpenGL32 Library
  12. //#include <gl\glu.h>           // Header File For The GLu32 Library
  13. #include "Glew\GL\glew.h"
  14. #include "Glew\GL\wglew.h"
  15. #include "DevIL\IL\il.h"
  16. #include "DevIL\IL\ilu.h"
  17. #include "DevIL\IL\ilut.h"
  18.  
  19. HDC         hDC=NULL;       // Private GDI Device Context
  20. HGLRC       hRC=NULL;       // Permanent Rendering Context
  21. HWND        hWnd=NULL;      // Holds Our Window Handle
  22. HINSTANCE   hInstance;      // Holds The Instance Of The Application
  23.  
  24. unsigned char   keys[256];          // Array Used For The Keyboard Routine
  25. bool    active=TRUE;        // Window Active Flag Set To TRUE By Default
  26. bool    fullscreen=TRUE;    // Fullscreen Flag Set To Fullscreen Mode By Default
  27.  
  28.  
  29. #define CAMERA_FOVY  60.0f
  30. #define CAMERA_ZFAR  10.0f
  31. #define CAMERA_ZNEAR 0.1f
  32.  
  33. #define MOUSE_ORBIT_SPEED 0.30f    
  34. #define MOUSE_DOLLY_SPEED 0.02f    
  35. #define MOUSE_TRACK_SPEED 0.005f
  36. float               g_cameraPos[3];
  37. float               g_targetPos[3];
  38.  
  39. float mouse_x;
  40. float mouse_y;
  41. GLfloat rtri;               // Angle For The Triangle ( NEW )
  42. bool boolTest = true;
  43. ILuint ImageName;
  44.  
  45. typedef struct tagVERTEX
  46. {
  47.     float x, y, z;
  48.     float u, v;
  49. } VERTEX;
  50.  
  51. typedef struct tagTRIANGLE
  52. {
  53.     VERTEX vertex[3];
  54. } TRIANGLE;
  55.  
  56. typedef struct tagSECTOR
  57. {
  58.     int numtriangles;
  59.     TRIANGLE* triangle;
  60. } SECTOR;
  61.  
  62. SECTOR sector1;             // Our Model Goes Here:
  63. VERTEX *vertexList;
  64. int vertexCount;
  65.  
  66. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);   // Declaration For WndProc
  67.  
  68. void readstr(FILE *f,char *string)
  69. {
  70.     do
  71.     {
  72.         fgets(string, 511, f);
  73.     } while ((string[0] == '/') || (string[0] == '\n'));
  74.     return;
  75. }
  76.  
  77. void SetupWorld()
  78. {
  79.     float x, y, z/*, u, v*/;
  80.     int numtriangles;
  81.     int numvertices;
  82.     FILE *filein;
  83.     char oneline[511];
  84.     //filein = fopen("Data/Models/Test_Shape.obj", "rt");               // File To Load World Data From
  85.     fopen_s(&filein, "Data/Models/Test_Shape.obj", "rt");
  86.  
  87.     readstr(filein,oneline);
  88.     sscanf_s(oneline, "#vertices %d\n", &numvertices);
  89.     sscanf_s(oneline, "#polygons 0 - triangles %d\n", &numtriangles);
  90.  
  91.     vertexCount = numvertices;
  92.  
  93.     vertexList = new VERTEX[numvertices];
  94.     //sector1.triangle = new TRIANGLE[numtriangles];
  95.     //sector1.numtriangles = numtriangles;
  96.     //for (int loop = 0; loop < numtriangles; loop++)
  97.     //{
  98.     //  for (int vert = 0; vert < 3; vert++)
  99.     //  {
  100.     //      readstr(filein,oneline);
  101.     //      sscanf(oneline, "v %f %f %f", &x, &y, &z/*, &u, &v*/);
  102.     //      sector1.triangle[loop].vertex[vert].x = x/25.0f;
  103.     //      sector1.triangle[loop].vertex[vert].y = y/25.0f;
  104.     //      sector1.triangle[loop].vertex[vert].z = z/25.0f;
  105.     //      sector1.triangle[loop].vertex[vert].u = 0;
  106.     //      sector1.triangle[loop].vertex[vert].v = 0;
  107.     //  }
  108.     //}
  109.     for (int loop = 0; loop < numvertices; ++loop){
  110.         readstr(filein, oneline);
  111.         sscanf_s(oneline, "v %f %f %f", &x, &y, &z);
  112.         vertexList[loop].x = x/100.0f;
  113.         vertexList[loop].y = y/100.0f;
  114.         vertexList[loop].z = z/100.0f;
  115.         vertexList[loop].u = 0;
  116.         vertexList[loop].v = 0;
  117.     }
  118.     fclose(filein);
  119.     return;
  120. }
  121.  
  122. /*AUX_RGBImageRec *LoadBMP(char *Filename)                // Loads A Bitmap Image
  123. {
  124.         FILE *File=NULL;                                // File Handle
  125.  
  126.         if (!Filename)                                  // Make Sure A Filename Was Given
  127.         {
  128.                 return NULL;                            // If Not Return NULL
  129.         }
  130.  
  131.         File=fopen(Filename,"r");                       // Check To See If The File Exists
  132.  
  133.         if (File)                                       // Does The File Exist?
  134.         {
  135.                 fclose(File);                           // Close The Handle
  136.                 return auxDIBImageLoad(Filename);       // Load The Bitmap And Return A Pointer
  137.         }
  138.        
  139.         return NULL;                                    // If Load Failed Return NULL
  140. }*/
  141.  
  142. int LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
  143. {
  144.     /*
  145.         int Status=FALSE;                               // Status Indicator
  146.  
  147.         AUX_RGBImageRec *TextureImage[1];               // Create Storage Space For The Texture
  148.  
  149.         memset(TextureImage,0,sizeof(void *)*1);        // Set The Pointer To NULL
  150.  
  151.         // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
  152.         if (TextureImage[0]=LoadBMP("Data/Mud.bmp"))
  153.         {
  154.                 Status=TRUE;                            // Set The Status To TRUE
  155.  
  156.                 glGenTextures(3, &texture[0]);          // Create Three Textures
  157.  
  158.                 // Create Nearest Filtered Texture
  159.                 glBindTexture(GL_TEXTURE_2D, texture[0]);
  160.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  161.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  162.                 glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
  163.  
  164.                 // Create Linear Filtered Texture
  165.                 glBindTexture(GL_TEXTURE_2D, texture[1]);
  166.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  167.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  168.                 glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
  169.  
  170.                 // Create MipMapped Texture
  171.                 glBindTexture(GL_TEXTURE_2D, texture[2]);
  172.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  173.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  174.                 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
  175.         }
  176.         if (TextureImage[0])                            // If Texture Exists
  177.         {
  178.                 if (TextureImage[0]->data)              // If Texture Image Exists
  179.                 {
  180.                         free(TextureImage[0]->data);    // Free The Texture Image Memory
  181.                 }
  182.  
  183.                 free(TextureImage[0]);                  // Free The Image Structure
  184.         }
  185.  
  186.         return Status;                                  // Return The Status
  187.         */
  188.     return 0;
  189. }
  190.  
  191. GLvoid KeyPressed(WPARAM wParam)
  192. {
  193.     if (wParam == VK_HOME){
  194.         ilutGLScreenie();
  195.        
  196.         /*int screenWidth  = GetSystemMetrics( SM_CXVIRTUALSCREEN ) ;
  197.         int screenHeight = GetSystemMetrics( SM_CYVIRTUALSCREEN ) ;
  198.  
  199.         int bytesToUsePerPixel = 3 ;
  200.         int sizeOfByte = sizeof( unsigned char ) ;
  201.         int theSize = screenWidth * screenHeight * sizeOfByte * bytesToUsePerPixel ;
  202.  
  203.         unsigned char * imData =(unsigned char*)malloc( theSize ) ;
  204.  
  205.         for( int i = 0 ; i < theSize ; i++ )
  206.         {
  207.             imData[ i ] = i % 255 ;
  208.         }
  209.  
  210.         ILuint imageID = ilGenImage();
  211.         ilBindImage( imageID );
  212.         ilutGLScreen();
  213.         ILuint width = ilGetInteger( IL_IMAGE_WIDTH );
  214.         ILuint heigth = ilGetInteger( IL_IMAGE_HEIGHT );
  215.         ILuint bpp = ilGetInteger( IL_IMAGE_BPP );
  216.  
  217.         ilTexImage(width, heigth, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, imData);
  218.  
  219.         ilSave( IL_PNG, L"output.png" ) ;*/
  220.     }
  221. }
  222.  
  223. bool IsKeyDown(int vKey)
  224. {
  225.     if(keys[vKey] & 0x80)
  226.         return true;
  227.     return false;
  228. }
  229.  
  230. GLvoid ReSizeGLScene(GLsizei width, GLsizei height)     // Resize And Initialize The GL Window
  231. {
  232.     if (height==0)                                      // Prevent A Divide By Zero By
  233.     {
  234.         height=1;                                       // Making Height Equal One
  235.     }
  236.  
  237.     glViewport(0,0,width,height);                       // Reset The Current Viewport
  238.  
  239.     glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
  240.     glLoadIdentity();                                   // Reset The Projection Matrix
  241.  
  242.     // Calculate The Aspect Ratio Of The Window
  243.     gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  244.  
  245.     glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
  246.     glLoadIdentity();                                   // Reset The Modelview Matrix
  247. }
  248.  
  249. bool InitGL()
  250. {
  251.     GLenum err = glewInit();
  252.     bool m_GlewOK = (err == GLEW_OK);
  253.  
  254.     glShadeModel(GL_SMOOTH);
  255.     glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  256.     glClearDepth(1.0f);
  257.     glEnable(GL_DEPTH_TEST);
  258.     glDepthFunc(GL_LEQUAL);
  259.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  260.  
  261.     // extension function - enable vertical syncing.
  262.     wglSwapIntervalEXT(1);
  263.  
  264.     ilInit();
  265.     iluInit();
  266.     ilutRenderer(ILUT_OPENGL);
  267.  
  268.     SetupWorld();
  269.  
  270.     return m_GlewOK;
  271. }
  272.  
  273. //int InitGL(GLvoid)                                        // All Setup For OpenGL Goes Here
  274. //{
  275. //  glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
  276. //  glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
  277. //  glClearDepth(1.0f);                                 // Depth Buffer Setup
  278. //  glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
  279. //  glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
  280. //  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations
  281. // 
  282. //  return TRUE;                                        // Initialization Went OK
  283. //}
  284.  
  285. void DrawGrid(int linesH, int linesV, double spaceH, double spaceV, float transX = 0, float transY = 0, float transZ = 0) {
  286.     glTranslatef(transX, transY, transZ);
  287.     glBegin(GL_LINES); // Begin line drawing
  288.     glColor3ub(0,255,0); // Set the color to [255,201,14]
  289.  
  290.     double i(0);
  291.     for(i = (linesH*spaceH)/2.0; i >= (linesH*spaceH)/(-2.0); i -= spaceH) {
  292.         if (i <= 0.0) { glColor3ub(60,115,60); }
  293.         glVertex3f((float)(-(linesH*spaceH)/2.0),(float)(i),0);
  294.         glVertex3f((float)((linesH*spaceH)/2.0),(float)(i),0);
  295.     }
  296.     glColor3ub(255,0,0);
  297.     for(i = (linesV*spaceV)/2.0; i >= (linesV*spaceV)/(-2.0); i -= spaceV) {
  298.         if (i <= 0.0) { glColor3ub(115,60,60); }
  299.         glVertex3f((float)(i),(float)(-(linesV*spaceV)/2.0),0);
  300.         glVertex3f((float)(i),(float)((linesV*spaceV)/2.0),0);
  301.     }
  302.    
  303.     glEnd(); // Stop line drawing
  304.     glTranslatef(-transX, -transY, -transZ);
  305.     glBegin(GL_LINES); // Begin line drawing
  306.  
  307.     glColor3ub(0,0,255);
  308.     glVertex3f((float)((linesH+2)*spaceH/2.0),0,0);
  309.     glVertex3f((float)(-(linesH+2)*spaceH/2.0),0,0);
  310.     glVertex3f(0,(float)((linesV+2)*spaceV/2.0),0);
  311.     glVertex3f(0,(float)(-(linesV+2)*spaceV/2.0),0);
  312.  
  313.     glEnd(); // Stop line drawing
  314. }
  315.  
  316. void DrawCircle(float radius, int segments) {    
  317.     glColor3ub(255,255,255);
  318.     glBegin ( GL_LINE_LOOP ); // start a line loop
  319.     for ( int i = 0; i <= segments; ++i)
  320.     {
  321.         float angle = (float)(i*2*M_PI) / segments; // the current angle
  322.         float x1 = radius * cos( angle ); // calculate x1 and y1
  323.         float y1 = radius * sin( angle );
  324.         glVertex3f(x1,y1,0); // one point in the line loop
  325.     }
  326.     glEnd();
  327. }
  328.  
  329. void DrawCircle(float x, float y, float z, float radius, int segments) {  
  330.    
  331.     glPushMatrix(); // save the current transformation
  332.     glTranslatef(x,y,z); // translate the current transformation
  333.     DrawCircle(radius, segments); // draw the circle
  334.     glPopMatrix(); // restore the save transformation
  335. }
  336. void ProcessMouseSpeed(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  337. {
  338.     enum CameraModus
  339.     {
  340.         NO_CAMERA =1,
  341.         PAN_CAMERA =2,
  342.         ZOOM_CAMERA =3,
  343.         TURN_CAMERA =4
  344.     };
  345.  
  346.     static CameraModus cameramodus = NO_CAMERA;
  347.     static POINT ptMousePrev = {0};
  348.     static POINT ptMouseCurrent = {0};
  349.     static int mouseButtonDown = 0;
  350.     static float dx = 0.0f;
  351.     static float dy = 0.0f;
  352.  
  353.    
  354.  
  355.     switch(msg)
  356.     {
  357.     case WM_MBUTTONDOWN:
  358.         cameramodus = PAN_CAMERA;
  359.         ++mouseButtonDown;
  360.         SetCapture(hWnd);
  361.         ptMousePrev.x = static_cast<int>(static_cast<short>(LOWORD(lParam)));
  362.         ptMousePrev.y = static_cast<int>(static_cast<short>(HIWORD(lParam)));
  363.         ClientToScreen(hWnd,&ptMousePrev);
  364.         break;
  365.     case WM_LBUTTONDOWN:
  366.         cameramodus = TURN_CAMERA;
  367.         ++mouseButtonDown;
  368.         SetCapture(hWnd);
  369.         ptMousePrev.x = static_cast<int>(static_cast<short>(LOWORD(lParam)));
  370.         ptMousePrev.y = static_cast<int>(static_cast<short>(HIWORD(lParam)));
  371.         ClientToScreen(hWnd,&ptMousePrev);
  372.         //OutputDebugString(L"mouse is down.\n");
  373.         break;
  374.     case WM_RBUTTONDOWN:
  375.         cameramodus = ZOOM_CAMERA;
  376.         //++mouseButtonDown;
  377.         SetCapture(hWnd);
  378.         ptMousePrev.x = static_cast<int>(static_cast<short>(LOWORD(lParam)));
  379.         ptMousePrev.y = static_cast<int>(static_cast<short>(HIWORD(lParam)));
  380.         ClientToScreen(hWnd, &ptMousePrev);
  381.         break;
  382.     case WM_MOUSEMOVE:
  383.         ptMouseCurrent.x = static_cast<int>(static_cast<short>(LOWORD(lParam)));
  384.         ptMouseCurrent.y = static_cast<int>(static_cast<short>(HIWORD(lParam)));
  385.         ClientToScreen(hWnd,&ptMouseCurrent);
  386.     default:
  387.         cameramodus = TURN_CAMERA;
  388.         //++mouseButtonDown;
  389.         //SetCapture(hWnd);
  390.         //ptMousePrev.x = static_cast<int>(static_cast<short>(LOWORD(lParam)));
  391.         //ptMousePrev.y = static_cast<int>(static_cast<short>(HIWORD(lParam)));
  392.         //ClientToScreen(hWnd,&ptMousePrev);
  393.         //OutputDebugString(L"meuh \n");
  394.  
  395.         switch(cameramodus)
  396.         {
  397.         case PAN_CAMERA:
  398.             dx=static_cast<float>(ptMouseCurrent.x - ptMousePrev.x);
  399.             dx *= MOUSE_TRACK_SPEED;
  400.  
  401.             dy=static_cast<float>(ptMouseCurrent.y - ptMousePrev.y);
  402.             dy *= MOUSE_TRACK_SPEED;
  403.  
  404.             g_cameraPos[0] -= dx;
  405.             g_cameraPos[1] += dy;
  406.  
  407.             g_targetPos[0] -= dx;
  408.             g_targetPos[1] += dy;
  409.  
  410.             break;
  411.         case ZOOM_CAMERA:
  412.             dy=static_cast<float>(ptMouseCurrent.y - ptMousePrev.y);
  413.             dy *= MOUSE_DOLLY_SPEED;
  414.  
  415.             g_cameraPos[2] -= dy;
  416.            
  417.             /*if (g_cameraPos[2] < g_model.getRadius() + CAMERA_ZNEAR)
  418.                 g_cameraPos[2] = g_model.getRadius() + CAMERA_ZNEAR;
  419.  
  420.             if (g_cameraPos[2] > CAMERA_ZFAR - g_model.getRadius())
  421.                 g_cameraPos[2] = CAMERA_ZFAR - g_model.getRadius();*/
  422.  
  423.             g_targetPos[0] -= dx;
  424.             g_targetPos[1] += dy;
  425.  
  426.             break;
  427.         case TURN_CAMERA:
  428.  
  429.             if(wParam == MK_LBUTTON)
  430.             {
  431.             dx = static_cast<float>(ptMouseCurrent.x - ptMousePrev.x);
  432.             dx *= MOUSE_ORBIT_SPEED;
  433.  
  434.             dy = static_cast<float>(ptMouseCurrent.y - ptMousePrev.y);
  435.             dy *= MOUSE_ORBIT_SPEED;
  436.  
  437.             rtri += dx;
  438.             }
  439.             //g_heading += dx;
  440.            // g_pitch += dy;
  441.  
  442.             //if (rtri > 90.0f)
  443.            //     rtri = 90.0f;
  444.  
  445.             //if (rtri < -90.0f)
  446.             //    rtri = -90.0f;
  447.  
  448.             break;
  449.        
  450.         }
  451.         ptMousePrev.x = ptMouseCurrent.x;
  452.         ptMousePrev.y = ptMouseCurrent.y;
  453.  
  454.     case WM_LBUTTONUP:
  455.     case WM_RBUTTONUP:
  456.     case WM_MBUTTONUP:
  457.         if(--mouseButtonDown <=0)
  458.         {
  459.             mouseButtonDown = 0;
  460.             cameramodus = NO_CAMERA;
  461.             ReleaseCapture();
  462.         }
  463.         else
  464.         {
  465.             if(wParam & MK_LBUTTON)
  466.                 cameramodus = TURN_CAMERA;
  467.             else if(wParam & MK_RBUTTON)
  468.                 cameramodus = ZOOM_CAMERA;
  469.             else if(wParam & MK_MBUTTON)
  470.                 cameramodus = PAN_CAMERA;
  471.  
  472.         }
  473.         break;
  474.    
  475.         break;
  476.     }
  477.  
  478.  
  479. }
  480. //POINT MousePos()
  481. //{
  482. //  gluUnproject(
  483. //}
  484. int DrawGLScene(GLvoid)                                 // Here's Where We Do All The Drawing
  485. {
  486.     //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Clear Screen And Depth Buffer
  487.     //glLoadIdentity();                                 // Reset The Current Modelview Matrix
  488.     //return TRUE;                                      // Everything Went OK
  489.     //GLint viewport[4];
  490.  
  491.     glClear(GL_COLOR_BUFFER_BIT |  // Clear the color and depth buffer
  492.             GL_DEPTH_BUFFER_BIT);
  493.     glLoadIdentity(); // Reset the model transform
  494.    
  495.     gluLookAt(5*cos(rtri/40),5*sin(rtri/40),2,0,0,0,0,0,1);
  496.     //gluPickMatrix((GLdouble) mouse_x,GLdouble (viewport[3]-mouse_y),1.0f,1.0f,viewport);
  497.  
  498.     DrawGrid(20,20,0.1f,0.1f);
  499.  
  500.    
  501.     //glRotatef(rtri,0.0f,0.0f,1.0f);
  502.     //glBegin(GL_TRIANGLES);                      // Drawing Using Triangles
  503.     //  glColor3ub(255,0,0);
  504.     //  glVertex3f( 0.0f, 0.0f, 1.0f);              // Top
  505.     //  glColor3ub(0,255,0);
  506.     //  glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
  507.     //  glColor3ub(255,255,255);
  508.     //  glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
  509.  
  510.     //  glColor3ub(255,0,0);
  511.     //  glVertex3f( 0.0f, 0.0f, 1.0f);              // Top
  512.     //  glColor3ub(255,255,255);
  513.     //  glVertex3f(1.0f,-1.0f, 0.0f);              // Bottom Left
  514.     //  glColor3ub(0,0,255);
  515.     //  glVertex3f( 0.0f,1.0f, 0.0f);              // Bottom Right
  516.  
  517.     //  glColor3ub(255,0,0);
  518.     //  glVertex3f( 0.0f, 0.0f, 1.0f);              // Top
  519.     //  glColor3ub(0,0,255);
  520.     //  glVertex3f(0.0f,1.0f, 0.0f);              // Bottom Left
  521.     //  glColor3ub(0,255,0);
  522.     //  glVertex3f( -1.0f,-1.0f, 0.0f);              // Bottom Right
  523.     //glEnd();                            // Finished Drawing The Triangle
  524.  
  525.  
  526.     //DrawCircle(-5.0f,5.0f,5.0f,0.5f,33);
  527.     //DrawCircle(-5.0f,-5.0f,-1.0f,0.5f,20);
  528.     //if(IsKeyDown('I'))
  529.     //{
  530.     //  rtri += 0.5f;
  531.     //}
  532.     //if(IsKeyDown(VK_MENU && VK_LBUTTON))
  533.     //{
  534.     //  rtri+=0.1f;
  535.     //  double MouseMovement = WM_MOUSEMOVE;
  536.  
  537.         //rtri-=WM_MOUSEMOVE;// Increase The Rotation Variable For The Triangle ( NEW )
  538.     //}
  539.     /*if(IsKeyDown('O')) {
  540.         rtri -= 0.1f;
  541.     }*/
  542.     //else { rtri -= 0.1f;}
  543.  
  544.     //if (boolTest == true && keys[36] == true){
  545.     // 
  546.     //}
  547.  
  548.     //float x_m, y_m, z_m;
  549.  
  550.     /*int numtriangles;
  551.     numtriangles = sector1.numtriangles;*/
  552.  
  553.     // Process Each Triangle
  554.     for (int loop_m = 0; loop_m < /*numtriangles*/vertexCount; loop_m++)
  555.     {
  556.         /*glBegin(GL_TRIANGLES);
  557.             glNormal3f( 0.0f, 0.0f, 1.0f);
  558.             x_m = sector1.triangle[loop_m].vertex[0].x;
  559.             y_m = sector1.triangle[loop_m].vertex[0].y;
  560.             z_m = sector1.triangle[loop_m].vertex[0].z;
  561.             u_m = sector1.triangle[loop_m].vertex[0].u;
  562.             v_m = sector1.triangle[loop_m].vertex[0].v;
  563.             glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
  564.            
  565.             x_m = sector1.triangle[loop_m].vertex[1].x;
  566.             y_m = sector1.triangle[loop_m].vertex[1].y;
  567.             z_m = sector1.triangle[loop_m].vertex[1].z;
  568.             u_m = sector1.triangle[loop_m].vertex[1].u;
  569.             v_m = sector1.triangle[loop_m].vertex[1].v;
  570.             glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
  571.            
  572.             x_m = sector1.triangle[loop_m].vertex[2].x;
  573.             y_m = sector1.triangle[loop_m].vertex[2].y;
  574.             z_m = sector1.triangle[loop_m].vertex[2].z;
  575.             u_m = sector1.triangle[loop_m].vertex[2].u;
  576.             v_m = sector1.triangle[loop_m].vertex[2].v;
  577.             glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
  578.         glEnd();*/
  579.         //glBegin(GL_TRIANGLES)
  580.             //glNormal3f(0.0f, 0.0f, 1.0f);
  581.             //glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
  582.             DrawCircle(vertexList[loop_m].x, vertexList[loop_m].y, vertexList[loop_m].z, 0.005f,3);
  583.     }
  584.  
  585.     return TRUE; // Everything is ok!
  586. }
  587.  
  588. GLvoid KillGLWindow(GLvoid)                             // Properly Kill The Window
  589. {
  590.     if (fullscreen)                                     // Are We In Fullscreen Mode?
  591.     {
  592.         ChangeDisplaySettings(NULL,0);                  // If So Switch Back To The Desktop
  593.         ShowCursor(TRUE);                               // Show Mouse Pointer
  594.     }
  595.  
  596.     if (hRC)                                            // Do We Have A Rendering Context?
  597.     {
  598.         if (!wglMakeCurrent(NULL,NULL))                 // Are We Able To Release The DC And RC Contexts?
  599.         {
  600.             MessageBox(NULL,L"Release Of DC And RC Failed.",L"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  601.         }
  602.  
  603.         if (!wglDeleteContext(hRC))                     // Are We Able To Delete The RC?
  604.         {
  605.             MessageBox(NULL,L"Release Rendering Context Failed.",L"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  606.         }
  607.         hRC=NULL;                                       // Set RC To NULL
  608.     }
  609.  
  610.     if (hDC && !ReleaseDC(hWnd,hDC))                    // Are We Able To Release The DC
  611.     {
  612.         MessageBox(NULL,L"Release Device Context Failed.",L"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  613.         hDC=NULL;                                       // Set DC To NULL
  614.     }
  615.  
  616.     if (hWnd && !DestroyWindow(hWnd))                   // Are We Able To Destroy The Window?
  617.     {
  618.         MessageBox(NULL,L"Could Not Release hWnd.",L"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  619.         hWnd=NULL;                                      // Set hWnd To NULL
  620.     }
  621.  
  622.     if (!UnregisterClass(L"OpenGL",hInstance))          // Are We Able To Unregister Class
  623.     {
  624.         MessageBox(NULL,L"Could Not Unregister Class.",L"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  625.         hInstance=NULL;                                 // Set hInstance To NULL
  626.     }
  627. }
  628.  
  629. /*  This Code Creates Our OpenGL Window.  Parameters Are:                   *
  630.  *  title           - Title To Appear At The Top Of The Window              *
  631.  *  width           - Width Of The GL Window Or Fullscreen Mode             *
  632.  *  height          - Height Of The GL Window Or Fullscreen Mode            *
  633.  *  bits            - Number Of Bits To Use For Color (8/16/24/32)          *
  634.  *  fullscreenflag  - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)   */
  635.  
  636. BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
  637. {
  638.     GLuint      PixelFormat;            // Holds The Results After Searching For A Match
  639.     WNDCLASS    wc;                     // Windows Class Structure
  640.     DWORD       dwExStyle;              // Window Extended Style
  641.     DWORD       dwStyle;                // Window Style
  642.     RECT        WindowRect;             // Grabs Rectangle Upper Left / Lower Right Values
  643.     WindowRect.left=(long)0;            // Set Left Value To 0
  644.     WindowRect.right=(long)width;       // Set Right Value To Requested Width
  645.     WindowRect.top=(long)0;             // Set Top Value To 0
  646.     WindowRect.bottom=(long)height;     // Set Bottom Value To Requested Height
  647.  
  648.     fullscreen=fullscreenflag;          // Set The Global Fullscreen Flag
  649.  
  650.     hInstance           = GetModuleHandle(NULL);                // Grab An Instance For Our Window
  651.     wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;   // Redraw On Size, And Own DC For Window.
  652.     wc.lpfnWndProc      = (WNDPROC) WndProc;                    // WndProc Handles Messages
  653.     wc.cbClsExtra       = 0;                                    // No Extra Window Data
  654.     wc.cbWndExtra       = 0;                                    // No Extra Window Data
  655.     wc.hInstance        = hInstance;                            // Set The Instance
  656.     wc.hIcon            = LoadIcon(NULL, IDI_WINLOGO);          // Load The Default Icon
  657.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);          // Load The Arrow Pointer
  658.     wc.hbrBackground    = NULL;                                 // No Background Required For GL
  659.     wc.lpszMenuName     = NULL;                                 // We Don't Want A Menu
  660.     wc.lpszClassName    = L"OpenGL";                                // Set The Class Name
  661.  
  662.     if (!RegisterClass(&wc))                                    // Attempt To Register The Window Class
  663.     {
  664.         MessageBox(NULL,L"Failed To Register The Window Class.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  665.         return FALSE;                                           // Return FALSE
  666.     }
  667.    
  668.     if (fullscreen)                                             // Attempt Fullscreen Mode?
  669.     {
  670.         DEVMODE dmScreenSettings;                               // Device Mode
  671.         memset(&dmScreenSettings,0,sizeof(dmScreenSettings));   // Makes Sure Memory's Cleared
  672.         dmScreenSettings.dmSize=sizeof(dmScreenSettings);       // Size Of The Devmode Structure
  673.         dmScreenSettings.dmPelsWidth    = width;                // Selected Screen Width
  674.         dmScreenSettings.dmPelsHeight   = height;               // Selected Screen Height
  675.         dmScreenSettings.dmBitsPerPel   = bits;                 // Selected Bits Per Pixel
  676.         dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  677.  
  678.         // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
  679.         if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
  680.         {
  681.             // If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
  682.             if (MessageBox(NULL,L"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?",L"NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
  683.             {
  684.                 fullscreen=FALSE;       // Windowed Mode Selected.  Fullscreen = FALSE
  685.             }
  686.             else
  687.             {
  688.                 // Pop Up A Message Box Letting User Know The Program Is Closing.
  689.                 MessageBox(NULL,L"Program Will Now Close.",L"ERROR",MB_OK|MB_ICONSTOP);
  690.                 return FALSE;                                   // Return FALSE
  691.             }
  692.         }
  693.     }
  694.  
  695.     if (fullscreen)                                             // Are We Still In Fullscreen Mode?
  696.     {
  697.         dwExStyle=WS_EX_APPWINDOW;                              // Window Extended Style
  698.         dwStyle=WS_POPUP;                                       // Windows Style
  699.         ShowCursor(FALSE);                                      // Hide Mouse Pointer
  700.     }
  701.     else
  702.     {
  703.         dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;           // Window Extended Style
  704.         dwStyle=WS_OVERLAPPEDWINDOW;                            // Windows Style
  705.     }
  706.  
  707.     AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);     // Adjust Window To True Requested Size
  708.  
  709.     // Create The Window
  710.     if (!(hWnd=CreateWindowEx(  dwExStyle,                          // Extended Style For The Window
  711.                                 L"OpenGL",                          // Class Name
  712.                                 L"Title",                               // Window Title
  713.                                 dwStyle |                           // Defined Window Style
  714.                                 WS_CLIPSIBLINGS |                   // Required Window Style
  715.                                 WS_CLIPCHILDREN,                    // Required Window Style
  716.                                 0, 0,                               // Window Position
  717.                                 WindowRect.right-WindowRect.left,   // Calculate Window Width
  718.                                 WindowRect.bottom-WindowRect.top,   // Calculate Window Height
  719.                                 NULL,                               // No Parent Window
  720.                                 NULL,                               // No Menu
  721.                                 hInstance,                          // Instance
  722.                                 NULL)))                             // Dont Pass Anything To WM_CREATE
  723.     {
  724.         KillGLWindow();                             // Reset The Display
  725.         MessageBox(NULL,L"Window Creation Error.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  726.         return FALSE;                               // Return FALSE
  727.     }
  728.  
  729.     static  PIXELFORMATDESCRIPTOR pfd=              // pfd Tells Windows How We Want Things To Be
  730.     {
  731.         sizeof(PIXELFORMATDESCRIPTOR),              // Size Of This Pixel Format Descriptor
  732.         1,                                          // Version Number
  733.         PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
  734.         PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
  735.         PFD_DOUBLEBUFFER,                           // Must Support Double Buffering
  736.         PFD_TYPE_RGBA,                              // Request An RGBA Format
  737.         bits,                                       // Select Our Color Depth
  738.         0, 0, 0, 0, 0, 0,                           // Color Bits Ignored
  739.         0,                                          // No Alpha Buffer
  740.         0,                                          // Shift Bit Ignored
  741.         0,                                          // No Accumulation Buffer
  742.         0, 0, 0, 0,                                 // Accumulation Bits Ignored
  743.         16,                                         // 16Bit Z-Buffer (Depth Buffer)  
  744.         0,                                          // No Stencil Buffer
  745.         0,                                          // No Auxiliary Buffer
  746.         PFD_MAIN_PLANE,                             // Main Drawing Layer
  747.         0,                                          // Reserved
  748.         0, 0, 0                                     // Layer Masks Ignored
  749.     };
  750.    
  751.     if (!(hDC=GetDC(hWnd)))                         // Did We Get A Device Context?
  752.     {
  753.         KillGLWindow();                             // Reset The Display
  754.         MessageBox(NULL,L"Can't Create A GL Device Context.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  755.         return FALSE;                               // Return FALSE
  756.     }
  757.  
  758.     if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
  759.     {
  760.         KillGLWindow();                             // Reset The Display
  761.         MessageBox(NULL,L"Can't Find A Suitable PixelFormat.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  762.         return FALSE;                               // Return FALSE
  763.     }
  764.  
  765.     if(!SetPixelFormat(hDC,PixelFormat,&pfd))       // Are We Able To Set The Pixel Format?
  766.     {
  767.         KillGLWindow();                             // Reset The Display
  768.         MessageBox(NULL,L"Can't Set The PixelFormat.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  769.         return FALSE;                               // Return FALSE
  770.     }
  771.  
  772.     if (!(hRC=wglCreateContext(hDC)))               // Are We Able To Get A Rendering Context?
  773.     {
  774.         KillGLWindow();                             // Reset The Display
  775.         MessageBox(NULL,L"Can't Create A GL Rendering Context.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  776.         return FALSE;                               // Return FALSE
  777.     }
  778.  
  779.     if(!wglMakeCurrent(hDC,hRC))                    // Try To Activate The Rendering Context
  780.     {
  781.         KillGLWindow();                             // Reset The Display
  782.         MessageBox(NULL,L"Can't Activate The GL Rendering Context.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  783.         return FALSE;                               // Return FALSE
  784.     }
  785.  
  786.     ShowWindow(hWnd,SW_SHOW);                       // Show The Window
  787.     SetForegroundWindow(hWnd);                      // Slightly Higher Priority
  788.     SetFocus(hWnd);                                 // Sets Keyboard Focus To The Window
  789.     ReSizeGLScene(width, height);                   // Set Up Our Perspective GL Screen
  790.  
  791.     if (!InitGL())                                  // Initialize Our Newly Created GL Window
  792.     {
  793.         KillGLWindow();                             // Reset The Display
  794.         MessageBox(NULL,L"Initialization Failed.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  795.         return FALSE;                               // Return FALSE
  796.     }
  797.  
  798.     return TRUE;                                    // Success
  799. }
  800.  
  801. LRESULT CALLBACK WndProc(   HWND    hWnd,           // Handle For This Window
  802.                             UINT    uMsg,           // Message For This Window
  803.                             WPARAM  wParam,         // Additional Message Information
  804.                             LPARAM  lParam)         // Additional Message Information
  805. {
  806.     switch (uMsg)                                   // Check For Windows Messages
  807.     {
  808.         case WM_ACTIVATE:                           // Watch For Window Activate Message
  809.         {
  810.             if (!HIWORD(wParam))                    // Check Minimization State
  811.             {
  812.                 active=TRUE;                        // Program Is Active
  813.             }
  814.             else
  815.             {
  816.                 active=FALSE;                       // Program Is No Longer Active
  817.             }
  818.  
  819.             return 0;                               // Return To The Message Loop
  820.         }
  821.  
  822.         case WM_SYSCOMMAND:                         // Intercept System Commands
  823.         {
  824.             switch (wParam)                         // Check System Calls
  825.             {
  826.                 case SC_SCREENSAVE:                 // Screensaver Trying To Start?
  827.                 case SC_MONITORPOWER:               // Monitor Trying To Enter Powersave?
  828.                 return 0;                           // Prevent From Happening
  829.             }
  830.             break;                                  // Exit
  831.         }
  832.  
  833.         case WM_CLOSE:                              // Did We Receive A Close Message?
  834.         {
  835.             PostQuitMessage(0);                     // Send A Quit Message
  836.             return 0;                               // Jump Back
  837.         }
  838.  
  839.         case WM_KEYDOWN:                            // Is A Key Being Held Down?
  840.         {
  841.             GetKeyboardState(keys);
  842.             //keys[wParam] = TRUE;                  // If So, Mark It As TRUE
  843.             return 0;                               // Jump Back
  844.         }
  845.  
  846.         case WM_KEYUP:                              // Has A Key Been Released?
  847.         {
  848.             keys[wParam] = FALSE;                   // If So, Mark It As FALSE
  849.             KeyPressed(wParam);
  850.             return 0;                               // Jump Back
  851.         }
  852.  
  853.         case WM_SIZE:                               // Resize The OpenGL Window
  854.         {
  855.             ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
  856.             return 0;                               // Jump Back
  857.         }  
  858.         case WM_MOUSEMOVE:
  859.         {
  860.             //ProcessMouseSpeed(hWnd,uMsg,wParam,lParam);
  861.         }
  862.         default:
  863.             ProcessMouseSpeed(hWnd,uMsg,wParam,lParam);
  864.     }
  865.  
  866.     // Pass All Unhandled Messages To DefWindowProc
  867.     return DefWindowProc(hWnd,uMsg,wParam,lParam);
  868. }
  869.  
  870. int WINAPI WinMain( HINSTANCE   hInstance,          // Instance
  871.                     HINSTANCE   hPrevInstance,      // Previous Instance
  872.                     LPSTR       lpCmdLine,          // Command Line Parameters
  873.                     int         nCmdShow)           // Window Show State
  874. {
  875.     MSG     msg;                                    // Windows Message Structure
  876.     BOOL    done=FALSE;                             // Bool Variable To Exit Loop
  877.  
  878.     // Ask The User Which Screen Mode They Prefer
  879.     if (MessageBox(NULL,L"Would You Like To Run In Fullscreen Mode?", L"Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
  880.     {
  881.         fullscreen=FALSE;                           // Windowed Mode
  882.     }
  883.  
  884.     // Create Our OpenGL Window
  885.     if (!CreateGLWindow("NeHe's OpenGL Framework",1280,960,16,fullscreen))
  886.     {
  887.         return 0;                                   // Quit If Window Was Not Created
  888.     }
  889.  
  890.     while(!done)                                    // Loop That Runs While done=FALSE
  891.     {
  892.         if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))   // Is There A Message Waiting?
  893.         {
  894.             if (msg.message==WM_QUIT)               // Have We Received A Quit Message?
  895.             {
  896.                 done=TRUE;                          // If So done=TRUE
  897.             }
  898.             else                                    // If Not, Deal With Window Messages
  899.             {
  900.                 TranslateMessage(&msg);             // Translate The Message
  901.                 DispatchMessage(&msg);              // Dispatch The Message
  902.             }
  903.         }
  904.         else                                        // If There Are No Messages
  905.         {
  906.             // Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
  907.             if (active)                             // Program Active?
  908.             {
  909.                 if (keys[VK_ESCAPE])                // Was ESC Pressed?
  910.                 {
  911.                     done=TRUE;                      // ESC Signalled A Quit
  912.                 }
  913.                 else                                // Not Time To Quit, Update Screen
  914.                 {
  915.                     DrawGLScene();                  // Draw The Scene
  916.                     SwapBuffers(hDC);               // Swap Buffers (Double Buffering)
  917.                 }
  918.             }
  919.  
  920.             if (keys[VK_F1])                        // Is F1 Being Pressed?
  921.             {
  922.                 keys[VK_F1]=FALSE;                  // If So Make Key FALSE
  923.                 KillGLWindow();                     // Kill Our Current Window
  924.                 fullscreen=!fullscreen;             // Toggle Fullscreen / Windowed Mode
  925.                 // Recreate Our OpenGL Window
  926.                 if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen))
  927.                 {
  928.                     return 0;                       // Quit If Window Was Not Created
  929.                 }
  930.             }
  931.         }
  932.     }
  933.  
  934.     // Shutdown
  935.     KillGLWindow();                                 // Kill The Window
  936.     return (msg.wParam);                            // Exit The Program
  937. }
Add Comment
Please, Sign In to add comment