Changeset b220f78 in opengl-game for new-game.cpp
- Timestamp:
- Mar 22, 2019, 3:36:04 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- de53394
- Parents:
- 49db5fc
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r49db5fc rb220f78 107 107 108 108 // TODO: Why not just use an initializer list for all the instance variables 109 // TODO: Maybe pass in startTime instead of calling glfwGetTime() here 109 110 EffectOverTime(float& effectedValue, float changePerSecond, SceneObject* object) 110 111 : effectedValue(effectedValue), changePerSecond(changePerSecond), effectedObject(object) { … … 186 187 void initModelGroupAttribs(ShaderModelGroup& modelGroup); 187 188 void bindUniformData(AttribInfo& attrib); 189 void bindUniformData(AttribInfo& attrib, GLfloat* data); 188 190 189 191 size_t GLsizeof(GLenum); … … 201 203 GLuint* model_mat_idx_vbo); 202 204 203 void initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view,205 void initializeParticleEffectBuffers(vec3 origin, 204 206 map<GLuint, BufferInfo>& shaderBufferInfo, 205 207 map<ObjectType, ShaderModelGroup>& modelGroups, … … 478 480 479 481 GLfloat laserColor[3] = {0.2f, 1.0f, 0.2f}; 482 GLfloat curTime, prevTime, elapsedTime; 480 483 481 484 GLuint points_vbo, colors_vbo, texcoords_vbo, normals_vbo, ubo, model_mat_idx_vbo; … … 600 603 shaderBufferInfo[modelGroups[TYPE_EXPLOSION].shaderProgram] = BufferInfo(); // temporary 601 604 605 // The last parameter (offset) is only used for populating the buffers since the distance 606 // between each item is needed there. However, that code isn't run for explosions right now anyway, 607 // so I may as well pass in 0 here. 608 defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "v_i", ATTRIB_POINT_VARYING, 609 3, GL_FLOAT, 0); 610 defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "start_time", ATTRIB_POINT_VARYING, 611 1, GL_FLOAT, 0); 612 613 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "explosion_start_time", ATTRIB_UNIFORM, 614 1, UNIFORM_1F, &curTime); 615 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "cur_time", ATTRIB_UNIFORM, 616 1, UNIFORM_1F, &curTime); 617 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "model_mat", ATTRIB_UNIFORM, 618 1, UNIFORM_MATRIX_4F, NULL); 619 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "view", ATTRIB_UNIFORM, 620 1, UNIFORM_MATRIX_4F, value_ptr(view_mat)); 621 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "proj", ATTRIB_UNIFORM, 622 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat)); 623 // Still need to do the model mat 624 625 initModelGroupAttribs(modelGroups[TYPE_EXPLOSION]); 626 602 627 cam_pos = vec3(0.0f, 0.0f, 2.0f); 603 628 float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f; … … 651 676 proj_mat = make_mat4(proj_arr); 652 677 653 initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f), proj_mat, view_mat,678 initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f), 654 679 shaderBufferInfo, 655 680 modelGroups, … … 703 728 GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_LASER].shaderProgram, "models"); 704 729 705 GLuint explosion_start_time_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "explosion_start_time");706 GLuint cur_time_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "cur_time");707 730 GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_EXPLOSION].shaderProgram, "models"); 708 731 … … 746 769 double elapsed_seconds_fps = 0.0f; 747 770 double elapsed_seconds_spawn = 0.0f; 748 double previous_seconds = glfwGetTime(); 771 772 prevTime = glfwGetTime(); 749 773 750 774 // This draws wireframes. Useful for seeing separate faces and occluded objects. … … 757 781 758 782 while (!glfwWindowShouldClose(window) && isRunning) { 759 double current_seconds= glfwGetTime();760 double elapsed_seconds = current_seconds - previous_seconds;783 curTime = glfwGetTime(); 784 elapsedTime = curTime - prevTime; 761 785 762 786 // temporary code to get around vsync issue in OSX Sierra 763 if (elapsed _seconds< (1.0f / TARGET_FPS)) {787 if (elapsedTime < (1.0f / TARGET_FPS)) { 764 788 continue; 765 789 } 766 790 767 prev ious_seconds = current_seconds;768 769 elapsed_seconds_fps += elapsed _seconds;791 prevTime = curTime; 792 793 elapsed_seconds_fps += elapsedTime; 770 794 if (elapsed_seconds_fps > 0.25f) { 771 795 fps = (double)frame_count / elapsed_seconds_fps; … … 806 830 if (curState == STATE_GAME) { 807 831 808 elapsed_seconds_spawn += elapsed _seconds;832 elapsed_seconds_spawn += elapsedTime; 809 833 if (elapsed_seconds_spawn > 0.5f) { 810 834 SceneObject* obj = createAsteroid(vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))); … … 918 942 919 943 objExplosion->model_mat = model_mat; 920 GLfloat curTime = glfwGetTime();921 944 922 945 // initiate an explosion 923 946 glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram); 924 947 925 GLuint model_mat_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "model_mat"); 926 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objExplosion->model_mat)); 927 928 glUniform1f(explosion_start_time_loc, (GLfloat)glfwGetTime()); 948 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["explosion_start_time"]); 949 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["model_mat"], value_ptr(objExplosion->model_mat)); 929 950 } 930 951 } … … 945 966 } else { 946 967 EffectOverTime* eot = *it; 947 eot->effectedValue = eot->startValue + (cur rent_seconds- eot->startTime) * eot->changePerSecond;968 eot->effectedValue = eot->startValue + (curTime - eot->startTime) * eot->changePerSecond; 948 969 949 970 it++; … … 955 976 } 956 977 957 float dist = cam_speed * elapsed _seconds;978 float dist = cam_speed * elapsedTime; 958 979 if (key_down[GLFW_KEY_A]) { 959 980 vec3 dir = vec3(inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f)); … … 982 1003 /* 983 1004 if (key_down[GLFW_KEY_LEFT]) { 984 cam_yaw += cam_yaw_speed * elapsed _seconds;1005 cam_yaw += cam_yaw_speed * elapsedTime; 985 1006 cam_moved = true; 986 1007 } 987 1008 if (key_down[GLFW_KEY_RIGHT]) { 988 cam_yaw -= cam_yaw_speed * elapsed _seconds;1009 cam_yaw -= cam_yaw_speed * elapsedTime; 989 1010 cam_moved = true; 990 1011 } 991 1012 if (key_down[GLFW_KEY_UP]) { 992 cam_pitch += cam_pitch_speed * elapsed _seconds;1013 cam_pitch += cam_pitch_speed * elapsedTime; 993 1014 cam_moved = true; 994 1015 } 995 1016 if (key_down[GLFW_KEY_DOWN]) { 996 cam_pitch -= cam_pitch_speed * elapsed _seconds;1017 cam_pitch -= cam_pitch_speed * elapsedTime; 997 1018 cam_moved = true; 998 1019 } … … 1017 1038 1018 1039 glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram); 1019 glUniform1f(cur_time_loc, (GLfloat)current_seconds);1040 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["cur_time"]); 1020 1041 1021 1042 // Render scene … … 2032 2053 break; 2033 2054 case UNIFORM_1F: 2034 glUniform 3fv(attrib.buffer, attrib.size, attrib.data);2055 glUniform1fv(attrib.buffer, attrib.size, attrib.data); 2035 2056 break; 2036 2057 case UNIFORM_3F: 2037 2058 glUniform3fv(attrib.buffer, attrib.size, attrib.data); 2059 break; 2060 case UNIFORM_NONE: 2061 break; 2062 } 2063 } 2064 2065 void bindUniformData(AttribInfo& attrib, GLfloat *data) { 2066 switch(attrib.uniType) { 2067 case UNIFORM_MATRIX_4F: 2068 glUniformMatrix4fv(attrib.buffer, attrib.size, GL_FALSE, data); 2069 break; 2070 case UNIFORM_1F: 2071 glUniform1fv(attrib.buffer, attrib.size, data); 2072 break; 2073 case UNIFORM_3F: 2074 glUniform3fv(attrib.buffer, attrib.size, data); 2075 break; 2076 case UNIFORM_NONE: 2038 2077 break; 2039 2078 } … … 2097 2136 } 2098 2137 2099 void initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view,2138 void initializeParticleEffectBuffers(vec3 origin, 2100 2139 map<GLuint, BufferInfo>& shaderBufferInfo, 2101 2140 map<ObjectType, ShaderModelGroup>& modelGroups, … … 2125 2164 glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram); 2126 2165 2127 GLuint proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "proj"); 2128 GLuint view_mat_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "view"); 2129 2130 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj)); 2131 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view)); 2132 2133 GLuint model_mat_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "model_mat"); 2134 2135 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat)); 2166 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["proj"]); 2167 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["view"]); 2168 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["model_mat"], value_ptr(model_mat)); 2136 2169 2137 2170 GLuint velocity_vbo; 2138 2171 glGenBuffers(1, &velocity_vbo); 2172 2173 GLuint time_vbo; 2174 glGenBuffers(1, &time_vbo); 2175 2176 glBindVertexArray(modelGroups[TYPE_EXPLOSION].vao); 2177 2178 // the glBufferData and glVertexAttribPointer need to stay here while the corresponding arrays 2179 // are local to this function. Once they're made part of the explosion object, I might be able 2180 // to move this code out of here 2181 2139 2182 glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo); 2140 2183 glBufferData(GL_ARRAY_BUFFER, sizeof(vv), vv, GL_STATIC_DRAW); 2141 2142 GLuint time_vbo; 2143 glGenBuffers(1, &time_vbo); 2184 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 2185 2144 2186 glBindBuffer(GL_ARRAY_BUFFER, time_vbo); 2145 2187 glBufferData(GL_ARRAY_BUFFER, sizeof(vt), vt, GL_STATIC_DRAW); 2146 2147 glBindVertexArray(modelGroups[TYPE_EXPLOSION].vao);2148 2149 glEnableVertexAttribArray(0);2150 glEnableVertexAttribArray(1);2151 2152 glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo);2153 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);2154 2155 glBindBuffer(GL_ARRAY_BUFFER, time_vbo);2156 2188 glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, NULL); 2157 2189 … … 2772 2804 2773 2805 float getRandomNum(float low, float high) { 2774 return low + ((float)rand() /RAND_MAX) * (high-low);2775 } 2806 return low + ((float)rand() / RAND_MAX) * (high-low); 2807 }
Note:
See TracChangeset
for help on using the changeset viewer.