Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多段线仅在调整窗口大小后完全渲染_C++_Opengl - Fatal编程技术网

C++ 多段线仅在调整窗口大小后完全渲染

C++ 多段线仅在调整窗口大小后完全渲染,c++,opengl,C++,Opengl,我正在OpenGL中制作一个3d项目,其中包含一个地面(绘制为线循环)。我遇到的问题是,当项目开始时,仅绘制一条直线,如下图所示: 当我调整窗口大小或最大化窗口时,实际地面显示如下: 你知道如何解决这个问题吗?我是OpenGL编程的初学者 代码如下: void drawHook(void); void timer(int); void drawFlorr(); float L = 100; const int screenWidth = 1000; // width of scr

我正在OpenGL中制作一个3d项目,其中包含一个地面(绘制为线循环)。我遇到的问题是,当项目开始时,仅绘制一条直线,如下图所示:

当我调整窗口大小或最大化窗口时,实际地面显示如下:

你知道如何解决这个问题吗?我是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();


        }
    }
}
void挂钩(void);
无效计时器(int);
void drawFlorr();
浮点数L=100;
常量int屏幕宽度=1000;//屏幕窗口的宽度(以像素为单位)
常数int屏幕高度=1000;//屏幕窗口的高度(像素)
浮点数ww=800;
浮动wh=800;
浮点数f=520,n=10.0;
静态GL1[]={-200200,-33140};
静态GLdouble查看器[]={525,25,-180};
静态GLdouble objec[]={525.0,25,-350};
浮动x,y=0.0,z,z1;
float xmax=屏幕宽度-200.0;
浮动zmax=屏幕宽度-200.0;
浮动xmin,zmin;
浮动步长=5.0;
浮动视野=80;
void myInit(void)
{
glClearColor(0.0,0.0,0.0,0.0);//背景色为白色
glPointSize(2.0);//点是2乘2像素
glMatrixMode(GL_投影);
glLoadIdentity();
gluOrtho2D(0.0,屏幕宽度,0.0,屏幕高度);//恐龙窗口
glViewport(0,0,屏幕宽度,屏幕高度);
}
void myDisplay(void)
{
glClear(GLU颜色缓冲位);
glLoadIdentity();
gluLookAt(查看器[0],查看器[1],查看器[2],对象[0],对象[1],对象[2],0,1,0);
glMatrixMode(GL_投影);
glLoadIdentity();
视野(fov,1.333,n,f);
glPointSize(2.0);
glMatrixMode(GLU模型视图);
drawFlorr();
glutSwapBuffers();
}
int main(int argc,字符**argv)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//设置显示模式
GLUTINITWindowsSize(屏幕宽度、屏幕高度);//设置窗口大小
glutInitWindowPosition(10,10);//在屏幕上设置窗口位置
glutCreateWindow(“恐龙线描”);//打开屏幕窗口
glutDisplayFunc(myDisplay);//寄存器重画函数
myInit();
//glutTimerFunc(1,定时器,1);
glutMainLoop();//进入永久循环
返回1;
}
void drawFlorr()
{
xmin=-100;
zmin=-100;
对于(x=xmin;x
您的代码在许多方面被破坏:

  • myDisplay
    函数使用当前矩阵模式设置视图矩阵
  • 最初,在
    myInit()
  • 这两个加在一起意味着,对于第一帧,您只需使用标识作为
    MODELVIEW
    matrix,然后只需覆盖投影矩阵两次。调整大小后,将再次绘制框架,并且您的代码不会达到您可能想要的效果

    然而,还有更多:

  • 您没有任何调整大小处理程序,因此在调整窗口大小时视口不会更改
  • 您正在为投影初始设置正交矩阵,尽管您根本不打算使用它
  • 最重要的一点是:

  • 您的所有代码都依赖于不推荐使用的功能,而这些功能在现代OpenGL中根本不可用。你不应该在2016年使用它,而应该学习现代OpenGL(这里的“现代”意思是“只有十年历史”)

  • 感谢@derhass对it工作的建议,我一定会遵循现代开放式gl