简体   繁体   English

打开gl画线错误?

[英]open gl drawing a line error?

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>

#define ROUND(a) ((int)+.5)

void display(void) {    
    int xa = 10, ya = 3, xb = 56, yb = 98;
    int dx = xb - xa, dy = yb - ya, steps, k;
    float xincre, yincre, x = xa, y = ya;
    glColor3f(0.0f, 0.0f, 1.0f);
    if (abs(dx) > abs(dy))
        steps = abs(dx);
    else
        steps = abs(dy);
    xincre = dx / (float)steps;
    yincre = dy / (float)steps;

    glBegin(GL_POINTS);
    glVertex2s(ROUND(x), ROUND(y));
    for (k = 0; k < steps; k++) {
        x += xincre;
        y += yincre;
        glVertex2s(ROUND(x), ROUND(y));
        printf("%f,%f", x, y);
    }
    glEnd();
    glFlush();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("CpViewer");
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

i tried to implement the dda algorithm for making a line but it seems just to print a point . 我试图实现dda算法来画一条线,但似乎只是打印一个点。 i am not able to understand where is the problem ? 我不明白问题出在哪里? i only get a blue point as the output in the white background 我只得到一个蓝点作为白色背景中的输出

The drawing coordinates are defined in terms of a normalized rectangle (x and y each between -1 and 1), not in terms of window coordinates. 绘制坐标是根据标准化矩形(x和y分别在-1和1之间)定义的,而不是根据窗口坐标定义的。 So, the left-most x coordinate for drawing is 0 in window coordinates and -1.0 in drawing coordinates, and the right-most x coordinate is the window width (500 in this case) in window coordinates, and 1.0 in drawing coordinates. 因此,用于绘制的最左x坐标在窗口坐标中为0,在绘制坐标中为-1.0,最右x坐标在窗口坐标中为窗口宽度(在这种情况下为500),在绘制坐标中为1.0。 The y-coordinates are the same, but the y-axis is flipped from window coordinates to drawing coordinates (-1 in drawing coordinates would be 500 in window coordinates). y坐标相同,但是y轴从窗口坐标翻转到图形坐标(图形坐标中的-1在窗口坐标中为500)。

Try drawing the points with glVertex2f and rescaling the points by the window size: 尝试使用glVertex2f绘制点,然后按窗口大小重新缩放点:

glBegin(GL_POINTS);
for (k = 0; k <= steps; k++) {
    glVertex2f((x - 250.f)/250.f, (250.f - y)/250.f);
    printf("%f,%f", x, y);
    x += xincre;
    y += yincre;
}
glEnd();

You'll also need to keep track of if the window gets resized, to make sure that you're scaling these coordinates correctly, but for the moment, this should work. 您还需要跟踪窗口是否被调整大小,以确保您正确地缩放了这些坐标,但是目前,这应该可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM