mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
feat: add sky box
This commit is contained in:
102
src/main.cpp
102
src/main.cpp
@@ -19,7 +19,7 @@
|
||||
|
||||
constexpr int NUM_VAO = 1;
|
||||
|
||||
GLuint rendering_program, outline_program;
|
||||
GLuint world_program, outline_program, sky_program;
|
||||
GLuint vao[NUM_VAO];
|
||||
GLuint mv_loc, proj_loc;
|
||||
int width ,height;
|
||||
@@ -34,7 +34,7 @@ Camera camera;
|
||||
TextureManager texture_manager;
|
||||
World world;
|
||||
|
||||
GLuint outline_vbo, outline_indices_vbo;
|
||||
GLuint outline_vbo, outline_indices_vbo, sky_vbo;
|
||||
|
||||
|
||||
|
||||
@@ -44,21 +44,27 @@ void setup_vertices(void) {
|
||||
glBindVertexArray(vao[0]);
|
||||
glBindVertexArray(0);
|
||||
glGenBuffers(1, &outline_vbo);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, outline_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(CUBE_VER), CUBE_VER, GL_STATIC_DRAW);
|
||||
|
||||
glGenBuffers(1, &outline_indices_vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, outline_indices_vbo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(OUTLINE_CUBE_INDICES), OUTLINE_CUBE_INDICES, GL_STATIC_DRAW);
|
||||
|
||||
glGenBuffers(1, &sky_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, sky_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES_POS), VERTICES_POS, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void init(GLFWwindow* window) {
|
||||
rendering_program = Shader::create_shader_program("shaders/vShader.glsl", "shaders/fShader.glsl");
|
||||
world_program = Shader::create_shader_program("shaders/block_v_shader.glsl", "shaders/block_f_shader.glsl");
|
||||
outline_program = Shader::create_shader_program("shaders/outline_v_shader.glsl", "shaders/outline_f_shader.glsl");
|
||||
|
||||
sky_program = Shader::create_shader_program("shaders/sky_v_shader.glsl", "shaders/sky_f_shader.glsl");
|
||||
|
||||
camera.camera_init(&world.get_player("TestPlayer"));
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
@@ -94,34 +100,9 @@ void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||
camera.update_cursor_position_camera(xpos, ypos);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void display(GLFWwindow* window, double current_time) {
|
||||
delta_time = current_time - last_time;
|
||||
last_time = current_time;
|
||||
world.update(delta_time);
|
||||
camera.update_move_camera();
|
||||
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glUseProgram(rendering_program);
|
||||
glBindVertexArray(vao[0]);
|
||||
mv_loc = glGetUniformLocation(rendering_program, "mv_matrix");
|
||||
proj_loc = glGetUniformLocation(rendering_program, "proj_matrix");
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, texture_array);
|
||||
m_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
v_mat = camera.get_camera_lookat();
|
||||
mv_mat = v_mat * m_mat;
|
||||
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(mv_mat));
|
||||
glUniformMatrix4fv(proj_loc, 1 ,GL_FALSE, glm::value_ptr(p_mat));
|
||||
|
||||
world.render();
|
||||
|
||||
void render_outline() {
|
||||
glUseProgram(outline_program);
|
||||
|
||||
mv_loc = glGetUniformLocation(outline_program, "mv_matrix");
|
||||
proj_loc = glGetUniformLocation(outline_program, "proj_matrix");
|
||||
const auto& block_pos = world.get_look_block_pos("TestPlayer");
|
||||
@@ -141,6 +122,61 @@ void display(GLFWwindow* window, double current_time) {
|
||||
glLineWidth(4.0f);
|
||||
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void render_sky() {
|
||||
glUseProgram(sky_program);
|
||||
|
||||
mv_loc = glGetUniformLocation(sky_program, "mv_matrix");
|
||||
proj_loc = glGetUniformLocation(sky_program, "proj_matrix");
|
||||
|
||||
m_mat = glm::translate(glm::mat4(1.0f), camera.get_camera_pos() - glm::vec3(0.5f, 0.5f, 0.5f));
|
||||
v_mat = camera.get_camera_lookat();
|
||||
mv_mat = v_mat * m_mat;
|
||||
|
||||
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(mv_mat));
|
||||
glUniformMatrix4fv(proj_loc, 1 ,GL_FALSE, glm::value_ptr(p_mat));
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, sky_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
}
|
||||
|
||||
void display(GLFWwindow* window, double current_time) {
|
||||
delta_time = current_time - last_time;
|
||||
last_time = current_time;
|
||||
world.update(delta_time);
|
||||
camera.update_move_camera();
|
||||
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
glBindVertexArray(vao[0]);
|
||||
render_sky();
|
||||
glUseProgram(world_program);
|
||||
|
||||
mv_loc = glGetUniformLocation(world_program, "mv_matrix");
|
||||
proj_loc = glGetUniformLocation(world_program, "proj_matrix");
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, texture_array);
|
||||
m_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
v_mat = camera.get_camera_lookat();
|
||||
mv_mat = v_mat * m_mat;
|
||||
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(mv_mat));
|
||||
glUniformMatrix4fv(proj_loc, 1 ,GL_FALSE, glm::value_ptr(p_mat));
|
||||
|
||||
world.render();
|
||||
|
||||
render_outline();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -226,10 +262,12 @@ int main() {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDeleteBuffers(1, &outline_vbo);
|
||||
glDeleteBuffers(1, &outline_indices_vbo);
|
||||
glDeleteBuffers(1, &sky_vbo);
|
||||
glBindVertexArray(0);
|
||||
glDeleteVertexArrays(NUM_VAO, vao);
|
||||
glDeleteProgram(rendering_program);
|
||||
glDeleteProgram(world_program);
|
||||
glDeleteProgram(outline_program);
|
||||
glDeleteProgram(sky_program);
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
exit(EXIT_SUCCESS);
|
||||
|
||||
Reference in New Issue
Block a user