+/*
+Copyright (C) 2003, 2010 - Wolfire Games
+
+This file is part of Lugaru.
+
+Lugaru is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+*/
+
+#include "Game.h"
#include "Terrain.h"
#include "Objects.h"
-#include "Game.h"
extern XYZ viewer;
extern float viewdistance;
-extern float lightambient[3],lightbrightness[3];
extern float fadestart;
extern int environment;
extern float texscale;
extern Light light;
extern float multiplier;
extern FRUSTUM frustum;
-extern float texdetail,terraindetail;
+extern float texdetail;
extern int detail;
extern bool decals;
extern float blurness;
extern float targetblurness;
extern Objects objects;
extern TGAImageRec texture;
-extern Game * pgame;
extern bool visibleloading;
extern bool skyboxtexture;
extern int tutoriallevel;
triangles[2].z=j;
intersecting=LineFacet(p1,p2,triangles[0],triangles[1],triangles[2],&point);
- distance=findDistancefast(&p1,&point);
+ distance=distsq(&p1,&point);
if((distance<olddistance||firstintersecting==-1)&&intersecting==1){olddistance=distance; firstintersecting=1; *p=point;}
triangles[0].x=i+1;
triangles[2].z=j+1;
intersecting=LineFacet(p1,p2,triangles[0],triangles[1],triangles[2],&point);
- distance=findDistancefast(&p1,&point);
+ distance=distsq(&p1,&point);
if((distance<olddistance||firstintersecting==-1)&&intersecting==1){olddistance=distance; firstintersecting=1; *p=point;}
}
}
vertex.x=i*scale;
vertex.z=j*scale;
vertex.y=heightmap[i][j]*scale;
- distance=findDistancefast(&viewer,&vertex);
+ distance=distsq(&viewer,&vertex);
if(distance>viewdistsquared)distance=viewdistsquared;
colors[i][j][3]=(viewdistsquared-(distance-(viewdistsquared*fadestart))*(1/(1-fadestart)))/viewdistsquared;
}
vertex.x=i*scale;
vertex.z=j*scale;
vertex.y=heightmap[i][j]*scale;
- distance=findDistancefast(&viewer,&vertex);
+ distance=distsq(&viewer,&vertex);
if(distance>viewdistsquared)distance=viewdistsquared;
colors[i][j][3]=(viewdistsquared-(distance-(viewdistsquared*fadestart))*(1/(1-fadestart)))/viewdistsquared;
}
}
-bool Terrain::load(char *fileName)
+bool Terrain::load(const char *fileName)
{
static long i,j;
static long x,y;
static float patch_size;
float temptexdetail=texdetail;
- texdetail=terraindetail;
//LoadTGA( fileName );
+
+ // Fixing filename so that it works with its own os
+ char * FixedFN = ConvertFileName(fileName);
+
unsigned char fileNamep[256];
- CopyCStringToPascal(fileName,fileNamep);
+ CopyCStringToPascal(FixedFN, fileNamep);
//Load Image
upload_image( fileNamep ,0);
}
}
texture.bpp=24;
- if(visibleloading)pgame->LoadingScreen();
+ if(visibleloading) Game::LoadingScreen();
texdetail=temptexdetail;
for(i=0;i<size;i++){
for(j=0;j<size;j++){
- heightmap[size-1-i][j]=(float)((texture.data[(i+(j*size))*texture.bpp/8]))/5/terraindetail;
+ heightmap[size-1-i][j]=(float)((texture.data[(i+(j*size))*texture.bpp/8]))/5;
}
}
}
- if(visibleloading)pgame->LoadingScreen();
+ if(visibleloading) Game::LoadingScreen();
float slopeness;
textureness[i][j]=-1;
}
}
- if(visibleloading)pgame->LoadingScreen();
+ if(visibleloading) Game::LoadingScreen();
for(i=0;i<size;i++){
}
}
}
- if(visibleloading)pgame->LoadingScreen();
+ if(visibleloading) Game::LoadingScreen();
for(i=0;i<size;i++){
for(j=0;j<size;j++){
}
}
}
- if(visibleloading)pgame->LoadingScreen();
+ if(visibleloading) Game::LoadingScreen();
/*float total;
int todivide;
}
}
}
- if(visibleloading)pgame->LoadingScreen();
+ if(visibleloading) Game::LoadingScreen();
patch_size=size/subdivision;
patch_elements=(patch_size)*(patch_size)*54;
endz=(viewer.z+viewdistance)/(patch_size)+1;
if(endz>subdivision)endz=subdivision;
- if(!layer)
+ if(!layer) {
for(i=beginx;i<endx;i++){
for(j=beginz;j<endz;j++){
terrainpoint.x=i*patch_size+(patch_size)/2;
terrainpoint.y=viewer.y;//heightmap[i][j]*scale;
terrainpoint.z=j*patch_size+(patch_size)/2;
- distance[i][j]=findDistancefast(&viewer,&terrainpoint);
+ distance[i][j]=distsq(&viewer,&terrainpoint);
}
}
-
- for(i=beginx;i<endx;i++){
- for(j=beginz;j<endz;j++){
- if(distance[i][j]<(viewdistance+patch_size)*(viewdistance+patch_size)){
- opacity=1;
- if(distance[i][j]>viewdistsquared*fadestart-viewdistsquared)opacity=0;
- if(opacity==1&&i!=subdivision)if(distance[i+1][j]>viewdistsquared*fadestart-viewdistsquared)opacity=0;
- if(opacity==1&&j!=subdivision)if(distance[i][j+1]>viewdistsquared*fadestart-viewdistsquared)opacity=0;
- if(opacity==1&&j!=subdivision&&i!=subdivision)if(distance[i+1][j+1]>viewdistsquared*fadestart-viewdistsquared)opacity=0;
- glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
- glPushMatrix();
- if(frustum.CubeInFrustum(i*patch_size+patch_size*.5,avgypatch[i][j],j*patch_size+patch_size*.5,heightypatch[i][j]/2))
- {
- if(environment==desertenvironment&&distance[i][j]>viewdistsquared/4)glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness);
- else if(environment==desertenvironment)glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
- if(!layer&&textureness[i][j]!=allsecond)drawpatch(i,j,opacity);
- if(layer==1&&textureness[i][j]!=allfirst)drawpatchother(i,j,opacity);
- if(layer==2&&textureness[i][j]!=allfirst)drawpatchotherother(i,j,opacity);
- }
- glPopMatrix();
- }
+ }
+ for(i=beginx;i<endx;i++){
+ for(j=beginz;j<endz;j++){
+ if(distance[i][j]<(viewdistance+patch_size)*(viewdistance+patch_size)){
+ opacity=1;
+ if(distance[i][j]>viewdistsquared*fadestart-viewdistsquared)
+ opacity=0;
+ if(opacity==1&&i!=subdivision)
+ if(distance[i+1][j]>viewdistsquared*fadestart-viewdistsquared)
+ opacity=0;
+ if(opacity==1&&j!=subdivision)
+ if(distance[i][j+1]>viewdistsquared*fadestart-viewdistsquared)
+ opacity=0;
+ if(opacity==1&&j!=subdivision&&i!=subdivision)
+ if(distance[i+1][j+1]>viewdistsquared*fadestart-viewdistsquared)
+ opacity=0;
+ glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
+ glPushMatrix();
+ if(frustum.CubeInFrustum(i*patch_size+patch_size*.5,avgypatch[i][j],j*patch_size+patch_size*.5,heightypatch[i][j]/2))
+ {
+ if(environment==desertenvironment&&distance[i][j]>viewdistsquared/4)
+ glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness);
+ else if(environment==desertenvironment)
+ glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
+ if(!layer&&textureness[i][j]!=allsecond)
+ drawpatch(i,j,opacity);
+ if(layer==1&&textureness[i][j]!=allfirst)
+ drawpatchother(i,j,opacity);
+ if(layer==2&&textureness[i][j]!=allfirst)
+ drawpatchotherother(i,j,opacity);
+ }
+ glPopMatrix();
}
}
- if(environment==desertenvironment)glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
+ }
+ if(environment==desertenvironment)
+ glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
}
void Terrain::drawdecals()
{
- if(decals){
+ if(decals) {
static int i,j;
static float distancemult;
static int lasttype;
for(i=0;i<numdecals;i++){
if(decaltype[i]==blooddecalfast&&decalalivetime[i]<2)decalalivetime[i]=2;
if((decaltype[i]==shadowdecal||decaltype[i]==shadowdecalpermanent)&&decaltype[i]!=lasttype){
- glBindTexture( GL_TEXTURE_2D, shadowtexture);
+ shadowtexture.bind();
if(!blend){
blend=1;
glAlphaFunc(GL_GREATER, 0.0001);
}
}
if(decaltype[i]==footprintdecal&&decaltype[i]!=lasttype){
- glBindTexture( GL_TEXTURE_2D, footprinttexture);
+ footprinttexture.bind();
if(!blend){
blend=1;
glAlphaFunc(GL_GREATER, 0.0001);
}
}
if(decaltype[i]==bodyprintdecal&&decaltype[i]!=lasttype){
- glBindTexture( GL_TEXTURE_2D, bodyprinttexture);
+ bodyprinttexture.bind();
if(!blend){
blend=1;
glAlphaFunc(GL_GREATER, 0.0001);
}
}
if((decaltype[i]==blooddecal||decaltype[i]==blooddecalslow)&&decaltype[i]!=lasttype){
- glBindTexture( GL_TEXTURE_2D, bloodtexture);
+ bloodtexture.bind();
if(blend){
blend=0;
glAlphaFunc(GL_GREATER, 0.15);
}
}
if((decaltype[i]==blooddecalfast)&&decaltype[i]!=lasttype){
- glBindTexture( GL_TEXTURE_2D, bloodtexture2);
+ bloodtexture2.bind();
if(blend){
blend=0;
glAlphaFunc(GL_GREATER, 0.15);
}
}
if(decaltype[i]==shadowdecal||decaltype[i]==shadowdecalpermanent){
- distancemult=(viewdistsquared-(findDistancefast(&viewer,&decalposition[i])-(viewdistsquared*fadestart))*(1/(1-fadestart)))/viewdistsquared;
+ distancemult=(viewdistsquared-(distsq(&viewer,&decalposition[i])-(viewdistsquared*fadestart))*(1/(1-fadestart)))/viewdistsquared;
if(distancemult>=1)glColor4f(1,1,1,decalopacity[i]);
if(distancemult<1)glColor4f(1,1,1,decalopacity[i]*distancemult);
}
if(decaltype[i]==footprintdecal||decaltype[i]==bodyprintdecal){
- distancemult=(viewdistsquared-(findDistancefast(&viewer,&decalposition[i])-(viewdistsquared*fadestart))*(1/(1-fadestart)))/viewdistsquared;
+ distancemult=(viewdistsquared-(distsq(&viewer,&decalposition[i])-(viewdistsquared*fadestart))*(1/(1-fadestart)))/viewdistsquared;
if(distancemult>=1){
glColor4f(1,1,1,decalopacity[i]);
if(decalalivetime[i]>3)glColor4f(1,1,1,decalopacity[i]*(5-decalalivetime[i])/2);
}
}
if((decaltype[i]==blooddecal||decaltype[i]==blooddecalfast||decaltype[i]==blooddecalslow)){
- distancemult=(viewdistsquared-(findDistancefast(&viewer,&decalposition[i])-(viewdistsquared*fadestart))*(1/(1-fadestart)))/viewdistsquared;
+ distancemult=(viewdistsquared-(distsq(&viewer,&decalposition[i])-(viewdistsquared*fadestart))*(1/(1-fadestart)))/viewdistsquared;
if(distancemult>=1){
glColor4f(decalbrightness[i],decalbrightness[i],decalbrightness[i],decalopacity[i]);
if(decalalivetime[i]<4)glColor4f(decalbrightness[i],decalbrightness[i],decalbrightness[i],decalopacity[i]*decalalivetime[i]*.25);
points[3].x=(size/subdivision)*i;
points[3].z=(size/subdivision)*(j+1);
points[3].y=heightmap[(int)points[3].x][(int)points[3].z];
- points[0]*=scale*terraindetail;
- points[1]*=scale*terraindetail;
- points[2]*=scale*terraindetail;
- points[3]*=scale*terraindetail;
+ points[0]*=scale;
+ points[1]*=scale;
+ points[2]*=scale;
+ points[3]*=scale;
if(!done&&where.x+radius>points[0].x&&where.x-radius<points[2].x&&where.z+radius>points[0].z&&where.z-radius<points[2].z){
patchobjects[i][j][patchobjectnum[i][j]]=id;
patchobjectnum[i][j]++;
}
void Terrain::MakeDecal(int type, XYZ where, float size, float opacity, float rotation){
- if(decals){
- if(opacity>0&&size>0){
+ if(decals) {
+ if(opacity>0&&size>0) {
static int patchx[4];
static int patchy[4];
decalbrightness[numdecals]=(rot.x+rot.y+rot.z)/3;
if(decalbrightness[numdecals]<.4)decalbrightness[numdecals]=.4;
- //if(type==blooddecal||type==blooddecalfast||type==blooddecalslow){
if(environment==grassyenvironment){
decalbrightness[numdecals]*=.6;
}
- //}
if(decalbrightness[numdecals]>1)decalbrightness[numdecals]=1;
decalbright=decalbrightness[numdecals];
if(objects.type[l]!=treetrunktype){
testpoint=terrainpoint;
testpoint2=terrainpoint+lightloc*50*(1-shadowed);
- if(objects.model[l].LineCheck(&testpoint,&testpoint2,&col,&objects.position[l],&objects.rotation[l])!=-1){
+ if(objects.model[l].LineCheck(&testpoint,&testpoint2,&col,&objects.position[l],&objects.yaw[l])!=-1){
shadowed=1-(findDistance(&terrainpoint,&col)/50);
}
}
}
- if(visibleloading)pgame->LoadingScreen();
+ if(visibleloading) Game::LoadingScreen();
}
brightness=dotproduct(&lightloc,&normals[i][j]);
if(shadowed)brightness*=1-shadowed;
}
}
- if(visibleloading)pgame->LoadingScreen();
+ if(visibleloading) Game::LoadingScreen();
//Smooth shadows
for(i=0;i<size;i++){
Terrain::Terrain()
{
- bloodtexture = 0;
- bloodtexture2 = 0;
- shadowtexture = 0;
- footprinttexture = 0;
- bodyprinttexture = 0;
- breaktexture = 0;
- terraintexture = 0;
size = 0;
memset(patchobjectnum, 0, sizeof(patchobjectnum));
memset(patchobjects, 0, sizeof(patchobjects));
- scale = 0;
+ scale = 1.0f;
type = 0;
memset(heightmap, 0, sizeof(heightmap));
memset(normals, 0, sizeof(normals));
}
Terrain::~Terrain()
{
- if(terraintexture)glDeleteTextures( 1, (const unsigned long *)&terraintexture );
- if(shadowtexture) glDeleteTextures( 1, (const unsigned long *)&shadowtexture );
- if(bodyprinttexture) glDeleteTextures( 1, (const unsigned long *)&bodyprinttexture );
- if(footprinttexture) glDeleteTextures( 1, (const unsigned long *)&footprinttexture );
- if(bloodtexture) glDeleteTextures( 1, (const unsigned long *)&bloodtexture );
- if(bloodtexture2) glDeleteTextures( 1, (const unsigned long *)&bloodtexture2 );
- if(breaktexture) glDeleteTextures( 1, (const unsigned long *)&breaktexture );
+ terraintexture.destroy();
+ shadowtexture.destroy();
+ bodyprinttexture.destroy();
+ footprinttexture.destroy();
+ bloodtexture.destroy();
+ bloodtexture2.destroy();
+ breaktexture.destroy();
}
+