void render_map(BSPMap* map) { int i; glUseProgram(main_program); camera_rotation = mat4f_camera_transform(cam.target, cam.up); camera_translation = mat4f_translate(-cam.pos.v[0], -cam.pos.v[1], -cam.pos.v[2]); transformation = mat4f_mult(perspective, camera_rotation); transformation = mat4f_mult(transformation, camera_translation); glUniformMatrix4fv(gWorldLocation, 1, GL_TRUE, &transformation.m[0][0]); glEnableVertexAttribArray(glGetAttribLocation(main_program, "Position")); /*vertex coords*/ glEnableVertexAttribArray(glGetAttribLocation(main_program, "TextureCoord")); /*texture coords*/ glEnableVertexAttribArray(glGetAttribLocation(main_program, "LightmapCoord")); /*lightmap coords*/ glBindBuffer(GL_ARRAY_BUFFER, 0); set_visible_faces(map, cam.pos); for (i = 0; i < map->numberOfFaces; i++) { /*if (!map.textureIds[map.loadedFaces[i].texture]) continue;*/ if (!map->visible_faces[i]) continue; if (map->loadedFaces[i].type == 1 || map->loadedFaces[i].type == 3) /*polygons and meshes*/ { glVertexAttribPointer(glGetAttribLocation(main_program, "Position"), 3, GL_FLOAT, GL_FALSE, sizeof(BSPVertex), &map->loadedVerts[map->loadedFaces[i].vertex].position); glActiveTexture(GL_TEXTURE0); /*texture*/ glBindTexture(GL_TEXTURE_2D, map->textureIds[map->loadedFaces[i].texture]); glVertexAttribPointer(glGetAttribLocation(main_program, "TextureCoord"), 2, GL_FLOAT, GL_FALSE, sizeof(BSPVertex), &map->loadedVerts[map->loadedFaces[i].vertex].texcoord[0]); glActiveTexture(GL_TEXTURE1); /*lightmap*/ glBindTexture(GL_TEXTURE_2D, map->lightmapTextureIds[map->loadedFaces[i].lm_index]); glVertexAttribPointer(glGetAttribLocation(main_program, "LightmapCoord"), 2, GL_FLOAT, GL_FALSE, sizeof(BSPVertex), &map->loadedVerts[map->loadedFaces[i].vertex].texcoord[1]); glDrawElements(GL_TRIANGLES, map->loadedFaces[i].n_meshverts, GL_UNSIGNED_INT, &map->loadedMeshVerts[map->loadedFaces[i].meshvert].offset); } } glDisableVertexAttribArray(glGetAttribLocation(main_program, "Position")); glDisableVertexAttribArray(glGetAttribLocation(main_program, "TextureCoord")); glDisableVertexAttribArray(glGetAttribLocation(main_program, "LightmapCoord")); glUseProgram(0); }
void render_iqm_model(IQMModel* model) { int i; glUseProgram(iqm_program); // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->ebo); // glBindBuffer(GL_ARRAY_BUFFER, model->vbo); camera_rotation = mat4f_camera_transform(cam.target, cam.up); camera_translation = mat4f_translate(model->position.v[0] - cam.pos.v[0], model->position.v[1] - cam.pos.v[1], model->position.v[2] - cam.pos.v[2]); transformation = mat4f_mult(perspective, camera_rotation); transformation = mat4f_mult(transformation, camera_translation); glUniformMatrix4fv(gWorldLocation, 1, GL_TRUE, &transformation.m[0][0]); glUniform1i(glGetUniformLocation(iqm_program, "iqm_sampler"), 0); glEnableVertexAttribArray(glGetAttribLocation(iqm_program, "Position")); /*vertex coords*/ glEnableVertexAttribArray(glGetAttribLocation(iqm_program, "TexCoord")); /*texture coords*/ //glBindBuffer(GL_ARRAY_BUFFER, 0); glActiveTexture(GL_TEXTURE0); for (i = 0; i < model->num_meshes; i++) { glVertexAttribPointer(glGetAttribLocation(iqm_program, "Position"), 3, GL_FLOAT, GL_FALSE, 0, &model->loadedVertexPositions[0]); glVertexAttribPointer(glGetAttribLocation(iqm_program, "TexCoord"), 2, GL_FLOAT, GL_FALSE, 0, &model->loadedVertexTexCoords[0]); glBindTexture(GL_TEXTURE_2D, model->textures[i]); glDrawElements(GL_TRIANGLES, 3 * model->loadedMeshes[i].num_triangles, GL_UNSIGNED_INT, &model->loadedTriangles[model->loadedMeshes[i].first_triangle]); } glDisableVertexAttribArray(glGetAttribLocation(iqm_program, "Position")); glDisableVertexAttribArray(glGetAttribLocation(iqm_program, "TexCoord")); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glUseProgram(0); }
int main(void) { if(!glfwInit()) { fprintf(stderr, "Could not load GLFW, aborting.\n"); return(EXIT_FAILURE); } int WIDTH, HEIGHT; WIDTH = 800; HEIGHT = 600; GLFWwindow *window; window = glfwCreateWindow(WIDTH,HEIGHT,"05 camera.", NULL, NULL); if(!window) { fprintf(stderr, "Could not create main window, aborting.\n"); return(EXIT_FAILURE); } glfwMakeContextCurrent(window); glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { fprintf(stderr, "Could not initialize GLEW, aborting.\n"); return(EXIT_FAILURE); } glEnable(GL_DEPTH_TEST); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); GLuint VAO, VBO, lightVAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(1); //glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, // (GLvoid*)(3*sizeof(GLfloat))); //glEnableVertexAttribArray(1); glBindVertexArray(0); /* LightVAO definition. */ glGenVertexArrays(1, &lightVAO); glBindVertexArray(lightVAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); // Same vertex data as cube. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)0); glEnableVertexAttribArray(0); glBindVertexArray(0); char* vertex_source = read_shader("shaders/07.vert"); char* fragment_source = read_shader("shaders/07.frag"); char* lamp_fragment_source = read_shader("shaders/07_lamp.frag"); GLuint shader_program, lamp_program; shader_program = create_shader_program(vertex_source, fragment_source); lamp_program = create_shader_program(vertex_source, lamp_fragment_source); free(vertex_source); free(fragment_source); free(lamp_fragment_source); //GLuint texture; //glActiveTexture(GL_TEXTURE0); //glGenTextures(1, &texture); //glBindTexture(GL_TEXTURE_2D, texture); //load_texture("textures/02_container.jpg"); //glUniform1i(glGetUniformLocation(shader_program, "texture_sampler"), 0); //glBindTexture(GL_TEXTURE_2D, 0); Mat4f *projection, *model, *view, *temp, *temp2; mat4f_allocate(&projection); mat4f_allocate(&model); mat4f_allocate(&view); mat4f_allocate(&temp); mat4f_allocate(&temp2); mat4f_translate(view, 0.0f, 0.0f, -3.0f); //mat4f_rotate_x(model, -M_PI/4); mat4f_rotate_x(model, 0.0f); Vec3f *light_position; vec3f_allocate(&light_position); vec3f_allocate(&camera_pos); vec3f_allocate(&camera_target); vec3f_allocate(&camera_up); vec3f_allocate(&camera_front); vec3f_allocate(&temp_vec3f); vec3f_set(camera_target, 0.0f, 0.0f, 0.0f); vec3f_set(camera_up, 0.0f, 1.0f, 0.0f); vec3f_set(camera_front, 0.0f, 0.0f, -1.0f); vec3f_set(camera_pos, 0.0f, 0.0f, 3.0f); vec3f_set(light_position, 1.2f, 1.0f, 2.0f); /* shader locations */ GLuint model_location, projection_location, view_location, light_position_location, view_position_location; glUseProgram(shader_program); model_location = glGetUniformLocation(shader_program, "model"); projection_location = glGetUniformLocation(shader_program, "perspective"); view_location = glGetUniformLocation(shader_program, "view"); light_position_location = glGetUniformLocation(shader_program, "light_position"); view_position_location = glGetUniformLocation(shader_program, "view_position"); GLuint object_color_location, light_color_location; object_color_location = glGetUniformLocation(shader_program, "object_color"); light_color_location = glGetUniformLocation(shader_program, "light_color"); glUniform3f(object_color_location, 1.0f, 0.5f, 0.31f); glUniform3f(light_color_location, 1.0f, 1.0f, 1.0); glUniform3f(light_position_location, light_position->data[0], light_position->data[1], light_position->data[2]); glUseProgram(0); glUseProgram(lamp_program); GLuint lamp_model_location, lamp_projection_location, lamp_view_location; lamp_model_location = glGetUniformLocation(lamp_program, "model"); lamp_projection_location = glGetUniformLocation(lamp_program, "perspective"); lamp_view_location = glGetUniformLocation(lamp_program, "view"); glUseProgram(0); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); current_frame = 0.0f; last_frame = 0.0f; last_x = WIDTH / 2.0f; last_y = HEIGHT / 2.0f; fov = M_PI/4; yaw = -M_PI/2; pitch = 0.0f; first_mouse = true; while(!glfwWindowShouldClose(window)) { glfwPollEvents(); current_frame = glfwGetTime(); delta_time = current_frame - last_frame; last_frame = current_frame; do_movement(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(shader_program); // glActiveTexture(GL_TEXTURE0); // glBindTexture(GL_TEXTURE_2D, texture); // cam_x = sinf(time) * radius; // cam_z = cosf(time) * radius; vec3f_add(camera_target, camera_pos, camera_front); mat4f_look_at(view, camera_pos, camera_target, camera_up); mat4f_perspective(projection, fov, (float)WIDTH/(float)HEIGHT, 0.1f, 100.0f); //mat4f_rotate_x(model, sinf(time) * M_PI); glUniformMatrix4fv(model_location, 1, GL_TRUE, mat4f_pointer(model)); glUniformMatrix4fv(view_location, 1, GL_TRUE, mat4f_pointer(view)); glUniformMatrix4fv(projection_location, 1, GL_TRUE, mat4f_pointer(projection)); glUniform3f(view_position_location, camera_pos->data[0], camera_pos->data[1], camera_pos->data[2]); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 36); // glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); glUseProgram(lamp_program); //glUseProgram(shader_program); mat4f_scale(temp, 0.2f, 0.2f, 0.2f); mat4f_mul(temp, temp, model); mat4f_translate_vec3f(temp2, light_position); mat4f_mul(temp2, temp2, temp); //mat4f_print(temp); glUniformMatrix4fv(lamp_model_location, 1, GL_TRUE, mat4f_pointer(temp2)); glUniformMatrix4fv(lamp_view_location, 1, GL_TRUE, mat4f_pointer(view)); glUniformMatrix4fv(lamp_projection_location, 1, GL_TRUE, mat4f_pointer(projection)); glBindVertexArray(lightVAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); glfwSwapBuffers(window); } glfwTerminate(); return(EXIT_SUCCESS); }
int main(void) { if (!glfwInit()) { fprintf(stderr, "Could not initialize GLFW, aborting.\n"); return(EXIT_FAILURE); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); int WIDTH, HEIGHT; WIDTH = 800; HEIGHT = 600; // Define the viewport dimensions glViewport(0, 0, WIDTH, HEIGHT); GLFWwindow* window; window = glfwCreateWindow(WIDTH, HEIGHT, "04 coordinate systems.", NULL, NULL); glfwSetKeyCallback(window, key_callback); if (!window) { fprintf(stderr, "Could not create window, aborting.\n"); return(EXIT_FAILURE); } glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { fprintf(stderr, "Could not initialize GLEW, aborting.\n"); return(EXIT_FAILURE); } glEnable(GL_DEPTH_TEST); GLuint VAO, VBO, EBO; glGenVertexArrays(1 ,&VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(3*sizeof(GLfloat))); glEnableVertexAttribArray(1); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBindVertexArray(0); char *vertex_source = read_shader("shaders/04.vert"); char *fragment_source = read_shader("shaders/04.frag"); GLuint shader_program; shader_program = create_shader_program(vertex_source, fragment_source); glUseProgram(shader_program); GLuint texture; glActiveTexture(GL_TEXTURE0); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); load_texture("textures/02_container.jpg"); glUniform1i(glGetUniformLocation(shader_program, "texture_sampler"), 0); glBindTexture(GL_TEXTURE_2D, 0); float rotation_step; rotation_step = M_PI/6; Mat4f *model, *view, *projection, *rotation, *temp; mat4f_allocate(&model); mat4f_allocate(&view); mat4f_allocate(&projection); mat4f_allocate(&rotation); mat4f_allocate(&temp); mat4f_translate(view, 0.0f, 0.0f, -3.0f); mat4f_perspective(projection, (float)M_PI/4, (float)WIDTH/(float)HEIGHT, 0.1f, 100.0f); GLuint model_location, view_location, projection_location; model_location = glGetUniformLocation(shader_program, "model"); view_location = glGetUniformLocation(shader_program, "view"); projection_location = glGetUniformLocation(shader_program, "projection"); int num_cubes = 10; srand (time(NULL)); Vec4f positions[num_cubes]; float a = 4.0f; for (int i = 0; i < num_cubes; i++) { Vec4f vector; for (int j = 0; j < 4; j++) { if (j == 2) { vector.data[j] = ((float)rand()/(float)(RAND_MAX/1.0f)-2.0f); } else { vector.data[j] = ((float)rand()/(float)(RAND_MAX/a))-2.0f; //vector.data[j] = 1.0f*i*j; } printf("%f, ",vector.data[j]); } printf("\n"); positions[i] = vector; }; glClearColor(1.0f, 0.5f, 1.0f, 1.0f); while(!glfwWindowShouldClose(window)) { glfwPollEvents(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindVertexArray(VAO); float time = glfwGetTime(); for(int i = 0; i < num_cubes; i++) { float *pos = positions[i].data; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); mat4f_rotate_x(rotation, rotation_step*time*(i+1)); mat4f_rotate_y(temp, rotation_step*time*(i+1)); mat4f_mul(rotation, rotation, temp); mat4f_translate(temp, pos[0], pos[1], pos[2]); mat4f_rotate_x(model, -M_PI/4); mat4f_mul(model, model, temp); mat4f_mul(model, model, rotation); glUniformMatrix4fv(model_location, 1, GL_TRUE, mat4f_pointer(model)); glUniformMatrix4fv(view_location, 1, GL_TRUE, mat4f_pointer(view)); glUniformMatrix4fv(projection_location, 1, GL_TRUE, mat4f_pointer(projection)); glDrawArrays(GL_TRIANGLES, 0, 36); glBindTexture(GL_TEXTURE_2D, 0); } //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); glfwSwapBuffers(window); } glfwTerminate(); return EXIT_SUCCESS; }