用OpenSceneGraph實現的NeHe OpenGL教程 - 第三十二課

來源:互聯網
上載者:User
簡介

本節課NeHe課程主要想我們介紹了OpenGL中的拾取(Selection)的方式來和情境中的物體進行交換,在OSG中並沒有封裝OpenGL中的拾取方式,而是用了更高效的一種方式來實現與情境中的模型進行求交的操作,對於OSG這樣處理的原因可以參考OSG中的這個MailList: New to OSG, some questions (selection buffer, parametric curves) 實現

在NeHe課程中本課中實現的關鍵在Selection函數中,它實現了OpenGL中Selection的方式,在我們OSG中進行選擇求交的代碼就相對比較簡單:

通過osgUtil提供的幾種求交器實現:本課中使用的是osgUtil::LIneSegmentIntersector,用來檢測指定線段與情境圖形之間的相交情況

case (osgGA::GUIEventAdapter::PUSH):{int choose = 0;osg::Node *node;osgUtil::LineSegmentIntersector::Intersections intersections;if (viewer->computeIntersections(ea,intersections)){osgUtil::LineSegmentIntersector::Intersection  intersection = *intersections.begin();osg::NodePath &nodepath = intersection.nodePath;node = (nodepath.size() >= 1) ? nodepath[nodepath.size()-1]:0;const char *name = node->getName().c_str();choose = atoi(name);}if (!object[choose].hit)// If The Object Hasn't Already Been Hit{object[choose].hit=TRUE;// Mark The Object As Being Hitscore+=1;// Increase Scorekills+=1;// Increase Level Killsif (kills>level*5)// New Level Yet?{miss=0;// Misses Reset Back To Zerokills=0;// Reset Level Killslevel+=1;// Increase Levelif (level>30)// Higher Than 30?level=30;// Set Level To 30 (Are You A God?)}}}

代碼中的其他部分涉及大量的回調更新與位置的計算,十分繁瑣,但這些內容在以前的課程中都有介紹,具體的過程讀者可以參考NeHe課程中的內容進行。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.