gpt4 book ai didi

c++ - 折线仅在调整窗口大小后完全渲染

转载 作者:行者123 更新时间:2023-12-01 14:50:40 25 4
gpt4 key购买 nike

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

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();


}
}
}

最佳答案

您的代码在很多方面都被破坏了:

  • 您的 myDisplay函数使用当前矩阵模式来设置 View 矩阵。
  • 最初,您将矩阵模式保留为 GL_PROJECTIONmyInit()

  • 这两个一起意味着对于第一帧,您只需将身份用作 MODELVIEW矩阵,只需覆盖投影矩阵两次。调整大小后,再次绘制框架,并且您的代码确实符合您的意图。

    然而,还有更多:
  • 您没有任何调整大小处理程序,因此当您调整窗口大小时,您的视口(viewport)不会改变。
  • 您最初为投影设置了一个正交矩阵,尽管您根本不打算使用它。

  • 而且,最重要的一点:
  • 您的所有代码都依赖于已弃用的功能,这些功能甚至在现代 OpenGL 中根本不可用。你真的不应该在 2016 年使用它,而是学习现代 OpenGL(这里的“现代”意思是“只有十年历史”)。
  • 关于c++ - 折线仅在调整窗口大小后完全渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41396995/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com