📄 hv_artk_demo.cpp
字号:
glVertex3d(x-w*2, -y/1.5, z/1.5); glVertex3d(x-w, -y/1.5, z/1.5); glVertex3d(x, -y/2, z/2); } if (dragging) { // draw closed claws glVertex3d(x+w*2, -y/1.5, z/1.5); glVertex3d(x+w, -y, z); glVertex3d(x, -y, z); glVertex3d(x+w, -y/1.5, z/1.5); glVertex3d(x-w*2, -y/1.5, z/1.5); glVertex3d(x-w, -y, z); glVertex3d(x, -y, z); glVertex3d(x-w, -y/1.5, z/1.5); } else if (hovering) { // draw open claws glVertex3d(x+w*2, -y/1.5, z/1.5); glVertex3d(x+w*2, -y, z); glVertex3d(x+w, -y, z); glVertex3d(x+w, -y/1.5, z/1.5); glVertex3d(x-w*2, -y/1.5, z/1.5); glVertex3d(x-w*2, -y, z); glVertex3d(x-w, -y, z); glVertex3d(x-w, -y/1.5, z/1.5); } else if (rotating) { // draw little points in 3 directions glVertex3d(x+w, -y/2, z/2); glVertex3d(x, -y, z); glVertex3d(x, -y, z); glVertex3d(x-w, -y/2, z/2); glVertex3d(x, -y/2, z/2); glVertex3d(x+y/2, -y, z); glVertex3d(x+y/2, -y, z); glVertex3d(x, -y/2-w, z/2); glVertex3d(x, -y/2, z/2); glVertex3d(x-y/2, -y, z); glVertex3d(x-y/2, -y, z); glVertex3d(x, -y/2-w, z/2); } else { // draw pointy top glVertex3d(x+w, -y/2, z/2); glVertex3d(x, -y, z); glVertex3d(x, -y, z); glVertex3d(x-w, -y/2, z/2); } glEnd(); glDisable(GL_BLEND); glPopMatrix();}void drawLights() { glMatrixMode(GL_MODELVIEW); // "ambient" light from behind GLenum use_light = GL_LIGHT0; { glEnable(use_light); float diffuse[] = {.8, .8, .8, .8}; float specular[] = {0, 0, 0, 1}; float ambient[] = {0.2, 0.2, 0.2, 1}; glLightfv(use_light, GL_DIFFUSE, diffuse); glLightfv(use_light, GL_SPECULAR, specular); glLightfv(use_light, GL_AMBIENT, ambient); // float pos[4] = {0, 0, -1, 0}; // glLightfv(use_light, GL_POSITION, pos); } use_light = GL_LIGHT1; if (anchor_visible) { glPushMatrix(); { glEnable(use_light); float diffuse[] = {1, 1, 1, 1}; float specular[] = {1, 1, 1, 1}; float ambient[] = {0, 0, 0, 1}; glLightfv(use_light, GL_DIFFUSE, diffuse); glLightfv(use_light, GL_SPECULAR, specular); glLightfv(use_light, GL_AMBIENT, ambient); glMultMatrixd(anchor); glTranslated(positions[IDX_ANCHORED_LIGHT].x, positions[IDX_ANCHORED_LIGHT].y, positions[IDX_ANCHORED_LIGHT].z); float pos[4] = {0, 0, 0, 1}; glLightfv(use_light, GL_POSITION, pos); // glLightf(use_light, GL_CONSTANT_ATTENUATION, 2.0); // glLightf(use_light, GL_LINEAR_ATTENUATION, 2.0); // glLightf(use_light, GL_QUADRATIC_ATTENUATION, 2.0); float dir[3]; dir[0] = positions[0].x-positions[IDX_ANCHORED_LIGHT].x; dir[1] = positions[0].y-positions[IDX_ANCHORED_LIGHT].y; dir[2] = positions[0].z-positions[IDX_ANCHORED_LIGHT].z; glLightfv(use_light, GL_SPOT_DIRECTION, dir); glLightf(use_light, GL_SPOT_CUTOFF, 45); // glLightf(use_light, GL_SPOT_EXPONENT, 3); // glRotatef(dir); } glPopMatrix(); } else { glDisable(use_light); } use_light = GL_LIGHT2; if (floater_visible[IDX_FLOATER_LIGHT]) { glPushMatrix(); { glEnable(use_light); float diffuse[] = {.5, .5, 1, 1}; float specular[] = {.5, .5, 1, 1}; float ambient[] = {0, 0, 0, 1}; glLightfv(use_light, GL_DIFFUSE, diffuse); glLightfv(use_light, GL_SPECULAR, specular); glLightfv(use_light, GL_AMBIENT, ambient); glMultMatrixd(&floaterpositions[IDX_FLOATER_LIGHT][0]); glTranslated( positions[IDX_FLOATER_LIGHT].x, positions[IDX_FLOATER_LIGHT].y, positions[IDX_FLOATER_LIGHT].z ); float pos[4] = {0, 0, 0, 1}; glLightfv(use_light, GL_POSITION, pos); float dir[3]; dir[0] = positions[2].x-positions[IDX_FLOATER_LIGHT].x; dir[1] = positions[2].y-positions[IDX_FLOATER_LIGHT].y; dir[2] = positions[2].z-positions[IDX_FLOATER_LIGHT].z; glLightfv(use_light, GL_SPOT_DIRECTION, dir); // glLightf(use_light, GL_SPOT_CUTOFF, 45); } glPopMatrix(); } else { glDisable(use_light); }}void hv_draw(bool rendering){ drawObjs(); if (rendering) { drawLights(); drawPointer(); }}void hv_set_anchor_transform(bool visible, double m[16]){ anchor_visible = visible; if (visible && m!=NULL) { memcpy(anchor, m, 16*sizeof(double)); //for (int i = 0; i<16; i++) printf("%e, ", anchor[i]); printf("\n"); }}void hv_set_floater_transform(int id, bool visible, double m[16]){ id += num_anchored; if (id>=(int) floater_visible.size()) { fprintf(stderr, "invalid floater id: %d\n", id); exit(-1); } floater_visible[id] = visible; if (visible && m!=NULL) { memcpy(&floaterpositions[id][0], m, 16*sizeof(double)); }}//----------------------------------------------------------------------static const int SELECT_BUF_SIZE = 1024 * 10;void drag( int x, int y ){ /* * new window coords are x,y plus old z */ d3 new_win = selected_win; new_win.x = x; new_win.y = viewport[ 3 ] - y - 1; /* * calculate new world coordinates */ d3 new_world; gluUnProject(new_win.x, new_win.y, new_win.z, selected_view, projection, viewport, &new_world.x, &new_world.y, &new_world.z ); /* * delta vector is new world minus old world */ d3 offset; offset.x = new_world.x - selected_rel.x; offset.y = new_world.y - selected_rel.y; offset.z = new_world.z - selected_rel.z; /* * update selected teapot position with delta vector */ positions[ selected ].x += offset.x; positions[ selected ].y += offset.y; positions[ selected ].z += offset.z; selected_win = new_win; selected_rel = new_world;}void rotate( int x, int y ){ /* * new window coords are x,y plus old z */ rotations[ selected ].x = x - selected_win.x; rotations[ selected ].y = (viewport[3]-y-1) - selected_win.y;}void hv_pointer_update(bool show, int button, int x, int y){ ptr_x = x; ptr_y = y; show_ptr = show; if (!show) { dragging = false; rotating = false; selected = -1; return; } if (button == HV_PTR_LBUTTON_PRESSED) { if (!dragging) { if( render_select( x, y-hv_pointer_dist, &selected, &selected_win ) ) { gluUnProject(selected_win.x, selected_win.y, selected_win.z, selected_view, projection, viewport, &selected_rel.x, &selected_rel.y, &selected_rel.z); gluUnProject(selected_win.x, selected_win.y-hv_pointer_dist, selected_win.z, modelview, projection, viewport, &selected_world.x, &selected_world.y, &selected_world.z); dragging = true; rotating = false; } } else { drag( x, y-hv_pointer_dist ); } } else if (button == HV_PTR_RBUTTON_PRESSED) { if (!rotating) { if( render_select( x, y-hv_pointer_dist, &selected, &selected_win ) ) { gluUnProject(selected_win.x, selected_win.y, selected_win.z, selected_view, projection, viewport, &selected_rel.x, &selected_rel.y, &selected_rel.z); gluUnProject(selected_win.x, selected_win.y-hv_pointer_dist, selected_win.z, modelview, projection, viewport, &selected_world.x, &selected_world.y, &selected_world.z); rotating = true; dragging = false; } } else { rotate( x, y-hv_pointer_dist ); } } else if (button == HV_PTR_BUTTON_RELEASED) { dragging = false; rotating = false; selected = -1; } else if (button == HV_PTR_NO_BUTTON_ACTION) { if (dragging) { drag( x, y-hv_pointer_dist ); } if (rotating) { rotate( x, y-hv_pointer_dist ); } } else { fprintf(stderr, "wrong 'button' value in hv_pointer_update\n"); exit(-1); } // printf("selected: %d\n", selected); if (!dragging && !rotating && render_select( x, y-hv_pointer_dist, &selected, &selected_win ) ) { gluUnProject(selected_win.x, selected_win.y, selected_win.z, selected_view, projection, viewport, &selected_rel.x, &selected_rel.y, &selected_rel.z); gluUnProject(selected_win.x, selected_win.y-hv_pointer_dist, selected_win.z, modelview, projection, viewport, &selected_world.x, &selected_world.y, &selected_world.z); hovering = true; } else { hovering = false; }}void ChangeObjectColor(){ if (selected!=-1) { colors[selected].x = (float)rand() / ( float )RAND_MAX; colors[selected].y = (float)rand() / ( float )RAND_MAX; colors[selected].z = (float)rand() / ( float )RAND_MAX; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -