void glview_close(glview * view) { if (view) { release_gl(view); free(view); } }
static plt_bool create(glview * view) { #define PLOTTER_WINDOW_CLASS_NAME L"Plotter Window" HINSTANCE instance = GetModuleHandle( NULL ); WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = glview_win_proc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = instance; wc.hIcon = LoadIcon( NULL, IDI_WINLOGO ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = NULL; wc.lpszMenuName = 0; wc.lpszClassName = PLOTTER_WINDOW_CLASS_NAME; if (!RegisterClass(&wc)) { return FALSE; } view->wnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, PLOTTER_WINDOW_CLASS_NAME, PLOTTER_WINDOW_CLASS_NAME, WS_CAPTION | WS_POPUPWINDOW | WS_MINIMIZEBOX, 0, 0, view->width, view->height, NULL, NULL, instance, NULL ); if (view->wnd == NULL) { return FALSE; } SetWindowLongPtr(view->wnd, GWLP_USERDATA, view->target); if(!init_gl(view)) { release_gl(view); return FALSE; } return TRUE; }
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); }