2011-07-04 21 views
-1

Cで山と水がいっぱいの風景があります。私は風景に太陽を追加したいです。しかし、太陽は景色に現れません。以下は私のコードです。これを手伝ってください。エラーはありませんが、まだオブジェクトが表示されていません

// Include files 
#include <stdio.h> 
#include <windows.h> 
#include <gl\gl.h> 
#include "gl/Glu.h" 
#include "Gl/glut.h" 
#include<math.h> 
#include<conio.h> 

#include "resources.h" 
#include "fovdemo.h" 

// Global variables 
HINSTANCE hInstance; 
HWND hWnd; 
HDC hDC; 
HGLRC hRC; 
float cam_x, cam_y, cam_z; 
float cam_yaw, cam_pitch; 
float hfov, aspect; 
int fovmode; 
bool active; 
static int useRGB = 1; 
static int useLighting = 1; 
static int useFog = 0; 
static int useDB = 1; 
static int useLogo = 0; 
static int useQuads = 1; 

static int tick = -1; 
static int moving = 1; 

#define GREY 0 
#define RED  1 
#define GREEN 2 
#define BLUE 3 
#define CYAN 4 
#define MAGENTA 5 
#define YELLOW 6 
#define BLACK 7 
static float lightPos[4] = 
{2.0, 4.0, 2.0, 1.0}; 

static float lightAmb[4] ={0.2, 0.2, 0.2, 1.0}; 
static float lightDiff[4] ={0.8, 0.8, 0.8, 1.0}; 
static float lightSpec[4] ={0.4, 0.4, 0.4, 1.0}; 
static float fogColor[4] ={0.0, 0.0, 0.0, 0.0}; 
static float fogIndex[1] ={0.0}; 

static unsigned char shadowPattern[128] = 
{ 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, /* 50% Grey */ 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 
    0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55 
}; 



// Function prototypes 
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 
BOOL CALLBACK DummyProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM) { return false; } 

float GetTime() 
{ 
    return (float)GetTickCount()/1000.0f; 
} 

///////////////////////////////////////////////////////////////////////////////////////////// 
// WINMAIN FUNCTION 
///////////////////////////////////////////////////////////////////////////////////////////// 

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) 
{ 
    MSG msg; 
    WNDCLASSEX wc; 
    // Copy instance handle into global variable 
    hInstance = hThisInstance; 

    // Check for previous instance 
    if (hPrevInstance) 
    { 
     MessageBox(NULL, "Sorry, you can only start one instance of this program at a time.", "Can't Start Program", MB_OK); 
     return 0; 
    } 

    // Register window class 
    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.lpszClassName = "FovDemoClass"; 
    wc.lpfnWndProc = MainWndProc; 
    wc.hInstance = hInstance; 
    wc.hbrBackground = NULL; 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hIcon = wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
    wc.style = CS_VREDRAW | CS_HREDRAW; 
    wc.lpszMenuName = NULL; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    if (!RegisterClassEx(&wc)) return 0; 
     // Create main window 
    hWnd = CreateWindow("FovDemoClass", "Fov Demo", WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL); 
    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 

    //Sun Code 
    glMatrixMode(GL_MODELVIEW); 
    glPushMatrix(); 
    glLoadIdentity(); 
    glTranslatef(200, 300, 0.0f); 
    static const int circle_points = 100; 
    static const float angle = 2.0f * 3.1416f/circle_points; 
     // this code (mostly) copied from question: 
    glBegin(GL_POLYGON); 
    double angle1=0.0; 
    glVertex2d(50 * cos(0.0) , 50 * sin(0.0)); 
    int i; 
    for (i=0; i<circle_points; i++) 
    {  
     glVertex2d(50 * cos(angle1), 50 *sin(angle1)); 
     angle1 += angle; 
    } 
    glEnd(); 
    glPopMatrix(); 

    // Main message loop 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return msg.wParam; 
} 

///////////////////////////////////////////////////////////////////////////////////////////// 
// MAINWNDPROC FUNCTION 
///////////////////////////////////////////////////////////////////////////////////////////// 

