将浮动文件保存在文本文件中并从文本文件中加载它们(OpenGL& C ++)

时间:2017-04-29 17:12:49

标签: c++ opengl

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

示例错误:

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

错误LNK2005:" float * starX" (?starX @@ 3PAMA)已在Graphics2DAssignment.obj中定义

starY发生相同的错误,从v1X,v1Y到v10X,V10Y

primitive.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;
}

primitive.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(这是使用那些浮点数和数组的唯一位

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.
    }

1 个答案:

答案 0 :(得分:1)

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

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