performance drop when drawing a line in OpenGL
文章推薦指數: 80 %
There is no any function in modern OpenGL to draw a sphere using a simple one line command. You need to generate is using triangles or lines ... AllContent Blogs Forums News Tutorials LogIn SignUp Login Username/Email Password Rememberme Forgotpassword? Login or Don'thaveaGameDev.netaccount?Signup Forgotyourpassword? EmailAddress ResetPassword Pleasecontactusifyouhaveanytroubleresettingyourpassword. Home Blogs Careers Careers Forums News Portfolios Projects Tutorials New?Learnaboutgamedevelopment FollowUs ChatintheGameDev.netDiscord! BacktoGraphicsandGPUProgramming performancedropwhendrawingalineinOpenGL GraphicsandGPUProgramming Programming Startedby jt.tarigan April22,202005:47PM 6 comments,lastbyfleabay2 years,4 monthsago Advertisement jt.tarigan Author 108 April22,202005:47PM ItriedtodrawasimplelineinOpenGL,thecodebelowvoiddisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(0.0,50.0f,0.0,0.0,0.0,0.0,0.0,0.0,-1.0); glBegin(GL_LINES); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(50.0f,50.0f,50.0f); glEnd(); //glutWireSphere(20.0f,20.0f,20.0f); glutSwapBuffers(); glFlush(); }However,whenIruntheprogram,theperformancedropsignificantlythenafterafewseconds,itstopsresponding(Ihavetoruntaskmanagerandforcequittheprogram).However,whenIdisablethelineanddrawtheglutWireSphere(orapolygon),theperformanceisnormal.Isthereaproblemwithmycode?OrperhapsGL_LINESisdeprecated?Windows10,1070MaxQ,VisualStudioThankyou Cancel Save Green_Baron 219 April22,202006:41PM Itlookslikeyou'remakingafirstcontactwithOpenGL.Nothingwrongwiththat!GL_LINESisamongthefewthingsthatarenotdeprecated,thoughitisusedinadifferentcontextthesedays,likedrawcallsandshaderpilelines.Ifyouarenotforcedtouseopengl1,wouldyoumindlearningviahttps://learnopengl.com/orhttps://www.opengl-tutorial.org/?Or,ifyoulovebooks,OpenGLProgrammingGuide9thandOpenGLSuperbible7th?Anyway,ifyouwantustodebugforyou,weneedmorethanjustaubiquitiouslookingfunction. Cancel Save 8Observer8 386 April23,202007:32PM Please,donotuselegacy/deprecatedOpenGL1.Iaddtwotutorialsinthelistabove:http://ogldev.org/andhttps://thebookofshaders.com/Youcanfindalotofverygoodbooksaboutshaders.Ilikethisone:WebGLProgrammingGuideThisismyexamplehowtodrawonelineusingshaders,WebGLandTypeScript:clicktoruninplaygroundMain.ts import*as$from"jquery"; import{mat4}from"gl-matrix"; $(()=> { //GetWebGLcontext letcanvas=document.getElementById("renderCanvas")asHTMLCanvasElement; letgl=canvas.getContext("webgl"); letvShaderSource=[ "attributevec3aPosition;", "uniformmat4uProjMatrix;", "voidmain()", "{", "gl_Position=uProjMatrix*vec4(aPosition,1.0);", "}" ].join("\n"); letfShaderSource=[ "voidmain()", "{", "gl_FragColor=vec4(1.0,0.0,0.0,1.0);", "}" ].join("\n"); letvShader=gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader,vShaderSource); gl.compileShader(vShader); letvMessage=gl.getShaderInfoLog(vShader); if(vMessage.length>0)console.log("VerexShaderError:"+vMessage); letfShader=gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader,fShaderSource); gl.compileShader(fShader); letfMessage=gl.getShaderInfoLog(fShader); if(fMessage.length>0)console.log("FragmentShaderError:"+fMessage); letprogram=gl.createProgram(); gl.attachShader(program,vShader); gl.attachShader(program,fShader); gl.linkProgram(program); letprogMessage=gl.getProgramInfoLog(program); if(progMessage.length>0)console.log("Failedtolinkaprogram:"+progMessage); gl.useProgram(program); letvertexPositions=[ 0.0,0.0,0.0, 70,70,0.0 ]; letvertexBuffer=gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array(vertexPositions),gl.STATIC_DRAW); letaPositionLocation=gl.getAttribLocation(program,"aPosition"); if(aPositionLocation<0)console.log("FailedtogetaaPositionlocation"); gl.vertexAttribPointer(aPositionLocation,3,gl.FLOAT,false,0,0); gl.enableVertexAttribArray(aPositionLocation); letaProjMatrixLocation=gl.getUniformLocation(program,"uProjMatrix"); if(!aProjMatrixLocation)console.log("FailedtogetauProjMatrixlocation"); letprojMatrix=mat4.ortho(mat4.create(),-100,100,-100,100,10,-10); gl.uniformMatrix4fv(aProjMatrixLocation,false,projMatrix); gl.clearColor(0.9,0.9,0.9,1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINES,0,2); }); Cancel Save 8Observer8 386 April23,202008:06PM jt.tarigansaid:glutWireSphere(20.0f,20.0f,20.0f);ThereisnoanyfunctioninmodernOpenGLtodrawasphereusingasimpleonelinecommand.Youneedtogenerateisusingtrianglesorlines.ButifyouwanttocreateshapesusingC++andsimplecommandsyoucanuserenderengineslike:Ogre3D,Urho3D,Irrlichtandsoon.SeehowsimpletocreateawiresphereusingBabylon.jsandTypeScript.YoucanrotateinonMobiledevicesbyfinger.ClicktorunonPlunker:https://plnkr.co/edit/Pr95JxPPbtRUzjB9?previewScene3D.ts import*asBABYLONfrom"babylonjs"; exportdefaultclassScene3D { private_engine:BABYLON.Engine; private_scene:BABYLON.Scene; publicconstructor(canvasName:string) { constcanvas=document.getElementById(canvasName)asHTMLCanvasElement; this._engine=newBABYLON.Engine(canvas,true); this._scene=newBABYLON.Scene(this._engine); constcamera=newBABYLON.ArcRotateCamera("camera",-120*Math.PI/180,70*Math.PI/180,5,newBABYLON.Vector3(0,0,0),this._scene); camera.attachControl(canvas,true); camera.wheelPrecision=100; camera.minZ=0.001; newBABYLON.HemisphericLight("light",newBABYLON.Vector3(0,1,0),this._scene); constskybox=BABYLON.MeshBuilder.CreateBox("skybox",{size:1000},this._scene); skybox.infiniteDistance=true; constskyboxMaterial=newBABYLON.StandardMaterial("skybox",this._scene); skyboxMaterial.backFaceCulling=false; constfiles=[ "https://dl.dropboxusercontent.com/s/d6pb1vco30tb1qd/skybox_px.jpg", "https://dl.dropboxusercontent.com/s/j8r319homxctq46/skybox_py.jpg", "https://dl.dropboxusercontent.com/s/owtkos3hjayv819/skybox_pz.jpg", "https://dl.dropboxusercontent.com/s/fn49xqtrz18h6vn/skybox_nx.jpg", "https://dl.dropboxusercontent.com/s/jdtd2cgpe13930o/skybox_ny.jpg", "https://dl.dropboxusercontent.com/s/shin4itwifrypl5/skybox_nz.jpg" ]; skyboxMaterial.reflectionTexture=BABYLON.CubeTexture.CreateFromImages(files,this._scene); skyboxMaterial.reflectionTexture.coordinatesMode=BABYLON.Texture.SKYBOX_MODE; skyboxMaterial.diffuseColor=newBABYLON.Color3(0,0,0); skyboxMaterial.specularColor=newBABYLON.Color3(0,0,0); skybox.material=skyboxMaterial; letsphere=BABYLON.MeshBuilder.CreateSphere("sphere",{segments:8,diameter:2},this._scene); sphere.material=newBABYLON.StandardMaterial("sphereMat",this._scene); sphere.material.wireframe=true; this.RunRenderLoop(); } privateRunRenderLoop():void { this._engine.runRenderLoop(()=> { this._scene.render(); }); window.onresize=()=> { this._engine.resize(); }; } } Cancel Save 21stCenturyMoose 13,459 April24,202006:12AM Noneoftheabovematters.TheadvicetonotuselegacyOpenGL,whilegood,isactuallycompletelyirrelevanttotheOP'sproblem,anditwouldbereallygoodifpeoplewouldstopdoingthiskindofthing.ThereareplentyofprogramsouttherethatuselegacyOpenGLbutdon'texhibittheproblemsencounteredbytheOP.UsinglegacyOpenGLisnotthecause,andmovingtomodernOpenGLwillnotfixit.There'snothingwrongwiththeOP'scodethatcouldcausethisproblem.Sothecauseisprobablyelsewhere,inothercodetheOPisn'tshowingus. Cancel Save Direct3Dhasneedofinstancing,butwedonot.WehaveplentyofglVertexAttribcalls. Green_Baron 219 April24,202009:55AM Wellyes,OPiskeepingbasicinformationtotackletheproblem,butIMOitisnotgoodadvicetoletanewcomerstartwithlegacyOpenGLifthereisnorealrequirementtodoso.Iftheycanchoose,whynotchoosetheso-calledmodernstyle.Corecontextdoesnotsupportlegacystyleanymore,thereisnoadvanceddebugging,andanywaysoonerthanlaterthey'dhavetolearnthemodernpathanyway,thereisnowayavoidingit.Andthereareenoughfirstclassresourcestostartwith.Itisalltrueandright,legacyisnotthecauseanduptodatecodewillnotfixamemoryleakorsomesuch,beweothernewcomerswhoonlyhadcursorycontactwithlegacy(e.g.whentranslatingoldtonewfromanexamplewefound)canbettercoveryourprofessionalguy'sbacksansweringquestions;-)AndOPhasaskedifsomethingisdeprecated:-) Cancel Save Share: Thistopicisclosedtonewreplies. Advertisement Advertisement PopularTopics IsitinbadtastetoincludeRussiaandUkraineinagameaboutrunningarestaurantchain? WritingforGames Howdoyoulikethisgameworldandbackstory? WritingforGames MayaorBlender? 2Dand3DArt GamedevduringaRecession? GDNetLounge Beziercurvecontrolpoint MathandPhysics CanyoutellmeagoodVPNservice? GDNetLounge Reticulatingsplines AboutGameDev.net TermsofService PrivacyPolicy ContactUs Copyright(c)1999-2021GameDev.net,LLC BacktoTop
延伸文章資訊
- 1Drawing Lines is Hard - Matt DesLauriers - Svbtle
Twitter: @mattdesl Drawing lines might not sound like rocket science, but it's damn difficult to ...
- 2Hello Triangle - LearnOpenGL
In modern OpenGL we are required to define at least a vertex and fragment ... and back of all tri...
- 3OpenGL 101: Drawing primitives - points, lines and triangles
OpenGL 101: Drawing primitives - points, lines and triangles. Posted on May 13, 2013 by Paul. The...
- 4Drawing thin 3D lines with modern OpenGL - Reddit
Drawing thin 3D lines with modern OpenGL. I'm a bit confused on how render thin 3D lines without ...
- 5Sample Modern OpenGL Programs - UCSD Math Department
SimpleDrawModern shows how to draw points, lines, line strips, line loops, and triangles. It incl...