gpt4 book ai didi

c++ - 将 float 保存在文本文件中并从文本文件中加载它们(OpenGL 和 C++)

转载 作者:行者123 更新时间:2023-11-28 05:10:07 26 4
gpt4 key购买 nike

我试图将 float 从 v1X、v1Y 保存到 v10X、v10Y 到一个名为 verticest.txt 的文本文件中,然后将它们加载回去,但是 visual studio 说它们已经定义好了,包括 starX 和 starY 数组,t 和 ti在 graphics2dassignment.cpp 文件中

示例错误:

错误 LNK1169:找到一个或多个定义的符号

error LNK2005: "float * starX"(?starX@@3PAMA)already defined in Graphics2DAssignment.obj

starY出现同样的错误,从v1X,v1Y到v10X,V10Y

原始.cpp

  #include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <gl\GL.h>
#include <gl\GLU.h>
#include "glut.h"
#include <fstream>
#include <math.h>
#include "primitive.h"

using namespace std;

GLuint listname = 1;
float vertices[10][2];


//saves the coordinates in a text file

void saveCoordinates()
{
ofstream outfile("vertices.txt");

v1X = 0;
v1Y = 1;
outfile << v1X << " " << v1Y << endl;;

v2X = v1X * cos(3.1415 * 72 / 180) - v1Y * sin(3.1415 * 72 / 180);
v2Y = v1Y * cos(3.1415 * 72 / 180) + v1X * sin(3.1415 * 72 / 180);
outfile << v2X << " " << v2Y << endl;;

v3X = v2X * cos(3.1415 * 72 / 180) - v2Y * sin(3.1415 * 72 / 180);
v3Y = v2Y * cos(3.1415 * 72 / 180) + v2X * sin(3.1415 * 72 / 180);
outfile << v3X << " " << v3Y << endl;;

v4X = v3X * cos(3.1415 * 72 / 180) - v3Y * sin(3.1415 * 72 / 180);
v4Y = v3Y * cos(3.1415 * 72 / 180) + v3X * sin(3.1415 * 72 / 180);
outfile << v4X << " " << v4Y << endl;;

v5X = v4X * cos(3.1415 * 72 / 180) - v4Y * sin(3.1415 * 72 / 180);
v5Y = v4Y * cos(3.1415 * 72 / 180) + v4X * sin(3.1415 * 72 / 180);
outfile << v5X << " " << v5Y << endl;;

v6X = (v1X * cos(3.1415 * 36 / 180) - v1Y * sin(3.1315 * 36 / 180)) / 2;
v6Y = (v1Y * cos(3.1415 * 36 / 180) + v1X * sin(3.1315 * 36 / 180)) / 2;
outfile << v6X << " " << v6Y << endl;;

v7X = (v2X * cos(3.1415 * 36 / 180) - v2Y * sin(3.1315 * 36 / 180)) / 2;
v7Y = (v2Y * cos(3.1415 * 36 / 180) + v2X * sin(3.1315 * 36 / 180)) / 2;
outfile << v7X << " " << v7Y << endl;;

v8X = (v3X * cos(3.1415 * 36 / 180) - v3Y * sin(3.1315 * 36 / 180)) / 2;
v8Y = (v3Y * cos(3.1415 * 36 / 180) + v3X * sin(3.1315 * 36 / 180)) / 2;
outfile << v8X << " " << v8Y << endl;;

v9X = (v4X * cos(3.1415 * 36 / 180) - v4Y * sin(3.1315 * 36 / 180)) / 2;
v9Y = (v4Y * cos(3.1415 * 36 / 180) + v4X * sin(3.1315 * 36 / 180)) / 2;
outfile << v9X << " " << v9Y << endl;;

v10X = (v5X * cos(3.1415 * 36 / 180) - v5Y * sin(3.1315 * 36 / 180)) / 2;
v10Y = (v5Y * cos(3.1415 * 36 / 180) + v5X * sin(3.1315 * 36 / 180)) / 2;
outfile << v10X << " " << v10Y << endl;;

outfile.close();
}

void loadCoordinates()
{
// loads the vertices from the text file
ifstream infile("vertices.txt");
for (int i = 0; i < 10; i++){
infile >> vertices[i][0];
infile >> vertices[i][1];
cout << vertices[i][0] << " " << vertices[i][1] << endl;
}

}

void interpolation()
{

//glEnable(GL_DEPTH);
saveCoordinates();
loadCoordinates();


glNewList(listname, GL_COMPILE);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 5; i++){
glVertex3f(vertices[i][0], vertices[i][1], 0.0);
}
glEnd();
glEndList();

starX = new float[10];
starY = new float[10];
t = 0.0;
ti = 0.0005;

starX[0] = v1X;
starY[0] = v1Y;

starX[2] = v2X;
starY[2] = v2Y;

starX[4] = v3X;
starY[4] = v3Y;

starX[6] = v4X;
starY[6] = v4Y;

starX[8] = v5X;
starY[8] = v5Y;

starX[1] = (1.0 - t) * v1X + t * v6X;
starY[1] = (1.0 - t) * v1Y + t * v6Y;

starX[3] = (1.0 - t) * v2X + t * v7X;
starY[3] = (1.0 - t) * v2Y + t * v7Y;

starX[5] = (1.0 - t) * v3X + t * v8X;
starY[5] = (1.0 - t) * v3Y + t * v8Y;

starX[7] = (1.0 - t) * v4X + t * v9X;
starY[7] = (1.0 - t) * v4Y + t * v9Y;

starX[9] = (1.0 - t) * v5X + t * v10X;
starY[9] = (1.0 - t) * v5Y + t * v10Y;
}

原始.h

#ifndef PRIMITIVES_H
#define PRIMITIVES_H

extern float vertices[10][2];



float* starX;
float* starY;

float t; //clock
float ti; //time interval

float v1X;
float v1Y;


float v2X;
float v2Y;


float v3X;
float v3Y;

float v4X;
float v4Y;

float v5X;
float v5Y;

float v6X;
float v6Y;

float v7X;
float v7Y;

float v8X;
float v8Y;

float v9X;
float v9Y;

float v10X;
float v10Y;

void interpolation();
void saveCoordinates();
void loadCoordinates();
#endif

Graphics2DAssignment.cpp(这是唯一使用那些 float 和数组的位

if (drawStar == true)
{

interpolation();

glPushMatrix(); // pushes the current matrix stack down by one, duplicating the current matrix.
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 10; i++)
glVertex2f(starX[i], starY[i]);
glEnd();

//glPopMatrix pops the current matrix stack, replacing the current matrix with the one below it on the stack.
}

最佳答案

float* starX;primitive.h 中的其他是定义。如果 primitive.h 包含在多个 TU 中,则会出现多重定义错误。

只需在 header 中声明它们并在单个 TU 中定义它们,就像您对顶点所做的那样。

关于c++ - 将 float 保存在文本文件中并从文本文件中加载它们(OpenGL 和 C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698401/

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