⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 leveledit.cpp

📁 一个96K的3D游戏源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:

float frustum[6][4];

bool BoxInFrustum(tP3D P1,tP3D P2)
{
   int p;

   for( p = 0; p < 6; p++ )
   {
      if( frustum[p][0] * P1.X + frustum[p][1] * P1.Y + frustum[p][2] * P1.Z + frustum[p][3] > 0 ) continue;
      if( frustum[p][0] * P1.X + frustum[p][1] * P1.Y + frustum[p][2] * P2.Z + frustum[p][3] > 0 ) continue;
      if( frustum[p][0] * P1.X + frustum[p][1] * P2.Y + frustum[p][2] * P1.Z + frustum[p][3] > 0 ) continue;
      if( frustum[p][0] * P1.X + frustum[p][1] * P2.Y + frustum[p][2] * P2.Z + frustum[p][3] > 0 ) continue;
      if( frustum[p][0] * P2.X + frustum[p][1] * P1.Y + frustum[p][2] * P1.Z + frustum[p][3] > 0 ) continue;
      if( frustum[p][0] * P2.X + frustum[p][1] * P1.Y + frustum[p][2] * P2.Z + frustum[p][3] > 0 ) continue;
      if( frustum[p][0] * P2.X + frustum[p][1] * P2.Y + frustum[p][2] * P1.Z + frustum[p][3] > 0 ) continue;
      if( frustum[p][0] * P2.X + frustum[p][1] * P2.Y + frustum[p][2] * P2.Z + frustum[p][3] > 0 ) continue;

	  return(false);
   }
   return true;
	
}

bool SphereInFrustum( tP3D Pos, float radius )
{
   int p;

   for( p = 0; p < 6; p++ )
      if( frustum[p][0] * Pos.X + frustum[p][1] * Pos.Y + frustum[p][2] * Pos.Z + frustum[p][3] <= -radius )
         return false;
   return true;
}

void ExtractFrustum()
{
   float   proj[16];
   float   modl[16];
   float   clip[16];
   float   t;

   /* Get the current PROJECTION matrix from OpenGL */
   glGetFloatv( GL_PROJECTION_MATRIX, proj );

   /* Get the current MODELVIEW matrix from OpenGL */
   glGetFloatv( GL_MODELVIEW_MATRIX, modl );

   /* Combine the two matrices (multiply projection by modelview) */
   clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
   clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
   clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14];
   clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15];

   clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
   clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
   clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14];
   clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15];

   clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12];
   clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13];
   clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14];
   clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15];

   clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12];
   clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13];
   clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14];
   clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15];

   /* Extract the numbers for the RIGHT plane */
   frustum[0][0] = clip[ 3] - clip[ 0];
   frustum[0][1] = clip[ 7] - clip[ 4];
   frustum[0][2] = clip[11] - clip[ 8];
   frustum[0][3] = clip[15] - clip[12];

   /* Normalize the result */
   t = sqrt( frustum[0][0] * frustum[0][0] + frustum[0][1] * frustum[0][1] + frustum[0][2] * frustum[0][2] );
   frustum[0][0] /= t;
   frustum[0][1] /= t;
   frustum[0][2] /= t;
   frustum[0][3] /= t;

   /* Extract the numbers for the LEFT plane */
   frustum[1][0] = clip[ 3] + clip[ 0];
   frustum[1][1] = clip[ 7] + clip[ 4];
   frustum[1][2] = clip[11] + clip[ 8];
   frustum[1][3] = clip[15] + clip[12];

   /* Normalize the result */
   t = sqrt( frustum[1][0] * frustum[1][0] + frustum[1][1] * frustum[1][1] + frustum[1][2] * frustum[1][2] );
   frustum[1][0] /= t;
   frustum[1][1] /= t;
   frustum[1][2] /= t;
   frustum[1][3] /= t;

   /* Extract the BOTTOM plane */
   frustum[2][0] = clip[ 3] + clip[ 1];
   frustum[2][1] = clip[ 7] + clip[ 5];
   frustum[2][2] = clip[11] + clip[ 9];
   frustum[2][3] = clip[15] + clip[13];

   /* Normalize the result */
   t = sqrt( frustum[2][0] * frustum[2][0] + frustum[2][1] * frustum[2][1] + frustum[2][2] * frustum[2][2] );
   frustum[2][0] /= t;
   frustum[2][1] /= t;
   frustum[2][2] /= t;
   frustum[2][3] /= t;

   /* Extract the TOP plane */
   frustum[3][0] = clip[ 3] - clip[ 1];
   frustum[3][1] = clip[ 7] - clip[ 5];
   frustum[3][2] = clip[11] - clip[ 9];
   frustum[3][3] = clip[15] - clip[13];

   /* Normalize the result */
   t = sqrt( frustum[3][0] * frustum[3][0] + frustum[3][1] * frustum[3][1] + frustum[3][2] * frustum[3][2] );
   frustum[3][0] /= t;
   frustum[3][1] /= t;
   frustum[3][2] /= t;
   frustum[3][3] /= t;

   /* Extract the FAR plane */
   frustum[4][0] = clip[ 3] - clip[ 2];
   frustum[4][1] = clip[ 7] - clip[ 6];
   frustum[4][2] = clip[11] - clip[10];
   frustum[4][3] = clip[15] - clip[14];

   /* Normalize the result */
   t = sqrt( frustum[4][0] * frustum[4][0] + frustum[4][1] * frustum[4][1] + frustum[4][2] * frustum[4][2] );
   frustum[4][0] /= t;
   frustum[4][1] /= t;
   frustum[4][2] /= t;
   frustum[4][3] /= t;

   /* Extract the NEAR plane */
   frustum[5][0] = clip[ 3] + clip[ 2];
   frustum[5][1] = clip[ 7] + clip[ 6];
   frustum[5][2] = clip[11] + clip[10];
   frustum[5][3] = clip[15] + clip[14];

   /* Normalize the result */
   t = sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * frustum[5][1] + frustum[5][2] * frustum[5][2] );
   frustum[5][0] /= t;
   frustum[5][1] /= t;
   frustum[5][2] /= t;
   frustum[5][3] /= t;
}


void DrawDaBox(tP3D P1,tP3D P2,float R,float G,float B,float A)
{
	tWandVertice *Temp;
	tWandPoly *TempP;
	tP3D Normal,DaPos;

	glDepthMask(false);
	glDisable(GL_TEXTURE_2D);

	glBegin(GL_TRIANGLES);
	glColor4f(R,G,B,A);
	glNormal3f(0,1,0);

	glVertex3f(P1.X,P1.Y,P1.Z);
	glVertex3f(P2.X,P1.Y,P1.Z);
	glVertex3f(P1.X,P2.Y,P1.Z);

	glVertex3f(P1.X,P2.Y,P1.Z);
	glVertex3f(P2.X,P1.Y,P1.Z);
	glVertex3f(P2.X,P2.Y,P1.Z);


	glVertex3f(P2.X,P1.Y,P2.Z);
	glVertex3f(P1.X,P1.Y,P2.Z);
	glVertex3f(P1.X,P2.Y,P2.Z);

	glVertex3f(P2.X,P1.Y,P2.Z);
	glVertex3f(P1.X,P2.Y,P2.Z);
	glVertex3f(P2.X,P2.Y,P2.Z);


	glVertex3f(P1.X,P1.Y,P1.Z);
	glVertex3f(P1.X,P2.Y,P1.Z);
	glVertex3f(P1.X,P1.Y,P2.Z);

	glVertex3f(P1.X,P1.Y,P2.Z);
	glVertex3f(P1.X,P2.Y,P1.Z);
	glVertex3f(P1.X,P2.Y,P2.Z);


	glVertex3f(P2.X,P2.Y,P1.Z);
	glVertex3f(P2.X,P1.Y,P1.Z);
	glVertex3f(P2.X,P1.Y,P2.Z);

	glVertex3f(P2.X,P2.Y,P1.Z);
	glVertex3f(P2.X,P1.Y,P2.Z);
	glVertex3f(P2.X,P2.Y,P2.Z);


	glVertex3f(P1.X,P1.Y,P1.Z);
	glVertex3f(P1.X,P1.Y,P2.Z);
	glVertex3f(P2.X,P1.Y,P1.Z);

	glVertex3f(P2.X,P1.Y,P1.Z);
	glVertex3f(P1.X,P1.Y,P2.Z);
	glVertex3f(P2.X,P1.Y,P2.Z);

	glVertex3f(P1.X,P2.Y,P2.Z);
	glVertex3f(P1.X,P2.Y,P1.Z);
	glVertex3f(P2.X,P2.Y,P1.Z);

	glVertex3f(P1.X,P2.Y,P2.Z);
	glVertex3f(P2.X,P2.Y,P1.Z);
	glVertex3f(P2.X,P2.Y,P2.Z);

	glEnd();
	glDepthMask(true);
//	DrawGrund(Cube,true,0,Pos,NULL,1,-1,1,1);
}

