WebGL自學課程(10):通過OpenStreetMap獲取資料繪製地球

孫群發表於2012-09-13

好久沒寫部落格了,今天再寫一篇。前幾天想通過OpenStreetMap訪問資料來繪製一個最最最最最簡單的WebGoogleEarth的雛形,這個Demo比較簡單,只是簡單的獲取OpenStreetMap某一個切片層級下面的所有的切片,然後按照正確的貼圖方式繪製在地球上,也就是說這個Demo沒有考慮圖層的動態管理等等。最後將渲染的程式碼以EarthOSM類的形式封裝到了自己的World.js中,可以用滑鼠進行三維互動,World.js的連結地址是http://blog.csdn.net/sunqunsunqun/article/details/7885639.

下面是截圖:


下面是放大後的截圖:


以下是前端程式碼:

<!DOCTYPE HTML>
<html>
<head>
    <title>WebGL Terrain Map</title>
	<meta http-equiv="Content-Type" content="text/html"/>
    <meta name="charset" content="utf-8"/>
	<style type="text/css">
        html, body { margin: 0; padding: 0; width: 100%; height: 100% ;font-family:"Times New Roman",Georgia,Serif;}
        div { margin: 0; padding: 0 }
		.head{width:100%; height:25px; background-color:#5998DD;color:#ffffff;font-size:13px;
			  line-height:25px;border-top-left-radius:5px;border-top-right-radius:5px;-webkit-border-top-left-radius:5px;
			  -webkit-border-top-right-radius:5px;-moz-border-radius-topleft:5px;-moz-border-radius-topright:5px;}
		.tip{font-size:8px;display:block;height:8px;margin-left:3px;margin-top:9px;}
    </style>	
	<script src='http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.1' data-dojo-config='parseOnLoad: true'>
	<script type="text/javascript" src="World0.3.4.js"></script>
    <script type="text/javascript" src="CanvasEventHandler.js"></script>
    <script type="text/javascript">
        var vertexShaderContent,fragmentShaderContent,camera,scene,renderer,earthOSM;
		
        function getShaderContent(shaderType){
            var shaderUrl;
            if(shaderType == "VERTEX_SHADER"){
                shaderUrl = "VertexShader.txt";
            }
            else if(shaderType == "FRAGMENT_SHADER"){
                shaderUrl = "FragmentShader.txt";
            }
            var xhrArgs = {
                url:shaderUrl,
                handleAs:"text",
                sync:true,
                preventCache:true,
                load:function(data){
                    if(shaderType == "VERTEX_SHADER"){
                        vertexShaderContent = data;
                    }
                    else if(shaderType == "FRAGMENT_SHADER"){
                        fragmentShaderContent = data;
                    }
                },
                error:function(error){
                    alert("獲取ShaderContent出錯!");
                }
            };
            dojo.xhrGet(xhrArgs);
        }

        function startWebGL(){
            getShaderContent("VERTEX_SHADER");
            getShaderContent("FRAGMENT_SHADER");
            renderer = new World.WebGLRenderer(dojo.byId("canvasId"),vertexShaderContent,fragmentShaderContent);
            //World.enableAmbientLight();
            //World.enableParallelLlight(new World.Vector(0,-30,-50),new World.Vertice(0,0,0.8));
            World.disableAmbientLight();
            World.disableParallelLlight();
            camera = new World.PerspectiveCamera(90,1,1.0,200.0);
            camera.look(new World.Vertice(0,0,60),new World.Vertice(0,0,0));
            scene = new World.Scene();
            earthOSM = new World.EarthOSM(40,5);
            scene.add(earthOSM);
            renderer.bindScene(scene);
            renderer.bindCamera(camera);
            renderer.setIfAutoRefresh(true);
        }

		function initEvents(){
			dojo.connect(dojo.byId("canvasId"),"mousedown",onMouseDown);
            dojo.connect(dojo.byId("canvasId"),"onmouseup",onMouseUp);
            dojo.connect(dojo.byId("canvasId"),'mousewheel',onMouseWheel);
            dojo.connect(dojo.byId("canvasId"),'DOMMouseScroll',onMouseWheel);
		}
		
        function initAll(){
			initEvents();
            startWebGL();
        }
		
        dojo.addOnLoad(initAll);
    </script>
</head>
<body>
	<canvas id="canvasId" width="625" height="625"></canvas>
</body>
</html>

注:轉載請註明出處

相關文章