void update_matrix_3d( float *matrix, float x, float y, float z, float rx, float ry) { float a[16]; float b[16]; int width, height; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); float aspect = (float)width / height; mat_identity(a); mat_translate(b, -x, -y, -z); mat_multiply(a, b, a); mat_rotate(b, cosf(rx), 0, sinf(rx), ry); mat_multiply(a, b, a); mat_rotate(b, 0, 1, 0, -rx); mat_multiply(a, b, a); if (ortho) { int size = 32; mat_ortho(b, -size * aspect, size * aspect, -size, size, -256, 256); } else { mat_perspective(b, fov, aspect, 0.1, 1024.0); } mat_multiply(a, b, a); mat_identity(matrix); mat_multiply(matrix, a, matrix); }
void set_matrix_3d( float *matrix, int width, int height, float x, float y, float z, float rx, float ry, float fov, int ortho) { float a[16]; float b[16]; float aspect = (float)width / height; mat_identity(a); mat_translate(b, -x, -y - 0.1, -z); mat_multiply(a, b, a); mat_rotate(b, cosf(rx), 0, sinf(rx), ry); mat_multiply(a, b, a); mat_rotate(b, 0, 1, 0, -rx); mat_multiply(a, b, a); if (ortho) { int size = 32; mat_ortho(b, -size * aspect, size * aspect, -size, size, -256, 256); } else { mat_perspective(b, fov, aspect, 1 / 8.0, 256.0); } mat_multiply(a, b, a); mat_identity(matrix); mat_multiply(matrix, a, matrix); }
void set_matrix_3d( float *matrix, int width, int height, float x, float y, float z, float rx, float ry, float fov, int ortho, int radius) { float a[16]; float b[16]; float aspect = (float)width / height; float znear = 0.125; float zfar = radius * 32 + 64; mat_identity(a); mat_translate(b, -x, -y, -z); mat_multiply(a, b, a); mat_rotate(b, cosf(rx), 0, sinf(rx), ry); mat_multiply(a, b, a); mat_rotate(b, 0, 1, 0, -rx); mat_multiply(a, b, a); if (ortho) { int size = ortho; mat_ortho(b, -size * aspect, size * aspect, -size, size, -zfar, zfar); } else { mat_perspective(b, fov, aspect, znear, zfar); } mat_multiply(a, b, a); mat_identity(matrix); mat_multiply(matrix, a, matrix); }
void update_matrix_3d( float *matrix, float x, float y, float z, float rx, float ry) { float a[16]; float b[16]; int width, height; glfwGetWindowSize(&width, &height); glViewport(0, 0, width, height); float aspect = (float)width / height; mat_identity(a); mat_translate(b, -x, -y, -z); mat_multiply(a, b, a); mat_rotate(b, cosf(rx), 0, sinf(rx), ry); mat_multiply(a, b, a); mat_rotate(b, 0, 1, 0, -rx); mat_multiply(a, b, a); if (ortho) { int size = 32; mat_ortho(b, -size * aspect, size * aspect, -size, size, -256, 256); } else { mat_perspective(b, 65.0, aspect, 0.1, 1024.0); } mat_multiply(a, b, a); for (int i = 0; i < 16; i++) { matrix[i] = a[i]; } }
void ParticleRenderer::display() { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glDepthMask(GL_FALSE); glUseProgram(m_programSprites); // Set modelview and projection matricies GLint h_ModelViewMatrix = glGetUniformLocation(m_programSprites, "modelview"); GLint h_ProjectionMatrix = glGetUniformLocation(m_programSprites, "projection"); matrix4 modelview; matrix4 projection; mat_identity(modelview); mat_identity(projection); mat_translate(modelview, m_camera); mat_perspective(projection, 60, (float)m_windowWidth / (float)m_windowHeight, 0.1, 1000.0); glUniformMatrix4fv(h_ModelViewMatrix, 1, GL_FALSE, (GLfloat*)modelview); glUniformMatrix4fv(h_ProjectionMatrix, 1, GL_FALSE, (GLfloat*)projection); // Set point size GLint h_PointSize = glGetUniformLocation(m_programSprites, "size"); glUniform1f(h_PointSize, m_spriteSize); // Set base and secondary colors GLint h_BaseColor = glGetUniformLocation(m_programSprites, "baseColor"); GLint h_SecondaryColor = glGetUniformLocation(m_programSprites, "secondaryColor"); glUniform4f(h_BaseColor, 1.0, 1.0, 1.0, 1.0); glUniform4f(h_SecondaryColor, m_baseColor[0], m_baseColor[1], m_baseColor[2], m_baseColor[3]); // Set position coords GLint h_position = glGetAttribLocation(m_programSprites, "a_position"); glBindBuffer(GL_ARRAY_BUFFER, m_pbo); glEnableVertexAttribArray(h_position); glVertexAttribPointer(h_position, 4, GL_FLOAT, GL_FALSE, 0, 0); GLuint texLoc = glGetUniformLocation(m_programSprites, "splatTexture"); glUniform1i(texLoc, 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_texture); glDrawArrays(GL_POINTS, 0, m_numParticles); glDisableVertexAttribArray(h_position); glUseProgram(0); glDisable(GL_BLEND); glDepthMask(GL_TRUE); }
void SimpleGLScene::resize(int width, int height) { sceneWidth = width, sceneHeight = height; glViewport(0, 0, width, height); projection = mat_perspective(60, width / (double) height, 1.0, 200.0); renderedColorTexture.allocate(width, height); renderedColorTexture.setParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); renderedColorTexture.setParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); renderedColorTexture.setParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); renderedColorTexture.setParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); renderedDepthTexture.allocate(width, height); renderedDepthTexture.setParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); renderedDepthTexture.setParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); renderedDepthTexture.setParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); renderedDepthTexture.setParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); framebuffer.attach( GL_COLOR_ATTACHMENT0, renderedColorTexture, GL_DEPTH_ATTACHMENT, renderedDepthTexture); GL_CHECK_ERROR(); }
void * glclient_thread(void * arg) { server_thread_args_t * a = (server_thread_args_t *)arg; static graphics_context_t gc; static struct js_event joy; int joy_fd; static char button[32]; glclient_context_t *glcc = a->user_context_ptr; joy_fd = open(glcc->joy_dev, O_RDONLY); if (joy_fd == -1) { printf("Error: Joystick device open\n"); } if (joy_fd != -1) { fcntl(joy_fd, F_SETFL, O_NONBLOCK); } gls_init(a); gls_cmd_get_context(); gc.screen_width = glsc_global.screen_width; gc.screen_height = glsc_global.screen_height; printf("width:%d height:%d\n",glsc_global.screen_width,glsc_global.screen_height); init_gl(&gc); float aspect = (float)gc.screen_width / (float)gc.screen_height; mat_perspective(proj_mat, aspect, 0.1f, 1024.0f, 60.0f); glUniform4fv(gc.uloc_light, 1, light_pos); srand(0x12345678); int j; for (j = 0; j < 1024; j++) { obj[j].z = randf() * 8.0f - 10.0f; obj[j].x = (randf() * 2.0f - 1.0f) * obj[j].z; obj[j].y = (randf() * 1.0f - 0.5f) * obj[j].z; obj[j].dx = randf() * 0.0f - 0.0f; obj[j].dy = randf() * 0.0f - 0.0f; obj[j].dz = randf() * 0.0f - 0.0f; obj[j].rx = randf() * 6.28; obj[j].ry = randf() * 6.28; obj[j].rz = randf() * 6.28; obj[j].drx = randf() * 0.1f - 0.05f; obj[j].dry = randf() * 0.1f - 0.05f; obj[j].drz = randf() * 0.1f - 0.05f; } float x = 1.57f; float y = 0.0f; float z = -2.0f; int k = 1; int i; for (i = 0; i < 432000; i++) { struct timeval times, timee; gettimeofday(×, NULL); if (joy_fd != -1) { while (read(joy_fd, &joy, sizeof(struct js_event)) == sizeof(struct js_event)) { if ((joy.type & JS_EVENT_BUTTON) == JS_EVENT_BUTTON) { button[joy.number] = joy.value; } } if (button[4] > 0) { y += 0.01f; } if (button[6] > 0) { y += -0.01f; } if (button[5] > 0) { x += 0.01f * aspect; } if (button[7] > 0) { x += -0.01f * aspect; } if (button[12] > 0) { z += -0.01f; } if (button[13] > 0) { k++; k = (k > 45) ? 45 : k; } if (button[14] > 0) { z += 0.01f; } if (button[15] > 0) { k--; k = (k < 1) ? 1 : k; } } glUseProgram(gc.program); glBindBuffer(GL_ARRAY_BUFFER, gc.vbo_pos); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gc.vbo_ind); glEnableVertexAttribArray(gc.vloc_pos); glEnableVertexAttribArray(gc.vloc_nor); glEnableVertexAttribArray(gc.vloc_tex); glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (j = 0; j < k; j++) { obj[j].rx += obj[j].drx; obj[j].ry += obj[j].dry; obj[j].rz += obj[j].drz; if (j == 0) { obj[j].x = 0.0f; obj[j].y = 0.0f; obj[j].z = z; obj[j].rx = -y; obj[j].ry = x; obj[j].rz = 0.0f; } mat_identity(model_mat); mat_translate(model_mat, obj[j].x, obj[j].y, obj[j].z); mat_rotate_x(model_mat, obj[j].rx); mat_rotate_y(model_mat, obj[j].ry); mat_rotate_z(model_mat, obj[j].rz); mat_copy(nor_mat, model_mat); mat_invert(nor_mat); mat_transpose(nor_mat); glUniformMatrix4fv(gc.uloc_nor, 1, GL_FALSE, nor_mat); mat_copy(obj[j].nor_mat, nor_mat); mat_copy(modelproj_mat, proj_mat); mat_mul(modelproj_mat, model_mat); glUniformMatrix4fv(gc.uloc_model, 1, GL_FALSE, modelproj_mat); mat_copy(obj[j].modelproj_mat, modelproj_mat); glDrawElements(GL_TRIANGLES, sizeof(ind_model) / sizeof(GLushort), GL_UNSIGNED_SHORT, 0); } glDisableVertexAttribArray(gc.vloc_tex); glDisableVertexAttribArray(gc.vloc_nor); glDisableVertexAttribArray(gc.vloc_pos); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); gls_cmd_flip(i); gettimeofday(&timee, NULL); //printf("%d:%f ms ", i, get_diff_time(times, timee) * 1000.0f); } glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gls_cmd_flip(i); release_gl(&gc); gls_free(); if (joy_fd != -1) { close(joy_fd); } pthread_exit(NULL); }