void BuildSphericImage(char *Buffer,long _X,long _Y,long BPP)
{
	double theta,phi,phi2;
	int i,i2,j;

	long Size;
	char *Temp;
	float X=_X;
	float Y=_Y;
	float Pos,PosX;

	Size=X*Y*BPP;
	Temp=(char*)malloc(Size);
	memcpy(Temp,Buffer,Size);
	memset(Buffer,128,Size);

	for (j=0;j<Y;j++) {
//	  theta = Pi * (j - (Y-1)/2.0) / (double)(Y-1);
	  Pos=(float)j*2/(float)(Y-1) - 1;
      for (i=0;i<X;i++) {
		 PosX=(float)i*2/(float)(X-1)-1;
		 i2=(X-1)/2-cos(Pos*PiHalb)*((X-1)/2)*PosX;
//         phi  = PiMal2 * (i - X/2.0) / (double)X;
//         phi2 = phi * cos(theta);
//         i2  = phi2 * X / PiMal2 + X/2;

//		 if(i2<0) i2=0;
//		 if(i2>X-1) i2=X-1;
         if (i2 < 0 || i2 > X-1) {
//            newpixel = red;                         /* Should not happen */
         } else {
//            newpixel = imagein[j*image.width+i2];
//	        imageout[j*width+i] = image.newpixel;
			memcpy((Buffer+(j*_X+i)*BPP),(Temp+(j*_X+i2)*BPP),BPP);
         }
      }
	}

	free(Temp);
}

