Windows Phone 7 3D开发中使用纹理贴图

简介:   Windows Phone 7对3D的支持还是不错的,据说是用OpenGL/ES做的,使用起来倒是也有点那种感觉。本文就不讲XNA 4.0的游戏框架了,直接上一段代码,该代码使用VertexPositionColor渲染了一个三角形,程序运行一切正常。

  Windows Phone 7对3D的支持还是不错的,据说是用OpenGL/ES做的,使用起来倒是也有点那种感觉。本文就不讲XNA 4.0的游戏框架了,直接上一段代码,该代码使用VertexPositionColor渲染了一个三角形,程序运行一切正常。

1 . + expand sourceview plaincopy to clipboardprint ?
2 .

  运行结果如下:

运行结果

  在确认了3D开发的这种代码结构以后,用VertexPositionTexture渲染同样的三角形,只是这次采用纹理贴图,代码如下:

1 .
2 . view plaincopy to clipboardprint ?
3 . VertexPositionTexture[] trangleTexture;
4 .
5 . protected override void LoadContent()
6 . {
7 . spriteBatch = new SpriteBatch(GraphicsDevice);
8 .
9 . image = Content.Load < Texture2D > (@ " Images/Tulips " );
10 . trangleTexture = new VertexPositionTexture[]{
11 . new VertexPositionTexture( new Vector3( 0 , 1 , 0 ), new Vector2( 0.5f , 0 ) ),
12 . new VertexPositionTexture( new Vector3( 1 , - 1 , 0 ), new Vector2( 1 ,1f) ),
13 . new VertexPositionTexture( new Vector3( - 1 , - 1 , 0 ), new Vector2( 0 ,1f) )
14 . };
15 .
16 . vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), trangleTexture.Length, BufferUsage.None);
17 . vertexBuffer.SetData < VertexPositionTexture > (trangleTexture);
18 .
19 . basicEffect = new BasicEffect(GraphicsDevice);
20 .
21 . GraphicsDevice.SetVertexBuffer(vertexBuffer);
22 . }
23 .
24 . protected override void Draw(GameTime gameTime)
25 . {
26 . GraphicsDevice.Clear(Color.CornflowerBlue);
27 .
28 . basicEffect.World = world;
29 . basicEffect.View = camera.view;
30 . basicEffect.Projection = camera.projection;
31 . basicEffect.Texture = image;
32 . basicEffect.TextureEnabled = true ;
33 .
34 . foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
35 . {
36 . pass.Apply();
37 . GraphicsDevice.DrawUserPrimitives < VertexPositionTexture > (PrimitiveType.TriangleStrip, trangleTexture, 0 , 1 );
38 . }
39 . base.Draw(gameTime);
40 . }

  啰嗦一句,在此代码中VertexPositionTexture的第二个Vetex2代表的是UV坐标,对应的含义是(0,0)点对应了纹理图片的左上角,(1,1)点对应了纹理图片的右下角。

  上述代码在运行的时候会在VS2010的输出窗口中显示:

A first chance exception of type 'System.NotSupportedException' occurred in Microsoft.Xna.Framework.Graphics.dll
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in Microsoft.Xna.Framework.dll

  同时模拟器里的程序直接退出,看不到结果。原因是什么呢?疑惑并仔细检视代码中……

  与前一个彩色三角形对比,顶点顺序没变,摄像机位置没变,投影矩阵没变,按说是不可能出现这种问题的,而且程序直接崩了,没有信息抛出,真是很郁闷。

  经过不断的试错,在宣布放弃之前,忽然想起来关于纹理方面的一个注意事项。有过3D开发经验的朋友都知道,纹理是要求符合2的整数次方对齐的,而我所加载的来自于外部任意图片的纹理不符合这一要求,所以程序挂了。

  又查了一些资料,找到了准确的原因。原来是Windows Phone 7 的XNA中默认的纹理寻址模式使用了Wrap,造成了与GPU的不兼容,如果改成Clamp就好了。

  看来在这个地方微软得要有文档说明才好,否则还真是难找问题所在。修改后的代码如下:

1 . view plaincopy to clipboardprint ?
2 . protected override void LoadContent()
3 . {
4 . // Create a new SpriteBatch, which can be used to draw textures.
5 . spriteBatch = new SpriteBatch(GraphicsDevice);
6 .
7 . image = Content.Load < Texture2D > (@ " Images/Tulips " );
8 .
9 . trangleTexture = new VertexPositionTexture[]{
10 . new VertexPositionTexture( new Vector3( 0 , 1 , 0 ), new Vector2( 0.5f , 0 ) ),
11 . new VertexPositionTexture( new Vector3( 1 , - 1 , 0 ), new Vector2( 1 ,1f) ),
12 . new VertexPositionTexture( new Vector3( - 1 , - 1 , 0 ), new Vector2( 0 ,1f) )
13 . };
14 .
15 . vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), trangleTexture.Length, BufferUsage.None);
16 . vertexBuffer.SetData < VertexPositionTexture > (trangleTexture);
17 .
18 . basicEffect = new BasicEffect(GraphicsDevice);
19 .
20 . GraphicsDevice.SetVertexBuffer(vertexBuffer);
21 . GraphicsDevice.SamplerStates[ 0 ] = SamplerState.PointClamp;
22 . }
23 .

  最终的模拟器结果是:

最终的模拟器结果

  不管怎么说,Windows Phone 7的XNA游戏开发框架以及3D方面的开发接口还是很出色的,顶一下微软,并希望这个平台能尽快发展起来。

  附Camera的代码:

1 . view plaincopy to clipboardprint ?
2 . using System;
3 . using System.Collections.Generic;
4 . using System.Linq;
5 . using Microsoft.Xna.Framework;
6 . using Microsoft.Xna.Framework.Audio;
7 . using Microsoft.Xna.Framework.Content;
8 . using Microsoft.Xna.Framework.GamerServices;
9 . using Microsoft.Xna.Framework.Graphics;
10 . using Microsoft.Xna.Framework.Input;
11 . using Microsoft.Xna.Framework.Media;
12 .
13 .
14 . namespace WindowsPhoneGame1
15 . {
16 . public class Camera : Microsoft.Xna.Framework.GameComponent
17 . {
18 . public Matrix view{get; protected set;}
19 . public Matrix projection { get; protected set; }
20 .
21 . public Camera(Game game,Vector3 pos,Vector3 target,Vector3 up)
22 . : base(game)
23 . {
24 . view = Matrix.CreateLookAt(pos, target, up);
25 . projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, ( float )game.Window.ClientBounds.Width / ( float )game.Window.ClientBounds.Height, 1 , 100 );
26 . }
27 .
28 . public override void Initialize()
29 . {
30 . base.Initialize();
31 . }
32 .
33 . public override void Update(GameTime gameTime)
34 . {
35 . base.Update(gameTime);
36 . }
37 . }
38 . }
39 .
目录
相关文章
|
10月前
|
IDE 关系型数据库 开发工具
使用Visual Basic进行Windows窗体开发
【4月更文挑战第27天】本文介绍了使用Visual Basic进行Windows窗体(WinForms)开发的步骤,从搭建开发环境到创建、设计用户界面,再到编写事件驱动的代码和数据绑定。Visual Basic结合WinForms提供了一种易学易用的桌面应用开发方案。通过调试、优化、部署和维护,开发者可以构建专业应用程序。随着技术发展,掌握最新UI设计和开发工具对于保持竞争力至关重要。本文为初学者提供了基础指导,鼓励进一步探索和学习。
280 0
|
5月前
|
Ubuntu Linux Python
如何利用wsl-Ubuntu里conda用来给Windows的PyCharm开发
如何在WSL(Windows Subsystem for Linux)的Ubuntu环境中使用conda虚拟环境来为Windows上的PyCharm开发设置Python解释器。
439 1
|
5月前
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
203 0
|
6月前
|
存储 安全 程序员
Windows任务管理器开发原理与实现
Windows任务管理器开发原理与实现
|
8月前
|
Linux Apache C++
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
该文介绍了如何在Windows环境下为FFmpeg集成SRT协议支持库libsrt。首先,需要安装Perl和Nasm,然后编译OpenSSL。接着,下载libsrt源码并使用CMake配置,生成VS工程并编译生成srt.dll和srt.lib。最后,将编译出的库文件和头文件按照特定目录结构放置,并更新环境变量,重新配置启用libsrt的FFmpeg并进行编译安装。该过程有助于优化直播推流的性能,减少卡顿问题。
182 2
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
|
7月前
|
开发者 C# Windows
WPF与游戏开发:当桌面应用遇见游戏梦想——利用Windows Presentation Foundation打造属于你的2D游戏世界,从环境搭建到代码实践全面解析新兴开发路径
【8月更文挑战第31天】随着游戏开发技术的进步,WPF作为.NET Framework的一部分,凭借其图形渲染能力和灵活的UI设计,成为桌面游戏开发的新选择。本文通过技术综述和示例代码,介绍如何利用WPF进行游戏开发。首先确保安装最新版Visual Studio并创建WPF项目。接着,通过XAML设计游戏界面,并在C#中实现游戏逻辑,如玩家控制和障碍物碰撞检测。示例展示了创建基本2D游戏的过程,包括角色移动和碰撞处理。通过本文,WPF开发者可更好地理解并应用游戏开发技术,创造吸引人的桌面游戏。
332 0
|
7月前
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
659 0
|
9月前
|
网络安全 C++ Windows
【Windows驱动开发】(主机)VS2017+(虚拟机)win10系统------双机调试
【Windows驱动开发】(主机)VS2017+(虚拟机)win10系统------双机调试
|
9月前
|
Windows
【Windows驱动开发】注册表的基本操作(创建、打开、修改、读取、枚举)(附源码)
【Windows驱动开发】注册表的基本操作(创建、打开、修改、读取、枚举)(附源码)
|
10月前
|
算法 Linux Windows
FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass
在Windows环境下为FFmpeg集成字幕渲染库libass涉及多个步骤,包括安装freetype、libxml2、gperf、fontconfig、fribidi、harfbuzz和libass。每个库的安装都需要下载源码、配置、编译和安装,并更新PKG_CONFIG_PATH环境变量。最后,重新配置并编译FFmpeg以启用libass及相关依赖。完成上述步骤后,通过`ffmpeg -version`确认libass已成功集成。
225 1
FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass