尝试使用glut for 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;
}
但这并没有在窗户上画任何东西。它只是保持白色。有人可以建议一个解决方案?
答案 0 :(得分:1)
您的setPixel
函数不使用其参数x
和y
。您没有启用线渲染模式或添加要绘制的顶点。
修复至少包含以下步骤:
在glBegin(GL_LINES);
glPointSize(5.0f);
在glEnd()
glFlush();
将setPixel
修改为如下所示:
void setPixel(GLint x, GLint y)
{
glVertex2f(x, y);
}