Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Raypick test

Sign in to follow this  
Norman Barrows


Someone asked to see my raypick test code.

Here it is:

D3DXVECTOR3 intersect_point; void calc_intersection(int x,int y){char s[100];D3DXVECTOR3 v,dir,origin, p1,p2, // line p3,p4,p5; // planeD3DXMATRIX m;D3DXPLANE p; v.x = ( ( ( 2.0f * x ) / screen_width ) - 1 ) / Zprojection_matrix._11; v.y = - ( ( ( 2.0f * y ) / screen_height ) - 1 ) / Zprojection_matrix._22;v.z = 1.0f; // MICROSOFT COMMENTS: // Get the inverse view matrix//const D3DXMATRIX matView = *g_Camera.GetViewMatrix(); // view matrix (camera orientation & location)//const D3DXMATRIX matWorld = *g_Camera.GetWorldMatrix(); // world matrix (orientation & location of the mesh - not used?) //D3DXMATRIX mWorldView = matWorld * ZmView;//D3DXMatrixInverse( &m, NULL, &mWorldView );D3DXMatrixInverse(&m,NULL,&ZmView);// MICROSOFT COMMENTS: // Transform the screen space pick ray into 3D space // ROCKLAND COMMENTS:// actually - object space of the mesh, not world space. the view matrix alone gets you to world space. // adding the world matrix gets you object space.// object space -> (world mat) -> world space -> (view mat) -> camera space -> (proj mat) -> screen space.// MS uses the world mat because they're picking tri's off a single meshdir.x = v.x * m._11 + v.y * m._21 + v.z * m._31;dir.y = v.x * m._12 + v.y * m._22 + v.z * m._32;dir.z = v.x * m._13 + v.y * m._23 + v.z * m._33;origin.x = m._41;origin.y = m._42;origin.z = m._43; Znewmenu("Pick ray results:");f2s(origin.x,s);Zaddmenu(s);f2s(origin.y,s);Zaddmenu(s);f2s(origin.z,s);Zaddmenu(s);f2s(dir.x,s);Zaddmenu(s);f2s(dir.y,s);Zaddmenu(s);f2s(dir.z,s);Zaddmenu(s);//menu(); // need intersect of line and plane// need to convert ray to linep1.x=origin.x;p1.y=origin.y;p1.z=origin.z;p2.x=dir.x*1000.0f+origin.x;p2.y=dir.y*1000.0f+origin.y;p2.z=dir.z*1000.0f+origin.z;// now we have a nice long line from p1 (origin) to p2 (dir)// now we need a plane...// calc 3 points on the plane, or a point and a normal.// use them to create a plane data structure.p3.x=0.0f;p3.y=0.0f;p3.z=0.0f;p4.x=0.0f;p4.y=0.0f;p4.z=1.0f;p5.x=1.0f;p5.y=0.0f;p5.z=0.0f; D3DXPlaneFromPoints(&p,&p3,&p4,&p5); // then test ray vs plane intersection. D3DXPlaneIntersectLine( &intersect_point, &p, &p1, &p2); Znewmenu("plane line intersect:");if (intersect_point == NULL) { Zaddmenu("intersect_point = NULL"); }else { f2s(intersect_point.x,s); Zaddmenu(s); f2s(intersect_point.y,s); Zaddmenu(s); f2s(intersect_point.z,s); Zaddmenu(s); }//menu(); } void draw_ground(){int x,z;Zdrawinfo a;ZeroMemory(&a,sizeof(Zdrawinfo));a.sx=10.0f;a.sy=10.0f;a.sz=10.0f;for (x=0; x<200; x+=10) for (z=0; z<200; z+=10) { a.x=(float)x; a.z=(float)z; Zdraw(&a); }} void picktest(){/*while ! quit clearscreen drawmouse if leftclick calc ray - plane intersection show results else if esc - quit*/int quit,x,y,b;Zdrawinfo a;Zsetcam(10.0f,50.0f,-10.0f,1.0f,0.0f,0.0f);Zsetlite(0,1.0f,-1.0f,1.0f,1.0f);Zlite(0,1);quit=0;ZeroMemory(&a,sizeof(Zdrawinfo));a.sx=1.0f;a.sy=1.0f;a.sz=1.0f;a.texID=2;a.y=0.1f;intersect_point.x=0.0f;intersect_point.y=0.0f;intersect_point.z=0.0f;while (! quit) { Zclearscreen(0,0,0); Zclearzbuf(); Zbeginscene(); Zcleardrawlist(); draw_ground(); a.x=intersect_point.x; a.z=intersect_point.z; Zdraw(&a); Ztext3d(0,0,"Press ESC to quit"); Zdrawlist(); Zbeginsprite(); drawmouse(); Zendsprite(); Zshowscene(); Zdomessages(); Zgetmouse(&x,&y,&b); if (b==1) { calc_intersection(x,y); } else if (Zkeypressed(VK_ESCAPE)) { quit=1; } }}
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!