游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1842|回复: 0

[求助]我想把2d图片作成3d游戏的背景,看看我这样做对吗

[复制链接]

2

主题

9

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2006-5-30 21:52:00 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace BackGround_2
{
    public partial class BackGround : Form
    {
        private Device bgDevice;
        private PresentParameters presentParams;
        private string TextureName;
        Texture bgTexture;
        VertexBuffer vertexBuffer = null;
        BackGround backGround;

        public void InitializeGraphics()
        {
            presentParams = new PresentParameters();
            presentParams.SwapEffect = SwapEffect.Discard;
            presentParams.EnableAutoDepthStencil = true;
            presentParams.AutoDepthStencilFormat = DepthFormat.D16;
            presentParams.PresentationInterval = PresentInterval.One;
            presentParams.Windowed = true;

            bgDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

            backGround = new BackGround(bgDevice,"background.jpg");

        }

        public BackGround()
        {
            InitializeComponent();
        }

        public BackGround(Device bgDevice,string FileName)
        {
            TextureName = FileName;

            bgTexture = TextureLoader.FromFile(bgDevice,TextureName);
            vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedTextured), 4, bgDevice, 0, CustomVertex.TransformedTextured.Format, Pool.Managed);

            Reset(800, 600);
        }

        public void Reset(int Width, int Height)
        {
            CustomVertex.TransformedTextured[] verts = (CustomVertex.TransformedTextured[])vertexBuffer.Lock(0, 0);

            verts[0].Position = new Vector4(0, 0, 0, 1);
            verts[0].Tu = 0.0f;
            verts[0].Tv = 0.0f;

            verts[1].Position = new Vector4(Width, 0, 0, 1);
            verts[1].Tu = 1.0f;
            verts[1].Tv = 0.0f;

            verts[2].Position = new Vector4(Width, Height, 0, 1);
            verts[2].Tu = 1.0f;
            verts[2].Tv = 1.0f;

            verts[3].Position = new Vector4(0, Height, 0, 1);
            verts[3].Tu = 0.0f;
            verts[3].Tv = 1.0f;

            vertexBuffer.Unlock();
        }

        public void Render()
        {
            bgDevice.Clear(ClearFlags.ZBuffer | ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
            bgDevice.BeginScene();

            bgDevice.SetTexture(0, bgTexture);
            bgDevice.RenderState.ZBufferEnable = false;
            bgDevice.SetStreamSource(0, vertexBuffer, 0);
            bgDevice.VertexFormat = CustomVertex.TransformedTextured.Format;
            bgDevice.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);就是这句出错
            bgDevice.RenderState.ZBufferEnable = true;
            bgDevice.EndScene();
            bgDevice.Present();
        }

    }
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-24 15:31

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表