void S_Tick() { // Riktningar och positioner vec4 up; vec4 forward; vec4 pos; mat4x4 inverted = {0}; mat4x4_invert(inverted, V_worldMat); mat4x4_mul_vec4(up, inverted, (vec4) {0, 1, 0, 0}); mat4x4_mul_vec4(forward, inverted, (vec4) {0, 0, -1, 0}); mat4x4_mul_vec4(pos, inverted, (vec4) {0, 0, 0, 1}); float orientation[6]; memcpy(orientation, forward, sizeof(vec3)); memcpy(&orientation[3], up, sizeof(vec3)); alListenerfv(AL_ORIENTATION, orientation); alListenerfv(AL_POSITION, pos); ListEntry* e = S_sources.first; for (int i = 0; i < S_sources.size; i++, e = e->next) { AudioSource* s = e->value; float* p = s->pos; float* v = s->vel; if (s->s != NULL) { p = s->s->pos; v = s->s->vel; } if (s->c != NULL) { p = s->c->pos; memset(v, 0, sizeof(vec3)); } alSourcefv(s->sourceID, AL_POSITION, p); alSourcefv(s->sourceID, AL_VELOCITY, v); } }
static void _norm_point_to_ray(game *g, Ray *r, float x, float y) { vec4 near_point_ndc = {x, y, 1, 1}; vec4 far_point_ndc = {x, y, -1, 1}; mat4x4 inv_mvp; mat4x4_invert(inv_mvp, g->d.mvp); vec4 near_point_world, far_point_world; mat4x4_mul_vec4(near_point_world, inv_mvp, near_point_ndc); mat4x4_mul_vec4(far_point_world, inv_mvp, far_point_ndc); divide_by_w(near_point_world); divide_by_w(far_point_world); vec3 between; vec3_sub(between, far_point_world, near_point_world); Ray ray = { {near_point_world[0], near_point_world[1], near_point_world[2]}, {between[0], between[1], between[2]} }; memcpy(r, &ray, sizeof(ray)); }
void lndr_get_current_bounding_box(Lander *lander, GLfloat *buf) { int i; for(i = 0; i < 4; i += 2) { GLfloat x = bounding_box_data[i]; GLfloat y = bounding_box_data[i + 1]; vec4 pt = {x, y, 0, 0}; vec4 out; mat4x4_mul_vec4(out, lander->mvMatrix, pt); buf[i] = out[0]; buf[i + 1] = out[1]; } }
void AABBTransformAsAABB_SIMD(AABB &aabb, const float4x4 &m) { simd4f minPt = aabb.minPoint; simd4f maxPt = aabb.maxPoint; simd4f centerPoint = muls_ps(add_ps(minPt, maxPt), 0.5f); simd4f newCenter = mat4x4_mul_vec4(m.row, centerPoint); simd4f halfSize = sub_ps(centerPoint, minPt); simd4f x = abs_ps(mul_ps(m.row[0], halfSize)); simd4f y = abs_ps(mul_ps(m.row[1], halfSize)); simd4f z = abs_ps(mul_ps(m.row[2], halfSize)); simd4f w = zero_ps(); simd4f newDir = hadd4_ps(x, y, z, w); aabb.minPoint = sub_ps(newCenter, newDir); aabb.maxPoint = add_ps(newCenter, newDir); }
void AABBTransformAsAABB_SIMD(AABB &aabb, const float4x4 &m) { simd4f minPt = aabb.MinPoint_SSE(); simd4f maxPt = aabb.MaxPoint_SSE(); simd4f centerPoint = _mm_mul_ps(_mm_add_ps(minPt, maxPt), _mm_set1_ps(0.5f)); simd4f halfSize = _mm_sub_ps(centerPoint, minPt); simd4f newCenter = mat4x4_mul_vec4(m.row, centerPoint); simd4f x = abs_ps(_mm_mul_ps(m.row[0], halfSize)); simd4f y = abs_ps(_mm_mul_ps(m.row[1], halfSize)); simd4f z = abs_ps(_mm_mul_ps(m.row[2], halfSize)); simd4f w = _mm_setzero_ps(); _MM_TRANSPOSE4_PS(x, y, z, w); // Contains 2x unpacklo's, 2x unpackhi's, 2x movelh's and 2x movehl's. (or 8 shuffles, depending on the compiler) simd4f newDir = _mm_add_ps(_mm_add_ps(x, y), _mm_add_ps(z, w)); aabb.MinPoint_SSE() = _mm_sub_ps(newCenter, newDir); aabb.MaxPoint_SSE() = _mm_add_ps(newCenter, newDir); }
void lndr_get_current_points(Lander *lander, GLfloat *buf) { int i; for(i = 0; i < 112; i += 2) { GLfloat x = position_data[i]; GLfloat y = position_data[i + 1]; vec4 pt = {x, y, 0, 1}; mat4x4 mat; mat4x4_transpose(mat, lander->mvMatrix); vec4 out; mat4x4_mul_vec4(out, mat, pt); buf[i] = out[0]; buf[i + 1] = out[1]; //printf("%f %f\n", out[0], out[1]); } }
void draw_textured_shape(const TexturedShape* shape, mat4x4 view_projection_matrix, texture_program_type program_type) { if (shape->params.alpha>0 && (fabs(shape->params.scale.x)>0 && fabs(shape->params.scale.y)>0 && fabs(shape->params.scale.z)>0)) { mat4x4 model_view_projection_matrix; mvp_matrix(model_view_projection_matrix, shape->params, view_projection_matrix); if (shape->params.const_params.is_star==1) { vec4 pos; vec4 vertex = {0,0,0,1}; mat4x4_mul_vec4(pos, model_view_projection_matrix, vertex); vec4 p_NDC = {pos[0]/pos[3], pos[1]/pos[3], pos[2]/pos[3], pos[3]/pos[3]}; // p_window = (p_NDC + 1)/2 * viewport.{width, height} + viewport{x, y} //vec4 p_window={p_NDC[0]*width, -p_NDC[1]*height, 0, 0}; vec4 p_window= {p_NDC[0]*width, -p_NDC[1]*height, 0, 0}; int d = 160; if (fabs(p_window[0])>d || p_window[1] > y_offset_absolute*2 + d || p_window[1] < y_offset_absolute*2 - d) { return; } } if (program_type==RED) { texture_program_temp=&texture_program_red; } else if (program_type==BLUE) { texture_program_temp=&texture_program_blue; } else if (program_type==LIGHT_RED) { texture_program_temp=&texture_program_light_red; } else if (program_type==LIGHT_BLUE) { texture_program_temp=&texture_program_light_blue; } else if (program_type==NORMAL_ONE) { texture_program_temp=&texture_program_one; } else { texture_program_temp=&texture_program; } glUseProgram(texture_program_temp->program); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, shape->texture); glUniformMatrix4fv(texture_program_temp->u_mvp_matrix_location, 1, GL_FALSE, (GLfloat*)model_view_projection_matrix); glUniform1i(texture_program_temp->u_texture_unit_location, 0); glUniform1f(texture_program_temp->u_alpha_loaction, shape->params.alpha); glBindBuffer(GL_ARRAY_BUFFER, shape->buffer); // glVertexAttribPointer (GLuint indx, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* ptr) glVertexAttribPointer(texture_program_temp->a_position_location, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GL_FLOAT), BUFFER_OFFSET(0)); glVertexAttribPointer(texture_program_temp->a_texture_coordinates_location, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GL_FLOAT), BUFFER_OFFSET(2 * sizeof(GL_FLOAT))); glEnableVertexAttribArray(texture_program_temp->a_position_location); glEnableVertexAttribArray(texture_program_temp->a_texture_coordinates_location); glDrawArrays(shape->params.const_params.triangle_mode, 0, shape->num_points); glBindBuffer(GL_ARRAY_BUFFER, 0); } }
int main(void) { Primative *edits[] = { primative_create(CUBE, ADDITIVE), primative_create(SPHERE, ADDITIVE), primative_create(SPHERE, SUBTRACTIVE), primative_create(SPHERE, ADDITIVE) }; primative_scale(edits[0], 0.4, 1.0, 1.5); primative_translate(edits[1], 0.2, 0, 0); primative_translate(edits[2], -0.3, 0, 0); primative_translate(edits[3], -0.5, 0, 0); primative_scale(edits[3], 0.5, 0.5, 0.5); edits[1]->blend = 0.2; sdf_args.edits = edits; sdf_args.num_edits = 4; vec3 origin = {0, 0, 0}; vec3 halfDim = {1, 1, 1}; OctTree *tree = octTree_create(origin, halfDim); octTree_populate(tree, distance); int num_verticies = octTree_count(tree); size_t vert_size = sizeof(Point); fprintf(stdout, "num verts %d\n", num_verticies); int vi = 0; Point *verticies = malloc(num_verticies * vert_size); octTree_get_points(tree, verticies, &vi); GLFWwindow* window; mat4x4 ident; mat4x4_identity(ident); glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); window = glfwCreateWindow(640, 480, "speed dream", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); glfwSwapInterval(1); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glfwSetKeyCallback(window, key_callback); mat4x4 proj; mat4x4 view; mat4x4 matrix; mat4x4_identity(view); mat4x4_identity(matrix); vec3_set(camera.pos, 0.0f, 0.0f, 0.0f); // Create Vertex Array Object GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); // Create a Vertex Buffer Object and copy the vertex data to it GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, num_verticies * vert_size, verticies, GL_STATIC_DRAW); // Create and compile the shaders GLuint vertexShader = loadShader(GL_VERTEX_SHADER, DREAM_VERT); GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, DREAM_FRAG); // Link the vertex and fragment shader into a shader program GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glBindFragDataLocation(shaderProgram, 0, "outColor"); glLinkProgram(shaderProgram); glUseProgram(shaderProgram); // Specify the layout of the vertex data GLint posAttrib = glGetAttribLocation(shaderProgram, "position"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, 0); GLint normAttrib = glGetAttribLocation(shaderProgram, "normal"); glEnableVertexAttribArray(normAttrib); glVertexAttribPointer(normAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, (void*)(3 * sizeof(GLfloat))); GLint colorAttrib = glGetAttribLocation(shaderProgram, "color"); glEnableVertexAttribArray(colorAttrib); glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, (void*)(6 * sizeof(GLfloat))); GLint uniModel = glGetUniformLocation(shaderProgram, "model"); GLint uniView = glGetUniformLocation(shaderProgram, "view"); GLint uniProj = glGetUniformLocation(shaderProgram, "proj"); vec3 lightPos = {3.f, 0.f, 3.f}; GLint uniLightPos = glGetUniformLocation(shaderProgram, "lightPos"); glUniform3fv(uniLightPos, 1, (const GLfloat *)&lightPos); while (!glfwWindowShouldClose(window)) { float ratio; int width, height; glfwGetFramebufferSize(window, &width, &height); ratio = width / (float) height; glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); vec4 movement = {0, 0, 0, 1}; if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) movement[0] -= 0.1; if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) movement[0] += 0.1; if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) movement[2] += 0.1; if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) movement[2] -= 0.1; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.pitch -= 0.05; if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) camera.pitch += 0.05; if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) camera.yaw += 0.05; if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) camera.yaw -= 0.05; mat4x4 pitch_mat; mat4x4_identity(pitch_mat); mat4x4_rotate_X(pitch_mat, pitch_mat, camera.pitch); mat4x4 yaw_mat; mat4x4_identity(yaw_mat); mat4x4_rotate_Y(yaw_mat, yaw_mat, camera.yaw); mat4x4 inv_yaw_mat; mat4x4_invert(inv_yaw_mat, yaw_mat); vec4 rotated_movement; mat4x4_mul_vec4(rotated_movement, inv_yaw_mat, movement); camera.pos[0] += rotated_movement[0]; camera.pos[1] += rotated_movement[1]; camera.pos[2] += rotated_movement[2]; mat4x4 translation_mat; mat4x4_translate(translation_mat, camera.pos[0], camera.pos[1], camera.pos[2]); mat4x4_mul(view, pitch_mat, yaw_mat); mat4x4_mul(view, view, translation_mat); glUniformMatrix4fv(uniView, 1, GL_FALSE, (const GLfloat *)&view); mat4x4_perspective(proj, 60 * M_PI / 180, ratio, 0.1f, 10.0f); glUniformMatrix4fv(uniProj, 1, GL_FALSE, (const GLfloat *)&proj); glUniformMatrix4fv(uniModel, 1, GL_FALSE, (const GLfloat *)&matrix); glDrawArrays(GL_POINTS, 0, num_verticies); glfwSwapBuffers(window); glfwPollEvents(); } glDeleteProgram(shaderProgram); glDeleteShader(fragmentShader); glDeleteShader(vertexShader); glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); glfwDestroyWindow(window); free(verticies); glfwTerminate(); exit(EXIT_SUCCESS); }