paysages: Started 3d explorer with OpenGL (WIP).

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@241 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2012-01-27 14:47:08 +00:00 committed by ThunderK
parent 08ca1e5d59
commit 42bbb32b7a
7 changed files with 138 additions and 11 deletions

View file

@ -1,16 +1,19 @@
#include "dialogwanderer.h"
#include <QGLWidget>
#include <QVBoxLayout>
#include "widgetwanderer.h"
DialogWanderer::DialogWanderer(QWidget *parent):
DialogWanderer::DialogWanderer(QWidget* parent, CameraDefinition* camera):
QDialog(parent)
{
setModal(true);
setWindowTitle("Paysages 3D - Explore");
setLayout(new QVBoxLayout());
layout()->addWidget(new WidgetWanderer(this, camera));
}
DialogWanderer::~DialogWanderer()
{
}

View file

@ -2,12 +2,13 @@
#define _PAYSAGES_QT_DIALOGWANDERER_H_
#include <QDialog>
#include "../lib_paysages/camera.h"
class DialogWanderer : public QDialog
{
Q_OBJECT
public:
explicit DialogWanderer(QWidget *parent);
explicit DialogWanderer(QWidget *parent, CameraDefinition* camera);
~DialogWanderer();
};

View file

@ -19,6 +19,7 @@
#include "dialogwanderer.h"
#include "../lib_paysages/auto.h"
#include "../lib_paysages/scenery.h"
#include "../lib_paysages/shared/functions.h"
int main(int argc, char** argv)
@ -133,7 +134,11 @@ void MainWindow::quickPreview()
void MainWindow::explore3D()
{
DialogWanderer* dialog = new DialogWanderer(this);
CameraDefinition camera;
sceneryGetCamera(&camera);
DialogWanderer* dialog = new DialogWanderer(this, &camera);
dialog->exec();
delete dialog;

View file

@ -3,10 +3,6 @@
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
@ -25,4 +21,4 @@ public slots:
void explore3D();
};
#endif // _PAYSAGES_QT_MAINWINDOW_H_
#endif

View file

@ -36,7 +36,8 @@ HEADERS += ../lib_paysages/shared/functions.h ../lib_paysages/shared/types.h \
inputnoise.h \
mainwindow.h \
preview.h \
tools.h
tools.h \
widgetwanderer.h
FORMS +=
SOURCES += \
baseform.cpp \
@ -58,4 +59,5 @@ SOURCES += \
inputint.cpp \
inputnoise.cpp \
mainwindow.cpp \
preview.cpp
preview.cpp \
widgetwanderer.cpp

94
gui_qt/widgetwanderer.cpp Normal file
View file

@ -0,0 +1,94 @@
#include "widgetwanderer.h"
#include <QGLWidget>
#include "../lib_paysages/scenery.h"
WidgetWanderer::WidgetWanderer(QWidget *parent, CameraDefinition* camera):
QGLWidget(parent)
{
setMinimumSize(400, 300);
this->camera = camera;
this->terrain = terrainCreateDefinition();
sceneryGetTerrain(&terrain);
this->water = waterCreateDefinition();
sceneryGetWater(&water);
}
void WidgetWanderer::initializeGL()
{
glClearColor(0.4, 0.7, 0.8, 0.0);
}
void WidgetWanderer::resizeGL(int w, int h)
{
double ratio = (double)h / (double)w;
glViewport(0, 0, (GLint)w, (GLint)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-2.5, 2.5, -2.5 * ratio, 2.5 * ratio, 2.0, 1000.0);
}
static inline void _pushTerrainVertex(TerrainDefinition* terrain, double x, double z)
{
glVertex3f(x, terrainGetHeight(terrain, x, z), z);
//glVertex3f(x, 1.0, z);
}
void WidgetWanderer::paintGL()
{
double x, z, step;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera->location.x, camera->location.y, camera->location.z, camera->target.x, camera->target.y, camera->target.z, camera->up.x, camera->up.y, camera->up.z);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glLineWidth(1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_QUADS);
glVertex3f(-500.0, water.height, -500.0);
glVertex3f(-500.0, water.height, 500.0);
glVertex3f(500.0, water.height, 500.0);
glVertex3f(500.0, water.height, -500.0);
glEnd();
step = 2.0;
for (x = -50.0; x < 50.0; x += step)
{
for (z = -50.0; z < 50.0; z += step)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_QUADS);
_pushTerrainVertex(&terrain, x, z);
_pushTerrainVertex(&terrain, x, z + step);
_pushTerrainVertex(&terrain, x + step, z + step);
_pushTerrainVertex(&terrain, x + step, z);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(0.0, 0.5, 0.0);
glBegin(GL_QUADS);
_pushTerrainVertex(&terrain, x, z);
_pushTerrainVertex(&terrain, x, z + step);
_pushTerrainVertex(&terrain, x + step, z + step);
_pushTerrainVertex(&terrain, x + step, z);
glEnd();
}
}
}

26
gui_qt/widgetwanderer.h Normal file
View file

@ -0,0 +1,26 @@
#ifndef _PAYSAGES_QT_WIDGETWANDERER_H_
#define _PAYSAGES_QT_WIDGETWANDERER_H_
#include <QGLWidget>
#include "../lib_paysages/camera.h"
#include "../lib_paysages/terrain.h"
#include "../lib_paysages/water.h"
class WidgetWanderer : public QGLWidget
{
Q_OBJECT
public:
WidgetWanderer(QWidget* parent, CameraDefinition* camera);
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
private:
CameraDefinition* camera;
TerrainDefinition terrain;
WaterDefinition water;
};
#endif