【问题标题】:OpenGL Not Drawing AnythingOpenGL不绘制任何东西
【发布时间】:2016-09-01 05:06:20
【问题描述】:

只是尝试使用 glut 为 openGL 3.0 版在两点之间画一条线。这是代码。

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

using namespace std;

void init(void) {

    glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,30.0,0.0,30.0);

}

void setPixel(GLint x, GLint y) {

    glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,30.0,0.0,30.0);

}

float roundValue(float v) {

    return floor(v + 0.5);
}

void lineDDA(int xa, int ya, int xb, int yb) {

    double dx = xb-xa, dy = yb-ya, steps;
    float xInc, yInc, x = xa, y = ya;    
    if (abs(dx) > abs(dy))
        steps = abs(dx);
    else
        steps = abs(dy);
    xInc = dx/(float)steps;
    yInc = dy/(float)steps;
    setPixel(x, y);
    int k;
    for (k = 0; k < steps; ++k) {
        /* code */
        x += xInc;
        y += yInc;
        setPixel(roundValue(x), roundValue(y));
    }
}

void update() {
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(5.0f);
    glColor3f(1, 0, 0);  
    lineDDA(1, 1, 8, 7);
    lineDDA(1, 1, 8, 2);
    glFlush();
}

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

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(200,100);
    glutInitWindowSize(640,480);
    glutCreateWindow("Tutorial-1: Q4");
    glLoadIdentity();
    init();
    glutDisplayFunc(update);
    glutMainLoop();
    return 0;
}

但这并不是在窗口上绘制任何东西。它只是保持白色。任何人都可以提出解决方案吗?

【问题讨论】:

  • 为什么setPixel() 只是init() 的复制/粘贴?

标签: c++ opengl


【解决方案1】:

您的setPixel 函数不使用其参数xy。您没有启用线渲染模式或添加要绘制的顶点。

修复至少包括以下步骤:

  • glPointSize(5.0f);之后调用glBegin(GL_LINES);

  • glFlush();之前调用glEnd()

setPixel 修改为如下所示:

void setPixel(GLint x, GLint y)
{
    glVertex2f(x, y);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多