void init_gl(void) { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); /*glViewport(0, 0, win_x, win_y);*/ perspective = mat4f_perspective(45.0f, (float)win_x / win_y, 0.5f, 1000.0f); orthographic = mat4f_orthographic(0.0f, win_x, win_y, 0.0f, -1.0f, 1.0f); create_vertex_buffer(); init_font(); main_program = compile_shaders("main.vert", "main.frag"); text_program = compile_shaders("text.vert", "text.frag"); iqm_program = compile_shaders("iqm.vert", "iqm.frag"); glUseProgram(main_program); /*initialize main shader variables*/ //~ printf("Map vertex attribute bound to %i\n", attribute_map_position); //~ printf("Map texcoords attribute bound to %i\n", attribute_map_texcoords); //~ printf("Map lightmap coords attribute bound to %i\n", attribute_map_texcoordsLM); glLinkProgram(main_program); /*Must link program before uniforms and after attributes?*/ gWorldLocation = glGetUniformLocation(main_program, "gWorld"); glUniform1i(glGetUniformLocation(main_program, "textureSampler"), /*GLTEXTURE*/0); glUniform1i(glGetUniformLocation(main_program, "lightmapSampler"), /*GLTEXTURE*/1); /*initialize text shader variables*/ /*Not sure if needed... glUseProgram(text_program);*/ glBindAttribLocation(text_program, 0, "coord"); glBindAttribLocation(text_program, 1, "texpos"); ortho_mat = glGetUniformLocation(text_program, "proj_mat"); uniform_color = glGetUniformLocation(text_program, "color"); uniform_alpha_scale = glGetUniformLocation(text_program, "alpha_mod"); glGenBuffers(1, &text_vbo); /*initialize iqm model shader variables*/ glUseProgram(iqm_program); //glLinkProgram(iqm_program); //glUniform1i(glGetUniformLocation(iqm_program, "iqm_sampler"), 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; }