![](/img/trans.png)
[英]How to implement Bresenham's line algorithm in C when trying to draw a line in bmp file?
[英]Error in execution of my OpenGL C program to implement Bresenham's Line Drawing algorithm. Aborted (core dumped)
I tried implementing Bresenham's Line Drawing algorithm using C and OpenGL but I am getting an empty window which automatically closes and Aborted (core dumped)
is printed on my Cygwin Terminal.I then noticed that a a.exe.stackdump file is generated and then I做了一个cat a.exe.stackdump
,它显示了一些十六进制值,如下图所示。 我使用gcc 1lab.c -lglut -lglu -lgl
和./a.exe
编译了程序。 谁能解释我的代码有什么本质上的问题? output 和程序附在下面。
注意:请忽略任何注释,因为它们无关紧要,仅因为我通过注释我不需要的内容来修改现有代码而使用它们。
#include<GL/glut.h>
#include<stdio.h>
#include<string.h>
void init (void)
{
glClearColor(0.0, 1.0, 0.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
GLint **plotterresizer(GLint** plotter)
{
return realloc(plotter,2*sizeof(plotter));
}
int **logic (void)
{
GLint **plotter=(GLint**)malloc(2*sizeof(GLint*)) ;
GLint sp[]={9,18};
GLint ep[]={14,22};
GLint xy[]={0,0};
GLint dxdy[]={ep[0]-sp[0],ep[1]-sp[1]};
GLint pk = 2*dxdy[1]-dxdy[0];
GLint *xkyk = sp;
int i=1;
plotter[0]=sp;
while(memcmp(xkyk,ep,sizeof(ep))!=0){
if(pk>=0){
pk+=2*(dxdy[1]-dxdy[0]);
xkyk[0]++;
xkyk[1]++;
}
else{
pk+=2*dxdy[1];
xkyk[0]++;
}
if(i==sizeof(plotter))
plotter=plotterresizer(plotter);
plotter[i]=xkyk;
i++;
}
return plotter;
}
void lineSegment(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
GLint **plt = logic();
// logic();
glBegin(GL_LINE_STRIP);
for(i=0;i<sizeof(plt);i++){
glVertex2iv(plt[i]);
}
// glVertex2i (180,15);
// glVertex2i (10, 145);
// glVertex2i (21,34);
// glVertex2i (111,9);
glEnd();
glFlush ();
}
void main(int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (25,50);
glutInitWindowSize (200,150);
glutCreateWindow ("Bresemham's Line Drawing Algorithm");
init();
glutDisplayFunc (lineSegment);
glutMainLoop();
}
这不是答案,而是调试程序的指南,将有助于应用您刚刚收到的其他人的建议。
由于该程序是 X 图形程序,因此您不需要从Cygwin Terminal
运行,而是从XTerm
运行。 所以安装xinit
,运行startxwin
并启动Xterm
使用调试信息编译程序,不进行优化,然后运行 GDB:
$ gcc -Wall -ggdb -O0 prova.c -o prova -lGL -lGLU -lglut
$ gdb ./prova.exe
在 GDB 里面你会有
....
Reading symbols from ./prova.exe...
运行程序
(gdb) run
Starting program: /tmp/GL/prova.exe
[New Thread 57376.0xef14]
....
Thread 1 "prova" received signal SIGSEGV, Segmentation fault.
0x00007ff97139bebc in ig9icd64!DrvSetLayerPaletteEntries ()
from /cygdrive/c/WINDOWS/System32/DriverStore/FileRepository/ki124757.inf_amd64_b607c305e0c4e0a1/ig9icd64.dll
显示堆栈中的各种帧
(gdb) bt
#0 0x00007ff97139bebc in ig9icd64!DrvSetLayerPaletteEntries ()
from /cygdrive/c/WINDOWS/System32/DriverStore/FileRepository/ki124757.inf_amd64_b607c305e0c4e0a1/ig9icd64.dll
#1 0x000000010040127b in lineSegment () at prova.c:55
#2 0x00000003d2ffb35b in fghRedrawWindow () from /usr/bin/cygglut-3.dll
#3 0x00000003d2ffb6dc in fgProcessWork () from /usr/bin/cygglut-3.dll
#4 0x00000003d2ffd075 in fgEnumWindows () from /usr/bin/cygglut-3.dll
#5 0x00000003d2ffb7dd in glutMainLoopEvent () from /usr/bin/cygglut-3.dll
#6 0x00000003d2ffb876 in glutMainLoop () from /usr/bin/cygglut-3.dll
#7 0x0000000100401306 in main (argc=1, argv=0xffffcc70) at prova.c:75
转到代码中的第一个
(gdb) frame 1
#1 0x000000010040127b in lineSegment () at prova.c:55
55 glVertex2iv(plt[i]);
我们检查你的变量
(gdb) p i
$1 = 6
(gdb) p plt[0]
$8 = (GLint *) 0xffffc9d0
(gdb) p plt[2]
$9 = (GLint *) 0xffffc9d0
(gdb) p plt[5]
$10 = (GLint *) 0xffffc9d0
(gdb) p plt[6]
$11 = (GLint *) 0x1000000010
所以现在你知道它在哪里了。 似乎 plt[6] 指向完全错误的 memory 区域。 检查 object 的尺寸似乎不是 6。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.