gpt4 book ai didi

C++/OpenGL - 数组指针

转载 作者:行者123 更新时间:2023-11-28 08:07:38 25 4
gpt4 key购买 nike

我目前在将鼠标坐标存储到一个数组中时遇到问题,在该数组中,该数组的指针被传递给一个函数,该函数随后将使用这些坐标在屏幕上显示多段线。

下面是我的代码。请注意,我已经评论了问题似乎出在哪里,但我想我还是会粘贴大部分代码:

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++/OpenGL - 数组指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9939369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com