PDA

View Full Version : Missing 3D Objects



StreetKing
04-13-2016, 02:20 PM
I am generating a random maze and trying to fill the walls with a simple draw cube function. The problem is that only one line of cubes appears and I am not sure what's wrong with my code here. Any help would be greatly appreciated.

// Glut.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <GL\freeglut.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <iostream>
#include <ctime>
#include <algorithm>
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle = 0.0;
float lastx, lasty;
int ** originMaze;
int mazeh;
int mazew;
const GLfloat HALF_CUBE = 1.25f;
const GLfloat FULL_CUBE = HALF_CUBE + HALF_CUBE;
void cube(GLfloat, GLfloat, GLfloat);
void generateRandomDirections(std::vector<int>&);
int** generateMaze(int**, int, int);
void recursion(int, int, int **, int, int);


void enable(void) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);

}

void camera(void) {
glRotatef(xrot, 1.0, 0.0, 0.0);
glRotatef(yrot, 0.0, 1.0, 0.0);
glTranslated(-xpos, -ypos, -zpos);
}

void display(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
camera();
enable();
glBegin(GL_QUADS);
for (int i = 0;i < mazeh;i++)
for (int j = 0;j < mazew;j++)
if (originMaze[i][j] == 1)
cube(i*4, j*4, 0);
glEnd();
glutSwapBuffers();
angle++;
}

void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);

}

void keyboard(unsigned char key, int x, int y) {
if (key == 'q')
{
xrot += 1;
if (xrot >360) xrot -= 360;
}

if (key == 'z')
{
xrot -= 1;
if (xrot < -360) xrot += 360;
}

if (key == 'w')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad));
zpos -= float(cos(yrotrad));
ypos -= float(sin(xrotrad));
}

if (key == 's')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad));
zpos += float(cos(yrotrad));
ypos += float(sin(xrotrad));
}

if (key == 'd')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos += float(cos(yrotrad)) * 0.2;
zpos += float(sin(yrotrad)) * 0.2;
}

if (key == 'a')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= float(cos(yrotrad)) * 0.2;
zpos -= float(sin(yrotrad)) * 0.2;
}

if (key == 27)
{
exit(0);
}
}

void mouseMovement(int x, int y) {
int diffx = x - lastx;
int diffy = y - lasty;
lastx = x;
lasty = y;
xrot += (float)diffy;
yrot += (float)diffx;
}

int main(int argc, char **argv) {
srand(time(0));
int ** maze = new int*[2];
mazeh = 30;
mazew = 30;
maze = generateMaze(maze, 30, 30);
originMaze = maze;
for (int i = 0;i <mazeh;i++)
{
for (int j = 0;j < mazew;j++)
std::cout << maze[i][j];
std::cout << std::endl;
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Maze");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);

glutPassiveMotionFunc(mouseMovement);

glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}



int** generateMaze(int** maze, int height, int width) {
delete[] maze;
maze = new int*[height];
for (int i = 0;i < height;i++)
maze[i] = new int[width];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
maze[i][j] = 1;
int r = rand() % height;
while (r % 2 == 0) {
r = rand() % height;
}
int c = rand() % width;
while (c % 2 == 0) {
c = rand() % width;
}
maze[r][c] = 0;
recursion(r, c, maze, height, width);
return maze;
}

void recursion(int r, int c, int ** maze, int height, int width) {
std::vector<int> x;
generateRandomDirections(x);
for (int i = 0; i < x.size(); i++) {

switch (x[i]) {
case 1:
if (r - 2 <= 0)
continue;
if (maze[r - 2][c] != 0) {
maze[r - 2][c] = 0;
maze[r - 1][c] = 0;
recursion(r - 2, c, maze, height, width);
}
break;
case 2:
if (c + 2 >= width - 1)
continue;
if (maze[r][c + 2] != 0) {
maze[r][c + 2] = 0;
maze[r][c + 1] = 0;
recursion(r, c + 2, maze, height, width);
}
break;
case 3:
if (r + 2 >= height - 1)
continue;
if (maze[r + 2][c] != 0) {
maze[r + 2][c] = 0;
maze[r + 1][c] = 0;
recursion(r + 2, c, maze, height, width);
}
break;
case 4:
if (c - 2 <= 0)
continue;
if (maze[r][c - 2] != 0) {
maze[r][c - 2] = 0;
maze[r][c - 1] = 0;
recursion(r, c - 2, maze, height, width);
}
break;
}
}

}

void generateRandomDirections(std::vector<int> &x) {
for (int i = 0; i < 4; i++)
x.push_back(i + 1);
std::random_shuffle(x.begin(), x.end());
}

void cube(GLfloat x, GLfloat y, GLfloat z) //Draws a cube centered at (x,y,z)
{
glPushMatrix();
glTexCoord2d(1.0, 1.0);
glVertex3f(x + HALF_CUBE, HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(0.0, 1.0);
glVertex3f(x - HALF_CUBE, HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(0.0, 0.0);
glVertex3f(x - HALF_CUBE, HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(1.0, 0.0);
glVertex3f(x + HALF_CUBE, HALF_CUBE, z + HALF_CUBE);


glTexCoord2d(1.0, 1.0);
glVertex3f(x + HALF_CUBE, -HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(0.0, 1.0);
glVertex3f(x - HALF_CUBE, -HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(0.0, 0.0);
glVertex3f(x - HALF_CUBE, -HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(1.0, 0.0);
glVertex3f(x + HALF_CUBE, -HALF_CUBE, z - HALF_CUBE);


glTexCoord2d(1.0, 1.0);
glVertex3f(x + HALF_CUBE, HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(0.0, 1.0);
glVertex3f(x - HALF_CUBE, HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(0.0, 0.0);
glVertex3f(x - HALF_CUBE, -HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(1.0, 0.0);
glVertex3f(x + HALF_CUBE, -HALF_CUBE, z + HALF_CUBE);


glTexCoord2d(1.0, 1.0);
glVertex3f(x - HALF_CUBE, HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(0.0, 1.0);
glVertex3f(x + HALF_CUBE, HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(0.0, 0.0);
glVertex3f(x + HALF_CUBE, -HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(1.0, 0.0);
glVertex3f(x - HALF_CUBE, -HALF_CUBE, z - HALF_CUBE);


glTexCoord2d(1.0, 1.0);
glVertex3f(x - HALF_CUBE, HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(0.0, 1.0);
glVertex3f(x - HALF_CUBE, HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(0.0, 0.0);
glVertex3f(x - HALF_CUBE, -HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(1.0, 0.0);
glVertex3f(x - HALF_CUBE, -HALF_CUBE, z + HALF_CUBE);


glTexCoord2d(1.0, 1.0);
glVertex3f(x + HALF_CUBE, HALF_CUBE, z - HALF_CUBE);
glTexCoord2d(0.0, 1.0);
glVertex3f(x + HALF_CUBE, HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(0.0, 0.0);
glVertex3f(x + HALF_CUBE, -HALF_CUBE, z + HALF_CUBE);
glTexCoord2d(1.0, 0.0);
glVertex3f(x + HALF_CUBE, -HALF_CUBE, z - HALF_CUBE);
glPopMatrix();
}