Vexator

Member
  • Content count

    259
  • Joined

  • Last visited

Community Reputation

138 Neutral

About Vexator

  • Rank
    Member
  1. I could use some help figuring out collision response. I'm trying to implement what is presented [url="http://cs.brown.edu/courses/cs195u/lectures/lecture6.pdf"]in this paper[/url] on slides 4 to 10. Collision detection works, and the colliding entity does slide along the collision plane but with severely reduced speed. I'm not sure whether I'm properly projecting the leftover velocity on collision plane. Would someone with a better understanding of the underlying math have a look? Thank you! [CODE]collideAndSlide(position: vec3, velocity: vec3, radius: number): vec3 { var packet = new CollisionPacket(); packet.colliderRadius = radius; packet.colliderPosition = position; packet.colliderVelocity = velocity; packet.collisionTime = 0.0; packet.collisionFound = false; var maxIterations = 5; do { this.collideWorld(packet); var newPosition = packet.colliderPosition.copy(); var newVelocity = packet.colliderVelocity.copy(); if (packet.collisionFound) { // scale velocity vector to collide with nearest triangle var scaledVelocity = packet.colliderVelocity.copy().scale(packet.collisionTime); // move a tiny bit away from collision along collision normal scaledVelocity.add(packet.collisionNormal.copy().scale(0.001)); // add scaled velocity to position newPosition.add(scaledVelocity); // and remove it from leftover velocity newVelocity.subtract(scaledVelocity); // extract part of velocity vector which is perpendicular to collision plane (CORRECT?) var v = packet.collisionNormal.copy().scale(vec3.dot(newVelocity, packet.collisionNormal)); // remove that part from velocity vector newVelocity = vec3.difference(newVelocity, v); // update values for next iteration packet.colliderPosition = newPosition; packet.colliderVelocity = newVelocity; } else { // no collision, move as requested newPosition.add(packet.colliderVelocity); } } while (--maxIterations > 0 && packet.collisionFound) return newPosition; }[/CODE] [b]EDIT:[/b] I visualized both [i]v[/i] and [i]newVelocity [/i]after [i]v[/i] has been subtracted from it and they look how they're supposed to: [i]v[/i] is perpendicular to the collision plane, [i]newVelocity [/i]runs parallel to it in the direction we're moving. If I add [i]newVelocity [/i]directly to [i]newPosition [/i]instead of running the recursion, like this: [CODE] ... // remove that part from velocity vector newVelocity = vec3.difference(newVelocity, v); newPosition.add(newVelocity); } else { // no collision, move as requested newPosition.add(packet.colliderVelocity); } } while (false)[/CODE] the entity is sliding as it should, without being slowed down, so I'm quite sure the vectors are ok. However, [i]maxIterations [/i]always runs down to zero, so there's always collisions. maybe that's slowing it down?
  2. [font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]I'm switching from regular 2D textures to 2D texture arrays. Texture creation and look-up in shaders works fine but I can't seem to get them working with fbo's.[/size][/font] [font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]Before, I've used [/size][/font][i]glFramebufferTexture2D [/i][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]to attach a texture to a framebuffer:[/size][/font] [font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3][CODE]glFramebufferTexture2D( GL_FRAMEBUFFER, attachmentIndex, GL_TEXTURE_2D, texture->id, 0 );[/CODE][/size][/font] [font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]Now I'm using [/size][/font][i]glFramebufferTextureLayer [/i][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]to attach a 2D texture array's first slice to a framebuffer:[/size][/font] [font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3][CODE]glFramebufferTextureLayer( GL_FRAMEBUFFER, attachmentIndex, texture->id, 0, 0 );[/CODE][/size][/font] [font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]The framebuffer is reported as being complete but the textures remain black. I've t[/size][/font][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]ested this on both ati and nvidia cards (latest drivers) with the same result. [/size][/font][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]Am I missing something?[/size][/font] [font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]Thank you![/size][/font]
  3. Please ignore, got it working: [CODE] matrix->matrix[3][0] = ( 0.5f - 0.5f * matrix->matrix[0][0] ); matrix->matrix[3][1] = ( 0.5f - 0.5f * matrix->matrix[1][1] );[/CODE]
  4. Hi! I've written the following function to scale texture matrices: [CODE] void ScaleMatrix( TextureMatrix *matrix, float scaleSpeed, double deltaTime ) const { vec2 scale = 1.0f/scaleSpeed; matrix->matrix[0][0] *= scale.x; matrix->matrix[1][1] *= scale.y; matrix->matrix[0][3] *= scale.x; matrix->matrix[1][3] *= scale.y; };[/CODE] Now there's two things missing and I can't figure out how to do it: - When a scaled texture is used to scale texture coordinates, the texture is scaled around the origin (0, 0). I would like them to be scaled around the center (0.5, 0.5) instead. How can I achieve this? - I'd like to include deltaTime in the computation so that the scaling is continous across several frames. Thank you!
  5. material system

    Just to give you some input, here's my Material class (stripped of its member functions): [CODE]class Material { public: enum CullFace { DONT_CULL, CULL_FRONT, CULL_BACK }; enum DepthTest { DONT_TEST, DEPTH_TEST_NEVER, DEPTH_TEST_ALWAYS, DEPTH_TEST_EQUAL, DEPTH_TEST_NOT_EQUAL, DEPTH_TEST_LESS, DEPTH_TEST_LESS_EQUAL, DEPTH_TEST_GREATER, DEPTH_TEST_GREATER_EQUAL }; enum BlendMode { DONT_BLEND, BLEND_MODULATE, BLEND_ADDITIVE, BLEND_TRANSPARENT }; vec4 ambient; vec4 diffuse; vec4 specular; bool colorWrite; bool depthWrite; CullFace cullFace; DepthTest depthTest; BlendMode blendMode; protected: // contains shader program id, etc. shared_ptr<MaterialProxy> m_proxy; // texture layers contain textures and texture coordinate // generators used to scroll, stretch and rotate textures vector<shared_ptr<TextureLayer>> m_textureLayers; };[/CODE] I use a uniform buffer object to update material colors etc. efficiently, I think there's a similar concept available in DX.
  6. Hi! I'm using the following code to create a perspective view frustum from a camera's view matrix and projection settings. How could I extend this to work with orthographic projections? Thank you! [CODE] void Frustum::compute( const mat4 &viewMatrix, float fieldOfView, const ivec4 &area, const vec2 &clipPlanes ) { // compute tangent and aspect ratio const float tangent = (float)tan( Degree2Radian(fieldOfView*0.5f) ); const float aspect = (float)(area.z - area.x) / ( float)(area.w - area.y); // compute near plane dimensions float nearHeight = clipPlanes.x*tangent; float nearWidth = nearHeight*aspect; // compute far plane dimensions float farHeight = clipPlanes.y*tangent; float farWidth = farHeight*aspect; mat4 viewMatrixInverse = glm::inverse( viewMatrix ); mat4 viewMatrixTranspose = glm::transpose( viewMatrix ); const vec3 position = vec3( viewMatrixInverse[3] ); const vec3 xAxis = glm::normalize( vec3(viewMatrixTranspose[0]) ); const vec3 yAxis = glm::normalize( vec3(viewMatrixTranspose[1]) ); const vec3 zAxis = glm::normalize( vec3(viewMatrixTranspose[2]) ); // compute centers of near and far planes vec3 nearCenter = position + ((-zAxis)*clipPlanes.x); vec3 farCenter = position + ((-zAxis)*clipPlanes.y); // compute frustum corners on near plane vec3 ntl = nearCenter + (yAxis*nearHeight) - (xAxis*nearWidth); vec3 ntr = nearCenter + (yAxis*nearHeight) + (xAxis*nearWidth); vec3 nbl = nearCenter - (yAxis*nearHeight) - (xAxis*nearWidth); vec3 nbr = nearCenter - (yAxis*nearHeight) + (xAxis*nearWidth); // compute frustum corners on far plane vec3 ftl = farCenter + (yAxis*farHeight) - (xAxis*farWidth); vec3 ftr = farCenter + (yAxis*farHeight) + (xAxis*farWidth); vec3 fbl = farCenter - (yAxis*farHeight) - (xAxis*farWidth); vec3 fbr = farCenter - (yAxis*farHeight) + (xAxis*farWidth); // set near and far planes m_planes[PLANE_NEAR] = computePlane( ntl, ntr, nbr ); m_planes[PLANE_FAR] = computePlane( ftr, ftl, fbl ); // set side planes m_planes[PLANE_RIGHT] = computePlane( nbr, ntr, fbr ); m_planes[PLANE_TOP] = computePlane( ntr, ntl, ftl ); m_planes[PLANE_LEFT] = computePlane( ntl, nbl, fbl ); m_planes[PLANE_BOTTOM] = computePlane( nbl, nbr, fbr ); } [/CODE]
  7. thank you! I adopted your suggestions and it seems to work just fine. I was just surprised that I had to change the bounding vertices' order; otherwise, the clip planes' normals were facing the wrong way. here's the final code: [code]const vec3 v1 = portal->getVertex(0); const vec3 v2 = portal->getVertex(1); const vec3 v3 = portal->getVertex(2); // compute plane reflecting portal's orientation const vec4 portalPlane = computePlane( v1, v2, v3 ); // compute plane matrix vec3 normal = vec3( portalPlane ); vec3 position( normal * (-portalPlane.w) ); vec3 up( 0, 1, 0 ); vec3 xAxis( glm::normalize(glm::cross(normal, up)) ); vec3 yAxis( glm::normalize(glm::cross(xAxis, normal)) ); mat4 planeMatrix; planeMatrix[0] = vec4( xAxis, 0.0f ); planeMatrix[1] = vec4( yAxis, 0.0f ); planeMatrix[2] = vec4( -normal, 0.0f ); planeMatrix[3] = vec4( position, 1.0f ); mat4 planeMatrixInverse = glm::inverse( planeMatrix ); // un-project portal vertices uint vertexCount = portal->getVertexCount(); vector<vec3> projectedVertices( vertexCount ); for( uint i = 0; i < vertexCount; i++ ) { const vec3 &position = portal->getVertex(i); projectedVertices[i] = vec3( planeMatrixInverse * vec4(position, 1.0f) ); } // compute bounding vertices // from projected portal vertices vec3 minimum = projectedVertices.at(0); vec3 maximum = projectedVertices.at(0); for( uint i = 0; i < projectedVertices.size(); i++ ) { const vec3 &vertex = projectedVertices.at(i); if( vertex.x < minimum.x ) minimum.x = vertex.x; if( vertex.y < minimum.y ) minimum.y = vertex.y; if( vertex.x > maximum.x ) maximum.x = vertex.x; if( vertex.y > maximum.y ) maximum.y = vertex.y; } // re-project bounding vertices vector<vec3> boundingVertices( 4 ); boundingVertices[3] = vec3( planeMatrix * vec4(maximum.x, minimum.y, 0, 1) ); boundingVertices[2] = vec3( planeMatrix * vec4(maximum.x, maximum.y, 0, 1) ); boundingVertices[1] = vec3( planeMatrix * vec4(minimum.x, maximum.y, 0, 1) ); boundingVertices[0] = vec3( planeMatrix * vec4(minimum.x, minimum.y, 0, 1) ); // create new portal from bounding vertices portal = shared_ptr<Portal>( new Portal(boundingVertices) ); [/code]
  8. thank you! i adopted your corrections: [code] const vec3 v1 = portal->getVertex(0); const vec3 v2 = portal->getVertex(1); const vec3 v3 = portal->getVertex(2); const vec4 portalPlane = computePlane( v1, v2, v3 ); // FIX: normalize normal vec3 normal = glm::normalize( vec3(portalPlane) ); vec3 up( 0, 1, 0 ); vec3 f( glm::normalize(normal) ); vec3 s( glm::normalize(glm::cross(f, up)) ); vec3 u( glm::normalize(glm::cross(s, f)) ); mat4 planeMatrix( s[0], u[0], -f[0], 0.0, s[1], u[1], -f[1], 0.0, s[2], u[2], -f[2], 0.0, 0.0f, 0.0f, 0.0f, 1.0f); vec3 v( normal*(-portalPlane.w) ); for (unsigned i = 0; i < 3; ++i) { double tmp = v[i]; if (tmp == 0) continue; planeMatrix[3][0] += tmp*planeMatrix[i][0]; planeMatrix[3][1] += tmp*planeMatrix[i][1]; planeMatrix[3][2] += tmp*planeMatrix[i][2]; planeMatrix[3][3] += tmp*planeMatrix[i][3]; } // FIX: transpose matrix mat4 planeMatrixInverse = glm::inverse( glm::transpose(planeMatrix) ); vector<vec3> newVertices( portal->getVertexCount() ); for( uint i = 0; i < portal->getVertexCount(); i++ ) { const vec3 &position = portal->getVertex(i); newVertices[i] = vec3( planeMatrixInverse * vec4(position, 1.0f) ); } portal = shared_ptr<Portal>( new Portal(newVertices) );[/code] now the resulting vertices are aligned on a 2D plane (see z-coordinate). however, they are still not at the origin: [code] [0] {x=-0.99051094 y=-1.2763327 z=-4.8927865 } [1] {x=-0.99051315 y=1.8201402 z=-4.8927865 } [2] {x=0.99051857 y=1.8201412 z=-4.8927865 } [3] {x=0.99051929 y=-0.56991905 z=-4.8927870 } [4] {x=0.20238473 y=-1.2763321 z=-4.8927870 } [/code]
  9. thanks for the input everybody! here's what i've come up with. results are not as expected, though. original portal vertices: [code] [0] { x=-1.5885940 y=-1.2763360 z=4.7325292 } [1] { x=-1.5885930 y=1.8201370 z=4.7325320 } [2] { x=-3.3042150 y=1.8201380 z=3.7420130 } [3] {x=-3.3042150 y=-0.56992221 z=3.7420115 } [4] { x=-2.6216710 y=-1.2763354 z=4.1360798 } [/code] vertices transformed by plane matrix inverse: [code] [0] { x=6.1884351 y=-1.2763298 z=-7.5414600 } [1] { x=6.1884346 y=1.8201432 z=-7.5414615 } [2] { x=7.1789441 y=1.8201436 z=-5.8258338 } [3] { x=7.1789441 y=-0.56991661 z=-5.8258338 } [4] { x=6.7848792 y=-1.2763295 z=-6.5083799 } [/code] computing a matrix from the portal's plane and multiplying its vertices with the inverse matrix: [code] const vec3 v1 = portal->getVertex(0); const vec3 v2 = portal->getVertex(1); const vec3 v3 = portal->getVertex(2); const vec4 portalPlane = computePlane( v1, v2, v3 ); vec3 normal = vec3( portalPlane ); vec3 up( 0, 1, 0 ); vec3 f( glm::normalize(normal) ); vec3 s( glm::normalize(glm::cross(f, up)) ); vec3 u( glm::normalize(glm::cross(s, f)) ); mat4 planeMatrix( s[0], u[0], -f[0], 0.0, s[1], u[1], -f[1], 0.0, s[2], u[2], -f[2], 0.0, 0.0f, 0.0f, 0.0f, 1.0f); vec3 v( normal*(-portalPlane.w) ); for (unsigned i = 0; i < 3; ++i) { double tmp = v[i]; if (tmp == 0) continue; planeMatrix[3][0] += tmp*planeMatrix[i][0]; planeMatrix[3][1] += tmp*planeMatrix[i][1]; planeMatrix[3][2] += tmp*planeMatrix[i][2]; planeMatrix[3][3] += tmp*planeMatrix[i][3]; } mat4 planeMatrixInverse = glm::inverse( planeMatrix ); vector<vec3> newVertices( portal->getVertexCount() ); for( uint i = 0; i < portal->getVertexCount(); i++ ) { const vec3 &position = portal->getVertex(i); newVertices[i] = vec3( planeMatrixInverse * vec4(position, 1.0f) ); }[/code] what do you think?
  10. [quote][color=#1C2837][size=2]Multiply each point's transform by the [i]inverse[/i] of the plane's transform. [/size][/color][/quote] How exaclty would I do that? I store my planes as normal plus distance to origin.
  11. thanks for the link! I'm having trouble computing the extreme points, though. If it was in 2D I'd know how to do it but on an arbitrary plane in 3D, I'm not sure [img]http://public.gamedev.net/public/style_emoticons/default/huh.gif[/img]
  12. I have an arbitrary number of points which are all located on a plane in 3D space. I need to find the four corner points of the rectangle which tightly encloses all points. how can i do that? thank you!
  13. Clipping frustum to portal

    great, thank you all for your input! the portal is clipped properly now. however, in some cases, more than four clipped vertices are generated. so i compute the bounding box around all these vertices. but now i'm not sure how to decide which four corners to choose from the eight corner vertices of this bounding box. any hints? [img]http://ah-fotografie.com/vexator/portal.jpg[/img]
  14. Clipping frustum to portal

    changed [code]linePlaneIntersection(plane, v2, v1)[/code] to [code]linePlaneIntersection(plane, v2-v1, v1)[/code] and the intersection points are now on the portal's edge like they're supposed to be. the sudden corruption of the frustum (like the one at the end of the video) are still sometimes happening, though, when i move the camera.
  15. Hi everybody! I'm working on the implementation of portal rendering into my personal engine project. Right now, portals are hard-coded, rectangular polygons. In general, there are three possible cases: [list][*]None of the portal's vertices are inside the frustum: the frustum will not be changed.[*]All of the portal's vertices are inside the frustum: compute new frustum planes from portal vertices and camera position.[*]Some of the portal's vertices are inside the frustum: clip frustum planes against portal to compute a set of intersection points between frustum planes and portal vertices. Compute new frustum planes from clipped vertices and camera position.[/list] Cases one and two seem to work fine but if only some of the portal vertices are inside the frustum and the frustum planes have to be clipped against the portal, something goes wrong. As you can see in the video, the intersection points which are generated by intersecting a frustum plane and a portal vertex are not on the portal itself, but move in a half-circle through the corridor instead (the portal is placed bewtween the room and the corridor: [url="http://www.youtube.com/watch?v=VPrEfiwt6ro"]http://www.youtube.c...h?v=VPrEfiwt6ro[/url] I just can't spot the error and I've been on this for a week now - what am I doing wrong? I am thankful for all the help/hints you can give. [u][b]class Portal[/b][/u] Declaration: [code]class Portal { public: vector<vec3> vertices; void clipToPlane( const vec4 &plane ); };[/code] Implementation: [code]vec3 linePlaneIntersection( vec4 plane, vec3 ray, vec3 rayOrigin ) { const vec3 normal = vec3(plane); const float t = (plane.w - glm::dot(normal, rayOrigin)) / glm::dot(normal, ray); const vec3 newRay = ray * t; return rayOrigin + newRay; } void Portal::clipToPlane( const vec4 &plane ) { vector<vec3> clipVertices; for (int i = 0; i < vertices.size(); i++) { int i2 = (i + 1) % vertices.size(); vec3 v1 = vertices[i]; vec3 v2 = vertices[i2]; float dist1 = glm::dot( vec3(plane), v1 ) + plane.w; float dist2 = glm::dot( vec3(plane), v2 ) + plane.w; // both are outside frustum if( dist1 < 0 && dist2 < 0 ) continue; // both are inside frustum if( dist1 > 0 && dist2 > 0 ) { clipVertices.push_back( v1 ); } // only v1 is inside else if( dist1 > 0 ) { clipVertices.push_back( v1 ); clipVertices.push_back( linePlaneIntersection(plane, v1, v2) ); } // only v2 is inside else { clipVertices.push_back( linePlaneIntersection(plane, v1, v2) ); } } if( clipVertices.size() >= 3 ) { vertices = clipVertices; } else { vertices.clear(); } }[/code] [u][b]class Frustum[/b][/u] Declaration: [code]class Frustum : public BoundingVolume { public: enum Plane { PLANE_NEAR, PLANE_FAR, PLANE_RIGHT, PLANE_TOP, PLANE_LEFT, PLANE_BOTTOM, PLANE_COUNT }; public: void compute( const mat4 &viewMatrix, const mat4 &projectionMatrix, float fieldOfView, float aspect, const vec2 &clipPlanes, shared_ptr<Portal> portal = shared_ptr<Portal>() ); /* ... */ protected: vec4 m_planes[PLANE_COUNT]; };[/code] Implementation: [code]vec4 computePlane( const vec3 &v1, const vec3 &v2, const vec3 &v3 ) { vec3 aux1 = v1 - v2; vec3 aux2 = v3 - v2; vec3 normal = glm::normalize( glm::cross(aux2, aux1) ); float d = glm::dot( normal, v2 ); return vec4( normal, -d ); } void Frustum::compute( const mat4 &viewMatrix, const mat4 &projectionMatrix, float fieldOfView, float aspect, const vec2 &clipPlanes, shared_ptr<Portal> portal ) { // compute tangent in randians const float tangent = (float)tan( Degree2Radian(fieldOfView*0.5f) ); // compute near plane dimensions float nearHeight = clipPlanes.x*tangent; float nearWidth = nearHeight*aspect; // compute far plane dimensions float farHeight = clipPlanes.y*tangent; float farWidth = farHeight*aspect; mat4 viewMatrixInverse = glm::inverse( viewMatrix ); mat4 viewMatrixTranspose = glm::transpose( viewMatrix ); const vec3 position = vec3( viewMatrixInverse[3] ); const vec3 xAxis = glm::normalize( vec3(viewMatrixTranspose[0]) ); const vec3 yAxis = glm::normalize( vec3(viewMatrixTranspose[1]) ); const vec3 zAxis = glm::normalize( vec3(viewMatrixTranspose[2]) ); // compute centers of near and far planes vec3 nearCenter = position + ((-zAxis)*clipPlanes.x); vec3 farCenter = position + ((-zAxis)*clipPlanes.y); // compute frustum corners on near plane vec3 ntl = nearCenter + (yAxis*nearHeight) - (xAxis*nearWidth); vec3 ntr = nearCenter + (yAxis*nearHeight) + (xAxis*nearWidth); vec3 nbl = nearCenter - (yAxis*nearHeight) - (xAxis*nearWidth); vec3 nbr = nearCenter - (yAxis*nearHeight) + (xAxis*nearWidth); // compute frustum corners on far plane vec3 ftl = farCenter + (yAxis*farHeight) - (xAxis*farWidth); vec3 ftr = farCenter + (yAxis*farHeight) + (xAxis*farWidth); vec3 fbl = farCenter - (yAxis*farHeight) - (xAxis*farWidth); vec3 fbr = farCenter - (yAxis*farHeight) + (xAxis*farWidth); // add near and far planes m_planes[PLANE_NEAR] = computePlane( ntl, ntr, nbr ); m_planes[PLANE_FAR] = computePlane( ftr, ftl, fbl ); // add side planes m_planes[PLANE_RIGHT] = computePlane( nbr, ntr, fbr ); m_planes[PLANE_TOP] = computePlane( ntr, ntl, ftl ); m_planes[PLANE_LEFT] = computePlane( ntl, nbl, fbl ); m_planes[PLANE_BOTTOM] = computePlane( nbl, nbr, fbr ); if( portal ) { // check whether all portal // vertices are inside frustum bool insideFrustum = true; for( int i = 0; i < portal->vertices.size(); i++ ) { if( testIntersection(portal->vertices[i]) == TEST_OUTSIDE ) { insideFrustum = false; break; } } // clip portal vertices against frustum planes if they some are not inside frustum // if no vertices are generated, frustum is already small enough to fit through portal if( !insideFrustum ) { // skip near and far planes! for( int i = 2; i < PLANE_COUNT; i++ ) { portal->clipToPlane( m_planes[i] ); } } // compute planes from (possibly clipped) portal vertices for( int i = 0; i < portal->vertices.size(); i++ ) { int i2 = (i+1) % portal->vertices.size(); vec3 v1 = position; vec3 v2 = portal->vertices[i]; vec3 v3 = portal->vertices[i2]; if( (i+2) > PLANE_COUNT ) break; // replace original plane by clipped plane m_planes[i+2] = computePlane( v1, v2, v3 ); } } }[/code]