Source

3dmaze / renderwidget.cpp

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* Copyright (C) 2010 by Eric Waller 
  
   Build and display a maze in either 2-D or 3-D

   This program 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 3 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, see <http://www.gnu.org/licenses/>.

   ewwaller@gmail.com

   Revision History

   Date        Author      Description
   ---------   ----------  -----------
   2010.7.20   E Waller    Initial Release
*/

#include "renderwidget.h"
#include <QtGui>
#include <math.h>

const double PI=3.14159;
const double MAX_ROTATION_COUNT=5760;
const double MOVE_DISTANCE= 0.005;
const double ROTATION_STEP_SIZE=16;
const double STEERING_AVERAGING_FACTOR = 4;
enum {STOP, MOVE_FORWARD, MOVE_AFT, PITCH_UP, PITCH_DOWN, MOVE_DOWN, MOVE_UP, ROTATE_RIGHT, ROTATE_LEFT};

RenderWidget::RenderWidget(QWidget* parent): QGLWidget(parent)
{
    object = 0;
    xRot = 5000;
    yRot = 0;
    zRot = 0;
    wallColor = QColor::fromRgb(0xa0,0xa0,0xa0,1);
    backgroundColor = QColor::fromRgb(0x00,0x00,0x00,1);
    pathColor = QColor::fromRgb(0xff,0x00,0x00,1);
    capColor = QColor::fromRgb(0xff,0xff,0xff,1);
    eyeX=0;
    eyeY=0;
    eyeZ=-0.1;
    ShowSolution=true;
    theMaze=0;
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(10);
    motionState=STOP;
    scale=0.05;
}

RenderWidget::~RenderWidget()
{
    makeCurrent();
    glDeleteLists(object, 1);
}

void RenderWidget::SetMazeEngine(MazeEngine *maze, bool theNewShowSolution)
{
    theMaze=maze;
    ShowSolution=theNewShowSolution;
    object=makeObject();
    updateGL();
}

QSize RenderWidget::minimumSizeHint() const
{
    return QSize(50, 50);
}

QSize RenderWidget::sizeHint() const
{
    return QSize(100, 50);
}

void RenderWidget::setXRotation(int angle)
{
    normalizeAngle(&angle);
    if (angle != xRot) {
        xRot = angle;
        emit xRotationChanged(angle);
        updateGL();
    }
}

void RenderWidget::setYRotation(int angle)
{
    normalizeAngle(&angle);
    if (angle != yRot) {
        yRot = angle;
        emit yRotationChanged(angle);
        updateGL();
    }
}

void RenderWidget::setZRotation(int angle)
{
    normalizeAngle(&angle);
    if (angle != zRot) {
        zRot = angle;
        emit zRotationChanged(angle);
        updateGL();
    }
}
void RenderWidget::setX(double x)
{
    eyeX=x;
    updateGL();
    emit xChanged(x);
}
void RenderWidget::setY(double y)
{
    eyeY=y;
    updateGL();
    yChanged(y);
}
void RenderWidget::setZ(double z)
{
    eyeZ=z;
    updateGL();
    emit zChanged(z);
}

void RenderWidget::initializeGL()
{
    qglClearColor(backgroundColor);
    object = makeObject();
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    GLfloat ambient[]={0.5, 0.5, 0.5, 0.0};
    GLfloat diffuse[]={1.0, 1.0, 1.0, 1.0};
    GLfloat specular[]={1.0, 1.0, 1.0, 1.0};
    GLfloat position[]={1.0, 1.0, 5.0, 0.0};

    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    glEnable(GL_COLOR_MATERIAL);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_FOG);
    glFogi (GL_FOG_MODE, GL_EXP);
    //GLfloat fogColor[]={1.0, 1.0, 5.0, 0.0};
    GLfloat fogColor[]={0.1, 0.1, 0.1, 0.0};
    glFogfv (GL_FOG_COLOR, fogColor);
    glFogf (GL_FOG_DENSITY, 2);
    glHint (GL_FOG_HINT, GL_DONT_CARE);
    glFogf (GL_FOG_START, 0.1);
    glFogf (GL_FOG_END, 3.0);
    glEnable(GL_CULL_FACE);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_POLYGON_SMOOTH);
}

