Optmierung

wayne42

Mitglied
Hallo Leute...

ich arbeite mich gerade in c++ rein und versuch ne kleine anwendung mit opengl und glut zu basteln. Ich habe die Anwendung halbwegs zusammengestochert, aber irgendwie is das Programm bei mir total langsam. Hab ich irgendeinen fiesen Fehler gemacht, wieso das so langsam ist?

ich poste mal das komplette prog...

Code:
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;

#include <math.h>
#include <iostream>
#include <list>
#include <string>
#include <windows.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <fstream>
#include <math.h>

#define XOFF          50
#define YOFF          50
#define WINDOW_WIDTH  600
#define WINDOW_HEIGHT 600

void display(void);
void myinit(void);
void idle(void);
void reshape(int w, int h);
void key(unsigned char key, int x, int y);

class Sphere
{	
	private :

	std::vector<std::vector <std::vector <GLfloat>>> polygons;	// storing the model data
	GLfloat	rotation;											// rotation angle
	std::vector<GLfloat> location;								// vector for location
	GLfloat	speed;												// defining the speed of movement
	std::vector<GLfloat> PointA;								// refering Point A for movement
	std::vector<GLfloat> PointB;								// refering Point B for movement
	std::vector<GLfloat> PointC;								// refering Point C for movement
	std::vector<GLfloat> axes;									// rotation axes
	int way;													//flag for : 0 = AB, 1 = BC, 2 = CA

	public :
	
	
	/**
	 * buildSphere
	 *
	 * this function is used to read the input file and to store it in an appropriate format
	 **/
	void buildSphere()
	{
		int size, subSize; 
		float value;
		std::ifstream file("sphere.128");
  
		if(file.is_open()) 
		{    
			file >> size; 
			polygons.resize(size); //defining the whole size of the sphere

			for(int i=0; i<size; i++)
			{
				file >> subSize; 

				for(int k=0; k<subSize; k++)
				{
					polygons[i].resize(subSize);  //the size of the sup polygon

					//saving the model data
					file >> value;
					polygons[i][k].push_back(value);

					file >> value;
					polygons[i][k].push_back(value);

					file >> value;
					polygons[i][k].push_back(value);
				}
			}

           file.close(); 
		}

		//set location and rotation
		speed = 10.0f;
		rotation = 0.0f;
		way = 0; //first flag

		//define all the points and axes used in the programm
		location.resize(3);
		PointA.resize(3);
		PointB.resize(3);
		PointC.resize(3);
		axes.resize(3);

		PointA[0] = location[0] = -4.0f;
		PointA[1] = location[1] = 1.0f;
		PointA[2] = location[2] = 4.0f;

		PointB[0] = -1.0f;
		PointB[1] =  1.0f;
		PointB[2] = -4.0f;
		
		PointC[0] = 3.0f;
		PointC[1] = 1.0f;
		PointC[2] = 5.0f;

		//cross product
		axes[0] = PointB[2]-PointA[2];
		axes[2] = PointB[0]-PointA[0];
		axes[1] = 0.0f;
	}

	/**
	 * getDistance(Vector 1, Vector 2)
	 *
	 * this functions computes the distance between to points regarding that the y doesn't matter because
	 * the programm is working on a plane
	 *
	 * returns the distance
	 **/
	GLfloat getDistance(std::vector <GLfloat> vec1, std::vector <GLfloat> vec2)
	{
		//don't refer on y because it's always 0
		return sqrt((vec1[0]-vec2[0])*(vec1[0]-vec2[0])+(vec1[2]-vec2[2])*(vec1[2]-vec2[2]));
	}

	/**
	 * getPolygons()
	 * 
	 * returns the model data of the sphere
	 **/
	std::vector<std::vector <std::vector <GLfloat>>> getPolygons()
	{
		return polygons;
	}

	/**
	 * getRotation()
	 * 
	 * returns the rotation angle of the sphere
	 **/
	GLfloat	getRotation()
	{
		return rotation;
	}

	/**
	 * getRotAxes()
	 * 
	 * returns the specific rotation angle
	 **/
	std::vector<GLfloat> getRotAxes()
	{
		return axes;
	}

	/**
	 * getLocation()
	 * 
	 * returns the location of the sphere for translation
	 **/
	std::vector<GLfloat> getLocation()
	{
		return location;
	}

	/**
	 * roll()
	 * 
	 * this function computes all the neccessary data for rolling.
	 **/
	void roll()
	{
		//rotate
		rotation += speed;
		//translate (y is always the same)
		GLfloat distance = /* 2*Pi*0.5/360 */ 3.14f / 360 * speed;

		//in this way the process is changing the location into the right direction... if a reference point is 
		//reached then the next Way has to be choosen
		switch(way)
		{
			//From A to B
			case 0 :	location[0] = location[0] + distance * (PointB[0]-PointA[0]) / getDistance(PointA, PointB);
						location[2] = location[2] + distance * (PointB[2]-PointA[2]) / getDistance(PointA, PointB);

						//if the new location is farther away than b of a then the new way has to be choosen
						if(getDistance(location, PointA) > getDistance(PointA, PointB))
						{
							location = PointB;
							way = 1;

							//rot axes From B to C
							//if direction has changed... then rotation axes changes : cross product of direction and ground plane
							axes[0] = PointC[2]-PointB[2];
							axes[2] = PointC[0]-PointB[0];
						}
						break;
			//From B to C
			case 1 :	location[0] = location[0] + distance * (PointC[0]-PointB[0]) / getDistance(PointB, PointC);
						location[2] = location[2] + distance * (PointC[2]-PointB[2]) / getDistance(PointB, PointC);

						if(getDistance(location, PointB) > getDistance(PointB, PointC))
						{
							location = PointC;
							way = 2;

							//rot axes From C to A
							axes[0] = PointC[2]-PointA[2];
							axes[2] = PointC[0]-PointA[0];
						}
						break;
			//From C to A
			case 2 :	location[0] = location[0] + distance * (PointA[0]-PointC[0]) / getDistance(PointC, PointA);
						location[2] = location[2] + distance * (PointA[2]-PointC[2]) / getDistance(PointC, PointA);

						if(getDistance(location, PointC) > getDistance(PointC, PointA))
						{
							location = PointA;
							way = 0;

							//rot axes From A to B
							axes[0] = PointB[2]-PointA[2];
							axes[2] = PointB[0]-PointA[0];
						}
						break;
		}
	}
};

Sphere sphere;				//this is the sphere that includes all the functions of the programm
bool animation = false;		// this is a flag for the naimation... if the animation is turned on then the programm shows and processes animation
							// if the flag is switched of then the process of animation is not regarded
							//this helps to start and stop the animation from any point 

/**
 * main
 *
 * this is the starting point of the programm... it loads the model and inits theopengl window
 **/
void main(int argc, char **argv)
{
	sphere.buildSphere();

	glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    glutInitWindowPosition(XOFF, YOFF);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	glutCreateWindow("CS653 Assignment 2 : Brauer");
    glutDisplayFunc(display);
	glutIdleFunc(idle);   
	glutReshapeFunc(reshape);
	glutKeyboardFunc(key);

    myinit();
    glutMainLoop();
}

