c++ - OpenGL:当两个对象与线绑定(bind)时卡住

标签 c++ opengl codeblocks freeglut

我的问题是我一直坚持创建简单的对象。

我正在使用 Code::Blocks 和 freeGlut。一开始我创建了一个黑色矩形,一切顺利。比我想添加曲线,但曲线点在 .txt 文件中的位置。我已将文件添加到项目中,然后添加了曲线。构建 gose 很好,但是当我运行项目时,我看到矩形的底部与曲线的左侧绑定(bind)。

我不知道连接矩形和曲线的黑线是从哪里来的。

下面是我的代码:

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void Display()
{
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );    
glClear( GL_COLOR_BUFFER_BIT );    
glColor3f( 0.0f, 0.0f, 0.0f );    
glBegin( GL_POLYGON );    
glVertex3f( -4, -150.0, 0.0 );
glVertex3f( -4, 150.0, 0.0 );
glVertex3f( 4, 150.0, 0.0 );
glVertex3f( 4, -150.0, 0.0 );

float XP,RE,IM;

ifstream plik;
plik.open("swop.txt");

ofstream pliko;
pliko.open("test.txt");

while(!plik.eof())
{
    plik >> XP >> RE >> IM;
    if(plik.good())
    {
        glPointSize( 2 );
        glBegin( GL_POINTS );
        glVertex3f(XP,100*RE,0);
        pliko << setprecision(5) << fixed;
        pliko << "    " << XP << "    " << setw(5) << RE << "    " << setw(5) << IM << "\n";
        glEnd();
        glFlush();
    }
 else break;
}
plik.close();
pliko.close();

glEnd();
glFlush();
glutSwapBuffers();
}

int main( int argc, char * argv[] )
{
glutInit( & argc, argv );   
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );    
glutInitWindowSize( 800, 400 );
glutCreateWindow( "Powierzchnia Swobodna" );
glutDisplayFunc( Display );
gluOrtho2D(-100,100,-150,150); 
glutMainLoop();
return 0;
}

当我从 void Display() 剪切曲线时它只显示多边​​形,当我粘贴曲线时出现线:

image

遇到这种情况怎么办?

最佳答案

将第二个 glEnd 移动到开始渲染曲线之前 的点,以完成多边形。
在最后一次 glVertex 调用之后很常见。

关于c++ - OpenGL:当两个对象与线绑定(bind)时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21435905/

相关文章:

c++ - Assimp 加载发射图

c++ - 代码块 - 混合 c 和 c++ 文件

c++ - 为什么代码块找不到 header ,即使它在那里?

c++ - 我可以在统一包装器中调用包含 block 的 iOS 代码吗?

c# - 在 Visual Studio C++ 中使用 DLL

c++ - (A + B + C) ≠ (A + C + B​) 和编译器重新排序

c++ - 旋转 4 圈后物体回到初始位置

c++ - 我的应用程序窃取焦点

C++ OpenGL : How to make a second circle over the cylinder?

c++ - 如何更改 code::blocks 中的文本颜色和控制台颜色?