drawing 3d objects using opengl ppt
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Physically based simulation on GPU using OpenGL and GLSL PowerPoint Presentation
Physically based simulation on GPU using OpenGL and GLSL
Download Presentation
Physically based simulation on GPU using OpenGL and GLSL
- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
-
Physically based simulation on GPU using OpenGL and GLSL Yalmar Ponce Atencio
-
Outline • Multitexture mapping OpenGL stock-still functionality vs. GLSL • Render to texture PBuffers vs. The OpenGL Framebuffer Object Extension • Applications (physical simulation) • Particle systems • Textile • Rigid and deformable objects
-
Outline • Multitexture mapping OpenGL fixed functionality vs. GLSL • Render to texture PBuffers vs. The OpenGL Framebuffer Object Extension • Applications (physical simulation) • Particle systems • Cloth • Rigid and deformable objects
-
Brief history • Since the OpenGL 1.0 definition (1992) • However, limited. But to utilise images to the surface of an object • Later, in OpenGL 1.1 (1995) texture objects was added • In OpenGL i.2 (1997) 3D textures was added • In OpenGL 1.iii (1999) new hardware capabilities was exposed. Permit access to two or more texture objects simultaneously • In OpenGL 1.iv (2003) was added support for depth textures and shadows
-
Unproblematic texturing sample • Good example http://www.xmission.com/~nate/tutors.html
-
Multitexture with fixed functionality = + • Need GL_ARB_multitexture extension (at present is part of OpenGL 2.0 cadre) • glActiveTextureARB (GL_TEXTUREn_ARB) • glMultiTexCoord…
-
Combining textures • Get-go, a class or part for loading prototype files is needed void DrawFrame(void) { ... glTranslatef(0.0,2.0,0.0); glBindTexture(GL_TEXTURE_2D, texture1); drawBox(ii.0); glTranslatef(0.0,-2.0,0.0); glBindTexture(GL_TEXTURE_2D, texture2); drawBox(2.0); ... }
-
Combining textures • Side by side, setup for using multitexturing void drawFrame(){ ... glEnable(GL_TEXTURE_2D); glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_2D, texture1); glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_COMBINE_EXT); glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE); glActiveTextureARB(GL_TEXTURE1_ARB); glBindTexture(GL_TEXTURE_2D, texture2); glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_COMBINE_EXT); glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_INCR); drawBox(ii.0); ... } void drawBox(float size){ // Texture Coordinate (0,0) is top left. glBegin(GL_QUADS); glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0); glVertex3f(0.0, 0.0, 0.0); glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0); glVertex3f(0.0, size, 0.0); glMultiTexCoord2f(GL_TEXTURE0, one.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0); glVertex3f(size, size, 0.0); glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0); glVertex3f(size, 0.0, 0.0); glEnd();}
-
Bump mapping instance = +
-
Bump mapping example • Configuring a given texture unit glActiveTexture(GL_TEXTUREn);glBindTexture(GL_TEXTURE_2D, texName);glTexImage2D(GL_TEXTURE_2D, …);glTexParameterfv(GL_TEXTURE_2D, …);…glTexEnvfv(GL_TEXTURE_ENV, …);glTexGenfv(GL_S, …);glMatrixMode(GL_TEXTURE);glLoadIdentity(); • Setting texture coordinates for a vertex glMultiTexCoord4f(GL_TEXTURE1,s1, t1, r1, q1);glMultiTexCoord3f(GL_TEXTURE2,s2, t2, r2);glMultiTexCoord2f(GL_TEXTURE3,s3, t3);glMultiTexCoord1f(GL_TEXTURE4,s4);glVertex3f(x, y, z);
-
Bump mapping example:Mapping code void display(){ ... //Prepare texture environment to do (tex0 dot tex1)*color glActiveTextureARB(GL_TEXTURE0_ARB); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE); glActiveTextureARB(GL_TEXTURE1_ARB); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB); drawTorus(ii.0, 0.5); ...}
-
Bump mapping case:Results • References • http://www.paulsprojects.net/opengl/ • http://world wide web.opengl.org/resources/tutorials/sig99/shading99/course_slides/
-
Why must be used GLSL? • Programmability allow to exploits much more the texture mapping capabilities • With programmable shaders, APPs can read and use the texture values in a way makes sense • Textures tin can also be used to shop intermediate results: • Lookup tables • normals • Factors • Visibility information • Etc.
-
Combining textures:GLSL version • A class or part that load vertex and/or fragment programs is needed • Changing the DrawFrame routine … ... GLSLKernel myShader; ... myShader.fragment_source("fragment_program"); myShader.vertex_source("vertex_program"); myShader.install(); ... void DrawFrame(void) { ... myShader.apply(); myShader.setUniform("texture0", 0); // Texture Unit 0 myShader.setUniform("texture1", 1); // Texture Unit one drawBox(2.0); myShader.use(faux); ... }
-
Combining textures:GLSL version • and the drawBox routine too void drawBox(float size){ // Texture Coordinate (0,0) is height left. glBegin(GL_QUADS); glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(0.0, 0.0); glVertex3f(0.0, size, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(size, size, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(size, 0.0, 0.0); glEnd(); }
-
Combining textures: GLSL version • The vertex program • The fragment program void main(void) { gl_TexCoord[0] = gl_TexCoord0; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } uniform sampler2D texture0, texture1; void main (void){ vec4 texel0 = texture2D(texture0, vec2(gl_TexCoord[0])); vec4 texel1 = texture2D(texture1, vec2(gl_TexCoord[0])); gl_FragColor = 0.v*(texel0 + texel1); }
-
Using multitexture with several shaders • References • OpenGL Shading Language book • http://world wide web.clockworkcoders.com/oglsl/ • http://www.lighthouse3d.com/opengl/glsl/
-
Conclusions • GLSL or another GPU programming language let exploit much more than texture mapping capabilities • Several shaders tin can be used in an APP, anyone to a specific APP feature • Walls with bump mapping • Water effects • Fire effects • Complex material effects (roughness, reflection, refraction, drinking glass, etc.)
-
Outline • Multitexture mapping OpenGL Fixed functionality vs. GLSL • Return to texture PBuffers vs. The OpenGL Framebuffer Object Extension • Applications (physical simulation) • Particle systems • Textile • Rigid and deformable objects
-
Subsequently, PBuffers • Pixel buffers • Designed for off-screen rendering • Similar to windows, but non-visible • Window system specific extension • Select from an enumerated list of available pixel formats using • ChoosePixelFormat() • DescribePixelFormat() • ...
-
Framebuffer Object • Only requires a single OpenGL context Only • switching between Framebuffers is faster than switching between PBuffers (wglMakeCurrent) • No need for complicated pixel format selection • Format of Framebuffer is determined by texture or Renderbuffer format • Puts burden of finding compatible formats on developer • More similar to Direct3D render target model • Makes porting code easier • Renderbuffer images and texture images can exist shared among Framebuffers • e.m. share depth buffers between color targets • saves memory
-
Framebuffer Object • OpenGL Framebuffer is a collection of logical buffers • Color, depth, stencil, accumulation • Framebuffer object extension provides a new mechanism for rendering to destinations other than those provided by the window system • Window system contained • Destinations known as "Framebuffer attachable images". Can be: • Off-screen buffers (Renderbuffers) • Textures
-
Framebuffer Object • Framebuffer-attachable image • 2D array of pixels that can be attached to a framebuffer • Texture images and renderbuffer images are examples. • Attachment point • State that references a framebuffer-attachable epitome. • One each for color, depth and stencil buffer of a framebuffer. • Adhere • The act of connecting one object to another. Similar to "demark". • Framebuffer attachment completeness
-
Framebuffer Object • Introduces two new OpenGL objects: • Framebuffers and Renderbuffers • Framebuffer (FBO) • Drove of framebuffer attachable images (e.yard. color, depth, stencil) • Plus state defining where output of GL rendering is directed • Equivalent to window system • When a framebuffer object is spring its attached images are the source and destination for fragment operations • Colour and depth textures • Supports multiple color attachments for MRT • Depth or stencil renderbuffers
-
Framebuffer Object arquitecture TEXTURE OBJECTS … GL_COLOR_ATTACHMENT0 TEXTURE Epitome GL_COLOR_ATTACHMENTn DEPTH Zipper RENDER BUFFER OBJECTS STENCIL Attachment RENDERBUFFER IMAGE OTHER STATES
-
Framebuffer Object API • Creating a FBO (similar to create a texture) void GenFramebuffersEXT(sizei northward, uint *framebuffers); • Delete a FBO void DeleteFramebuffersEXT(sizei due north, const uint *framebuffers); • Check if a given identifier is a FBO boolean IsFramebufferEXT(uint framebuffer); • Binding a FBO void BindFramebufferEXT(enum target, uint framebuffer); • Bank check condition of the FBO enum CheckFramebufferStatusEXT(enum target);
-
Framebuffer Object API • Attaches image from a texture object to 1 the logical buffers of the electric current bound FBO void FramebufferTexture1DEXT(enum target, enum attachment, enum textarget, uint texture, int level); void FramebufferTexture2DEXT(enum target, enum attachment, enum textarget, uint texture, int level); void FramebufferTexture3DEXT(enum target, enum attachment, enum textarget, uint texture, int level, int zoffset); • Automatically generate mipmaps for texture images attached to target void GenerateMipmapEXT(enum target);
-
Bounden a FBO void BindFramebufferEXT(enum target, uint framebuffer); • target must be FRAMEBUFFER_EXT • framebuffer is FBO identifier • All GL operations occur on fastened images • If (framebuffer == 0), GL operations operate on window system provided framebuffer (information technology's default)
-
Attaching textures to a FBO void FramebufferTexturenDEXT(enum target, enum attachment, enum textarget, uint texture, int level); • target must be FRAMEBUFFER_EXT • attachment Is one of: • GL_COLOR_ATTACHMENTn_EXT • DEPTH_ATTACHMENT_EXT • STENCIL_ATTACHMENT_EXT • textarget must exist one of: • GL_TEXTURE_2D • GL_TEXTURE_RECTANGLE_ARB • GL_TEXTURE_CUBE_MAP_... • level is the mipmap level of the texture to attach • texture is the texture object to adhere • If (texture==0), the image is detached from the framebuffer
-
Framebuffer completeness • Framebuffer is complete if all attachments are consistent • Texture formats make sense for attachment points i.e., don't try and attach a depth texture to a color zipper • All attached images must have the same width and summit • All images attached to COLOR_ATTACHMENTn_EXT must take same format • If non consummate, glBegin volition generate error INVALID_FRAMEBUFFER_OPERATION
-
Checking Framebuffer Condition enum CheckFramebufferStatusEXT(enum target); • Should always be called after setting upward FBOs • Returns enum indicating why FBO is incomplete FRAMEBUFFER_COMPLETE COMPLETEFRAMEBUFFER_INCOMPLETE_ATTACHMENTFRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENTFRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENTFRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXTFRAMEBUFFER_INCOMPLETE_FORMATS_EXTFRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXTFRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXTFRAMEBUFFER_UNSUPPORTED FRAMEBUFFER_STATUS_ERROR • If consequence is "FRAMEBUFFER_UNSUPPORTED", application should try different format combinations until one succeeds
-
FBO usage • FBO allows several ways of switching between rendering destinations • In lodge of increasing performance: • Multiple FBOs • Create a separate FBO for each texture you want to return to??? • Single FBO, multiple texture attachments • Textures should have same format and dimensions • Apply FramebufferTexture() to switch between textures • Single FBO, multiple texture attachments • Adhere textures to dissimilar color attachments • Use glDrawBuffer() to switch rendering to different color attachments
-
FBO Performance Tips • Don't create and destroy FBO every frame • Try to avoid modifying textures used equally rendering destinations using TexImage, CopyTexImage, etc. • More than references and examples • http://www.gpgpu.org/developer/
-
Outline • Multitexture mapping OpenGL Fixed functionality vs. GLSL • Return to texture PBuffers vs. The OpenGL Framebuffer Object Extension • Applications (physical simulation) • Particle systems • Cloth • Rigid and deformable objects
-
Cursory overview • Utilise textures to represent physical features (e.one thousand. positions, velocities, forces) • Fragment shader calculates new values and render this results back to textures • Each rendering pass draws a unmarried quad, calculating values to adjacent time step on the simulation • Actually, graphic processors supports textures with NPT (non-ability-of-two) and signed float values in each (RGBA) color aqueduct • GL_TEXTURE_RECTANGLE_ARB • Float precision of fragment programs allows GPU physic simulation match CPU accurateness • New fragment programming model (longer programs, flexible dependent texture reads) allows much more interesting simulations
-
Basic Case:Cloth simulation • Tin be used the Verlet integrator • Jakobsen, GDC 2001 • Avoids storing explicit velocity ten' = 2x – x* + ā.∆t²x* = x • Not always authentic, just stable! • In a GPU version, current and previous positions should be stored in ii RGB bladder textures • So swap current and previous textures for each fourth dimension step
-
Cloth simulation:The algorithm • GPU • 2 passes (two shaders) • Perform integration (move particles) • Satisfy constraints • Distance constraints betwixt particles • Floor collision constraints • Standoff constraints with other objects (spheres) • CPU • XYZ vertices RGB texture pixels (read pixels) • Compute normals and render terminal mesh
-
Cloth simulation:Diagram old positions TEX0 VERLET INTEGRATOR FRAGMENT SHADER adjacent positions SATISFY CONSTRAINTS FRAGMENT SHADER curr positions next positions TEX2 curr positions TEX1 Adjacency TEX3
-
Fabric simulation:Integration pass code uniform sampler2DRect oldp_tex;compatible sampler2DRect currp_tex;void Integrate(inout vec3 10, vec3 oldx, vec3 a, float timestep2){ x = 2*x - oldx + a*timestep2; }void main(){ float timestep = 0.002; vec3 gravity = vec3(0.0, -9.8, 0.0); vec2 uv = gl_TexCoord[0].st; // get electric current and previous position vec3 oldx = texture2DRect(oldp_tex, uv).rgb; vec3 10 = texture2DRect(currp_tex, uv).rgb; // move the particle Integrate(10, oldx, gravity, timestep*timestep); // satisfy world constraints x = clamp(x, worldMin, worldMax); SphereConstraint(x, spherePos, i.0); gl_FragColor = vec4(x, 1.0);}
-
Cloth simulation:Satisfy constraints lawmaking // constrain a particle to exist a fixed distance from another particlevec3 DistanceConstraint(vec3 10, vec3 x2, float restlength, float stiffness){ vec3 delta = x2 - 10; float deltalength = length(delta); bladder diff = (deltalength - restlength) / deltalength; return delta*stiffness*diff;}// as above, but using sqrt approximation sqrt(a) ~= r + ((a- r*r) / 2*r), if a ~= r*rvec3 DistanceConstraint2(vec3 x, vec3 x2, bladder restlength, bladder stiffness){ vec3 delta = x2 - x; float deltalength = dot(delta, delta); deltalength = restlength + ((deltalength - restlength*restlength) / (two.0 * restlength)); bladder unequal = (deltalength - restlength) / deltalength; return delta*stiffness*diff;}// constrain particle to be outside book of a spherevoid SphereConstraint(inout vec3 x, vec3 center, float r){ vec3 delta = x - center; float dist = length(delta); if (dist < r) { x = centre + delta*(r / dist); }}
-
Textile simulation:Constraints laissez passer code compatible vec2 ms; // mesh sizeuniform float constraintDist;uniform vec3 spherePos;uniform sampler2DRect x_tex;uniform vec3 worldMin;uniform vec3 worldMax;void primary(){ const bladder stiffness = 0.2; // this should actually be 0.five vec2 uv = gl_TexCoord[0].st; // go electric current position vec3 x = texture2DRect(x_tex, uv).rgb; // get positions of neighbouring particles vec3 x1 = texture2DRect(x_tex, uv + vec2(one.0, 0.0)).rgb; vec3 x2 = texture2DRect(x_tex, uv + vec2(-1.0, 0.0)).rgb; vec3 x3 = texture2DRect(x_tex, uv + vec2(0.0, 1.0)).rgb; vec3 x4 = texture2DRect(x_tex, uv + vec2(0.0, -one.0)).rgb; // apply distance constraints // this could exist done more than efficiently with separate passes for the edge cases vec3 dx = vec3(0.0); if (uv.x < ms.ten) dx = DistanceConstraint(x, x1, constraintDist, stiffness); if (uv.ten > 0.5) dx = dx + DistanceConstraint(x, x2, constraintDist, stiffness); if (uv.y < ms.y) dx = dx + DistanceConstraint(x, x3, constraintDist, stiffness); if (uv.y > 0.5) dx = dx + DistanceConstraint(ten, x4, constraintDist, stiffness); 10 = ten + dx; gl_FragColor = vec4(x, 1.0);}
-
Cloth simulation:A screenshot 128x128 mesh 256x256 mesh
-
Cloth simulation:Used textures Previous positions Current positions
-
More circuitous example:Rigid and deformable bodies • Equally cloth modeling: • Vertices particles • Electric current and previous positions are mapped on textures • Edges contraints • A rigid tetrahedron • Four vertices • Six constraints v3 v1 v2 v0 Texture0 Texture1 ContraintsTexture
-
Rigid tetrahedron simulation
-
h e 1000 f d a c b Extensions:Rigid cube Texture0 Texture1 ContraintsTexture
-
Rigid cubes:Screenshot
-
Improving the operation:Render to vertex array • Enables interpretation of floating indicate textures equally geometry • Possible on NVIDIA hardware using the "NV_pixel_data_range" (PDR) extension • Allocate vertex array in video retention (VAR) • Setup PDR to point to same video memory • Do glReadPixels from float buffer to PDR memory • Render vertex array • Happens entirely on card, no CPU intervention
-
Videos • Fabric with 64x64 mesh on CPU • Fabric with 64x64 mesh on GPU • Fabric with 128x128 mesh on CPU • Textile with 128x128 mesh on GPU • 400 cubes on GPU
-
Results:P4 three.6Ghz + NVidia GeForce 6800
Source: https://www.slideserve.com/brent/physically-based-simulation-on-gpu-using-opengl-and-glsl
0 Response to "drawing 3d objects using opengl ppt"
Post a Comment