2016-12-30 45 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 –