/*
Copyright (C) 2003, 2010 - Wolfire Games
+Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
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.
+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,
+Lugaru 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.
+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.
+along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Game.h"
#include "Terrain.h"
#include "Objects.h"
+#include "Utils/Folders.h"
+
extern XYZ viewer;
extern float viewdistance;
extern float fadestart;
extern float blurness;
extern float targetblurness;
extern Objects objects;
-extern TGAImageRec texture;
extern bool visibleloading;
extern bool skyboxtexture;
extern int tutoriallevel;
static int firstintersecting;
static XYZ point;
static int startx, starty;
- static float slope;
static int endx, endy;
- static int numtris = (size - 1) * (size - 1) * 2;
static float highest, lowest;
firstintersecting = -1;
void Terrain::UpdateTransparencyother(int whichx, int whichy)
{
- static XYZ vertex;
static int i, j, a, b, c, d, patch_size, stepsize;
- static float distance;
patch_size = size / subdivision;
static float patch_size;
float temptexdetail = texdetail;
- //LoadTGA( fileName );
- // Fixing filename so that it works with its own os
- char * FixedFN = ConvertFileName(fileName);
+ ImageRec texture;
- unsigned char fileNamep[256];
- CopyCStringToPascal(FixedFN, fileNamep);
//Load Image
- upload_image( fileNamep , 0);
+ if (!load_image(Folders::getResourcePath(fileName).c_str(), texture)) {
+ return false;
+ }
//Is it valid?
if (texture.bpp > 24) {
texdetail = temptexdetail;
- size = 128;
- if (1 == 1) {
- /*if ( texture.bpp == 24 )
- type = GL_RGB;
- else
- type = GL_RGBA;
-
- glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
-
- if(!terraintexture)glGenTextures( 1, &terraintexture );
- glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
-
- glBindTexture( GL_TEXTURE_2D, terraintexture);
- //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
-
- gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
- */
+ size = texture.sizeX;
- size = texture.sizeX;
-
- 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;
- }
+ 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;
}
-
}
+
if (visibleloading)
Game::LoadingScreen();
if (visibleloading)
Game::LoadingScreen();
- /*float total;
- int todivide;
- //Smooth opacityother
- for(i=0;i<size;i++){
- for(j=0;j<size;j++){
- total=0;
- todivide=0;
- if(i!=0){total+=opacityother[j][i-1]; todivide++;}
- if(i!=size-1){total+=opacityother[j][i+1]; todivide++;}
- if(j!=0){total+=opacityother[j-1][i]; todivide++;}
- if(j!=size-1){total+=opacityother[j+1][i]; todivide++;}
- if(i!=0&&j!=0){total+=opacityother[j-1][i-1]; todivide++;}
- if(i!=size-1&&j!=0){total+=opacityother[j-1][i+1]; todivide++;}
- if(j!=size-1&&i!=size-1){total+=opacityother[j+1][i+1]; todivide++;}
- if(j!=size-1&&i!=0){total+=opacityother[j+1][i-1]; todivide++;}
- total+=opacityother[j][i]; todivide++;
-
- opacityother[j][i]=total/(float)todivide;
- }
- }*/
-
-
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
if (opacityother[i][j] < .1)
patch_size = size / subdivision;
patch_elements = (patch_size) * (patch_size) * 54;
CalculateNormals();
- /*DoShadows();
- for(i=0;i<subdivision;i++){
- for(j=0;j<subdivision;j++){
- UpdateVertexArray(i,j);
- }
- }*/
-
- return 1;
+ return true;
}
void Terrain::CalculateNormals()
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
Normalise(&normals[i][j]);
- normals[i][j] = normals[i][j];
}
}
}
float Terrain::getHeight(float pointx, float pointz)
{
- static float height1, height2;
static int tilex, tiley;
- static XYZ startpoint, endpoint, intersect, triangle[3], average;
+ static XYZ startpoint, endpoint, intersect, triangle[3];
pointx /= scale;
pointz /= scale;
LineFacetd(&startpoint, &endpoint, &triangle[0], &triangle[1], &triangle[2], &intersect);
}
return intersect.y * scale + getOpacity(pointx * scale, pointz * scale) / 8;
-
- //height1=heightmap[tilex][tiley]*(1-(pointx-tilex))+heightmap[tilex+1][tiley]*(pointx-tilex);
- //height2=heightmap[tilex][tiley+1]*(1-(pointx-tilex))+heightmap[tilex+1][tiley+1]*(pointx-tilex);
-
- //return height1*(1-(pointz-tiley))*scale+height2*(pointz-tiley)*scale;
-}
-
-float Terrain::getHeightExtrude(float pointx, float pointz, float point2x, float point2z)
-{
- static float height1, height2;
- static int tilex, tiley;
- static XYZ startpoint, endpoint, intersect, triangle[3], average;
-
- pointx /= scale;
- pointz /= scale;
- point2x /= scale;
- point2z /= scale;
-
- if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
- return 0;
- if (point2x >= size - 1 || point2z >= size - 1 || point2x <= 0 || point2z <= 0)
- return 0;
-
- startpoint.x = point2x;
- startpoint.y = -1000;
- startpoint.z = point2z;
-
- endpoint = startpoint;
- endpoint.y = 1000;
-
- tilex = pointx;
- tiley = pointz;
-
- triangle[0].x = tilex;
- triangle[0].z = tiley;
- triangle[0].y = heightmap[tilex][tiley];
-
- triangle[1].x = tilex + 1;
- triangle[1].z = tiley;
- triangle[1].y = heightmap[tilex + 1][tiley];
-
- triangle[2].x = tilex;
- triangle[2].z = tiley + 1;
- triangle[2].y = heightmap[tilex][tiley + 1];
-
- XYZ mid;
-
- mid = (triangle[0] + triangle[1] + triangle[2]) / 2;
-
- triangle[0] = mid + (triangle[0] - mid) * 10;
- triangle[1] = mid + (triangle[0] - mid) * 10;
- triangle[2] = mid + (triangle[0] - mid) * 10;
-
- /*
- if(!LineFacetd(&startpoint,&endpoint,&triangle[0],&triangle[1],&triangle[2],&intersect)){
- triangle[0].x=tilex+1;
- triangle[0].z=tiley;
- triangle[0].y=heightmap[tilex+1][tiley];
-
- triangle[1].x=tilex+1;
- triangle[1].z=tiley+1;
- triangle[1].y=heightmap[tilex+1][tiley+1];
-
- triangle[2].x=tilex;
- triangle[2].z=tiley+1;
- triangle[2].y=heightmap[tilex][tiley+1];
- LineFacetd(&startpoint,&endpoint,&triangle[0],&triangle[1],&triangle[2],&intersect);
- }*/
- return intersect.y * scale + getOpacity(pointx * scale, pointz * scale) / 8;
-
- //height1=heightmap[tilex][tiley]*(1-(pointx-tilex))+heightmap[tilex+1][tiley]*(pointx-tilex);
- //height2=heightmap[tilex][tiley+1]*(1-(pointx-tilex))+heightmap[tilex+1][tiley+1]*(pointx-tilex);
-
- //return height1*(1-(pointz-tiley))*scale+height2*(pointz-tiley)*scale;
}
-
float Terrain::getOpacity(float pointx, float pointz)
{
static float height1, height2;
void Terrain::drawdecals()
{
if (decals) {
- static int i, j;
+ static int i;
static float distancemult;
static int lasttype;
- static float patch_size = size / subdivision * scale;
static float viewdistsquared;
static bool blend;
patchy[2] = (where.z + size) / scale;
patchy[3] = (where.z + size) / scale;
- /*if(patchx[1]<subdivision-1&&patchy[1]<subdivision-1&&patchx[1]>0&&patchy[1]>0)
- if(patchx[2]<subdivision-1&&patchy[2]<subdivision-1&&patchx[2]>0&&patchy[2]>0)
- if(patchx[3]<subdivision-1&&patchy[3]<subdivision-1&&patchx[3]>0&&patchy[3]>0)
- if(patchx[0]<subdivision-1&&patchy[0]<subdivision-1&&patchx[0]>0&&patchy[0]>0){
- */
if ((patchx[0] != patchx[1] || patchy[0] != patchy[1]) && (patchx[0] != patchx[2] || patchy[0] != patchy[2]) && (patchx[0] != patchx[3] || patchy[0] != patchy[3])) {
MakeDecalLock(type, where, patchx[0], patchy[0], size, opacity, rotation);
}
}
}
-void Terrain::DoLighting()
-{
- static int i, j, k, todivide;
- static float brightness, total;
- static XYZ blank, terrainpoint, lightloc;
- lightloc = light.location;
- Normalise(&lightloc);
- //Calculate shadows
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- terrainpoint.x = (float)i * scale;
- terrainpoint.z = (float)j * scale;
- terrainpoint.y = heightmap[i][j] * scale + .1;
- /*brightness=0;
- if(lineTerrain(lightlocation*10+terrainpoint,terrainpoint,&blank)==-1)
- */
- brightness = dotproduct(&lightloc, &normals[i][j]);
-
- if (brightness > 1)
- brightness = 1;
- if (brightness < 0)
- brightness = 0;
-
- colors[i][j][0] = light.color[0] * brightness + light.ambient[0];
- colors[i][j][1] = light.color[1] * brightness + light.ambient[1];
- colors[i][j][2] = light.color[2] * brightness + light.ambient[2];
-
- if (colors[i][j][0] > 1) colors[i][j][0] = 1;
- if (colors[i][j][1] > 1) colors[i][j][1] = 1;
- if (colors[i][j][2] > 1) colors[i][j][2] = 1;
- if (colors[i][j][0] < 0) colors[i][j][0] = 0;
- if (colors[i][j][1] < 0) colors[i][j][1] = 0;
- if (colors[i][j][2] < 0) colors[i][j][2] = 0;
- }
- }
-
- //Smooth shadows
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- for (k = 0; k < 3; k++) {
- total = 0;
- todivide = 0;
- if (i != 0) {
- total += colors[j][i - 1][k];
- todivide++;
- }
- if (i != size - 1) {
- total += colors[j][i + 1][k];
- todivide++;
- }
- if (j != 0) {
- total += colors[j - 1][i][k];
- todivide++;
- }
- if (j != size - 1) {
- total += colors[j + 1][i][k];
- todivide++;
- }
- if (i != 0 && j != 0) {
- total += colors[j - 1][i - 1][k];
- todivide++;
- }
- if (i != size - 1 && j != 0) {
- total += colors[j - 1][i + 1][k];
- todivide++;
- }
- if (j != size - 1 && i != size - 1) {
- total += colors[j + 1][i + 1][k];
- todivide++;
- }
- if (j != size - 1 && i != 0) {
- total += colors[j + 1][i - 1][k];
- todivide++;
- }
- total += colors[j][i][k];
- todivide++;
-
- colors[j][i][k] = total / todivide;
- }
- }
- }
-}
-
void Terrain::DoShadows()
{
static int i, j, k, l, todivide;