OpenGL不会画图,但算法是正确的
OpenGL doesn't paint, but the algorithm is correct
您好,我正在用 openGl 实现 bresenham 算法。计算公式正确,
它无法在屏幕上正确绘制。问题是什么?
即使将值放入硬编码中也不会绘制。
它编译得很好。
#include <iostream>
#include <GLUT/glut.h>
void sp(int x, int y){
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void bsh_al(int xs,int ys,int xe,int ye){
int x,y=ys,W=xe-xs,H=ye-ys;
int F=2*H-W,dF1=2*H, dF2=2*(H-W);
for(x=xs;x<=xe;x++){
sp(x,y);
std::cout << "x : "<< x << " | y : "<< y << std::endl;
if(F<0)
F+=dF1;
else{
y++;
F+=dF2;
}
}
}
void Draw() {
bsh_al(1,1,6,4);
glFinish();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("OpenGL");
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
编码 Xcode
顶点坐标必须在 [-1.0, 1.0] 范围内。左下坐标是(-1, -1),右下坐标是(1, 1)。
如果要以像素为单位指定顶点坐标,则必须通过glOrtho
设置正交投影。例如:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("OpenGL");
glutDisplayFunc(Draw);
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
glMatrixMode(GL_PROJECTION);
glOrtho(0, width, height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}
您好,我正在用 openGl 实现 bresenham 算法。计算公式正确, 它无法在屏幕上正确绘制。问题是什么? 即使将值放入硬编码中也不会绘制。 它编译得很好。
#include <iostream>
#include <GLUT/glut.h>
void sp(int x, int y){
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void bsh_al(int xs,int ys,int xe,int ye){
int x,y=ys,W=xe-xs,H=ye-ys;
int F=2*H-W,dF1=2*H, dF2=2*(H-W);
for(x=xs;x<=xe;x++){
sp(x,y);
std::cout << "x : "<< x << " | y : "<< y << std::endl;
if(F<0)
F+=dF1;
else{
y++;
F+=dF2;
}
}
}
void Draw() {
bsh_al(1,1,6,4);
glFinish();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("OpenGL");
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
编码 Xcode
顶点坐标必须在 [-1.0, 1.0] 范围内。左下坐标是(-1, -1),右下坐标是(1, 1)。
如果要以像素为单位指定顶点坐标,则必须通过glOrtho
设置正交投影。例如:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("OpenGL");
glutDisplayFunc(Draw);
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
glMatrixMode(GL_PROJECTION);
glOrtho(0, width, height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}