我正在研究一个简单的3D迷宫游戏,其中迷宫每次启动都会自动发芽。 到目前为止,我拥有的迷宫在每次奔跑中都不同,具有可以移动的角色,一旦到达目标(与确切位置发生冲突),我就打印出'胜利'。
[我现在想做的事情:
我想用以前做过的相同方法重新创建迷宫,然后将新迷宫打印到屏幕上,以允许玩家在其他地图上重播。我开始写一条if语句:如果玩家在获胜位置,我应该删除缓冲区和顶点数组,然后创建一个新的缓冲区,然后再次用我的数据填充它。但是不。我非常困在这里,为您的帮助我感到非常高兴!如果有人可以向我展示可行的解决方案,我将非常高兴。我在下面显示一些代码片段。这是我检查球员在球门上的位置:
static void MoveCharacter(Cube& character, Keyboard keyboard, std::vector<Cube> maze)
{
//... key handling...
if (keyboard.getKey(GLFW_KEY_LEFT))
{
//...
}
if (character.position.x >= MazeHeight - 1.0f && character.position.z >= MazeWidth - 1.0f)
{
std::cout << "YOU WIN" << std::endl;
win = true;
//reached the goal
}
}
我向您展示了我的主要代码的某些部分:
int main(void)
{
// some settings here...
// Cube vertices
static const GLfloat g_vertex_buffer_data[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
//...and so on
};
// Import the shader files
Shader cube("shaders/cube.vert", "shaders/cube.frag");
Shader lightShader("shaders/light.vert", "shaders/light.frag");
// Create the VBO and VAO for the cubes
GLuint VBO, vao;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glBindVertexArray(vao);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)0); // stride: byte offset between consecutive generic vertex attributes, offset init: 0
glEnableVertexAttribArray(0);
// Normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
///////////////////////
// Then, we set the light's VAO (VBO stays the same. After all, the vertices are the same for the light object (also a 3D cube))
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
// We only need to bind to the VBO (to link it with glVertexAttribPointer), no need to fill it; the VBO's data already contains all we need.
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// Set the vertex attributes (only position data for the lamp))
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); // Note that we skip over the normal vectors
glEnableVertexAttribArray(0);
glBindVertexArray(0);
///////////////////////
// Creating the character
Cube character = { glm::vec3(0, 1.1, 0), glm::vec3(0.2f) }; //position (0,0,0 is the START)
// Creating the maze
std::vector<Cube> ground;
createGround(ground);
std::vector<glm::vec3> initMaze = GenerateMaze();
std::vector<Cube> maze = PutTheWalls(initMaze);
ground.reserve(ground.size() + maze.size()); //allocate places for the walls
ground.insert(ground.end(), maze.begin(), maze.end()); // put all together (walls + ground)
// Set projection
glm::mat4 projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 150.0f);
while (!glfwWindowShouldClose(window)) {
// Calculate delta time of the current frame
float currentFrame = glfwGetTime();
delta = currentFrame - lastFrame;
lastFrame = currentFrame;
glfwPollEvents();
// Movements
MoveCamera(camera, keyboard, delta);
MoveCharacter(character, keyboard, maze);
if (win == true)
{
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &VBO);
// I don't know how to go on with this one. Here I should handle the recreation of the maze or shouldn't I? This is a complete mess in my head now.
}
// Clear the colorbuffer
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera.update();
// Use the CUBE shader and set uniform objects
cube.use();
glBindVertexArray(vao);
glUniform3f(glGetUniformLocation(cube.program, "lightPos"), character.position.x, character.position.y, character.position.z);
glUniform3f(glGetUniformLocation(cube.program, "viewPos"), ((GLfloat)MazeWidth / 2), ((GLfloat)MazeWidth * 2), ((GLfloat)MazeWidth / 2));
glUniform3f(glGetUniformLocation(cube.program, "lightColor"), 0.1f, 0.1f, 0.1f); //1.0 világos, 0.1 sötét
glUniform3f(glGetUniformLocation(cube.program, "objectColor"), 0.1f, 0.9f, 0.9f); //color of the walls
glUniformMatrix4fv(glGetUniformLocation(cube.program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(cube.program, "view"), 1, GL_FALSE, glm::value_ptr(camera.matrix()));
for (unsigned i = 0; i < ground.size(); i++) {
glm::mat4 model;
model = glm::translate(model, ground[i].position);
model = glm::scale(model, ground[i].size);
glUniformMatrix4fv(glGetUniformLocation(cube.program, "model"), 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 12 * 3);
}
// Use the shader and set uniform objects
lightShader.use();
glUniformMatrix4fv(glGetUniformLocation(lightShader.program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(lightShader.program, "view"), 1, GL_FALSE, glm::value_ptr(camera.matrix()));
glm::mat4 model;
model = glm::translate(model, character.position);
model = glm::scale(model, character.size);
glUniformMatrix4fv(glGetUniformLocation(lightShader.program, "model"), 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 12 * 3);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glUseProgram(0);
glDeleteVertexArrays(1, &vao);
glDeleteVertexArrays(1, &lightVAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return EXIT_SUCCESS;
}
我正在研究一个简单的3D迷宫游戏,其中迷宫每次启动都会自动发芽。到目前为止,我所拥有的迷宫每次运行都不同,具有一个字符...
如我所见,渲染的内容实际上存储在ground
数组中,用于移动的数据位于maze
中。像这样重置: