【问题标题】:OpenGL not displaying 2D linesOpenGL不显示二维线
【发布时间】:2019-02-17 06:57:00
【问题描述】:

我正在使用 Ubuntu 18.04 LTS 并尝试使用 OpenGL 实现 2d 行,但它没有显示任何内容。我正在使用 ubuntu 编译器。我已经使用 终端 安装了库。

这是我的代码

#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_LINES);
    glVertex2i(10,10);
    glVertex2i(100,100);
    glEnd();

    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("points and lines");
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

这就是我编译和运行程序的方式

 g++ jamshaid.cpp -o jamoo -lglut -lGLU -lGL
./jamoo

更新 1

添加了 init2D 并尝试了其他答案,但没有奏效。它甚至不会改变窗口的标题。

更新 2

this 站点更新代码后。我现在有这个代码并且它正在工作。你能解释一下为什么会这样吗?谢谢

#include <GL/glut.h>

 void init2D(float r, float g, float b)
 {
 glClearColor(r,g,b,0.0);  
 glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
    glVertex2i(10,10);
    glVertex2i(100,100);
 glEnd();
 glFlush();
 }

int main(int argc,char *argv[])
 {
 glutInit(&argc,argv);
 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize (500, 500);
 glutInitWindowPosition (100, 100);
 glutCreateWindow ("points and lines");
 init2D(0.0,0.0,0.0);
 glutDisplayFunc(display);
 glutMainLoop();
 return 0;}

【问题讨论】:

  • 你为什么以 root 运行 GCC? init2D() 的代码在哪里?
  • @genpfault 它在显示功能中。不是吗?事实上我是opengl的初学者
  • 如果我不root它。结果是一样的
  • 我在任何地方都没有看到 init2D() 的定义,只是在 main() 中调用。
  • 即使没有它也可以工作。

标签: c++ linux opengl glut


【解决方案1】:

如示例here所示,在绘制之前需要一个正确的init2D函数来初始化3D投影矩阵。

void init2D(float r, float g, float b)
{
    glClearColor(r,g,b,0.0);  
    glMatrixMode (GL_PROJECTION);
    gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}

【讨论】:

  • 请看问题
  • @TungLeThanh 请看一下答案...因为它告诉您您没有有效的视口...我认为默认值为 1 x 1 ...然后您开始在位置 10 绘制。增加像素只会拉伸 1×1 视口。
  • 能否请您再看一遍问题并解释您的建议?
  • 由于窗口大小为(500, 500),建议在同一范围内设置一个正交投影。因此,世界坐标将 1:1 投影到窗口坐标:gluOrtho2D (0.0, 500.0, 0.0, 500.0)
【解决方案2】:

正如 Tung Le Thanh 告诉你的,你需要一个正确的投影矩阵初始化,这就是它的作用:

glMatrixMode (GL_PROJECTION)

glMatrixMode — 指定哪个矩阵是当前矩阵

                GL_PROJECTION
                    Applies subsequent matrix operations to the projection matrix stack.
                GL_TEXTURE
                    Applies subsequent matrix operations to the texture matrix stack.
                GL_COLOR
                    Applies subsequent matrix operations to the color matrix stack.
                
        To find out which matrix stack is currently the target of all matrix
        operations, call glGet with argument GL_MATRIX_MODE. The initial
        value is GL_MODELVIEW.

gluOrtho2D

gluOrtho2D — 定义一个二维正交投影矩阵

规格

void gluOrtho2D(GLdouble left,  GLdouble right,  GLdouble bottom,  GLdouble top); 

参数

  left, right
            Specify the coordinates for the left and right vertical clipping planes.
  bottom, top
            Specify the coordinates for the bottom and top horizontal clipping planes.

说明

        gluOrtho2D sets up a two-dimensional orthographic viewing region.  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2015-07-10
    相关资源
    最近更新 更多