2012-07-13 12:23:58 +00:00
|
|
|
#include "widgetheightmap.h"
|
|
|
|
|
|
|
|
#include <QTime>
|
2012-07-15 14:42:50 +00:00
|
|
|
#include <QMouseEvent>
|
2012-07-13 12:23:58 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <GL/glu.h>
|
|
|
|
#include "tools.h"
|
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
#define HEIGHTMAP_RESOLUTION 256
|
2012-12-10 22:05:53 +00:00
|
|
|
|
|
|
|
WidgetHeightMap::WidgetHeightMap(QWidget *parent, TerrainDefinition* terrain):
|
2012-07-13 12:23:58 +00:00
|
|
|
QGLWidget(parent)
|
|
|
|
{
|
|
|
|
setMinimumSize(500, 500);
|
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2012-07-15 14:42:50 +00:00
|
|
|
setMouseTracking(true);
|
2012-07-16 20:34:01 +00:00
|
|
|
setCursor(Qt::CrossCursor);
|
|
|
|
startTimer(100);
|
2012-07-13 12:23:58 +00:00
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
_terrain = terrain;
|
2013-01-10 15:41:14 +00:00
|
|
|
_renderer = rendererCreate();
|
2013-01-20 15:07:45 +00:00
|
|
|
TerrainRendererClass.bind(_renderer, _terrain);
|
2012-12-10 22:05:53 +00:00
|
|
|
_vertices = new _VertexInfo[HEIGHTMAP_RESOLUTION * HEIGHTMAP_RESOLUTION];
|
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
_dirty = true;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-13 12:23:58 +00:00
|
|
|
_average_frame_time = 0.0;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-16 20:34:01 +00:00
|
|
|
_last_brush_action = 0;
|
2012-07-13 12:23:58 +00:00
|
|
|
_last_mouse_x = 0;
|
|
|
|
_last_mouse_y = 0;
|
2012-07-24 14:59:06 +00:00
|
|
|
_last_time = QDateTime::currentDateTime();
|
2012-07-23 09:58:06 +00:00
|
|
|
_mouse_moved = false;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
_position_x = 0;
|
|
|
|
_position_z = 0;
|
2012-07-13 21:24:19 +00:00
|
|
|
_angle_h = 0.0;
|
|
|
|
_angle_v = 0.3;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
_brush_x = 0.0;
|
|
|
|
_brush_z = 0.0;
|
2012-07-16 15:48:05 +00:00
|
|
|
_brush_mode = HEIGHTMAP_BRUSH_RAISE;
|
2012-07-15 14:42:50 +00:00
|
|
|
_brush_size = 10.0;
|
|
|
|
_brush_smoothing = 0.5;
|
2012-07-16 20:34:01 +00:00
|
|
|
_brush_strength = 1.0;
|
2012-07-24 14:59:06 +00:00
|
|
|
_brush_noise = noiseCreateGenerator();
|
2013-01-27 19:57:43 +00:00
|
|
|
noiseAddLevelsSimple(_brush_noise, 10, 1.0, -0.5, 0.5);
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WidgetHeightMap::~WidgetHeightMap()
|
|
|
|
{
|
2013-01-20 15:07:45 +00:00
|
|
|
rendererDelete(_renderer);
|
2012-07-24 14:59:06 +00:00
|
|
|
noiseDeleteGenerator(_brush_noise);
|
2012-08-08 13:30:40 +00:00
|
|
|
delete[] _vertices;
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 21:24:19 +00:00
|
|
|
void WidgetHeightMap::setHorizontalViewAngle(double angle_h)
|
|
|
|
{
|
|
|
|
_angle_h = angle_h;
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetHeightMap::setVerticalViewAngle(double angle_v)
|
|
|
|
{
|
|
|
|
_angle_v = angle_v;
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
2012-07-16 15:48:05 +00:00
|
|
|
void WidgetHeightMap::setBrushMode(HeightMapBrushMode mode)
|
|
|
|
{
|
|
|
|
_brush_mode = mode;
|
2012-07-16 20:34:01 +00:00
|
|
|
_brush_x = 0.0;
|
|
|
|
_brush_z = 0.0;
|
|
|
|
updateGL();
|
2012-07-16 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
void WidgetHeightMap::setBrushSize(double size)
|
|
|
|
{
|
|
|
|
_brush_size = size;
|
|
|
|
_brush_x = 0.0;
|
|
|
|
_brush_z = 0.0;
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetHeightMap::setBrushSmoothing(double smoothing)
|
|
|
|
{
|
|
|
|
_brush_smoothing = smoothing;
|
|
|
|
_brush_x = 0.0;
|
|
|
|
_brush_z = 0.0;
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
2012-07-16 20:34:01 +00:00
|
|
|
void WidgetHeightMap::setBrushStrength(double strength)
|
|
|
|
{
|
|
|
|
_brush_strength = strength;
|
|
|
|
_brush_x = 0.0;
|
|
|
|
_brush_z = 0.0;
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetHeightMap::revert()
|
|
|
|
{
|
|
|
|
_dirty = true;
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetHeightMap::mousePressEvent(QMouseEvent* event)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2012-07-16 20:34:01 +00:00
|
|
|
_last_mouse_x = event->x();
|
|
|
|
_last_mouse_y = event->y();
|
|
|
|
if (event->buttons() & Qt::LeftButton)
|
|
|
|
{
|
|
|
|
_last_brush_action = 1;
|
|
|
|
}
|
|
|
|
else if (event->buttons() & Qt::RightButton)
|
|
|
|
{
|
|
|
|
_last_brush_action = -1;
|
|
|
|
}
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 21:21:56 +00:00
|
|
|
void WidgetHeightMap::mouseReleaseEvent(QMouseEvent*)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2012-07-16 20:34:01 +00:00
|
|
|
_last_brush_action = 0;
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetHeightMap::mouseMoveEvent(QMouseEvent* event)
|
|
|
|
{
|
2012-07-16 20:34:01 +00:00
|
|
|
if (event->buttons() & Qt::MiddleButton)
|
|
|
|
{
|
|
|
|
// Rotate around the turntable
|
|
|
|
int move_x = event->x() - _last_mouse_x;
|
|
|
|
int move_y = event->y() - _last_mouse_y;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-16 20:34:01 +00:00
|
|
|
_angle_h -= (double)move_x * 0.008;
|
|
|
|
_angle_v += (double)move_y * 0.003;
|
|
|
|
}
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-16 20:34:01 +00:00
|
|
|
_last_mouse_x = event->x();
|
|
|
|
_last_mouse_y = event->y();
|
2012-07-23 09:58:06 +00:00
|
|
|
_mouse_moved = true;
|
2012-07-16 20:34:01 +00:00
|
|
|
|
|
|
|
updateGL();
|
|
|
|
}
|
|
|
|
|
2012-07-24 14:59:06 +00:00
|
|
|
void WidgetHeightMap::timerEvent(QTimerEvent*)
|
2012-07-16 20:34:01 +00:00
|
|
|
{
|
2012-07-24 14:59:06 +00:00
|
|
|
QDateTime new_time = QDateTime::currentDateTime();
|
|
|
|
double duration = 0.001 * (double)_last_time.msecsTo(new_time);
|
|
|
|
_last_time = new_time;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-24 14:59:06 +00:00
|
|
|
if (_last_brush_action != 0)
|
2012-07-16 15:48:05 +00:00
|
|
|
{
|
2012-07-24 14:59:06 +00:00
|
|
|
double brush_strength;
|
2012-12-10 22:05:53 +00:00
|
|
|
TerrainBrush brush;
|
2012-07-16 15:48:05 +00:00
|
|
|
|
2013-01-14 12:08:38 +00:00
|
|
|
brush.relative_x = _brush_x;
|
|
|
|
brush.relative_z = _brush_z;
|
|
|
|
brush.hard_radius = _brush_size * (1.0 - _brush_smoothing);
|
|
|
|
brush.smoothed_size = _brush_size * _brush_smoothing;
|
2012-07-26 11:55:19 +00:00
|
|
|
brush.total_radius = brush.hard_radius + brush.smoothed_size;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-24 14:59:06 +00:00
|
|
|
brush_strength = _brush_strength * duration / 0.1;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-16 15:48:05 +00:00
|
|
|
switch (_brush_mode)
|
|
|
|
{
|
|
|
|
case HEIGHTMAP_BRUSH_RAISE:
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainBrushElevation(_terrain->height_map, &brush, brush_strength * _last_brush_action * 20.0);
|
2012-07-24 14:59:06 +00:00
|
|
|
break;
|
|
|
|
case HEIGHTMAP_BRUSH_SMOOTH:
|
|
|
|
if (_last_brush_action < 0)
|
|
|
|
{
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainBrushSmooth(_terrain->height_map, &brush, brush_strength * 0.1);
|
2012-07-24 14:59:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainBrushAddNoise(_terrain->height_map, &brush, _brush_noise, brush_strength * 10.0);
|
2012-07-24 14:59:06 +00:00
|
|
|
}
|
2012-07-16 15:48:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-07-16 20:34:01 +00:00
|
|
|
return;
|
2012-07-16 15:48:05 +00:00
|
|
|
}
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-24 14:59:06 +00:00
|
|
|
// TODO Only mark dirty the updated area
|
2012-07-16 20:34:01 +00:00
|
|
|
_dirty = true;
|
2012-07-15 14:42:50 +00:00
|
|
|
updateGL();
|
|
|
|
}
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetHeightMap::initializeGL()
|
|
|
|
{
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
|
|
2012-08-02 19:26:22 +00:00
|
|
|
GLfloat light_diffuse[] = { 0.75, 0.74, 0.7, 1.0 };
|
2012-07-15 14:42:50 +00:00
|
|
|
GLfloat light_specular[] = { 0.0, 0.0, 0.0, 0.0 };
|
|
|
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
|
|
|
|
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
|
2012-08-02 19:26:22 +00:00
|
|
|
light_diffuse[0] = 0.3;
|
|
|
|
light_diffuse[1] = 0.3;
|
|
|
|
light_diffuse[2] = 0.4;
|
|
|
|
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
|
|
|
|
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
|
|
|
|
glShadeModel(GL_SMOOTH);
|
2012-07-15 14:42:50 +00:00
|
|
|
glEnable(GL_LIGHTING);
|
|
|
|
glEnable(GL_LIGHT0);
|
2012-08-02 19:26:22 +00:00
|
|
|
glEnable(GL_LIGHT1);
|
2012-07-15 14:42:50 +00:00
|
|
|
glEnable(GL_COLOR_MATERIAL);
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
//glFrontFace(GL_CCW);
|
|
|
|
//glCullFace(GL_BACK);
|
|
|
|
glDisable(GL_CULL_FACE);
|
2012-07-13 12:23:58 +00:00
|
|
|
|
|
|
|
glDepthFunc(GL_LESS);
|
|
|
|
glDepthMask(true);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
2012-07-15 14:42:50 +00:00
|
|
|
//glEnable(GL_LINE_SMOOTH);
|
|
|
|
//glLineWidth(1.0);
|
2012-07-13 12:23:58 +00:00
|
|
|
|
|
|
|
glDisable(GL_FOG);
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetHeightMap::resizeGL(int w, int h)
|
|
|
|
{
|
|
|
|
glViewport(0, 0, w, h);
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2012-07-13 21:24:19 +00:00
|
|
|
gluPerspective(1.57 * 180.0 / M_PI, 1.0, 1.0, 1000.0);
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-13 12:23:58 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetHeightMap::paintGL()
|
|
|
|
{
|
|
|
|
GLenum error_code;
|
|
|
|
QTime start_time;
|
|
|
|
double frame_time;
|
2012-07-13 21:24:19 +00:00
|
|
|
int rx, rz;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
// Update vertex cache
|
|
|
|
if (_dirty)
|
|
|
|
{
|
|
|
|
updateVertexInfo();
|
|
|
|
_dirty = false;
|
|
|
|
}
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-16 20:34:01 +00:00
|
|
|
// Picking mouse position using z-buffer (for brush)
|
2012-07-23 09:58:06 +00:00
|
|
|
if (_mouse_moved)
|
|
|
|
{
|
|
|
|
GLint viewport[4];
|
|
|
|
GLdouble modelview[16];
|
|
|
|
GLdouble projection[16];
|
|
|
|
GLfloat winX, winY, winZ;
|
|
|
|
Vector3 point;
|
2012-07-16 20:34:01 +00:00
|
|
|
|
2012-07-23 09:58:06 +00:00
|
|
|
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
|
|
|
|
glGetDoublev(GL_PROJECTION_MATRIX, projection);
|
|
|
|
glGetIntegerv(GL_VIEWPORT, viewport);
|
2012-07-16 20:34:01 +00:00
|
|
|
|
2012-07-23 09:58:06 +00:00
|
|
|
winX = (float)_last_mouse_x;
|
|
|
|
winY = (float)height() - (float)_last_mouse_y;
|
|
|
|
glReadPixels(_last_mouse_x, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
|
2012-07-16 20:34:01 +00:00
|
|
|
|
2012-07-23 09:58:06 +00:00
|
|
|
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &point.x, &point.y, &point.z);
|
2012-07-16 20:34:01 +00:00
|
|
|
|
2012-07-23 09:58:06 +00:00
|
|
|
_brush_x = point.x;
|
|
|
|
_brush_z = point.z;
|
|
|
|
_mouse_moved = false;
|
|
|
|
}
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-13 12:23:58 +00:00
|
|
|
// Place camera
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
2012-07-13 21:24:19 +00:00
|
|
|
gluLookAt(50.0 * cos(_angle_h) * cos(_angle_v), 50.0 * sin(_angle_v), -50.0 * sin(_angle_h) * cos(_angle_v), 0.0, 0.0, 0.0, -cos(_angle_h) * sin(_angle_v), cos(_angle_v), sin(_angle_h) * sin(_angle_v));
|
2012-07-13 12:23:58 +00:00
|
|
|
|
2012-08-02 19:26:22 +00:00
|
|
|
// Place lights
|
|
|
|
GLfloat light_position[] = { 40.0, 40.0, 40.0, 0.0 };
|
|
|
|
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
|
|
|
|
light_position[0] = -40.0;
|
|
|
|
light_position[2] = -60.0;
|
|
|
|
glLightfv(GL_LIGHT1, GL_POSITION, light_position);
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-13 12:23:58 +00:00
|
|
|
// Background
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
// Height map
|
2012-12-10 22:05:53 +00:00
|
|
|
rx = HEIGHTMAP_RESOLUTION;
|
|
|
|
rz = HEIGHTMAP_RESOLUTION;
|
2012-07-15 14:42:50 +00:00
|
|
|
for (int x = 0; x < rx - 1; x++)
|
2012-07-13 21:24:19 +00:00
|
|
|
{
|
2012-07-15 14:42:50 +00:00
|
|
|
glBegin(GL_QUAD_STRIP);
|
2012-07-13 21:24:19 +00:00
|
|
|
for (int z = 0; z < rz; z++)
|
|
|
|
{
|
2012-08-08 13:30:40 +00:00
|
|
|
_VertexInfo* vertex = _vertices + z * rx + x;
|
2012-07-15 14:42:50 +00:00
|
|
|
double diff_x, diff_z, diff;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
diff_x = (vertex + 1)->point.x - _brush_x;
|
|
|
|
diff_z = (vertex + 1)->point.z - _brush_z;
|
|
|
|
diff = sqrt(diff_x * diff_x + diff_z * diff_z);
|
|
|
|
if (diff > _brush_size)
|
|
|
|
{
|
|
|
|
diff = 0.0;
|
|
|
|
}
|
|
|
|
else if (diff > _brush_size * (1.0 - _brush_smoothing))
|
|
|
|
{
|
|
|
|
diff = 1.0 - (diff - _brush_size * (1.0 - _brush_smoothing)) / (_brush_size * _brush_smoothing);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff = 1.0;
|
|
|
|
}
|
|
|
|
glColor3d(1.0, 1.0 - diff, 1.0 - diff);
|
|
|
|
glNormal3d((vertex + 1)->normal.x, (vertex + 1)->normal.y, (vertex + 1)->normal.z);
|
|
|
|
glVertex3d((vertex + 1)->point.x, (vertex + 1)->point.y, (vertex + 1)->point.z);
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
diff_x = vertex->point.x - _brush_x;
|
|
|
|
diff_z = vertex->point.z - _brush_z;
|
|
|
|
diff = sqrt(diff_x * diff_x + diff_z * diff_z);
|
|
|
|
if (diff > _brush_size)
|
|
|
|
{
|
|
|
|
diff = 0.0;
|
|
|
|
}
|
|
|
|
else if (diff > _brush_size * (1.0 - _brush_smoothing))
|
|
|
|
{
|
|
|
|
diff = 1.0 - (diff - _brush_size * (1.0 - _brush_smoothing)) / (_brush_size * _brush_smoothing);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff = 1.0;
|
|
|
|
}
|
|
|
|
glColor3d(1.0, 1.0 - diff, 1.0 - diff);
|
|
|
|
glNormal3d(vertex->normal.x, vertex->normal.y, vertex->normal.z);
|
|
|
|
glVertex3d(vertex->point.x, vertex->point.y, vertex->point.z);
|
2012-07-13 21:24:19 +00:00
|
|
|
}
|
2012-07-15 14:42:50 +00:00
|
|
|
glEnd();
|
2012-07-13 21:24:19 +00:00
|
|
|
}
|
2012-07-13 12:23:58 +00:00
|
|
|
|
|
|
|
// Time stats
|
|
|
|
frame_time = 0.001 * (double)start_time.msecsTo(QTime::currentTime());
|
|
|
|
_average_frame_time = _average_frame_time * 0.8 + frame_time * 0.2;
|
|
|
|
//printf("%d %f\n", quality, average_frame_time);
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-13 12:23:58 +00:00
|
|
|
while ((error_code = glGetError()) != GL_NO_ERROR)
|
|
|
|
{
|
|
|
|
logDebug(QString("[OpenGL] ERROR : ") + (const char*)gluErrorString(error_code));
|
|
|
|
}
|
|
|
|
}
|
2012-07-15 14:42:50 +00:00
|
|
|
|
|
|
|
void WidgetHeightMap::updateVertexInfo()
|
|
|
|
{
|
2012-12-10 22:05:53 +00:00
|
|
|
int rx = HEIGHTMAP_RESOLUTION;
|
|
|
|
int rz = HEIGHTMAP_RESOLUTION;
|
2013-01-14 13:28:42 +00:00
|
|
|
int dx, dz;
|
2012-08-08 13:30:40 +00:00
|
|
|
|
|
|
|
_VertexInfo* old_vertices = _vertices;
|
|
|
|
_vertices = new _VertexInfo[rx * rz];
|
|
|
|
delete[] old_vertices;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
// Update positions
|
|
|
|
for (int x = 0; x < rx; x++)
|
|
|
|
{
|
|
|
|
for (int z = 0; z < rz; z++)
|
|
|
|
{
|
2012-08-08 13:30:40 +00:00
|
|
|
_VertexInfo* vertex = _vertices + z * rx + x;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
dx = _position_x + x - rx / 2;
|
|
|
|
dz = _position_z + z - rz / 2;
|
2013-01-10 21:21:56 +00:00
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
vertex->point.x = (double)dx;
|
|
|
|
vertex->point.z = (double)dz;
|
|
|
|
|
|
|
|
vertex->point.y = terrainGetGridHeight(_terrain, dx, dz, 1);
|
2012-07-15 14:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
// Update normals
|
|
|
|
for (int x = 0; x < rx; x++)
|
|
|
|
{
|
|
|
|
for (int z = 0; z < rz; z++)
|
|
|
|
{
|
2012-08-08 13:30:40 +00:00
|
|
|
_VertexInfo* vertex = _vertices + z * rx + x;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
if (x == rx - 1)
|
|
|
|
{
|
|
|
|
vertex->normal = (vertex - 1)->normal;
|
|
|
|
}
|
|
|
|
else if (z == rz - 1)
|
|
|
|
{
|
|
|
|
vertex->normal = (vertex - rx)->normal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Vector3 dx, dz;
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
dx = v3Sub((vertex + 1)->point, vertex->point);
|
|
|
|
dz = v3Sub((vertex + rx)->point, vertex->point);
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2012-07-15 14:42:50 +00:00
|
|
|
vertex->normal = v3Cross(v3Normalize(dz), v3Normalize(dx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|