I can´t implement collisions in my code, all the walls of the maze are represented with a 1 in the vector, the camera only can moves forward and backward and rotate the camera to continue moving, I don’t know if it has to do with the fact that I rotated the maze 180° in Z and 180° in Y
#include <iostream>
#include <math.h>
#include <string.h>
#include "stb_image.h"
#include <vector>
#define M_PI 3.14159265358979323846
#include <openGL/glut.h>
#define WINDOW_WIDTH 1000
#define WINDOW_HEIGHT 1000
using namespace std;
// angle of rotation for the camera direction
float angle = 0.0, yAngle = 0.0;
// actual vector representing the camera's direction
float lx = 0.0f, ly = 0.0f, lz = -1.0f;
// XZ position of the camera
float x = 155.0f, z = 160.0f;
float roll = 0.0f;
float y = 0.1f;
float width = 15.0f; // Anchura de la pared en cm
//vector para almacenar las texturas
std::vector<GLuint> textures;
// Para los movimientos del mouse
float halfWidth = (float)(WINDOW_WIDTH / 2.0);
float halfHeight = (float)(WINDOW_HEIGHT / 2.0);
float mouseX = 0.0f, mouseY = 0.0f;
// Definir la matriz directamente en el código
std::vector<std::vector<int>> matriz = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
void cargarTextura(GLuint texture) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
static void drawBox(GLfloat size, GLenum type) {
static GLfloat n[6][3] =
{
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, -1.0}
};
static GLint faces[6][4] =
{
{0, 1, 2, 3},
{3, 2, 6, 7},
{7, 6, 5, 4},
{4, 5, 1, 0},
{5, 6, 2, 1},
{7, 4, 0, 3}
};
GLfloat v[8][3];
GLint i;
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;
for (i = 5; i >= 0; i--) {
cargarTextura(textures[0]);
glBegin(type);
glNormal3fv(&n[i][0]);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(&v[faces[i][0]][0]);
glTexCoord2f(0.0f, 9.0f); glVertex3fv(&v[faces[i][1]][0]);
glTexCoord2f(9.0f, 9.0f); glVertex3fv(&v[faces[i][2]][0]);
glTexCoord2f(9.0f, 0.0f); glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}
void glutSolidCubes(GLdouble size) {
drawBox(size, GL_QUADS);
}
void drawCubesFromMatrix() {
float cubeSize = 10.0f; // Tamaño del cubo
int numFilas = matriz.size();
int numColumnas = matriz[0].size();
// Recorre la matriz para dibujar cubos
for (int i = 0; i < numFilas; ++i) {
for (int j = 0; j < numColumnas; ++j) {
if (matriz[i][j] == 1) {
glPushMatrix();
glTranslatef(j * cubeSize, 0.0f, -i * cubeSize);
glutSolidCubes(cubeSize);
glPopMatrix();
}
}
}
}
void drawFloor() {
float floorSize = 10.0f; // Floor tile size
int numFilas = matriz.size();
int numColumnas = matriz[0].size();
cargarTextura(textures[1]); // Assume the floor texture is the second texture in the list
glBegin(GL_QUADS);
for (int i = 0; i < numFilas; ++i) {
for (int j = 0; j < numColumnas; ++j) {
glTexCoord2f(0.0f, 0.0f); glVertex3f(j * floorSize, 0.0f, -i * floorSize);
glTexCoord2f(1.0f, 0.0f); glVertex3f((j + 1) * floorSize, 0.0f, -i * floorSize);
glTexCoord2f(1.0f, 1.0f); glVertex3f((j + 1) * floorSize, 0.0f, -(i + 1) * floorSize);
glTexCoord2f(0.0f, 1.0f); glVertex3f(j * floorSize, 0.0f, -(i + 1) * floorSize);
}
}
glEnd();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
roll + 0.0f, 2.5f, 0.0f);
glRotatef(180, 0, 0, 1);
glRotatef(180, 0, 1, 0);
drawCubesFromMatrix();
glTranslatef(0, 5, 0);
drawFloor();
glFlush();
glutSwapBuffers();
}
void teclasEspeciales(int key, int xx, int yy) {
float fraction = 0.6f;
switch (key) {
case GLUT_KEY_LEFT:
angle -= 0.06f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_RIGHT:
angle += 0.06f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_UP:
x += lx * fraction;
z += lz * fraction;
break;
case GLUT_KEY_DOWN:
x -= lx * fraction;
z -= lz * fraction;
break;
}
}
void changeSize(int w, int h) {
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45.0f, ratio, 0.1f, 200.0f);
glMatrixMode(GL_MODELVIEW);
}
void animate() {
glutPostRedisplay();
}
GLuint loadTexture(const char* filename) {
int width, height, channels;
unsigned char* data = stbi_load(filename, &width, &height, &channels, 0);
if (!data) {
std::cerr << "Error: could not load texture " << filename << std::endl;
exit(1);
}
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, channels == 4 ? GL_RGBA : GL_RGB, width, height, 0, channels == 4 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
stbi_image_free(data);
return texture;
}
int main(int argc, char** argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Classroom");
// register callbacks
glutDisplayFunc(display);
glutReshapeFunc(changeSize);
glutIdleFunc(animate);
glutSpecialFunc(teclasEspeciales);
// OpenGL init
glEnable(GL_DEPTH_TEST);
textures.push_back(loadTexture("brick.bmp"));
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
textures.push_back(loadTexture("ventana.png"));
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
If you could help me, since I am new to this world of openGL and c++
New contributor
Arturo Rodríguez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.