bool LoadTGA(TextureImage *texture, char *filename)				// Loads A TGA File Into Memory
{    
	GLubyte		TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};		// UncomPressed TGA Header
	GLubyte		TGAcomPare[12];									// Used To ComPare TGA Header
	GLubyte		header[6];										// First 6 Useful Bytes From The Header
	GLuint		bytesPerPixel;									// Holds Number Of Bytes Per Pixel Used In The TGA File
	GLuint		imageSize;										// Used To Store The Image Size When Setting Aside Ram
	GLuint		temP;											// TemPorary Variable
	GLuint		tyPe=GL_RGBA;									// Set The Default GL Mode To RBGA (32 BPP)

	FILE *file = fopen(filename, "rb");							// OPen The TGA File

	if(	file==NULL ||											// Does File Even Exist?
		fread(TGAcomPare,1,sizeof(TGAcomPare),file)!=sizeof(TGAcomPare) ||	// Are There 12 Bytes To Read?
		memcmp(TGAheader,TGAcomPare,sizeof(TGAheader))!=0				||	// Does The Header Match What We Want?
		fread(header,1,sizeof(header),file)!=sizeof(header))				// If So Read Next 6 Header Bytes
	{
		if (file == NULL)										// Did The File Even Exist? *Added Jim Strong*
			return FALSE;										// Return False
		else													// Otherwise
		{
			fclose(file);										// If Anything Failed, Close The File
			return FALSE;										// Return False
		}
	}

	texture->width  = header[1] * 256 + header[0];				// Determine The TGA Width	(highbyte*256+lowbyte)
	texture->height = header[3] * 256 + header[2];				// Determine The TGA Height	(highbyte*256+lowbyte)
    
 	if(	texture->width	<=0	||									// Is The Width Less Than Or Equal To Zero
		texture->height	<=0	||									// Is The Height Less Than Or Equal To Zero
		(header[4]!=24 && header[4]!=32))						// Is The TGA 24 or 32 Bit?
	{
		fclose(file);											// If Anything Failed, Close The File
		return FALSE;											// Return False
	}

	texture->bPP	= header[4];								// Grab The TGA's Bits Per Pixel (24 or 32)
	bytesPerPixel	= texture->bPP/8;							// Divide By 8 To Get The Bytes Per Pixel
	imageSize		= texture->width*texture->height*bytesPerPixel;	// Calculate The Memory Required For The TGA Data

	texture->imageData=(GLubyte *)malloc(imageSize);			// Reserve Memory To Hold The TGA Data

	if(	texture->imageData==NULL ||								// Does The Storage Memory Exist?
		fread(texture->imageData, 1, imageSize, file)!=imageSize)	// Does The Image Size Match The Memory Reserved?
	{
		if(texture->imageData!=NULL)							// Was Image Data Loaded
			free(texture->imageData);							// If So, Release The Image Data

		fclose(file);											// Close The File
		return FALSE;											// Return False
	}

//	BuildSphericImage((char*)texture->imageData,texture->width,texture->height,bytesPerPixel);

	for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)			// LooP Through The Image Data
	{															// SwaPs The 1st And 3rd Bytes ('R'ed and 'B'lue)
		temP=texture->imageData[i];								// TemPorarily Store The Value At Image Data 'i'
		texture->imageData[i] = texture->imageData[i + 2];		// Set The 1st Byte To The Value Of The 3rd Byte
		texture->imageData[i + 2] = temP;						// Set The 3rd Byte To The Value In 'temP' (1st Byte Value)
	}

	fclose (file);												// Close The File

	// Build A Texture From The Data
	glGenTextures(1, &texture[0].texID);						// Generate OPenGL texture IDs

	glBindTexture(GL_TEXTURE_2D, texture[0].texID);				// Bind Our Texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Filtered
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Filtered
	
	if (texture[0].bPP==24)										// Was The TGA 24 Bits
	{
		tyPe=GL_RGB;											// If So Set The 'tyPe' To GL_RGB
	}

	glTexImage2D(GL_TEXTURE_2D, 0, tyPe, texture[0].width, texture[0].height, 0, tyPe, GL_UNSIGNED_BYTE, texture[0].imageData);

	return true;												// Texture Building Went Ok, Return True
}


BOOL LoadGLTexture(GLuint *texPntr, char* name,bool Spheric)
{
	BOOL success = FALSE;
	AUX_RGBImageRec *TextureImage = NULL;
	
	glGenTextures(1, texPntr);						// Generate 1 texture

	FILE* test=NULL;
	TextureImage = NULL;

	test = fopen(name, "r");						// test to see if the file exists
	if (test != NULL) {								// if it does
		fclose(test);									// close the file
		TextureImage = auxDIBImageLoad(name);			// and load the texture
	}
	
	if(Spheric) BuildSphericImage((char*)TextureImage->data,TextureImage->sizeX,TextureImage->sizeY,3);

	if (TextureImage != NULL) {						// if it loaded
		success = TRUE;

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, *texPntr);
		glTexImage2D(GL_TEXTURE_2D, 0, 3,    TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	}

	if (TextureImage->data)
		free(TextureImage->data);

	return success;
}

