c++ - 无法在 OpenGL 中绘制我的模型两次?

标签 c++ opengl

我想绘制一个椅子模型,我从 3ds max 导出为一个 txt 文件。我读取文件并在 OpenGL 中显示它(使用 Visual Studio -> C++)。我的问题是我必须多次乘以椅子 (10)。我确实尝试将它乘以两次,但它不起作用。程序因这些“程序未响应”Windows 错误而崩溃。我有一个主类,它像这样从 CDrawModel 调用所有必需的方法:

CDrawModel  mModel;


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

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize( 1000, 600 );
glutInitWindowPosition(100, 100);
glutCreateWindow("Georgi Koemdzhiev - 1306794");

glutKeyboardFunc(KeyResponse);

glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);

mModel.initGL();

glutMainLoop();
}

void DrawGLScene(void) {
   mModel.myDrawGLScene();
}


GLvoid ReSizeGLScene(GLsizei fwidth, GLsizei fheight) {
   mModel.MyReSizeGLScene(fwidth, fheight);
}

CDrawModal 初始化一个 CPolygonMesh 类型的对象,它处理从文件中读取和在屏幕上绘图的功能。我知道我的代码有效,因为我在屏幕上绘制了模态: enter image description here

这是我的 CDrawModal 类:

oid CDrawModel::initGL(void) {
glClearColor(1.0, 1.0, 1.0, 1.0);

near = 1.0;
far = 1000.0;
height = 1.5;


glCullFace(GL_BACK);            // don’t draw back facing faces
glEnable(GL_CULL_FACE);         // enable face culling
glPolygonMode(GL_FRONT, GL_LINE);   // select front polygons and draw edges only



}




 void CDrawModel::MyReSizeGLScene(int fwidth, int fheight) // Resize And             Initialize The GL Window{  


// Store window size so it can be accessed in myDrawGLScene()
wWidth = fwidth;
wHeight = fheight;

//  Set fovy so that the viewing frustum has the specified height at the 
//  near clipping plane
fovy = (360 / PI) * atan(height / (2.0 * near));

//  Calculate the aspect ratio of the VIEWPORT
//  so that we can set the camera’s aspect ratio to the same value
aspect_ratio = (double)fwidth / (double)fheight;


glMatrixMode(GL_PROJECTION);                    // Select The Projection Stack
glLoadIdentity();
/*  void glOrtho(   GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,


GLdouble farVal);*/
gluPerspective(90, aspect_ratio, near, far);  // perspective view
//glOrtho(-130.0,130.0,-130.0,130.0,near,far);

glViewport(0, 0, wWidth, wHeight);              // Viewport fills the window


// Print values of parameters   
cout << fixed;      // Use fixed-point notation
cout.precision(3);  // Number of digits after the decimal point

cout << "fovy = " << fovy << endl;
cout << "aspect_ratio = " << aspect_ratio << endl;
cout << "near = " << near << endl;
cout << "far = " << far << endl;

}

void CDrawModel::myDrawGLScene(GLvoid)      // Here's Where We Do All The    Drawing
  {
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // clear the drawing area
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

 glTranslatef(0.0, 0.0, -200);
 mesh.draw_model();


glutSwapBuffers(); // Needed if we're running an animation
glFlush();

}

为了绘制它两次我需要做什么。这是我在 CPolugonMesh 中的 drawModal 方法:

 void CPolygonMesh::draw_model(void){
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
 glColor3f(1.0, 0.0, 0.0); // draw red things

 glBegin(GL_TRIANGLES);                 
    for (int i = 0; i < Mesh_NumFaces; i++) {       
        // Look up the coordinates of each vertex
        // in vertex_list[] 
        glVertex3fv(vertex_list[ face_list[i][0] ]);
        glVertex3fv(vertex_list[ face_list[i][1] ]);
        glVertex3fv(vertex_list[ face_list[i][2] ]);

        //cout << "Drawing face: " << i << endl;
    }

 glEnd(); 

 }

最佳答案

您的 CPolygonMesh::draw_model(void) 方法实际上会在您每次绘制模型时清除屏幕,因此只有最后一次调用才会在屏幕上留下任何内容。

线

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  

完全错了。您已经在帧的开头清除了屏幕。

关于c++ - 无法在 OpenGL 中绘制我的模型两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35876311/

相关文章:

c++ - 使用 glDrawArrays() 的绘制函数需要调用两次才能显示任何内容

C++ 多态静态可变子函数指针

c - 如何在openGL立方体堆栈中的对象顶部打一个洞?

图像翻转,OpenGL 使用 libjpeg 输出到 JPEG

java - 禁用数组缓冲区对象时无法使用偏移量 - lwjgl

c++ - glCreateShader 给出相同的 ID

c++ - 跨命名空间的 typedef 解析

c++ - opencv cvtColor 断言失败 C++

检查可变参数模板中没有重复类型的 C++ 概念

c++ - 将 vector<T> 转换为 vector<const T>