LRESULT CALLBACK MainWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    static POINT mousepos; 
    static float last_frame_time; 

    switch (message) 
    { 
     case WM_CREATE: 
     { 
      HWND hWndLoading; 
      PIXELFORMATDESCRIPTOR pfd; 
      int nBestMatch; 
      // Show loading dialog box 
      SetCursor(LoadCursor(NULL, IDC_WAIT)); 
      hWndLoading = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_LOADING), hWnd, DummyProc); 
      ShowWindow(hWndLoading, SW_SHOWNORMAL); 
      UpdateWindow(hWndLoading); 

      // Get window DC 
      hDC = GetDC(hWnd); 

      // Set up the OpenGL pixel format for this window 
      memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); 
      pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); 
      pfd.nVersion = 1; 
      pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 
      pfd.iPixelType = PFD_TYPE_RGBA; 
      pfd.cColorBits = 32; 
      pfd.cDepthBits = 32; 
      nBestMatch = ChoosePixelFormat(hDC, &pfd); 
      SetPixelFormat(hDC, nBestMatch, &pfd); 

      // Set up OpenGL 
      hRC = wglCreateContext(hDC); 
      wglMakeCurrent(hDC, hRC); 
      OpenGL_Init(); 

      // Get rid of loading dialog 
      SetCursor(LoadCursor(NULL, IDC_ARROW)); 
      DestroyWindow(hWndLoading); 
      last_frame_time = GetTime(); 
      active = true; 
      fovmode = FF_NORMAL; 
      aspect = (float)GetSystemMetrics(SM_CXSCREEN)/GetSystemMetrics(SM_CYSCREEN); 
      hfov = 90.0f; 
      return 0; 


     } 

     case WM_ACTIVATE: 
     { 
      if (LOWORD(wParam) == WA_INACTIVE) 
      { 
       active = false; 
       ShowWindow(hWnd, SW_MINIMIZE);  // Minimize when alt-tabbed away, since it's a fullscreen window 
      } 
      else 
      { 
       active = true; 
       last_frame_time = GetTime(); 
       InvalidateRect(hWnd, NULL, false); 
      } 
      return 0; 
     } 

     case WM_PAINT: 
     { 
      if (active) 
      { 
       // Calculate time it took to draw last frame 
       float current_time = GetTime(); 
       float frametime = current_time - last_frame_time; 
       last_frame_time = current_time; 
       // Handle input, logic, whatnot 
       OpenGL_Logic(frametime); 
       // Render the scene 
       OpenGL_SetupView(fovmode); 
       OpenGL_Render(); 
       // Flush and flip 
       glFlush(); 
       SwapBuffers(hDC); 
      } 
      else 
      { 
       // If inactive, just rerender when we need to 
       OpenGL_SetupView(fovmode); 
       OpenGL_Render(); 
       glFlush(); 
       SwapBuffers(hDC); 
       ValidateRect(hWnd, NULL); 
      } 

      return 0; 
     } 

     case WM_CHAR: 
     { 
      // Respond to keypresses 
      switch (wParam) 
      { 
       // 'F' switches between preset fov modes 
       case 'f': 
       case 'F': 
       { 
        ++fovmode; 
        if (fovmode == NUM_FOVMODES) fovmode = 0; 

        switch (fovmode) 
        { 
         case FF_NORMAL:  // Normal fov, full frame viewport 
         { 
          aspect = (float)GetSystemMetrics(SM_CXSCREEN)/GetSystemMetrics(SM_CYSCREEN); 
          hfov = 90.0f; 
          break; 
         } 
         case WS_WIDE:  // Wide fov with a widescreen viewport (aspect ratio of 2.35, same as theatre) 
         { 
          aspect = 2.35f; 
          hfov = 140.0f; 
          break; 
         } 
         case FF_WIDE:  // Wide fov with a full-frame viewport 
         { 
          aspect = (float)GetSystemMetrics(SM_CXSCREEN)/GetSystemMetrics(SM_CYSCREEN); 
          hfov = 140.0f; 
          break; 
         } 
         case 'd': 
          { 
           if (useLighting) 
           { 
            glEnable(GL_LIGHTING); 
           } 
           glEnable(GL_LIGHT0); 
           glLightfv(GL_LIGHT0, GL_POSITION, lightPos); 
           glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb); 
           glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiff); 
           glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec); 
           glEnable(GL_NORMALIZE); 
           if (useFog){ 
            glEnable(GL_FOG); 
           } 
           glFogfv(GL_FOG_COLOR, fogColor); 
           glFogfv(GL_FOG_INDEX, fogIndex); 
           glFogf(GL_FOG_MODE, GL_EXP); 
           glFogf(GL_FOG_DENSITY, 0.5); 
           glFogf(GL_FOG_START, 1.0); 
           glFogf(GL_FOG_END, 3.0); 
           glEnable(GL_CULL_FACE); 
           glCullFace(GL_BACK); 
           glShadeModel(GL_SMOOTH); 
           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
           if (useLogo) { 
           } 
           else { 
            glPolygonStipple((const GLubyte *) shadowPattern); 
           } 

           glClearColor(0.0, 0.0, 0.0, 1); 
           glClearIndex(0); 
           glClearDepth(1); 

          } 
        break; 
       } 

       // Escape key exits 
       case 27: 
       { 
        DestroyWindow(hWnd); 
        break; 
       } 
      } 

      // Make sure window is redrawn 
      InvalidateRect(hWnd, NULL, false); 

      return 0; 
      } 

     case WM_LBUTTONDOWN: 
     { 
      if (active) 
      { 
       // Capture mouse and hide cursor 
       SetCapture(hWnd); 
       ShowCursor(false); 

       // Save off mouse position 
       GetCursorPos(&mousepos); 
      } 

      return 0; 
     } 


     case WM_MOUSEMOVE: 
     { 
      // Move view angles if dragging left button 
      if (active && (wParam & MK_LBUTTON)) 
      { 
       POINT pt; 
       int deltax, deltay; 
       bool wrap = false; 

       // Get current mouse position 
       GetCursorPos(&pt); 

       // Calculate deltas 
       deltax = pt.x - mousepos.x; 
       deltay = pt.y - mousepos.y; 

       // Wrap mouse position if needed 
       if (pt.x == GetSystemMetrics(SM_CXSCREEN) - 1) 
        pt.x = 0, wrap = true; 
       else if (pt.x == 0) 
        pt.x = GetSystemMetrics(SM_CXSCREEN) - 1, wrap = true; 
       if (pt.y == GetSystemMetrics(SM_CYSCREEN) - 1) 
        pt.y = 0, wrap = true; 
       else if (pt.y == 0) 
        pt.y = GetSystemMetrics(SM_CYSCREEN) - 1, wrap = true; 
       if (wrap) 
        SetCursorPos(pt.x, pt.y); 

       // Save off new mouse position 
       mousepos = pt; 

       // Change view angles accordingly 
       cam_yaw -= (float)deltax * MOUSE_CAMERA_ANGLE_FACTOR; 
       cam_pitch -= (float)deltay * MOUSE_CAMERA_ANGLE_FACTOR; 

       // Wrap yaw and clamp pitch 
       if (cam_yaw > 360.0f) cam_yaw -= 360.0f; 
       if (cam_yaw < 0.0f) cam_yaw += 0.0f; 
       if (cam_pitch > 90.0f) cam_pitch = 90.0f; 
       if (cam_pitch < -90.0f) cam_pitch = -90.0f; 

       InvalidateRect(hWnd, NULL, false); 
      } 

      return 0; 
     } 

     case WM_LBUTTONUP: 
     { 
      if (active) 
      { 
       // Release mouse capture and show cursor 
       ReleaseCapture(); 
       ShowCursor(true); 
      } 
      return 0; 
     } 

     case WM_DESTROY: 
     { 
      // Shut down OpenGL 
      wglMakeCurrent(NULL, NULL); 
      wglDeleteContext(hRC); 
      ReleaseDC(hWnd, hDC); 
      PostQuitMessage(0); 
      return 0; 
     } 

     default: 
     { 
      return DefWindowProc(hWnd, message, wParam, lParam); 
     } 
    } 
} 
} 
+1

コードをフォーマットする必要があります。コードは完全に読み取れません。 – SurvivalMachine

+2

GLUTヘッダーを使用していない場合、 '#include'のポイントは何ですか?また、シーンをレンダリングするコードを投稿することもできます。 – genpfault

+2

@genpfault:彼は実際に太陽を「追加」するコードを掲載しました。 WinMainでは、誤ってOpenGLが何らかのシーングラフであると仮定していました。 – datenwolf

答えて

3

OpenGLはシーングラフではなく、描画APIです。 オブジェクトを「追加」しない場合は、描画ルーチンでを描画します。 WinMainには、OpenGLの描画関数がたくさん呼ばれています。それらに何らかの効果があったとしても(おそらくそうでなかった)、すぐに、つまり次のウィンドウが再描画されるまで、結果はただちに表示されます。

WM_PAINTメッセージのメッセージハンドラでは、一部のOpenGL_*関数が呼び出されます。太陽の描画コードをそこに置く必要があります。

関連する問題