BOOL LoadTCMTexture(unsigned int *texPntr, char* name)
{
	BOOL success = FALSE;
	AUX_RGBImageRec *TextureImage = NULL;
	tLayer *Layer;

	glGenTextures(1, texPntr);						// Generate 1 texture

	FILE* test=NULL;
	TextureImage = NULL;
/*
	test = fopen(name, "r");						// test to see if the file exists
	if (test != NULL) {								// if it does
		fclose(test);									// close the file
		TextureImage = auxDIBImageLoad(name);			// and load the texture
	}
*/
	LoadToLoadedImage(name);

//	if (TextureImage != NULL) 
	{						// if it loaded
		success = TRUE;

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, *texPntr);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,    LoadedImage->XSize, LoadedImage->YSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, LoadedImage->Data);
//		glTexImage2D(GL_TEXTURE_2D, 0, 3,    TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	}
/*
	if (TextureImage->data)
		free(TextureImage->data);
*/
	DoneWithLoadedImage();
	return success;
}


GLvoid BuildFont(GLvoid)										// Build Our Font DisPlay List
{
	base=glGenLists(95);										// Creating 95 DisPlay Lists
	glBindTexture(GL_TEXTURE_2D, textures[9].texID);			// Bind Our Font Texture
	for (int looP=0; looP<95; looP++)							// LooP Through All 95 Lists
	{
		float cx=float(looP%16)/16.0f;							// X Position Of Current Character
		float cy=float(looP/16)/8.0f;							// Y Position Of Current Character

		glNewList(base+looP,GL_COMPILE);						// Start Building A List
			glBegin(GL_QUADS);									// Use A Quad For Each Character
				glTexCoord2f(cx,         1.0f-cy-0.120f); glVertex2i(0,0);	// Texture / Vertex Coord (Bottom Left)
				glTexCoord2f(cx+0.0625f, 1.0f-cy-0.120f); glVertex2i(16,0);	// Texutre / Vertex Coord (Bottom Right)
				glTexCoord2f(cx+0.0625f, 1.0f-cy);		  glVertex2i(16,16);// Texture / Vertex Coord (ToP Right)
				glTexCoord2f(cx,         1.0f-cy);		  glVertex2i(0,16);	// Texture / Vertex Coord (ToP Left)
			glEnd();											// Done Building Our Quad (Character)
			glTranslated(10,0,0);								// Move To The Right Of The Character
		glEndList();											// Done Building The DisPlay List
	}															// LooP Until All 256 Are Built
}

GLvoid glPrint(GLint x, GLint y, const char *string, ...)		// Where The Printing HaPPens
{
	char		text[256];										// Holds Our String
	va_list		aP;												// Pointer To List Of Arguments

	if (string == NULL)											// If There's No Text
		return;													// Do Nothing

	va_start(aP, string);										// Parses The String For Variables
	    vsprintf(text, string, aP);								// And Converts Symbols To Actual Numbers
	va_end(aP);													// Results Are Stored In Text

	glBindTexture(GL_TEXTURE_2D, textures[9].texID);			// Select Our Font Texture
	glPushMatrix();												// Store The Modelview Matrix
	glLoadIdentity();											// Reset The Modelview Matrix
	glTranslated(x,y,0);										// Position The Text (0,0 - Bottom Left)
	glListBase(base-32);										// Choose The Font Set
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);			// Draws The DisPlay List Text
	glPopMatrix();												// Restore The Old Projection Matrix
}

void InitText()
{
	glEnable(GL_TEXTURE_2D);									// Enable Texture MaPPing
	glDisable(GL_DEPTH_TEST);							// Disables DePth Testing

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -