[Solved] Compiler bug was in fact optimizier bug.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

[Solved] Compiler bug was in fact optimizier bug.

Post by Kojima »

I'm writing a 3d engine(Open source) but I've run into a problem.

Whenever I use glMultMatrixf to multiply the entities position/rotation matrix ti doesn't work. The camera sits at 0,0,0, and so does the entity.

I confirmed it was this by adding a glTranslate3f(0,0,-40) into my rendering code, which resulted in the entity being drawn 40 units into the distance as it should have been with glMultMatrixf

So just wondering, has anyone else run into problems with glMultMatrixf? or should I be looking elsewhere?
Last edited by Kojima on Mon Jul 10, 2006 3:14 am, edited 2 times in total.
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

I tracked the problem down, there seems to be a serious bug with virtual methods in the C++ compiler.

My code presently now works. If I had one more virtual method/deconstructor though, it no longer works. The 3d mesh is visible for half a second and then is gone. I remove the virtual keyword and like magic it works.

And this is on a function that is *not* called, so it's not because it's changing which function is being called due to the virtual method (There are no overloads, they're inherited from the base class)

I can provide code to demonstrate the bug if the writers of the compiler back end wish to take a look.
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

sure this is not an alignment issue? often, or even most of the times, when changing something that isnt even used changes the behaviour of the program it is because what you effectivly change is how things are organized in memory.
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

Wouldn't alignment only be an issue when sending data to psp system calls and render calls?

If so, the only candidate is my matrix class, I don't align the data.

The rest is only accessed by my own code in C++, not asm. Or does the mips have further restrictions not typically found on pc concerning alignment?
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

Well it's definitely not right.

If I add an empty function called Move and call it in the main loop, again the cube disappears after one frame.

This function returns immediately so can't possibly have any effect on the code. and if I remove the call to the empty function, it runs perfectly again.
zilt
Posts: 45
Joined: Tue Feb 21, 2006 11:59 pm
Location: Ontario, Canada
Contact:

Post by zilt »

Most likely it's a bug in your code. I have dozens of classes with 0-6 virtual methods in each class, and I've _never_ had any problem with g++. And this is not some toy engine - it does substantial 3D code with blended skeleton 3d character animation, bezier curves, hardware lighting, transparent 2D animated gui and networking. Anyways, I should be releasing a alpha sometime in the next two weeks.
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

I agree that is something in my code, but it's not a simple coding bug.
My code is more likely ehibiting a bug that has not be found in the psp sdk yet.

I mean, how else could an *empty* function being called make the graphics disappear after one frame?
the function does nothing. not a single line of code is touched, it returns immediately, with a hard 'return'

I've aligned all my structures to 16/32/64 bit, no difference.
I even used a new replacement,

Code: Select all


#define new(x) memalign(64, sizeof(x) );

to align my classes, but again it makes no difference.

If I remove the call to the function, it works. I don't have to remove the function it's self, so it's not likely to be it's inclusion causing something else to go out of alignment.

Tbh I have no idea what could be causing it.

Here's the source, I've only been working on it for a day or two so it's not some uber advanced engine yet, but it has meshes/surfaces/camers etc.
I'd appreciate it if you take a look and see if you get the same problems. it requires no external media.

If you scroll down to main, you'll see a function called Move being called. Remove this line and it works perfectly. If you check the camera class for move, you'll see it has but one line. "return" and nothing else.

Code: Select all



#include <stdlib.h> // needed in order to have "exit" function @@@
#include <stdio.h>
#include <GL/glut.h>    // Header File For The GLUT Library 
#include <GL/gl.h>	// Header File For The OpenGL32 Library
#include <GL/glu.h>	// Header File For The GLu32 Library
//#include <unistd.h>     // Header File for sleeping. @@@
#include <pspctrl.h>
#include <pspdebug.h>
#include <math.h>
int window; 

const float RAD_TO_DEG = 57.2957795130823208767981548141052;


template <class T>
class ListNode
&#123;

public&#58;
    T &get&#40;&#41;
    &#123;
        return object;
    &#125;;
    void set&#40;T &object&#41;
    &#123;
        this->object = object;
    &#125;;

    ListNode<T> *getNext&#40;&#41;
    &#123;
        return nextNode;
    &#125;;
    void setNext&#40;ListNode<T> *nextNode&#41;
    &#123;
        this->nextNode = nextNode;
    &#125;;

private&#58;
    T object;
    ListNode<T> *nextNode;
&#125;;

template <class T>
class List
&#123;

public&#58;
    // Constructor
    List&#40;&#41;
    &#123;
        headNode = new ListNode<T>;
        headNode->setNext&#40;NULL&#41;;

        currentNode = NULL;
        size = 0;
    &#125;;

    // Destructor
    ~List&#40;&#41;
    &#123;

        ListNode<T> *pointerToDelete, *pointer = headNode;

        while &#40;pointer != NULL&#41;
        &#123;
            pointerToDelete = pointer;
            pointer = pointer->getNext&#40;&#41;;
            delete pointerToDelete;
        &#125;
    &#125;;

    T &get&#40;&#41;
    &#123;

        if &#40;currentNode == NULL&#41;
            start&#40;&#41;;

        return currentNode->get&#40;&#41;
               ;
    &#125;;

    void add&#40;T addObject&#41;
    &#123;

        ListNode<T> *newNode = new ListNode<T>;

        newNode->set&#40;addObject&#41;
        ;

        newNode->setNext&#40;headNode->getNext&#40;&#41;&#41;;
        headNode->setNext&#40;newNode&#41;;

        size++;
    &#125;;

    void remove&#40;&#41;
    &#123;

        lastCurrentNode->setNext&#40;currentNode->getNext&#40;&#41;&#41;;

        delete currentNode;

        currentNode = lastCurrentNode;

        size--;
    &#125;;

    void start&#40;&#41;
    &#123;
        lastCurrentNode = headNode;
        currentNode = headNode;
    &#125;;

    bool next&#40;&#41;
    &#123;

        // If the currentNode now points at nothing, we've reached the end
        if &#40;currentNode == NULL&#41;
            return false;

        // Update the last node and current node
        lastCurrentNode = currentNode;
        currentNode = currentNode->getNext&#40;&#41;;

        // If currentNode points at nothing or there is nothing added, we can immediately return false
        if &#40;currentNode == NULL || size == 0&#41;
            return false;
        else
            return true;
    &#125;;

    int getSize&#40;&#41;
    &#123;
        return size;
    &#125;;

private&#58;
    int size;
    ListNode<T> *headNode;
    ListNode<T> *currentNode, *lastCurrentNode;
&#125;;



/* Image type - contains height, width, and data */
struct Image &#123;
    unsigned long sizeX;
    unsigned long sizeY;
    char *data;
&#125;;
typedef struct Image Image;

// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only.  
// See http&#58;//www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info.
int ImageLoad&#40;char *filename, Image *image&#41; &#123;
    FILE *file;
    unsigned long size;                 // size of the image in bytes.
    unsigned long i;                    // standard counter.
    unsigned short int planes;          // number of planes in image &#40;must be 1&#41; 
    unsigned short int bpp;             // number of bits per pixel &#40;must be 24&#41;
    char temp;                          // temporary color storage for bgr-rgb conversion.

    // make sure the file is there.
    if &#40;&#40;file = fopen&#40;filename, "rb"&#41;&#41;==NULL&#41;
    &#123;
	printf&#40;"File Not Found &#58; %s\n",filename&#41;;
	return 0;
    &#125;
    
    // seek through the bmp header, up to the width/height&#58;
    fseek&#40;file, 18, SEEK_CUR&#41;;

    // read the width
    if &#40;&#40;i = fread&#40;&image->sizeX, 4, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading width from %s.\n", filename&#41;;
	return 0;
    &#125;
    printf&#40;"Width of %s&#58; %lu\n", filename, image->sizeX&#41;;
    
    // read the height 
    if &#40;&#40;i = fread&#40;&image->sizeY, 4, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading height from %s.\n", filename&#41;;
	return 0;
    &#125;
    printf&#40;"Height of %s&#58; %lu\n", filename, image->sizeY&#41;;
    
    // calculate the size &#40;assuming 24 bits or 3 bytes per pixel&#41;.
    size = image->sizeX * image->sizeY * 3;

    // read the planes
    if &#40;&#40;fread&#40;&planes, 2, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading planes from %s.\n", filename&#41;;
	return 0;
    &#125;
    if &#40;planes != 1&#41; &#123;
	printf&#40;"Planes from %s is not 1&#58; %u\n", filename, planes&#41;;
	return 0;
    &#125;

    // read the bpp
    if &#40;&#40;i = fread&#40;&bpp, 2, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading bpp from %s.\n", filename&#41;;
	return 0;
    &#125;
    if &#40;bpp != 24&#41; &#123;
	printf&#40;"Bpp from %s is not 24&#58; %u\n", filename, bpp&#41;;
	return 0;
    &#125;
	
    // seek past the rest of the bitmap header.
    fseek&#40;file, 24, SEEK_CUR&#41;;

    // read the data. 
    image->data = &#40;char *&#41; malloc&#40;size&#41;;
    if &#40;image->data == NULL&#41; &#123;
	printf&#40;"Error allocating memory for color-corrected image data"&#41;;
	return 0;	
    &#125;

    if &#40;&#40;i = fread&#40;image->data, size, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading image data from %s.\n", filename&#41;;
	return 0;
    &#125;

    for &#40;i=0;i<size;i+=3&#41; &#123; // reverse all of the colors. &#40;bgr -> rgb&#41;
	temp = image->data&#91;i&#93;;
	image->data&#91;i&#93; = image->data&#91;i+2&#93;;
	image->data&#91;i+2&#93; = temp;
    &#125;
    
    // we're done.
    return 1;
&#125;
  

class Display
&#123;
public&#58;
	Display&#40;int argc,char **argv&#41;
	&#123;
		glutInit&#40;&argc,argv&#41;;
		glutInitDisplayMode&#40;GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH&#41;;  
	  glutInitWindowSize&#40;480, 272&#41;; // @@@
  	glutInitWindowPosition&#40;0, 0&#41;;  
 		_win = glutCreateWindow&#40;"Raptor Engine - Main Window"&#41;;  
   	_w = 480;
		_h = 272;
    InitGl&#40;&#41;;
		Draw2D&#40;&#41;;
	
	&#125;
	void InitGl&#40;&#41;
	&#123;
			glClearColor&#40;0.0f, 0.0f, 0.0f, 0.0f&#41;;		// This Will Clear The Background Color To Black
		  glClearDepth&#40;1.0&#41;;				// Enables Clearing Of The Depth Buffer
		  glDepthFunc&#40;GL_LESS&#41;;				// The Type Of Depth Test To Do
		  glEnable&#40;GL_DEPTH_TEST&#41;;			// Enables Depth Testing
		  glShadeModel&#40;GL_SMOOTH&#41;;			// Enables Smooth Color Shading
		
		  glMatrixMode&#40;GL_PROJECTION&#41;;
		  glLoadIdentity&#40;&#41;;				// Reset The Projection Matrix
		
		  gluPerspective&#40;45.0f,&#40;float&#41;_w/&#40;float&#41;_h,0.1f,100.0f&#41;;	// Calculate The Aspect Ratio Of The Window
		  glMatrixMode&#40;GL_MODELVIEW&#41;;
		  glEnable&#40;GL_TEXTURE_2D&#41;;
	&#125;
	void Draw2D&#40;&#41;
	&#123;
		
		
		glViewport&#40; 0,0,480,272 &#41;;
		glMatrixMode&#40;GL_PROJECTION&#41;;
		glLoadIdentity&#40;&#41;;
		glOrtho&#40;0.0, 480.0, 0.0, 272.0, -2.0, 2.0 &#41;;
		glMatrixMode&#40;GL_MODELVIEW&#41;;
		glLoadIdentity&#40;&#41;;
		glDisable&#40;GL_DEPTH_TEST&#41;;
		glDisable&#40;GL_CULL_FACE&#41;;
		glDisable&#40;GL_LIGHTING&#41;;	
	
	&#125;
	
	int _w,_h;
	int _win;
&#125;;


class Texture
&#123;
public&#58;
	Texture&#40;char *file&#41;
	&#123;
		 Image *image1;
     image1 = &#40;Image *&#41; malloc&#40;sizeof&#40;Image&#41;&#41;;
    	if &#40;image1 == NULL&#41; &#123;
				printf&#40;"Error&#58;Unable to allocate memory for texture.\n"&#41;;
				exit&#40;0&#41;;
    	&#125;
      if &#40;!ImageLoad&#40;file, image1&#41;&#41; &#123;
				printf&#40;"Error&#58;Unable to load image.\n"&#41;;
				exit&#40;0&#41;;
			&#125;
			
    	glGenTextures&#40;1, &_gltex&#41;;
    	glBindTexture&#40;GL_TEXTURE_2D, _gltex&#41;;   // 2d texture &#40;x and y size&#41;

   	 glTexParameteri&#40;GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR&#41;; 
   	 glTexParameteri&#40;GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR&#41;; 

    	glTexImage2D&#40;GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data&#41;;
			glBindTexture&#40;GL_TEXTURE_2D,0&#41;;
			
	&#125;
	void Bind&#40;&#41;
	&#123;
		glBindTexture&#40;GL_TEXTURE_2D,_gltex&#41;;
	&#125;
	void Unbind&#40;&#41;
	&#123;
		glBindTexture&#40;GL_TEXTURE_2D,0&#41;;
	&#125;
	GLuint _gltex;
&#125;;


class Material
&#123;
public&#58;
	Material&#40;&#41;
	&#123;
		_r=_g=_b=_a=1;
	&#125;
	void AddTexture&#40;Texture *tex&#41;
	&#123;
		_texs.add&#40; tex &#41;;
	&#125;
	void Bind&#40; int stage = 0 &#41;
	&#123;
		glColor4f&#40; _r,_g,_b,_a &#41;;
		_texs.start&#40;&#41;;
		while&#40; _texs.next&#40;&#41;==true &#41;
		&#123;
			if&#40;stage == 0&#41;
			&#123;
				Texture *tex = _texs.get&#40;&#41;;
				tex->Bind&#40;&#41;;
			&#125;
			stage--;
		&#125;
	&#125;
	void Unbind&#40; int stage &#41;
	&#123;
		_texs.start&#40;&#41;;
		while&#40; _texs.next&#40;&#41;==true &#41;
		&#123;
			if&#40;stage == 0&#41;
			&#123;
				Texture *tex = _texs.get&#40;&#41;;
				tex->Unbind&#40;&#41;;
			&#125;
			stage--;
		&#125;
	&#125;
	float _r,_g,_b,_a;
	List<Texture *>_texs;
&#125;;

const float PI = 3.14159265;

inline float DegToRad&#40;float ang&#41;
&#123;

	return ang*180.0f/PI;
&#125;

inline float RadToDeg&#40;float rad&#41;
&#123;

	return rad * 180.0f / PI;
&#125;


class Matrix
&#123;
public&#58;
		
	void LoadIdentity&#40;&#41;
	&#123;
		grid&#91;0&#93;&#91;0&#93;=1.0;
		grid&#91;1&#93;&#91;0&#93;=0.0;
		grid&#91;2&#93;&#91;0&#93;=0.0;
		grid&#91;3&#93;&#91;0&#93;=0.0;
		grid&#91;0&#93;&#91;1&#93;=0.0;
		grid&#91;1&#93;&#91;1&#93;=1.0;
		grid&#91;2&#93;&#91;1&#93;=0.0;
		grid&#91;3&#93;&#91;1&#93;=0.0;
		grid&#91;0&#93;&#91;2&#93;=0.0;
		grid&#91;1&#93;&#91;2&#93;=0.0;
		grid&#91;2&#93;&#91;2&#93;=1.0;
		grid&#91;3&#93;&#91;2&#93;=0.0;
		
		grid&#91;0&#93;&#91;3&#93;=0.0;
		grid&#91;1&#93;&#91;3&#93;=0.0;
		grid&#91;2&#93;&#91;3&#93;=0.0;
		grid&#91;3&#93;&#91;3&#93;=1.0;
	&#125;
	
	
		/*

		*/
	
	
	void Multiply&#40;Matrix *mat&#41;
	&#123;
		Matrix *new_mat=new Matrix;
	
		float sum=0;
		int row=0;
		int col=0;
	
		for&#40;row=0;row<4;row++&#41;
		&#123;
			for&#40;col=0;col<4;col++&#41;
			&#123;
				for&#40;int r=0;r<4;r++&#41;
				&#123;
					float a=grid&#91;r&#93;&#91;col&#93;;
					float b=mat->grid&#91;row&#93;&#91;r&#93;;
					float c=a*b;
					sum=sum+c;
				&#125;
				new_mat->grid&#91;row&#93;&#91;col&#93;=sum;
				sum=0;
			&#125;
		&#125;
	
		for&#40;row=0;row<4;row++&#41;
		&#123;
			for&#40;col=0;col<4;col++&#41;
			&#123;
				grid&#91;row&#93;&#91;col&#93;=new_mat->grid&#91;row&#93;&#91;col&#93;;
			&#125;
		&#125;
		delete new_mat;
	&#125;
	
	Matrix * Copy&#40;&#41;
	&#123;
		Matrix *mat = new Matrix;
		mat->grid&#91;0&#93;&#91;0&#93;=grid&#91;0&#93;&#91;0&#93;;
		mat->grid&#91;1&#93;&#91;0&#93;=grid&#91;1&#93;&#91;0&#93;;
		mat->grid&#91;2&#93;&#91;0&#93;=grid&#91;2&#93;&#91;0&#93;;
		mat->grid&#91;3&#93;&#91;0&#93;=grid&#91;3&#93;&#91;0&#93;;
		mat->grid&#91;0&#93;&#91;1&#93;=grid&#91;0&#93;&#91;1&#93;;
		mat->grid&#91;1&#93;&#91;1&#93;=grid&#91;1&#93;&#91;1&#93;;
		mat->grid&#91;2&#93;&#91;1&#93;=grid&#91;2&#93;&#91;1&#93;;
		mat->grid&#91;3&#93;&#91;1&#93;=grid&#91;3&#93;&#91;1&#93;;
		mat->grid&#91;0&#93;&#91;2&#93;=grid&#91;0&#93;&#91;2&#93;;
		mat->grid&#91;1&#93;&#91;2&#93;=grid&#91;1&#93;&#91;2&#93;;
		mat->grid&#91;2&#93;&#91;2&#93;=grid&#91;2&#93;&#91;2&#93;;
		mat->grid&#91;3&#93;&#91;2&#93;=grid&#91;3&#93;&#91;2&#93;;
		mat->grid&#91;0&#93;&#91;3&#93;=grid&#91;0&#93;&#91;3&#93;;
		mat->grid&#91;1&#93;&#91;3&#93;=grid&#91;1&#93;&#91;3&#93;;
		mat->grid&#91;2&#93;&#91;3&#93;=grid&#91;2&#93;&#91;3&#93;;
		mat->grid&#91;3&#93;&#91;3&#93;=grid&#91;3&#93;&#91;3&#93;;
		return mat;
	&#125;
	
		
	void Overwrite&#40;Matrix *mat&#41;
	&#123;
		grid&#91;0&#93;&#91;0&#93;=mat->grid&#91;0&#93;&#91;0&#93;;
		grid&#91;1&#93;&#91;0&#93;=mat->grid&#91;1&#93;&#91;0&#93;;
		grid&#91;2&#93;&#91;0&#93;=mat->grid&#91;2&#93;&#91;0&#93;;
		grid&#91;3&#93;&#91;0&#93;=mat->grid&#91;3&#93;&#91;0&#93;;
		grid&#91;0&#93;&#91;1&#93;=mat->grid&#91;0&#93;&#91;1&#93;;
		grid&#91;1&#93;&#91;1&#93;=mat->grid&#91;1&#93;&#91;1&#93;;
		grid&#91;2&#93;&#91;1&#93;=mat->grid&#91;2&#93;&#91;1&#93;;
		grid&#91;3&#93;&#91;1&#93;=mat->grid&#91;3&#93;&#91;1&#93;;
		grid&#91;0&#93;&#91;2&#93;=mat->grid&#91;0&#93;&#91;2&#93;;
		grid&#91;1&#93;&#91;2&#93;=mat->grid&#91;1&#93;&#91;2&#93;;
		grid&#91;2&#93;&#91;2&#93;=mat->grid&#91;2&#93;&#91;2&#93;;
		grid&#91;3&#93;&#91;2&#93;=mat->grid&#91;3&#93;&#91;2&#93;;
		grid&#91;0&#93;&#91;3&#93;=mat->grid&#91;0&#93;&#91;3&#93;;
		grid&#91;1&#93;&#91;3&#93;=mat->grid&#91;1&#93;&#91;3&#93;;
		grid&#91;2&#93;&#91;3&#93;=mat->grid&#91;2&#93;&#91;3&#93;;
		grid&#91;3&#93;&#91;3&#93;=mat->grid&#91;3&#93;&#91;3&#93;;
		
	&#125;
	

	
	void Translate&#40;float x,float y,float z&#41;
	&#123;
		Matrix *mat=new Matrix;
	
		mat->grid&#91;0&#93;&#91;0&#93;=1;
		mat->grid&#91;1&#93;&#91;0&#93;=0;
		mat->grid&#91;2&#93;&#91;0&#93;=0;
		mat->grid&#91;3&#93;&#91;0&#93;=x;
		mat->grid&#91;0&#93;&#91;1&#93;=0;
		mat->grid&#91;1&#93;&#91;1&#93;=1;
		mat->grid&#91;2&#93;&#91;1&#93;=0;
		mat->grid&#91;3&#93;&#91;1&#93;=y;
		mat->grid&#91;0&#93;&#91;2&#93;=0;
		mat->grid&#91;1&#93;&#91;2&#93;=0;
		mat->grid&#91;2&#93;&#91;2&#93;=1;
		mat->grid&#91;3&#93;&#91;2&#93;=z;
		
		mat->grid&#91;0&#93;&#91;3&#93;=0;
		mat->grid&#91;1&#93;&#91;3&#93;=0;
		mat->grid&#91;2&#93;&#91;3&#93;=0;
		mat->grid&#91;3&#93;&#91;3&#93;=1;
		
		Multiply&#40;mat&#41;;

	&#125;
	void Rotate&#40; float ax,float ay,float az &#41;
	&#123;
		RotateYaw&#40; ay &#41;;
		RotatePitch&#40; ax &#41;;
		RotateRoll&#40; az &#41;;
	&#125;
	void RotatePYR&#40; float ax,float ay,float az&#41;
	&#123;
		RotatePitch&#40; ax &#41;;
		RotateYaw&#40; ay &#41;;
		RotateRoll&#40; az &#41;;
	&#125;
	void RotateI&#40; float ax,float ay,float az &#41;
	&#123;
		RotatePitch&#40; az &#41;;
		RotateRoll&#40; az &#41;;
		RotateYaw&#40; ay &#41;;
	&#125;
	void RotateRPY&#40; float ax,float ay,float az &#41;
	&#123;
		RotateRoll&#40; az &#41;;
		RotatePitch&#40; ax &#41;;
		RotateYaw&#40; ay &#41;;
	&#125;
	void RotateYaw&#40; float ang &#41;
	&#123;
		
		Matrix *mat=new Matrix;
		//ang = DegToRad&#40; ang &#41;;
		mat->grid&#91;0&#93;&#91;0&#93;=cos&#40;ang&#41;;
		mat->grid&#91;1&#93;&#91;0&#93;=0;
		mat->grid&#91;2&#93;&#91;0&#93;=sin&#40;ang&#41;;
		mat->grid&#91;3&#93;&#91;0&#93;=0;
		mat->grid&#91;0&#93;&#91;1&#93;=0;
		mat->grid&#91;1&#93;&#91;1&#93;=1;
		mat->grid&#91;2&#93;&#91;1&#93;=0;
		mat->grid&#91;3&#93;&#91;1&#93;=0;
		mat->grid&#91;0&#93;&#91;2&#93;=-sin&#40;ang&#41;;
		mat->grid&#91;1&#93;&#91;2&#93;=0;
		mat->grid&#91;2&#93;&#91;2&#93;=cos&#40;ang&#41;;
		mat->grid&#91;3&#93;&#91;2&#93;=0;
		
		mat->grid&#91;0&#93;&#91;3&#93;=0;
		mat->grid&#91;1&#93;&#91;3&#93;=0;
		mat->grid&#91;2&#93;&#91;3&#93;=0;
		mat->grid&#91;3&#93;&#91;3&#93;=1;
		
		Multiply&#40;mat&#41;;
		delete mat;
	

	&#125;
	void RotateRoll&#40; float ang &#41;
	&#123;
		
		Matrix *mat=new Matrix;
		//ang = DegToRad&#40; ang &#41;;
		
		mat->grid&#91;0&#93;&#91;0&#93;=cos&#40;ang&#41;;
		mat->grid&#91;1&#93;&#91;0&#93;=-sin&#40;ang&#41;;
		mat->grid&#91;2&#93;&#91;0&#93;=0;
		mat->grid&#91;3&#93;&#91;0&#93;=0;
		mat->grid&#91;0&#93;&#91;1&#93;=sin&#40;ang&#41;;
		mat->grid&#91;1&#93;&#91;1&#93;=cos&#40;ang&#41;;
		mat->grid&#91;2&#93;&#91;1&#93;=0;
		mat->grid&#91;3&#93;&#91;1&#93;=0;
		mat->grid&#91;0&#93;&#91;2&#93;=0;
		mat->grid&#91;1&#93;&#91;2&#93;=0;
		mat->grid&#91;2&#93;&#91;2&#93;=1;
		mat->grid&#91;3&#93;&#91;2&#93;=0;
		
		mat->grid&#91;0&#93;&#91;3&#93;=0;
		mat->grid&#91;1&#93;&#91;3&#93;=0;
		mat->grid&#91;2&#93;&#91;3&#93;=0;
		mat->grid&#91;3&#93;&#91;3&#93;=1;
		
		Multiply&#40;mat&#41;;
		delete mat;		
	&#125;
	void RotatePitch&#40; float ang &#41;
	&#123;
		Matrix *mat=new Matrix;
		//ang = DegToRad&#40; ang &#41;;
		mat->grid&#91;0&#93;&#91;0&#93;=1;
		mat->grid&#91;1&#93;&#91;0&#93;=0;
		mat->grid&#91;2&#93;&#91;0&#93;=0;
		mat->grid&#91;3&#93;&#91;0&#93;=0;
		mat->grid&#91;0&#93;&#91;1&#93;=0;
		mat->grid&#91;1&#93;&#91;1&#93;=cos&#40;ang&#41;;
		mat->grid&#91;2&#93;&#91;1&#93;=-sin&#40;ang&#41;;
		mat->grid&#91;3&#93;&#91;1&#93;=0;
		mat->grid&#91;0&#93;&#91;2&#93;=0;
		mat->grid&#91;1&#93;&#91;2&#93;=sin&#40;ang&#41;;
		mat->grid&#91;2&#93;&#91;2&#93;=cos&#40;ang&#41;;
		mat->grid&#91;3&#93;&#91;2&#93;=0;
		
		mat->grid&#91;0&#93;&#91;3&#93;=0;
		mat->grid&#91;1&#93;&#91;3&#93;=0;
		mat->grid&#91;2&#93;&#91;3&#93;=0;
		mat->grid&#91;3&#93;&#91;3&#93;=1;
		
		Multiply&#40;mat&#41;;
		delete mat;
	&#125;
	
	
	float grid&#91;4&#93;&#91;4&#93; __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41;;
&#125;;

#define true 1
#define false 0

const int Typ_Ent = 1;
const int Typ_Piv = 2;
const int Typ_Bas = 3;
const int Typ_Lgt = 4;

class Base
&#123;
public&#58;
	Base&#40;&#41;
	&#123;
		_sx=1;
		_sy=1;
		_sz=1;
		_parent = NULL;
		_px=0;
		_py=0;
		_pz=0;
		_qw=0;
		_qx=0;
		_qy=0;
		_qz=0;
		_hidden = false;
		_order = 0;
		_mat = new Matrix;
	&#125;
	Base&#40; Base *parent &#41;
	&#123;
		_sx=1;
		_sy=1;
		_sz=1;
		_parent = NULL;
		_px=0;
		_py=0;
		_pz=0;
		_qw=0;
		_qx=0;
		_qy=0;
		_qz=0;
		_hidden = false;
		_order = 0;
		_mat = new Matrix;
	&#125;
	List<Base *>_childs;
	Base *_parent;
	Matrix *_mat;
	float _px,_py,_pz;
	float _sx,_sy,_sz;
	float _rx,_ry,_rz;
	float _qw,_qx,_qy,_qz;
	int _order;
	int _hidden;
	const char *_name;
	const char *_type;
	float _ed;
	float _pit,_yaw;
	void PointAt&#40; float tx,float ty,float tz,float roll=0 &#41;
	&#123;
		
	
	//	printf&#40;"Reached Function\n"&#41;;
		float x=tx;
		float y=ty;
		float z=tz;

		float xdiff=XPos&#40;true&#41;-x;
		float ydiff=YPos&#40;true&#41;-y;
		float zdiff=ZPos&#40;true&#41;-z;

		float dist22=sqrt&#40;&#40;xdiff*xdiff&#41;+&#40;zdiff*zdiff&#41;&#41;;
		float pitch= atan2&#40;ydiff,dist22&#41;;// *RAD_TO_DEG;
		float yaw= atan2&#40;xdiff,-zdiff&#41;;//  *RAD_TO_DEG;
		
		//printf&#40;"Reached Print\n"&#41;;
		//printf&#40;" Pit&#58;%d Yaw&#58;%d \n",&#40;int&#41;pitch,&#40;int&#41;yaw&#41;;
		_pit = pitch;
		_yaw = yaw;
		Rotate&#40; pitch,yaw,roll,true &#41;;
	&#125;
	
	
	
	void Position&#40; float x,float y,float z,int global = false &#41;
	&#123;
		
		_px=x;
		_py=y;
		_pz=-z;

		if&#40; &#40;global==1&#41; && &#40;!&#40;_parent==NULL&#41;&#41; &#41;
		&#123;
			
			_px=_px-_parent->XPos&#40;true&#41;;
			_py=_py-_parent->YPos&#40;true&#41;;
			_pz=_pz+_parent->ZPos&#40;true&#41;;
		 			
		 			
			float ax=Pitch&#40;true&#41;;
			float ay=Yaw&#40;true&#41;;
			float az=Roll&#40;true&#41;;
						
			Matrix *new_mat=new Matrix;
			
			new_mat->LoadIdentity&#40;&#41;;
			new_mat->Rotate&#40;-ax,-ay,-az&#41;;
			new_mat->Translate&#40;_px,_py,_pz&#41;;

			_px=new_mat->grid&#91;3&#93;&#91;0&#93;;
			_py=new_mat->grid&#91;3&#93;&#91;1&#93;;
			_pz=new_mat->grid&#91;3&#93;&#91;2&#93;;
			
			delete new_mat;
	
		&#125;
	
		if&#40; !&#40;_parent==NULL&#41; &#41;
		&#123;
			_mat->Overwrite&#40;_parent->_mat&#41;;
			UpdateMat&#40;&#41;;
		&#125;

		if&#40; _parent==NULL &#41; 
		&#123;
			UpdateMat&#40;true&#41;;
		&#125;
		
		UpdateChildren&#40;this&#41;;
		
	&#125;	
	

	void Rotate&#40; float pitch,float yaw,float roll,int global = false&#41;
	&#123;
		
		_rx=-pitch;
		_ry=yaw;
		_rz=roll;
		
		if&#40; &#40;global==true&#41; && &#40;!&#40;_parent==NULL &#41;&#41; &#41;
		&#123;
			_rx=_rx-_parent->Pitch&#40;true&#41;;
			_ry=_ry-_parent->Yaw&#40;true&#41;;
			_rz=_rz-_parent->Roll&#40;true&#41;;
		&#125;
				
		if&#40; !&#40;_parent==NULL&#41; &#41;
		&#123;		
			_mat->Overwrite&#40;_parent->_mat&#41;;
			UpdateMat&#40;&#41;;
		&#125;
			
		if&#40; _parent==NULL &#41; 
		&#123;
			UpdateMat&#40;true&#41;;
		&#125;
		
		UpdateChildren&#40;this&#41;;


	&#125;
	

	
	float XPos&#40;int global&#41;
	&#123;
		if&#40; global == false &#41;
				return _px;
		return _mat->grid&#91;3&#93;&#91;0&#93;;
	&#125;
	float YPos&#40;int global&#41;
	&#123;
		if&#40; global == false &#41;
			return _py;
		
		return _mat->grid&#91;3&#93;&#91;1&#93;;
	&#125;
	float ZPos&#40;int global&#41;
	&#123;
		if&#40;global == false&#41;
			return -_pz;
		return -_mat->grid&#91;3&#93;&#91;2&#93;;
	&#125;
	
	float Roll&#40;int global=false&#41;
	&#123;
		if&#40;global == false&#41;
		&#123;
			return _rz;
		&#125;
		float a=_mat->grid&#91;0&#93;&#91;1&#93;;
		float b=_mat->grid&#91;1&#93;&#91;1&#93;;
		if&#40; fabs&#40;a&#41;<0.01 &#41; a=0;
		if&#40; fabs&#40;b&#41;<0.01 &#41; b=0;
			
		return RadToDeg&#40; atan2&#40;a,b&#41; &#41;;
			

	&#125;
	
	
	float Yaw&#40;int global=false&#41;
	&#123;
		if&#40;global == false&#41;
		&#123;
			return _ry;
		&#125;
		float a=_mat->grid&#91;2&#93;&#91;0&#93;;
		float b=_mat->grid&#91;2&#93;&#91;2&#93;;
		if&#40; fabs&#40;a&#41;<0.01 &#41; a=0;
		if&#40; fabs&#40;b&#41;<0.01 &#41; b=0;
		return RadToDeg&#40; atan2&#40;a,b&#41; &#41;;
	&#125;
	float Pitch&#40;int global=false&#41;
	&#123;
		if&#40;global == false&#41;
		&#123;
			return -_rx;
		&#125;
	
		float ang = atan2&#40; _mat->grid&#91;2&#93;&#91;1&#93;,sqrt&#40; _mat->grid&#91;2&#93;&#91;0&#93;*_mat->grid&#91;2&#93;&#91;0&#93;+_mat->grid&#91;2&#93;&#91;2&#93;*_mat->grid&#91;2&#93;&#91;2&#93; &#41; &#41;;
		ang = RadToDeg&#40; ang &#41;;
		if&#40;fabs&#40;ang&#41;<0.01&#41; ang=0;
		return ang;
	&#125;
	
	void UpdateChildren&#40;Base *from&#41;
	&#123;
	
		_childs.start&#40;&#41;;
		while&#40; _childs.next&#40;&#41;==true &#41;
		&#123;
			Base *ent = _childs.get&#40;&#41;;
			ent->_mat->Overwrite&#40; from->_mat &#41;;
			ent->UpdateMat&#40;&#41;;
			UpdateChildren&#40; ent &#41;;
		&#125;
		
	&#125;
	
	void UpdateMat&#40;int loadid = false&#41;
	&#123;
		
		if&#40;loadid==true&#41; _mat->LoadIdentity&#40;&#41;;
		_mat->Translate&#40;_px,_py,_pz&#41;;								
		_mat->RotateYaw&#40;_ry&#41;;
		_mat->RotatePitch&#40;_rx&#41;;
		_mat->RotateRoll&#40;_rz&#41;;
		//_mat.Scale&#40;sx,sy,sz&#41;
					
	&#125;
	
	
&#125;;

List<Base *>entlist;

// Todo&#58;Add some dynamic sizing so you can safelty build undefined meshes.
class Surface
&#123;
public&#58;
	Surface&#40;int verts,int tris&#41;
	&#123;
		_verts = &#40;float *&#41;malloc&#40; verts*3*4 &#41;;
		_tris = &#40;int *&#41;malloc&#40; tris*3*4 &#41;;
		_vertm = verts;
		_trim = tris;
		_vertc=_tric=0;
	&#125;
	int AddVertex&#40; float x,float y,float z &#41;
	&#123;
		_verts&#91; _vertc*3 &#93; = x;
		_verts&#91; _vertc*3+1 &#93; = y;
		_verts&#91; _vertc*3+2 &#93; = z;
		_vertc++;
		return _vertc-1; 
	&#125;
	int AddTriangle&#40; int v0,int v1,int v2 &#41;
	&#123;
		_tris&#91; _tric*3 &#93; = v0;
		_tris&#91; _tric*3+1 &#93; = v1;
		_tris&#91; _tric*3+2 &#93; = v2;
		_tric++;
		return _tric-1;
	&#125;
	float VertexX&#40; int index &#41;
	&#123;
		return _verts&#91; index * 3 &#93;;
	&#125;
	float VertexY&#40; int index &#41;
	&#123;
		return _verts&#91; index * 3 + 1 &#93; ; 
	&#125;
	float VertexZ&#40; int index &#41;
	&#123;
		return _verts&#91; index * 3 + 2 &#93; ;
	&#125;
	int TriVertex&#40; int tri,int vertex &#41;
	&#123;
		return _tris&#91; tri*3+vertex &#93;;
	&#125;
	void Render&#40;&#41;
	&#123;
		glColor3f&#40;1,1,1&#41;;
		glBegin&#40;GL_TRIANGLES&#41;;
		for&#40;int j=0;j<_tric;j++&#41;
		&#123;
			for&#40;int k=0;k<3;k++&#41;
			&#123;
				glVertex3f&#40; VertexX&#40; TriVertex&#40; j,k &#41; &#41;,VertexY&#40; TriVertex&#40; j,k &#41; &#41;,VertexZ&#40; TriVertex&#40;j,k&#41; &#41; &#41;;
			&#125;
		&#125;
		glEnd&#40;&#41;;
	&#125;
	int _vertc,_tric;
	int _vertm,_trim;
	float *_verts;
	int *_tris;
	
&#125;;

class Entity &#58; public Base
&#123;
public&#58;
	Entity&#40;Base * parent&#41;
	&#123;
		_parent = parent;
	&#125;

	void AddSurface&#40; Surface *in&#41;
	&#123;
		_surf.add&#40; in &#41;;
	&#125;
	void ClearSurfaces&#40;&#41;
	&#123;
		_surf.start&#40;&#41;;
		while&#40; _surf.next&#40;&#41;==true &#41;;
		&#123;
			_surf.remove&#40;&#41;;
		&#125;
		_surf.start&#40;&#41;;
	&#125;
	void Render&#40;&#41;
	&#123;
		_surf.start&#40;&#41;;
		while&#40; _surf.next&#40;&#41;==true &#41;
		&#123;
			Surface *surf = _surf.get&#40;&#41;;
			surf->Render&#40;&#41;;
		&#125;
	&#125;
	void Cycle&#40;&#41;
	&#123;
		if&#40; _hidden == true &#41; 
			return ;
		glDisable&#40;GL_TEXTURE_2D&#41;;
		_surf.start&#40;&#41;;
		while&#40; _surf.next&#40;&#41;==true &#41;
		&#123;
			Surface *surf = _surf.get&#40;&#41;;
			glMatrixMode&#40;GL_MODELVIEW&#41;;
			glPushMatrix&#40;&#41;;
			surf->Render&#40;&#41;;
			glPopMatrix&#40;&#41;;
			
		&#125;
		
	&#125;
	List<Surface *>_surf;
	int _surfc;
	int _pickable;
	
&#125;;


Matrix * MatInverse&#40; Matrix *mat &#41;
&#123;
	Matrix * new_mat=new Matrix;

	float tx=0;
	float ty=0;
	float tz=0;
	
	new_mat->grid&#91;0&#93;&#91;0&#93; = mat->grid&#91;0&#93;&#91;0&#93;;
  new_mat->grid&#91;1&#93;&#91;0&#93; = mat->grid&#91;0&#93;&#91;1&#93;;
  new_mat->grid&#91;2&#93;&#91;0&#93; = mat->grid&#91;0&#93;&#91;2&#93;;

	new_mat->grid&#91;0&#93;&#91;1&#93; = mat->grid&#91;1&#93;&#91;0&#93;;
	new_mat->grid&#91;1&#93;&#91;1&#93; = mat->grid&#91;1&#93;&#91;1&#93;;
	new_mat->grid&#91;2&#93;&#91;1&#93; = mat->grid&#91;1&#93;&#91;2&#93;;

	new_mat->grid&#91;0&#93;&#91;2&#93; = mat->grid&#91;2&#93;&#91;0&#93;;
	new_mat->grid&#91;1&#93;&#91;2&#93; = mat->grid&#91;2&#93;&#91;1&#93;;
	new_mat->grid&#91;2&#93;&#91;2&#93; = mat->grid&#91;2&#93;&#91;2&#93;;

	new_mat->grid&#91;0&#93;&#91;3&#93; = 0;
	new_mat->grid&#91;1&#93;&#91;3&#93; = 0;
	new_mat->grid&#91;2&#93;&#91;3&#93; = 0;
	new_mat->grid&#91;3&#93;&#91;3&#93; = 1;
	
	tx = mat->grid&#91;3&#93;&#91;0&#93;;
	ty = mat->grid&#91;3&#93;&#91;1&#93;;
	tz = mat->grid&#91;3&#93;&#91;2&#93;;


	new_mat->grid&#91;3&#93;&#91;0&#93; = -&#40; &#40;mat->grid&#91;0&#93;&#91;0&#93; * tx&#41; + &#40;mat->grid&#91;0&#93;&#91;1&#93; * ty&#41; + &#40;mat->grid&#91;0&#93;&#91;2&#93; * tz&#41; &#41;;
	new_mat->grid&#91;3&#93;&#91;1	3; = -&#40; &#40;mat->grid&#91;1&#93;&#91;0&#93; * tx&#41; + &#40;mat->grid&#91;1&#93;&#91;1&#93; * ty&#41; + &#40;mat->grid&#91;1&#93;&#91;2&#93; * tz&#41; &#41;;
	new_mat->grid&#91;3&#93;&#91;2&#93; = -&#40; &#40;mat->grid&#91;2&#93;&#91;0&#93; * tx&#41; + &#40;mat->grid&#91;2&#93;&#91;1&#93; * ty&#41; + &#40;mat->grid&#91;2&#93;&#91;2&#93; * tz&#41; &#41;;
	
	return new_mat;
	
&#125;


class Camera &#58; public Base
&#123;
public&#58;
	float _viewx,_viewy,_vwidth,_vheight;
	float _clsr,_clsg,_clsb;
	float _zDepth,_z2Depth;
	float _zoom;
	Camera&#40;&#41;
	&#123;
		_viewx=0;
		_viewy=0;
		_vwidth=480;
		_vheight=272;
		_clsr=0;
		_clsg=0;
		_clsb=0;
		_zDepth=0.1;
		_z2Depth = 1000;
		_zoom = 1;
	&#125;	
	Camera&#40;Base *parent&#41;
	&#123;
		_viewx=0;
		_viewy=0;
		_vwidth=480;
		_vheight=272;
		_clsr=0;
		_clsg=0;
		_clsb=0;
		_zDepth=0.1;
		_z2Depth = 1000;
		_zoom = 1;
		_parent = parent;
		
	&#125;
void Move&#40;float x,float y,float z&#41;
	&#123;
		return;
		
		float mx=x;
		float my=y;
		float mz=-z;

		Matrix *new_mat=new Matrix;
		new_mat->LoadIdentity&#40;&#41;;
		new_mat->Rotate&#40;_rx,_ry,_rz&#41;;
		new_mat->Translate&#40;mx,my,mz&#41;;
	
		mx=new_mat->grid&#91;3&#93;&#91;0&#93;;
		my=new_mat->grid&#91;3&#93;&#91;1&#93;;
		mz=new_mat->grid&#91;3&#93;&#91;2&#93;;
		
		_px=_px+mx;
		_py=_py+my;
		_pz=_pz+mz;

		if&#40; !&#40;_parent==NULL&#41; &#41;
		&#123;
			_mat->Overwrite&#40;_parent->_mat&#41;;
			UpdateMat&#40;&#41;;
		&#125;

		if&#40; _parent==NULL &#41;
		&#123;
			UpdateMat&#40;true&#41;;
		&#125;
		
		UpdateChildren&#40;this&#41;;
		
		delete new_mat;
		
		
	
	&#125;
	void Draw3D&#40;&#41;
	&#123;
		SetViewPort3D&#40;&#41;;
		glShadeModel&#40;GL_SMOOTH&#41;;
    glClearDepth&#40;1.0&#41;;
    glEnable&#40;GL_DEPTH_TEST&#41;;
    glDepthFunc&#40;GL_LEQUAL&#41;;
    glDisable&#40;GL_CULL_FACE&#41;;
    glDisable&#40;GL_LIGHTING&#41;;
	&#125;
	
	void SetViewPort3D&#40;&#41;
	&#123;
		glViewport&#40;_viewx,_viewy, _vwidth, _vheight&#41;;
  	glMatrixMode&#40;GL_PROJECTION&#41;;
    glLoadIdentity&#40;&#41;;
    float aspect = &#40;&#40;float&#41;_vwidth /&#40;float&#41;_vheight&#41;;
		gluPerspective&#40; RadToDeg&#40; atan&#40; &#40;1.0/&#40;_zoom*aspect&#41;&#41;&#41;&#41;*2.0,aspect,_zDepth,_z2Depth&#41;;
		glMatrixMode&#40;GL_MODELVIEW&#41;;
	&#125;
	
	void Viewport&#40; int x,int y,int w,int h&#41;
	&#123;
		_viewx = x;
		_viewy = 272 - h-y;
		_vwidth = w;
		_vheight = h;
	&#125;
	
	void Zoom&#40;float zoom&#41;
	&#123;
		_zoom = zoom;
	&#125;
	void ClsColor&#40;float r,float g,float b&#41;
	&#123;
		_clsr = r;
		_clsg = g;
		_clsb = b;
	&#125;
	void Range&#40;float z1,float z2&#41;
	&#123;
		_zDepth = z1;
		_z2Depth = z2;
	&#125;
	void Cycle&#40;&#41;
	&#123;
		glDepthMask&#40; GL_TRUE &#41;;
		glClear&#40;GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT&#41;;
		Draw3D&#40;&#41;;
		Matrix *new_mat = MatInverse&#40; _mat &#41;;
		glLoadMatrixf&#40; &#40;GLfloat *&#41;new_mat->grid &#41;;
		delete new_mat;
	&#125;
	
&#125;;



class EngineFactory
&#123;
public&#58;
	
	Entity * ProduceEntity&#40;Base *parent=NULL&#41;
	&#123;
		Entity *out = new Entity&#40;parent&#41;;
		if&#40; !&#40;out->_parent==NULL&#41;&#41;
		&#123;
			out->_mat->Overwrite&#40; out->_parent->_mat &#41;;
			out->UpdateMat&#40;&#41;;
		&#125;
		else
		&#123;
			out->UpdateMat&#40;true&#41;;
		&#125;
		return out;
	&#125;
	
	Camera * ProduceCamera&#40;Base *parent=NULL&#41;
	&#123;
		Camera *out = new Camera&#40;parent&#41;;
		if&#40; !&#40;out->_parent==NULL&#41;&#41;
		&#123;
			out->_mat->Overwrite&#40;out->_parent->_mat&#41;;
			out->UpdateMat&#40;&#41;;
		&#125;else
		&#123;
			out->UpdateMat&#40;true&#41;;
		&#125;
		return out;
	&#125;
&#125;;

class RenderEngine
&#123;
public&#58;
	void RenderScene&#40;&#41;
	&#123;
		if&#40;_ActiveCam==NULL&#41; return;
		_ActiveCam->Cycle&#40;&#41;;
		entlist.start&#40;&#41;;
		
		while&#40; entlist.next&#40;&#41;==true &#41;
		&#123;
			//glTranslatef&#40;0,0,-40&#41;;
			Entity *ent =&#40;Entity *&#41;entlist.get&#40;&#41;;
			ent->Cycle&#40;&#41;;
		&#125;	
		
	&#125;
	Camera *_ActiveCam;
&#125;;

RenderEngine *Renderer;

EngineFactory *Factory;
void InitRaptor&#40;&#41;
&#123;
	Factory = new EngineFactory;
	Renderer = new RenderEngine;
&#125;

class Prefab
&#123;
public&#58;
	Entity * CreateCube&#40;float size,Base *parent = NULL&#41;
	&#123;
		Entity *out = Factory->ProduceEntity&#40;parent&#41;;
		Surface *surf = new Surface&#40;8,12&#41;;
		size = size / 2.0;
		int v0,v1,v2,v3,v4,v5,v6,v7,v8;
		
		v0 = surf->AddVertex&#40; -size,-size,-size &#41;;
		v1 = surf->AddVertex&#40; size,-size,-size &#41;;
		v2 = surf->AddVertex&#40; size,size,-size &#41;;
		v3 = surf->AddVertex&#40; -size,size,-size &#41;;
		
		v4 = surf->AddVertex&#40; -size,-size,size &#41;;
		v5 = surf->AddVertex&#40; size,-size,size &#41;;
		v6 = surf->AddVertex&#40; size,size,size &#41;;
		v7 = surf->AddVertex&#40; -size,size,size &#41;;
		
		surf->AddTriangle&#40; v0,v1,v2 &#41;;
		surf->AddTriangle&#40; v2,v3,v0 &#41;;
		
		surf->AddTriangle&#40; v4,v5,v6 &#41;;
		surf->AddTriangle&#40; v6,v7,v4 &#41;;
		
		surf->AddTriangle&#40; v0,v4,v3 &#41;;
		surf->AddTriangle&#40; v4,v7,v3 &#41;;
		
		surf->AddTriangle&#40; v1,v5,v2 &#41;;
		surf->AddTriangle&#40; v5,v6,v2 &#41;;
		
		surf->AddTriangle&#40; v0,v1,v4 &#41;;
		surf->AddTriangle&#40; v1,v5,v4 &#41;;
		
		surf->AddTriangle&#40; v3,v2,v7 &#41;;
		surf->AddTriangle&#40; v2,v6,v7 &#41;;
		
		out->AddSurface&#40; surf &#41;;
		entlist.add&#40; &#40;Base *&#41;out &#41;; 		
		return out;
		
	&#125;
&#125;;



/*
  For all 2d drawing operations. 
*/
class Pen
&#123;
public&#58;
	Pen&#40;&#41;
	&#123;
		_r=1;
		_g=1;
		_b=1;
		_a=1;
		_tex = NULL;
	&#125;
	void Bind&#40;&#41;
	&#123;
		//Todo&#58;Make a note of current color value.&#40;If possible with PspGL??&#41;
		glColor4f&#40; _r,_g,_b,_a &#41;;
		if&#40;!&#40;_tex==NULL&#41;&#41;
		&#123;
			_tex->Bind&#40;&#41;;
		&#125;
	&#125;
	void Unbind&#40;&#41;
	&#123;
		//Todo&#58;Return color to previous value.
		if&#40;!&#40;_tex==NULL&#41;&#41;
		&#123;
			_tex->Unbind&#40;&#41;;
		&#125;
	&#125;
	
	void SetColor&#40; float r,float g,float b,float a&#41;
	&#123;
		_r=r;
		_g=g;
		_b=b;
		_a=a;
	&#125;
	void Setcolor&#40; float r,float g,float b &#41;
	&#123;
		_r=r;
		_g=g;
		_b=b;
		_a=1; //Or leave it at it's present color?
	&#125;
	void Rect&#40; float x,float y,float w,float h &#41;
	&#123;
		glBegin&#40;GL_QUADS&#41;;
		glTexCoord2f&#40; 0,1 &#41;;
		glVertex2f&#40; x,272-y &#41;;
		glTexCoord2f&#40; 1,1 &#41;;
		glVertex2f&#40; x+w,272-y &#41;;
		glTexCoord2f&#40; 1,0 &#41;;
		glVertex2f&#40; x+w,272-y-h &#41;;
		glTexCoord2f&#40; 0,0 &#41;;
		glVertex2f&#40; x,272-y-h &#41;;
		glEnd&#40;&#41;;
	&#125;
	
	void Line&#40; float x1,float y1,float x2,float y2 &#41;
	&#123;
		glBegin&#40;GL_LINES&#41;;
		glVertex2f&#40; x1,y1 &#41;;
		glVertex2f&#40; x2,y2 &#41;;
		glEnd&#40;&#41;;
	&#125;
	
	void RectHollow&#40; float x1,float y1,float w,float h &#41;
	&#123;
		glBegin&#40;GL_LINES&#41;;
		
		glVertex2f&#40; x1,y1 &#41;;
		glVertex2f&#40; x1+w,y1 &#41;;
		
		glVertex2f&#40; x1+w,y1 &#41;;
		glVertex2f&#40; x1+w,y1+h &#41;;
		
		glVertex2f&#40; x1+w,y1+h &#41;;
		glVertex2f&#40; x1,y1+h &#41;;
		
		glVertex2f&#40; x1,y1 &#41;;
		glVertex2f&#40; x1,y1+h &#41;;
		
	&#125;
	void SetTexture&#40;Texture *tex&#41;
	&#123;
		_tex = tex;
	&#125;
	Texture *_tex;
	float _r,_g,_b,_a;
&#125;;

class Joypad
&#123;
public&#58;
	Joypad&#40;&#41;
	&#123;
  	sceCtrlSetSamplingCycle&#40;0&#41;;
		sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;	  
	&#125;
	void Update&#40;&#41;
	&#123;
		_select = _square = _circle = _cross = _ltrigger = _rtrigger = _start = _left = _right = _up = _down = 0;
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;; 
  	float xi,yi;
  	xi = &#40;float&#41;pad.Lx / 255.0f;
  	yi = &#40;float&#41;pad.Ly / 255.0f;
  	_x = &#40;xi-0.5f&#41;*2.0f;
  	_y =-&#40; &#40;yi-0.5f&#41;*2.0f&#41;;
  	if&#40; fabs&#40;_x&#41;<0.2 &#41; _x=0;
  	if&#40; fabs&#40;_y&#41;<0.2 &#41; _y=0;
 		if &#40;pad.Buttons != 0&#41;&#123;
			if &#40;pad.Buttons & PSP_CTRL_SQUARE&#41;&#123;
				_square = 1;
			&#125;
			if &#40;pad.Buttons & PSP_CTRL_TRIANGLE&#41;&#123;
				_triangle = 1;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;&#123;
				_circle = 1;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;&#123;
				_cross = 1;
			&#125; 

			if &#40;pad.Buttons & PSP_CTRL_UP&#41;&#123;
				_up = 1;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_DOWN&#41;&#123;
				_down = 1;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_LEFT&#41;&#123;
				_left = 1;
			&#125; 
			if &#40;pad.Buttons & PSP_CTRL_RIGHT&#41;&#123;
				_right = 1;
			&#125;      

			if &#40;pad.Buttons & PSP_CTRL_START&#41;&#123;
				_start = 1;
			&#125;
			if &#40;pad.Buttons & PSP_CTRL_SELECT&#41;&#123;
				_select = 1;
			&#125;
			if &#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41;&#123;
				_ltrigger = 1;
			&#125;
			if &#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41;&#123;
				_rtrigger = 1;
			&#125;      
		&#125;
  &#125;
  int _cross,_square,_triangle,_circle;
  int _left,_right,_up,_down;
  int _ltrigger,_rtrigger;
  int _select,_start;
  float _x,_y;
	SceCtrlData pad;
&#125;;

class Cursor
&#123;
public&#58;
	Cursor&#40;Texture *tex&#41;
	&#123;
		_size = 32;
		_tex = tex;
		_style = new Pen&#40;&#41;;
		_style->SetTexture&#40; _tex &#41;;
		_x = 480/2.0f;
		_y = 272/2.0f;
	&#125;
	void Move&#40; float x,float y&#41;
	&#123;
		_x+=x;
		_y+=y;
		if&#40;_x<0&#41;_x=0;
		if&#40;_y<0&#41;_y=0;
		if&#40;_x>480&#41;_x=480;
		if&#40;_y>272&#41;_y=272;
	&#125;
	void Render&#40;&#41;
	&#123;
		_style->Bind&#40;&#41;;
		_style->Rect&#40;_x,_y,_size,_size &#41;;
		_style->Unbind&#40;&#41;;
	&#125;
	void SetSize&#40;float size&#41;
	&#123;
		_size = size;
	&#125;
	Pen *_style;
	float _x,_y;
	float _size;
	Texture *_tex;
&#125;;
class Gui
&#123;
public&#58;
	Gui&#40; Cursor *cursor,Joypad *joy &#41;
	&#123;
		_cur = cursor;
		_joy = joy;
	&#125;
	void Update&#40;&#41;
	&#123;
		_cur->Move&#40; _joy->_x,_joy->_y &#41;;
	&#125;
	void Render&#40;&#41;
	&#123;
		_cur->Render&#40;&#41;;
	&#125;
	Cursor *_cur;
	Joypad *_joy;
&#125;;

#define printf pspDebugScreenPrintf

void AddTest&#40;&#41; 
&#123; 


int num1=5; 
int num2=10; 
int out=0; 
int to = 5;
float r17,r16;
r16=0;
r17=0;
for&#40;int i=0;i<to;i++&#41;
&#123;
	float res = num1*num2;
	r16 = res;
	r17 = r16+r17;
&#125;
printf&#40;"C Res&#58;%f",r17&#41;;

num1 =5;
num2 = 10;
out = 0;
to = 5;
asm __volatile__ &#40;"\n\ 
ori $5,$0,0\n\
ori $17,$0,0\n\
loop&#58;\n\
mul %1,%2\n\
mflo $16\n\
addu $19,$16,$17\n\
move $17,$19\n\
addiu $5,$5,1\n\
bne $5,%3,loop\n\
nop\n\
move %0,$17\n\
"&#58;"=r"&#40;out&#41;&#58;"r"&#40;num1&#41;,"r"&#40;num2&#41;,"r"&#40;to&#41;&#41;; 

	printf&#40;"Asm Result&#58;%d \n",out&#41;; 
&#125;

class c1
&#123;
	public&#58;
		void test&#40;&#41;
		&#123;
			printf&#40;"Test 1 called \n"&#41;;
		&#125;
&#125;;

class c2 &#58; c1
&#123;
	public&#58;
		void test&#40;&#41;
		&#123;
			printf&#40;"Test 2 called.\n"&#41;;
		&#125;
&#125;;

#define new&#40;x&#41; memalign&#40;64, sizeof&#40;x&#41; &#41;;

int main&#40;int argc, char **argv&#41; 
&#123;	
//	pspDebugScreenInit&#40;&#41;;

	InitRaptor&#40;&#41;;
	
	
	Display *sys = new Display&#40;argc,argv&#41;;
	
	
	Joypad *joy = new Joypad;
  
	Prefab *prefab = new Prefab;
 	Camera *vcam = Factory->ProduceCamera&#40;&#41;;
 	Entity *box = prefab->CreateCube&#40; 10 &#41;;
 	vcam->Position&#40;0,20,-40&#41;;

	Renderer->_ActiveCam = vcam;
	box->Position&#40;0,0,0&#41;;
	box->Rotate&#40;0,0,0&#41;;
	vcam->PointAt&#40;0,0,0&#41;;
	float bx,by;
	bx=0;
	by=0;
	float cp=0,cy=0;
 	while&#40;1&#41;
  &#123;
  	//vcam->Rotate&#40; vcam->Pitch&#40;&#41;,vcam->Yaw&#40;&#41;,vcam->Roll&#40;&#41; &#41;;
  	joy->Update&#40;&#41;;
  	
  	glClear&#40;GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT&#41;;		// Clear The Screen And The Depth Buffer
		vcam->Move&#40;0,0,0&#41;;
		
  	cy+=0.1;
  	if&#40;cy>360&#41;cy=0;
	
  	
  	  	//mgui->Update&#40;&#41;;
  	//mgui->Render&#40;&#41;;
 		Renderer->RenderScene&#40;&#41;; 	  	
  	
  	glutSwapBuffers&#40;&#41;;
  &#125;

  return &#40;0&#41;;
&#125;

Here's the makefile

Code: Select all

TARGET = lesson2
OBJS = main.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

PSPBIN = $&#40;PSPSDK&#41;/../bin
CFLAGS += -I$&#40;PSPSDK&#41;/../include  -fsingle-precision-constant -g -Wall -O2 
LIBS += -lGLU -lglut -lGLU -lGL -lm -lc -lpsputility -lpspdebug -lpspge -lpspdisplay -lpspctrl -lpspsdk -lpspvfpu -lpsplibc -lpspuser -lpspkernel -lpsprtc -lpsppower -lstdc++
LDFLAGS += -DMODULE_NAME="lesson2" psp-setup.c


EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = LESSON2
# PSP_EBOOT_ICON = hero.png
# PSP_EBOOT_PIC1 = bg.png


PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

Ah, closing to understanding it. In cleanign up the code for this post I removed the supposedly redundent code beyond the return call that would never be reached.
Doing so actually fixes the problem. The box no longer disappears.
But putting it back in (as I now have, check above) results in a broken app again, despite the fact the code *should* never be executed as the function returns immediately.

So, alignment issue I'm just not well trained enough in psp to grasp yet?
zilt
Posts: 45
Joined: Tue Feb 21, 2006 11:59 pm
Location: Ontario, Canada
Contact:

Post by zilt »

I can't see anything obviously wrong with the code. Try compiling without -O2 to see if it makes a difference (you currenlty add it in twice - and there might be a bug in the optimizer). Other then that, it's looks ok. I also wouldn't redefine

Code: Select all

new
.
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

You sir are a wonderful wonderful man. That solved it :)

I would never have thought to remove the optimization flag. Thanks alot :)
zilt
Posts: 45
Joined: Tue Feb 21, 2006 11:59 pm
Location: Ontario, Canada
Contact:

Post by zilt »

I'm glad that it's working for you now. However, as far as I'm aware, '-O2' should be a safe optimizing level. I'll have to read up on it a bit. Try with just "-O" - that should be safe.
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

O2 should be safe indeed....but optimizer bugs occur every now and then :=P try O1, Os too (Os even results in better code in many cases)
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

Yeah I can confirm it works fine just with -o, just tried it.

wish I could help you with why -o2 isn't working but that's a bit out of my depth really.
ector
Posts: 195
Joined: Thu May 12, 2005 10:22 pm

Post by ector »

The compiler isn't THAT buggy. If you're having bugs that disappear when turning off optimizing in such a simple program, it's far more likely that you have some kind of broken edge case or alignment bug in your code, than you actually running into a real compiler bug.

I recommend trying to get the thing working with -O2. Believe me, you will want the speed if you keep adding features to the program, the optimizer makes a big difference.
http://www.dtek.chalmers.se/~tronic/PSPTexTool.zip Free texture converter for PSP with source. More to come.
Post Reply