MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setWindowTitle("Model/View example"); setupModelView(); QAction *openAction = new QAction(tr("&Open"), this); QAction *quitAction = new QAction(tr("E&xit"), this); QMenu *fileMenu = new QMenu(tr("&File"), this); fileMenu->addAction(openAction); fileMenu->addAction(quitAction); menuBar()->addMenu(fileMenu); connect(openAction, SIGNAL(triggered()), this, SLOT(selectOpenFile())); connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); setCentralWidget(view); }
///////////////////////////////////////////////////////////////////////////// // CObjectView interaction void CObjectView::OnLButtonDown(UINT nFlags, CPoint point) { if (mode == POINT){ selID = -1; SELECT_CONTEXT(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // setup select mode GLuint selBuf[1024]; glSelectBuffer(1024, selBuf); glRenderMode(GL_SELECT); glInitNames(); glPushName(~0); // save projection matrix glMatrixMode(GL_PROJECTION); glPushMatrix(); setupPerspective(true, point.x, point.y); glMatrixMode(GL_MODELVIEW); setupModelView(); // draw scene for selection drawObject(true); int sI = glRenderMode(GL_RENDER); if (sI){ // find closest hit /* GLint minZ = 1000000; for (int l = 0; l < sI; l++){ GLint z = selBuf[4*l+1]; if (z < minZ){ minZ = z; selID = selBuf[4*l+3]; } }*/ selID = selBuf[4*(sI-1)+3]; TRACE("selID = %d\n", selID); clearDisplayList(); InvalidateRect(NULL); } else{ selID = -1; TRACE("No Hit\n"); clearDisplayList(); InvalidateRect(NULL); } // restore matrix glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); UNSELECT_CONTEXT(); } else if (mode != ZOOM){ // record starting point and hook mouse lastPt = point; SetCapture(); RedrawWindow(); } else{ scale *= 2.0f; // zoom in RedrawWindow(); } CView ::OnLButtonDown(nFlags, point); }
void CObjectView::drawScene(GLint *dList, GLuint tex){ // erase background glClearColor(bkCol[0], bkCol[1], bkCol[2], 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // setup state glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); // setup lighting (can do after modelview if wanted to move with object) glMatrixMode(GL_MODELVIEW); glLoadMatrixf(mL); GLfloat lightPos[] = {1.0f, 1.0f, 1.0f, 0.0f}; glLightfv(GL_LIGHT0, GL_POSITION, lightPos); GLfloat spec[] = {1.0f, 1.0f, 1.0f, 1.0f}; glLightfv(GL_LIGHT0, GL_SPECULAR, spec); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); setupModelView(); // draw object if (dList && glIsList(*dList)) glCallList(*dList); else{ if (dList){ *dList = glGenLists(1); glNewList(*dList, GL_COMPILE_AND_EXECUTE); } /* // texturing & color if (glIsTexture(tex)){ glBindTexture(GL_TEXTURE_2D, tex); glEnable(GL_TEXTURE_2D); } else{ glDisable(GL_TEXTURE_2D); } if (hilite > 0){ float e = 128.0 - hilite*128.0/100.0; glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, e); float spec[4]; spec[0] = spec[1] = spec[2] = shine/100.0; spec[3] = 1.0; glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec); } else{ float spec[] = {0.0, 0.0, 0.0, 0.0}; glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec); // fully non specular } */ glEnable(GL_COLOR_MATERIAL); glColor3f(col[0], col[1], col[2]); drawObject(false); if (dList) glEndList(); } drawExtraObjects(); }