/**
 * display
 *
 * this function processes the data to displays what has to be on the screen
 **/
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	//draw a quadrilateral in color green (i.e., (0; 1; 0)) with vertices at 
	//(5; 0; 8; 1), (5; 0;-4; 1), (-5; 0;-4; 1), and (-5; 0; 8; 1) to indicate the x-z plane
	glBegin(GL_QUADS); //quad
		glColor3f( 0.0f, 1.0f, 0.0f);
		glVertex3f( 5.0f, 1.0f, 8.0f);
		glVertex3f( 5.0f, 1.0f,-4.0f);
		glVertex3f(-5.0f, 1.0f,-4.0f);
		glVertex3f(-5.0f, 1.0f, 8.0f);
	glEnd();

	//draw the x-, y-, and z-axes in colors red (i.e., (1; 0; 0)), magenta (i.e., (1; 0; 1)) and blue (i.e, (0; 0; 1)),
	glLineWidth(2.0f);
	glBegin(GL_LINES); //lines
		glColor3f( 1.0f, 0.0f, 0.0f);  //x
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f( 9.0f, 1.0f, 0.0f);

		glColor3f( 1.0f, 0.0f, 1.0f);  //y
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f( 0.0f, 9.0f, 0.0f);

		glColor3f( 0.0f, 0.0f, 1.0f);  //z
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f( 0.0f, 1.0f, 9.0f);
	glEnd();

	glPushMatrix(); //save settings
	
	//translate sphere
	glTranslatef(sphere.getLocation()[0], sphere.getLocation()[1], sphere.getLocation()[2]);
	glRotatef(sphere.getRotation(),sphere.getRotAxes()[0],sphere.getRotAxes()[1],sphere.getRotAxes()[2]); 
	
	//Draw the sphere lines in golden yellow (1:0; 0:84; 0)
	glColor3f( 1.0f, 0.84f, 1.0f);
	glLineWidth(1.0f);
	std::vector<std::vector <std::vector <GLfloat>>> sphereMesh = sphere.getPolygons();

	for(int i=0; i<(int)sphereMesh.size(); i++)
	{
		glBegin(GL_LINE_LOOP);
			glVertex3f(sphereMesh[i][0][0], sphereMesh[i][0][1], sphereMesh[i][0][2]);
			glVertex3f(sphereMesh[i][1][0], sphereMesh[i][1][1], sphereMesh[i][1][2]);
			glVertex3f(sphereMesh[i][2][0], sphereMesh[i][2][1], sphereMesh[i][2][2]);
		glEnd();
	}
	
	glPopMatrix(); // discard the modelling transformations
	glFlush();
	glutSwapBuffers();
}

/**
 * idle 
 *
 * this function is used to process the animation
 **/
void idle(void)
{
	if(animation)
	{
		sphere.roll();
	}

	glutPostRedisplay();
}

/**
 * reshape
 *
 * this function is used set up the viewing
 **/
void reshape(int w, int h)
{
	if(h == 0)	h = 1;

	//set the background color to sky blue (0.529; 0.807; 0.92; 0.0)
	glClearColor(0.529, 0.807, 0.92, 0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity(); 
	glViewport(0, 0, w, h);
	gluPerspective(45.0f, w/h, 0.1f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//(1) VRP = (7|3|10), VPN = (-7|-3|10), and VUP = (0|1|0),
	//(2) the VRP maps to the center of the (graphics) window -> LookAt = vrp + vpn
	gluLookAt(7, 3, -10, 0, 0, 0, 0, 1, 0);
}

/**
 * key
 *
 * this function is used to process the input from the keyboard
 **/
void key(unsigned char key, int x, int y)
{
	switch(key)
	{
		case 115 :	animation = !animation; //pressed s
					break;
		//until the user hits the key 'q'; this keyboard event should be captured by your program and your program should then exit
		case 113 :	exit(0); //pressed q
	}
}

/**
 * myinit
 *
 * this function is used to initialize the view settings by just calling the first reshape
 **/
void myinit()
{
	reshape(WINDOW_WIDTH, WINDOW_HEIGHT);
}

Also das Prog an sich t, nur frag ich mich, warum das so langsam das so langsam ist :-/
 

Anhänge

Hi Leute,

keiner ne Idee? Es geht hier nich darum, meine Hausaufgaben zu machen... die sind schon gemacht :-)
Es interessiert mich aber echt, wo da in dem Tool der Flaschenhals ist :-S
 
Zurück