Changeset f7d35da in opengl-game


Ignore:
Timestamp:
Jun 10, 2018, 9:31:48 PM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
5c403fe
Parents:
0d5c100
Message:

Add a system to keep track of which keys are pressed or held down and add another light source to help illuminate the scene from different directions.

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • color.frag

    r0d5c100 rf7d35da  
    11#version 410
    22
    3 in vec3 position_eye, normal_eye, color, light_position_eye;
     3in vec3 position_eye, normal_eye, color, light_position_eye, light2_position_eye;
    44
    55out vec4 frag_color;
     
    2424
    2525  // diffuse intensity
    26   vec3 Id = Ls * color * dot_prod;
     26  vec3 Id = Ld * color * dot_prod;
     27
     28  vec3 direction_to_light2_eye = normalize(light2_position_eye - position_eye);
     29  float dot_prod2 = max(dot(direction_to_light2_eye, normal_eye), 0.0);
     30
     31  // diffuse intensity
     32  vec3 Id2 = Ld * color * dot_prod2;
     33
     34  vec3 surface_to_viewer_eye = normalize(-position_eye);
    2735
    2836  vec3 reflection_eye = reflect(-direction_to_light_eye, normal_eye);
    29   vec3 surface_to_viewer_eye = normalize(-position_eye);
    3037  float dot_prod_specular = max(dot(reflection_eye, surface_to_viewer_eye), 0.0);
    3138  float specular_factor = pow(dot_prod_specular, specular_exponent);
    3239
     40  vec3 reflection_eye2 = reflect(-direction_to_light2_eye, normal_eye);
     41  float dot_prod_specular2 = max(dot(reflection_eye2, surface_to_viewer_eye), 0.0);
     42  float specular_factor2 = pow(dot_prod_specular2, specular_exponent);
     43
    3344  // specular intensity
    3445  vec3 Is = Ls * Ks * specular_factor;
     46  vec3 Is2 = Ls * Ks * specular_factor2;
    3547
    36   frag_color = vec4(Is + Id + Ia, 1.0);
     48  frag_color = vec4(Is + Id + Ia + Is2 + Id2, 1.0);
    3749}
  • color.vert

    r0d5c100 rf7d35da  
    1414layout(location = 3) in uint ubo_index;
    1515
    16 out vec3 position_eye, normal_eye, color, light_position_eye;
     16out vec3 position_eye, normal_eye, color, light_position_eye, light2_position_eye;
    1717
    1818// fixed point light position
    1919vec3 light_position_world = vec3(0.0, 0.0, 2.0);
     20vec3 light2_position_world = vec3(0.0, -3.0, -2.0);
    2021
    2122void main() {
     
    2425  color = vertex_color;
    2526  light_position_eye = vec3(view * vec4(light_position_world, 1.0));
     27  light2_position_eye = vec3(view * vec4(light2_position_world, 1.0));
    2628
    2729  gl_Position = proj * vec4(position_eye, 1.0);
  • new-game.cpp

    r0d5c100 rf7d35da  
    3636using namespace std;
    3737using namespace glm;
    38 
    39 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
    4038
    4139struct SceneObject {
     
    6361};
    6462
     63#define NUM_KEYS (512)
     64#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 (maybe make this a const instead)
     65
     66const int KEY_STATE_UNCHANGED = -1;
    6567const bool FULLSCREEN = false;
    66 const bool SHOW_FPS = true;
    67 const bool DISABLE_VSYNC = true; // disable vsync to see real framerate
    68 unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime
     68const bool SHOW_FPS = false;
     69const bool DISABLE_VSYNC = false; // disable vsync to see real framerate
     70unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const
     71
     72int key_state[NUM_KEYS];
     73bool key_pressed[NUM_KEYS];
    6974
    7075int width = 640;
     
    95100
    96101void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
     102void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
    97103
    98104bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point);
     
    212218
    213219   glfwSetMouseButtonCallback(window, mouse_button_callback);
     220   glfwSetKeyCallback(window, key_callback);
    214221
    215222   const GLubyte* renderer = glGetString(GL_RENDERER);
     
    388395      1.0f, 1.0f,
    389396      0.0f, 0.0f,
    390       1.0f, 0.0f
     397      1.0f, 0.0f,
    391398   };
    392399   obj.selected_colors = {
     
    399406   };
    400407
    401    T_model = translate(mat4(), vec3(0.0f, -0.65f, 0.0f));
     408   T_model = translate(mat4(), vec3(0.0f, -0.9f, 0.0f));
    402409   R_model = rotate(mat4(), -1.0f, vec3(1.0f, 0.0f, 0.0f));
    403    obj.model_mat = T_model * R_model;
     410   obj.model_mat = T_model; //T_model * R_model;
    404411
    405412   addObjectToScene(obj);
     
    463470   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
    464471
    465    float speed = 1.0f;
    466    float last_position = 0.0f;
    467 
    468472   float cam_speed = 1.0f;
    469473   float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
     
    553557      }
    554558
    555       if (fabs(last_position) > 1.0f) {
    556          speed = -speed;
    557       }
    558 
    559       // Handle events (Ideally, move all event-handling code
    560       // before the render code)
     559      // Handle events
    561560
    562561      clickedObject = NULL;
     562
     563      // reset the all key states to KEY_STATE_UNCHANGED (something the GLFW key callback can never return)
     564      // so that GLFW_PRESS and GLFW_RELEASE are only detected once
     565      // TODO: Change this if we ever need to act on GLFW_REPEAT (which is when a key is held down continuously)
     566      fill(key_state, key_state + NUM_KEYS, KEY_STATE_UNCHANGED);
     567
    563568      glfwPollEvents();
    564569
     
    585590            selectedObject = &objects[1];
    586591         }
    587       }
    588 
    589       /*
    590       model[12] = last_position + speed*elapsed_seconds;
    591       last_position = model[12];
    592       */
     592
     593         /*
     594         if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
     595            //transformObject(objects[1], translate(mat4(), vec3(0.3f, 0.0f, 0.0f)), ubo);
     596         }
     597         if (key_pressed[GLFW_KEY_RIGHT]) {
     598            transformObject(objects[2], translate(mat4(), vec3(0.01f, 0.0f, 0.0f)), ubo);
     599         }
     600         if (key_pressed[GLFW_KEY_LEFT]) {
     601            transformObject(objects[2], translate(mat4(), vec3(-0.01f, 0.0f, 0.0f)), ubo);
     602         }
     603         */
     604      }
     605
     606      // Render scene
    593607
    594608      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    621635         cam_pos.x -= cos(cam_yaw)*dist;
    622636         cam_pos.z += sin(cam_yaw)*dist;
     637
    623638         cam_moved = true;
    624639      }
     
    626641         cam_pos.x += cos(cam_yaw)*dist;
    627642         cam_pos.z -= sin(cam_yaw)*dist;
     643
    628644         cam_moved = true;
    629645      }
     
    631647         cam_pos.x -= sin(cam_yaw)*dist;
    632648         cam_pos.z -= cos(cam_yaw)*dist;
     649
    633650         cam_moved = true;
    634651      }
     
    636653         cam_pos.x += sin(cam_yaw)*dist;
    637654         cam_pos.z += cos(cam_yaw)*dist;
     655
    638656         cam_moved = true;
    639657      }
     
    649667         T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    650668         R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
     669
    651670         view_mat = R*T;
    652671
     
    720739      if (closest_object == NULL) {
    721740         cout << "No object was clicked" << endl;
    722       }
    723       else {
     741      } else {
    724742         clickedObject = closest_object;
    725743         cout << "Clicked object: " << clickedObject->id << endl;
     
    727745   }
    728746}
     747
     748void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
     749   key_state[key] = action;
     750
     751   // should be true for GLFW_PRESS and GLFW_REPEAT
     752   key_pressed[key] = (action != GLFW_RELEASE);
     753}
     754
    729755
    730756GLuint loadShader(GLenum type, string file) {
     
    819845   vec3 normal = vec3(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
    820846
    821    print4DVector("Full world ray", world_ray);
    822 
    823847   vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
    824848   vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
     
    827851
    828852   float d = -glm::dot(points[0], normal);
    829    cout << "d: " << d << endl;
    830 
    831853   float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
    832    cout << "t: " << t << endl;
    833854
    834855   vec3 intersection = local_cam + t*local_ray;
    835    printVector("Intersection", intersection);
    836856
    837857   if (insideTriangle(intersection, points)) {
     
    842862   }
    843863}
     864
    844865bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
    845866   vec3 v21 = triangle_points[1] - triangle_points[0];
  • texture.frag

    r0d5c100 rf7d35da  
    2929
    3030  // diffuse intensity
    31   vec3 Id = Ls * vec3(texel) * dot_prod;
     31  vec3 Id = Ld * vec3(texel) * dot_prod;
    3232
    3333  vec3 reflection_eye = reflect(-direction_to_light_eye, normal_eye);
Note: See TracChangeset for help on using the changeset viewer.