C++/OpenGL-数组的指针
C++ / OpenGL - Pointer of an array
我目前在将鼠标坐标存储到一个数组中时遇到问题,在该数组中,该数组的指针将传递给一个函数,然后该函数将使用这些坐标在屏幕上显示多段线。
下面是我的代码。注意,我已经评论了问题的位置,但我想我无论如何都会粘贴大部分代码:
struct mousePoint{
int x, y;
};
struct Node{
mousePoint *pointer;
int corner;
Node *next;
};
Node *Top;
Node *Bottom;
void init(void){ // doesnt need to be shown, but initialises linked list
// Adds the mouse coords array to the top of the linked list
void AddLines(mousePoint Lines[], int corner){
Node *temp;
temp = new Node;
cout << "(AddLines func) array x1: ";
cout << Lines[0].x << endl;
cout << "(AddLines func) array y1: ";
cout << Lines[0].y << endl;
cout << "(AddLines func) array x2: ";
cout << Lines[1].x << endl;
cout << "(AddLines func) array y1: ";
cout << Lines[1].y << endl;
temp->pointer = Lines; // <--- I believe this is the error
temp->corner = corner;
temp->next = NULL;
if(Top == NULL){
Top = temp;
}else{
temp->next = Top;
Top = temp;
if(Bottom == NULL){
Bottom = Top;
}
}
}
// Draws the polyline based on the coords in the array
void DrawLines(mousePoint Lines[], int corner){
cout << "(DrawLines func) array x1: ";
cout << Lines[0].x << endl;
cout << "(DrawLines func) array y1: ";
cout << Lines[0].y << endl;
cout << "(DrawLines func) array x2: ";
cout << Lines[1].x << endl;
cout << "(DrawLines func) array y1: ";
cout << Lines[1].y << endl;
glBegin(GL_LINE_STRIP);
for(int i = 0; i < corner; i++){
glVertex2i(Lines[i].x, Lines[i].y);
}
glEnd();
}
void display(void){
Node *current;
current = Top;
// cycle through all polylines in linked list
for(; current != NULL; current = current->next){
DrawLines(current->pointer, current->corner);
}
glFlush();
}
void mouse(int button, int state, int x, int y)
{
static mousePoint Lines[100]; // array to store mouse coords
static int NumCorner = 0; // counter for mouse click
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
Lines[NumCorner].x = x;
Lines[NumCorner].y = 480 - y;
// draw individual points
glBegin(GL_POINTS);
glVertex2i(Lines[NumCorner].x, Lines[NumCorner].y);
glEnd();
NumCorner++;
}else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
AddLines(Lines, NumCorner); // add mouse coords to linked list
NumCorner = 0; // reset counter back to 0
}
}
int main(int argc, char** argv) // doesnt need to be shown
基本上,当我用鼠标点击屏幕时,应该画一个点,同时,这些坐标被保存到一个数组中。用户可以在保存坐标的同时,继续用鼠标左键单击以绘制点。直到按下鼠标右键(数组存储在链表中),才会绘制线条。链接列表用于存储所有不同的多段线形状。然而,问题是指针没有正确指向数组,drawlines函数也没有正确绘制线条。
例如,如果单击显示器上的两个点(注意代码中的cout语句),然后右键单击,则会使用这两个点绘制一条线。但是,如果我单击另一个点,则会在没有按下鼠标右键的情况下从以前的坐标中绘制一条线。
(AddLines func) array x1: 338
(AddLines func) array y1: 395
(AddLines func) array x2: 325
(AddLines func) array y1: 308
(DrawLines func) array x1: 338
(DrawLines func) array y1: 395
(DrawLines func) array x2: 325
(DrawLines func) array y1: 308
(DrawLines func) array x1: 383
(DrawLines func) array y1: 224
(DrawLines func) array x2: 325
(DrawLines func) array y1: 308
注意到它是如何在不将其添加到指针的情况下绘制一条额外的线的吗?
我曾经尝试过使用memcpy——如果使用的点不到5-6,那么线条就画得很完美,再使用的点就会导致应用程序崩溃。因此,我认为这是一个指针问题
好吧,我想我明白了。
您使用的是静态数组Lines[]。当您执行"temp->pointer=Lines;"时,您指向的是每个多边形的静态数组。因此,当绘制第一个多边形,然后尝试开始第二个多边形时,您正在编辑第一个多边形的线。
您可以复制for循环中的每个点,迭代"角点"次数,而不是您已经发现有问题的线。
相反:
temp->pointer = Lines; // <--- I believe this is the error
尝试:
temp->pointer = new mousePoint[corner];
for(int a = 0; a < corner; a++)
{
temp->pointer[a] = Lines[a];
}
相关文章:
- 指向指向字符数组的指针数组的指针
- 通过指向指针数组的指针访问子类的属性
- C++,指针数组,指向双链表中的条目
- 在C++中,如何初始化指向wchar_t*的指针数组(生成wchar_t**)
- C++从函数指针数组调用函数
- 关于指向指针数组的指针
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 将链表转换为指针数组时出错
- C++ 对象指针数组的复制构造函数
- C++ - 循环访问指针数组会导致错误
- 删除指针数组 (C++) 中的元素
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 将函数指针数组中的函数指针作为模板参数传递
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 具有推导参数的模板函数指针数组变量
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- C++指针数组到字符数组中的特定位置
- 如何在C++中复制指针数组的数据
- 初始化类中的指针数组,并在另一个类中检索它
- 尽管直接设置了指针数组,但仍为空