void RenderWidget::paintGL()
{
    glMatrixMode (GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
    glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
    glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
    glTranslatef(eyeX,eyeY,eyeZ);
    GLfloat position[]={1.0, 1.0, 5.0, 0.0};
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glFrustum (-.02, .02, -.02, .02, 0.02, 8.0);
    glCallList(object);
}

void RenderWidget::resizeGL(int width, int height)
{
    int side = qMax(width, height);
    glViewport((width - side) / 2, (height - side) / 2, side, side);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
    glMatrixMode(GL_MODELVIEW);
}

void RenderWidget::mousePressEvent(QMouseEvent *event)
{
    lastPos = event->pos();
    mousedx=width()/2-event->x();
    mousedy=height()/2-event->y();
    if (event->buttons() & Qt::LeftButton)
    {
        if (abs(mousedx) > abs(mousedy))
        {
            if (mousedx>0) motionState=ROTATE_RIGHT;
            if (mousedx<0) motionState=ROTATE_LEFT;
        }
        else
        {
            if (mousedy>0) motionState=MOVE_FORWARD;
            if (mousedy<0) motionState=MOVE_AFT;
        }
    }
    if (event->buttons() & Qt::RightButton)
    {
        if (abs(mousedx) > abs(mousedy))
        {
            if (mousedx>0) motionState=PITCH_UP;
            if (mousedx<0) motionState=PITCH_DOWN;
        }
        else
        {
            if (mousedy>0) motionState=MOVE_UP;
            if (mousedy<0) motionState=MOVE_DOWN;
        }
    }
}

void RenderWidget::mouseReleaseEvent(QMouseEvent *event)
{
    motionState=STOP;
}

void RenderWidget::mouseMoveEvent(QMouseEvent *event)
{
}

void RenderWidget::update()
{
    switch (motionState)
    {
    case STOP: return;
    case ROTATE_RIGHT:
    case ROTATE_LEFT:
        setZRotation(zRot - ((motionState==ROTATE_RIGHT)-(motionState==ROTATE_LEFT))*ROTATION_STEP_SIZE);
        break;
    case MOVE_FORWARD:
    case MOVE_AFT:
        // We want to steer such that we are orthogonal to the x or y axis and be on the centerline
        // between the walls.  Calculate which direction to turn to the nearest N,S,E or W heading and
        // figure our how far we are from the centerline.  Turn towards the desired heading and and a bias
        // to steer towars the centerline.  First, figure out which centerline axis
        //to use of heading.
        {
            double distanceFromCenterline = eyeX;
            double roughDirection=0;
            if (zRot > MAX_ROTATION_COUNT/8)
            {
                roughDirection = MAX_ROTATION_COUNT/4;
                distanceFromCenterline = -eyeY;
            }
            if (zRot > 3*MAX_ROTATION_COUNT/8)
            {
                roughDirection = MAX_ROTATION_COUNT/2;
                distanceFromCenterline = -eyeX;
            }
            if (zRot > 5*MAX_ROTATION_COUNT/8)
            {
                roughDirection = 3*MAX_ROTATION_COUNT/4 ;
                distanceFromCenterline = eyeY;
            }
            if (zRot > 7*MAX_ROTATION_COUNT/8)
            {
                roughDirection = MAX_ROTATION_COUNT ;
                distanceFromCenterline = eyeX;
            }

            // Now determine how far we are from the "center" of the path and which direction.

            int sign= (distanceFromCenterline >=0)-(distanceFromCenterline<0);
            distanceFromCenterline =  (fabs(distanceFromCenterline/(2*scale))) - int(fabs(distanceFromCenterline/(2*scale))) ;
            distanceFromCenterline = (distanceFromCenterline-10*scale)*sign;

            // Steer towards the nearest orthogonal axis, with a bias towards the centerline

            double deltaAngle= (roughDirection - zRot)/STEERING_AVERAGING_FACTOR;
            deltaAngle += 100*distanceFromCenterline*((motionState==MOVE_FORWARD)-(motionState==MOVE_AFT));
            double newHeading = zRot+deltaAngle;

            // And fix any wrap around at 0.

            if (newHeading > MAX_ROTATION_COUNT) newHeading -= MAX_ROTATION_COUNT;
            if (newHeading < 0) newHeading = MAX_ROTATION_COUNT + newHeading;
            //setZRotation(newHeading );
            zRot=newHeading;
            emit (zRotationChanged(zRot));
        }
        eyeX -= sin(2*PI*zRot/MAX_ROTATION_COUNT)*((motionState==MOVE_FORWARD)-(motionState==MOVE_AFT))*MOVE_DISTANCE;
        eyeY -= cos(2*PI*zRot/MAX_ROTATION_COUNT)*((motionState==MOVE_FORWARD)-(motionState==MOVE_AFT))*MOVE_DISTANCE;
        emit(xChanged(eyeX));
        emit(yChanged(eyeY));
        break;
    case MOVE_UP:
    case MOVE_DOWN:
        setZ(eyeZ - ((motionState==MOVE_UP)-(motionState==MOVE_DOWN))*MOVE_DISTANCE);
        break;
    case PITCH_UP:
    case PITCH_DOWN:
        setXRotation(xRot + ((motionState==PITCH_UP)-(motionState==PITCH_DOWN))*ROTATION_STEP_SIZE);
        break;
    default:
        break;
    }
}

GLuint RenderWidget::makeObject()
{
    if (! theMaze)
        return 0;
    QPixmap thePixMap,thePixMapFloor;

    if (thePixMap.load( QString(":/images/images/texture.jpg")))
        texture = bindTexture(thePixMap,GL_TEXTURE_2D);
    if (thePixMapFloor.load( QString(":/images/images/floor.jpg")))
        floor = bindTexture(thePixMapFloor,GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    GLuint list = glGenLists(1);
    glNewList(list, GL_COMPILE);
    for ( int x =0; x < theMaze->GetXSize() ; x++)
        for ( int y=0 ; y < theMaze->GetYSize() ; y++)
        {
        if ( theMaze->GetMapData(x,y)== theMaze->WALL )
        {
            for (int i=0 ; i<4 ; i++)
            {
                int  xLook=x+dx[i];
                int  yLook=y+dy[i];
                
                if (!(xLook < 0 || xLook>theMaze->GetXSize()-1 || yLook <0 || yLook > theMaze->GetYSize()-1))
                    if (theMaze->GetMapData(xLook, yLook) == theMaze->WALL)
                    {
                    GLdouble x1=double(x)*scale + scale/2-theMaze->GetXSize()*scale/2;
                    GLdouble y1=double(y)*scale+scale/2-theMaze->GetYSize()*scale/2;
                    GLdouble x2= x1+dx[i]*scale/2+.005*(!dx[i]);
                    GLdouble y2= y1+dy[i]*scale/2+.005*(!dy[i]);
                    glBindTexture(GL_TEXTURE_2D, floor);
                    glBegin(GL_QUADS);
                    quad(x1, y1, x2, y2, 0.05);
                    quad(x1, y1, x2, y2, -0.05);
                    glEnd();
                    glBindTexture(GL_TEXTURE_2D, texture);
                    glBegin(GL_QUADS);
                    extrude(x1, y1, x2, y1);
                    extrude(x1, y1, x1, y2);
                    extrude(x2, y2, x1, y2);
                    extrude(x2, y2, x2, y1);
                    glEnd();
                }
            }
        }
        else if (ShowSolution && (theMaze->GetMapData(x,y)== theMaze->SOLUTION))
        {
            GLfloat x1=x*scale-theMaze->GetXSize()*scale/2;
            GLfloat y1=y*scale-theMaze->GetYSize()*scale/2 ;
            GLfloat x2=x1 + scale;
            GLfloat y2=y1 + scale;
            glBindTexture(GL_TEXTURE_2D, floor);
            glBegin(GL_QUADS);
            quad(x1, y1, x2, y2,-0.05,true);
            glEnd();
        }
    }
    glEndList();
    return list;
}

void RenderWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, GLdouble z, bool floor)
{
    if (floor)
        qglColor(pathColor);
    else
        qglColor(capColor);
    glTexCoord2d(0.0,10.0);
    glVertex3f(x1, y1, z);
    glTexCoord2d(10.0,10.0);
    glVertex3f(x2, y1, z);
    glTexCoord2d(10.0,0.0);
    glVertex3f(x2, y2, z);
    glTexCoord2d(0,0.0);
    glVertex3f(x1, y2, z);
    glTexCoord2d(0.0,10.0);
    glVertex3f(x1, y1, z);
    glTexCoord2d(10.0,10.0);
    glVertex3f(x1, y2, z);
    glTexCoord2d(10.0,0.0);
    glVertex3f(x2, y2, z);
    glTexCoord2d(0.0,0.0);
    glVertex3f(x2, y1, z);
}

void RenderWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
{
    qglColor(wallColor);
    glTexCoord2d(0.0,5.0);
    glVertex3d(x1, y1, +0.05);
    glTexCoord2d(1.0,5.0);
    glVertex3d(x2, y2, +0.05);
    glTexCoord2d(1.0,0.0);
    glVertex3d(x2, y2, -0.05);
    glTexCoord2d(0.0,0.0);
    glVertex3d(x1, y1, -0.05);
    glTexCoord2d(0,5.0);
    glVertex3d(x1, y1, +0.05);
    glTexCoord2d(0.0,0.0);
    glVertex3d(x1, y1, -0.05);
    glTexCoord2d(1.0,0.0);
    glVertex3d(x2, y2, -0.05);
    glTexCoord2d(1.0,5.0);
    glVertex3d(x2, y2, +0.05);
}

void RenderWidget::normalizeAngle(int *angle)
{
    while (*angle < 0)
        *angle += 360 * 16;
    while (*angle > 360 * 16)
        *angle -= 360 * 16;
}