2016-12-30 42 views
1

我正在OpenGL中製作一個包含地面(繪製爲線循環)的3D項目。我的問題是當只有一個單一的線繪製如圖所示的下一個圖像中的項目啓動:Polyline只在調整窗口大小後才完成渲染

enter image description here

當我調整或將窗口最大化,那麼實際地被顯示如下:

enter image description here

任何想法如何解決這個問題?我是OpenGL編程的初學者。

下面是代碼:

void drawHook(void); 
void timer(int); 
void drawFlorr(); 
float L = 100; 

const int screenWidth = 1000;  // width of screen window in pixels 
const int screenHeight = 1000;  // height of screen window in pixels 
float ww = 800; 
float wh = 800; 
float f = 520, n = 10.0; 
static GLdouble ort1[] = { -200, 200, -33, 140 }; 
static GLdouble viewer[] = { 525, 25, -180 }; 
static GLdouble objec[] = { 525.0, 25, -350 }; 
float x, y = 0.0, z, z1; 
float xmax = screenWidth - 200.0; 
float zmax = screenWidth - 200.0; 
float xmin, zmin; 
float step = 5.0; 

float fov = 80; 

void myInit(void) 
{ 
     glClearColor(0.0,0.0,0.0,0.0);  // background color is white 

    glPointSize(2.0);     // a 'dot' is 2 by 2 pixels 
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity(); 
    gluOrtho2D(0.0, screenWidth, 0.0, screenHeight);//dino window 
    glViewport(0, 0, screenWidth, screenHeight); 

} 

void myDisplay(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glLoadIdentity(); 
    gluLookAt(viewer[0], viewer[1], viewer[2], objec[0], objec[1], objec[2], 0, 1, 0); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(fov, 1.333, n, f); 
    glPointSize(2.0); 
    glMatrixMode(GL_MODELVIEW); 

    drawFlorr(); 


    glutSwapBuffers(); 


} 

int main(int argc, char** argv) 
{ 

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode 
    glutInitWindowSize(screenWidth, screenHeight); // set window size 
    glutInitWindowPosition(10, 10); // set window position on screen 
    glutCreateWindow("Dino Line Drawing"); // open the screen window 
    glutDisplayFunc(myDisplay);  // register redraw function 
    myInit();    
    //glutTimerFunc(1,timer,1); 
    glutMainLoop();    // go into a perpetual loop 
    return 1; 
} 
void drawFlorr() 
{ 

    xmin = -100; 
    zmin = -100; 

    for (x = xmin; x < xmax; x += step) 
    { 
     for (z = zmin; z < zmax; z += step) 
     { 
      z1 = -z; 

      glBegin(GL_LINE_LOOP); 

      glVertex3f(x, y, z1); 
      glVertex3f(x, y, z1-step+1.0); 
      glVertex3f(x + step - 1.0, y, z1 - step + 1.0); 
      glVertex3f(x+step-1.0, y, z1); 

      glEnd(); 


     } 
    } 
} 

回答

2

您的代碼在很多方面打破:

  1. myDisplay功能使用任何當前的矩陣模式是設置視圖矩陣。
  2. 最初,你離開矩陣模式GL_PROJECTIONmyInit()

這兩個共同表示,對於第一幀,你只需要使用身份MODELVIEW矩陣,只是簡單地覆蓋投影矩陣的兩倍。調整大小後,再次繪製框架,並且您的代碼確實可能適合您。

然而,還有更多:

  • 您沒有任何調整大小的處理程序,所以當你調整窗口的大小視口不會改變。
  • 您正在爲投影設置初始矩陣,儘管您並未計劃使用它。
  • 和最進口點:

  • 的所有代碼所依賴的棄用功能,這是甚至在現代的OpenGL可在所有。你應該不會在2016年使用它,而應該學習現代OpenGL(與「現代」意味着「僅十年前的這裏」)。
  • +0

    而且清晰的顏色不是白色的。 – BDL

    +0

    感謝@derhass爲你的suggesstion工作,我一定會遵循現代Open gl <3 –