Created
October 30, 2020 05:29
-
-
Save zzNuAzz/ec390458479c202d68f1602c5c1384bd to your computer and use it in GitHub Desktop.
Dino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <GL/glut.h> | |
#include <fstream> | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
const string FILE_NAME = "dinosaur.dat"; | |
class Piece | |
{ | |
public: | |
Piece(); | |
void setPiece(int nop); | |
int numOfPoints; | |
float *x; | |
float *y; | |
}; | |
Piece::Piece() | |
{ | |
this->numOfPoints = 0; | |
} | |
void Piece::setPiece(int nop) | |
{ | |
numOfPoints = nop; | |
this->x = new float[numOfPoints]; | |
this->y = new float[numOfPoints]; | |
} | |
class FileHelper | |
{ | |
public: | |
void ReadData(string, Piece *&pieces, int &numOfPieces); | |
}; | |
void FileHelper::ReadData(string fileName, Piece *&pieces, int &numOfPieces) | |
{ | |
ifstream inStream; | |
inStream.open(fileName.c_str()); | |
if (!inStream) | |
{ | |
cout << "Can not open this file" << endl; | |
} | |
else | |
{ | |
// read data from file to array "pieces" | |
inStream >> numOfPieces; | |
pieces = new Piece[numOfPieces]; | |
for (int i = 0; i < numOfPieces; i++) | |
{ | |
int nop; | |
inStream >> nop; | |
pieces[i] = Piece(); | |
pieces[i].setPiece(nop); | |
for (int j = 0; j < nop; j++) | |
{ | |
inStream >> pieces[i].x[j] >> pieces[i].y[j]; | |
} | |
} | |
} | |
inStream.close(); | |
} | |
Piece *pieces; | |
int numOfPieces = 0; | |
// Use FileHelper to open and read the data file | |
void readData() | |
{ | |
FileHelper fileHelper; | |
fileHelper.ReadData(FILE_NAME, pieces, numOfPieces); | |
} | |
void init() | |
{ | |
glClearColor(1, 1, 1, 0); | |
glColor3f(0, 0, 0); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(-800, 800, -200, 800, -1, 1); | |
} | |
void display() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
// draw the left dinosaur | |
glColor3f(0.5, 0.5, 0.5); | |
for (int i = 0; i < numOfPieces; i++) | |
{ | |
glBegin(GL_LINE_STRIP); | |
for (int j = 0; j < pieces[i].numOfPoints; j++) | |
{ | |
glVertex2f(pieces[i].x[j], pieces[i].y[j]); | |
} | |
glEnd(); | |
} | |
// draw the right dinosaur | |
for (int i = 0; i < numOfPieces; i++) | |
{ | |
glBegin(GL_LINE_STRIP); | |
for (int j = 0; j < pieces[i].numOfPoints; j++) | |
{ | |
glVertex2f(-pieces[i].x[j], pieces[i].y[j]); | |
} | |
glEnd(); | |
} | |
glFlush(); | |
glutSwapBuffers(); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | |
glutInitWindowSize(500, 300); | |
glutInitWindowPosition(100, 0); | |
glutCreateWindow("Lab_05"); | |
init(); | |
// read the data from dinosaur.dat | |
readData(); | |
glutDisplayFunc(display); | |
glutMainLoop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment