2016-12-25 4 views
0

Ich habe ein großes Problem, das auftritt, wenn ich das Objekt auf ein Framebuffer-Objekt rende und dann eine Quad-Fläche der resultierenden Textur auf einer Webgl-Leinwand rendere. In meinem Projekt versuche ich Objekte mit (45 Projektionsmatrizen) darzustellen. Ich benutzte eine Schleife in der Zeichnungsfunktion, um das Rendern in 45 Framebuffern zu machen, aber wenn ich das gerenderte Texturobjekt wiedergab, verschwand es für einige Frames, tauchte wieder auf und verschwand dann wieder. Was das noch verrückter macht ist, dass manchmal, wenn ich meine Anwendung benutze, alles perfekt und ohne Flackern angezeigt wird.Ursachen für flackernde/verschwindende Objekte

Wenn jemand Ursachen für die Symptome vorschlagen könnte, die ich erwähnte, würde ich es sehr schätzen.

Vielen Dank im Voraus

+0

Von der SO-Hilfe: „Fragen Debug-Hilfe zu suchen (“? Warum dieser Code nicht funktioniert ") muss das gewünschte Verhalten, um ein bestimmtes Problem oder Fehler enthalten ** und die der kürzeste Code, der notwendig ist, um ihn in der Frage selbst zu reproduzieren **. " – gman

+0

Ich speichere die Projektionsmatrizen in 45 Projektionsmatrizen Array: ProjectionMatrix [0] ..... ProjectionMatrix [45] und CameraPositions auch im 45er Array: CameraPositions [0] .... CameraPositions [45] das gleiche für Focals. – nina

+0

Ich habe das Rendering in Framebuffers gemacht: 45 Framebuffer auch. – nina

Antwort

-1
var shCam1={}; 
shCam1.init=function() 
{ 
var fragShader = getShader(gl, "shader-fCam1"); 
var vertexShader = getShader(gl, "shader-vCam1"); 

shCam1.prog = gl.createProgram(); 
gl.attachShader(shCam1.prog, vertexShader); 
gl.attachShader(shCam1.prog, fragShader); 

gl.linkProgram(shCam1.prog); 


if (!gl.getProgramParameter(shCam1.prog, gl.LINK_STATUS)) { 
    alert("Could not initialise shaders"); 
} 

gl.useProgram(shCam1.prog); 
shCam1.prog.vertices = gl.getAttribLocation(shCam1.prog, "vertex"); 
gl.enableVertexAttribArray(shCam1.prog.vertices); 

shCam1.prog.normal = gl.getAttribLocation(shCam1.prog, "normal"); 
gl.enableVertexAttribArray(shCam1.prog.normal); 

shCam1.prog.radius = gl.getAttribLocation(shCam1.prog, "radius"); 
gl.enableVertexAttribArray(shCam1.prog.radius); 

shCam1.prog.pMatrixUniform1 = gl.getUniformLocation(shCam1.prog,"ProjCam"); 
shCam1.prog.camPos1 = gl.getUniformLocation(shCam1.prog, "PosCam"); 
shCam1.prog.focalLoc=gl.getUniformLocation(shCam1.prog, "focal"); 
shCam1.prog.HeightLoc=gl.getUniformLocation(shCam1.prog, "height"); 
shCam1.prog.WidthLoc=gl.getUniformLocation(shCam1.prog, "width"); 
} 


shCam1.draw = function() 
{ 
if(!mesh) return; 
for(var i=0;i<45;i++){ 
gl.bindTexture(gl.TEXTURE_2D, null); 
gl.bindBuffer(gl.ARRAY_BUFFER, null); 
gl.bindFramebuffer(gl.FRAMEBUFFER, null); 
gl.bindFramebuffer(gl.FRAMEBUFFER, rtFrameBuffers[i][0]); 
gl.viewport(0,0,512,512); 
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 
gl.useProgram(shCam1.prog); 
gl.bindBuffer(gl.ARRAY_BUFFER, mesh.vertexBuffer); 
gl.vertexAttribPointer(shCam1.prog.vertices, mesh.vertexBuffer.itemSize,  gl.FLOAT, false, 0, 0); 

gl.bindBuffer(gl.ARRAY_BUFFER, mesh.normalBuffer); 
gl.vertexAttribPointer(shCam1.prog.normal, mesh.normalBuffer.itemSize, gl.FLOAT, false, 0, 0); 

gl.bindBuffer(gl.ARRAY_BUFFER, mesh.radiusBuffer); 
gl.vertexAttribPointer(shCam1.prog.radius, mesh.radiusBuffer.itemSize, gl.FLOAT, false, 0, 0); 

if((i==12)||(i==13)||(i==14)||(i==27)||(i==28)||(i==29)||(i==42)||(i==43)||(i==44)){ 
gl.uniform1f(shCam1.prog.HeightLoc,5616.0); 
gl.uniform1f(shCam1.prog.WidthLoc,3744.0); 
} 
else{ 
gl.uniform1f(shCam1.prog.HeightLoc,3744.0); 
gl.uniform1f(shCam1.prog.WidthLoc,5616.0); 
} 
gl.uniformMatrix4fv(shCam1.prog.pMatrixUniform1, false,ProjectionMatrix[i]); 
gl.uniform3fv(shCam1.prog.camPos1, CameraPosition[i]); 
gl.uniform1f(shCam1.prog.focalLoc,Focals[i]); 

gl.drawArrays(gl.POINTS,0,mesh.vertexBuffer.numItems); 
} 
} 

<script id="shader-vCam1" type="x-shader/x-vertex"> 
attribute vec3 vertex; 
attribute vec3 normal; 
uniform mat4 ProjCam; 
uniform vec3 CamPos; 
uniform float focal; 
uniform float height; 
uniform float width;   
attribute float radius; 
varying vec3 vN; 
varying float splat_radius; 
varying float depth; 
varying float scale_radius; 
void main(void) { 
float n=0.1; 
float f=100.0; 
float fovy=2.0* atan(0.5*(height/focal)); 
scale_radius=1.0/(2.0*tan(fovy/2.0)); 
float dot_product=dot(normalize(vertex-Campos),normal); 
if(dot_product>=0.0){ 
splat_radius=0.0; 
gl_Position=vec4(1.0,1.0,1.0,1.0); 
} 
else{ 
splat_radius=radius; 
vN=normal; 
depth=(ProjCam*vec4(vertex, 1.0)).z; 
vec4 Frag_clipSpace=ProjCam*vec4(vertex, 1.0); 
vec2 Frag_NdcSpace=(Frag_clipSpace.xy/Frag_clipSpace.z); 
float x=Frag_NdcSpace.x/width; 
float y=Frag_NdcSpace.y/height; 
float tx=x*2.0-1.0; 
float ty=1.0-2.0*y; 
float invz=(-1.0*Frag_clipSpace.z); 
float newz=(invz*(f+n))/(n-f)+(2.0*f*n)/(n-f); 
float zndc=newz/Frag_clipSpace.z; 
gl_Position = vec4(tx,ty,zndc,1.0); 
gl_PointSize=1.0; 
}       

} 



<script id="shader-fCam1" type="x-shader/x-fragment"> 
#extension GL_EXT_draw_buffers : require 
precision mediump float; 
varying vec3 vN; 
varying float splat_radius; 
varying float depth; 
varying float scale_radius; 
void main(void) { 
if(splat_radius<=0.0) 
discard; 

float proj_radius=splat_radius*0.7*scale_radius/depth; 
float x=(gl_FragCoord.x-0.5)/512.0; 
float y=(gl_FragCoord.y-0.5)/512.0; 

gl_FragData[0]=vec4(proj_radius,normalize(vN)); 
gl_FragData[1]=vec4(depth,proj_radius,x,y); 
}   

</script> 
+0

das ist, was ich getan habe, aber ich verstehe nicht, wo ist das Problem? – nina

+0

Dies ist keine ** Antwort **. Bitte verschiebe Updates deiner Frage ** auf die Frage selbst ** – gman

Verwandte Themen