XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

Manual Box With Light! unexpected error

Last post 05-12-2008 1:15 PM by Shawn Hargreaves. 1 replies.
  • 05-12-2008 7:37 AM

    Manual Box With Light! unexpected error

    Hi, i just started writing and playing around with 3d code so well now got that far that i can create a box and display it.. any way it didnt look so good with no shading... so what i wanted to do is add some light effect and by looking at samples i found was basicEffect.EnableDefaultLighting();(line 322)  and basicEffect.PreferPerPixelLighting = true;(line 323) but if i run that code i just get a exception: unexpected error has ocurred.
    tnx alot
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework;

    namespace DrawLines
    {
    class Box
    {
    float x;

    public float X
    {
    get
    {
    return x;
    }
    set
    {
    x = value;
    setVertextPositions();
    }
    }

    float y;

    public float Y
    {
    get
    {
    return y;
    }
    set
    {
    y = value;
    setVertextPositions();
    }
    }

    float z;

    public float Z
    {
    get
    {
    return z;
    }
    set
    {
    z = value;
    setVertextPositions();
    }
    }

    Color topColor;
    public Color TopColor
    {
    get
    {
    return topColor;
    }
    set
    {
    topColor = value;
    setVertextPositions();
    }
    }

    Color leftColor;
    public Color LeftColor
    {
    get
    {
    return leftColor;
    }
    set
    {
    leftColor = value;
    setVertextPositions();
    }
    }

    Color rightColor;
    public Color RightColor
    {
    get
    {
    return rightColor;
    }
    set
    {
    rightColor = value;
    setVertextPositions();
    }
    }

    Color bottomColor;
    public Color BottomColor
    {
    get
    {
    return bottomColor;
    }
    set
    {
    bottomColor = value;
    setVertextPositions();
    }
    }

    Color frontColor;
    public Color FrontColor
    {
    get
    {
    return frontColor;
    }
    set
    {
    frontColor = value;
    setVertextPositions();
    }
    }

    Color backColor;
    public Color BackColor
    {
    get
    {
    return backColor;
    }
    set
    {
    backColor = value;
    setVertextPositions();
    }
    }

    float width;

    public float Width
    {
    get
    {
    return width;
    }
    set
    {
    width = value;
    setVertextPositions();
    }
    }

    float heigth;
    public float Heigth
    {
    get
    {
    return heigth;
    }
    set
    {
    heigth = value;
    setVertextPositions();
    }
    }

    float depth;
    float Depth
    {
    get
    {
    return depth;
    }
    set
    {
    depth = value;
    setVertextPositions();
    }
    }

    //Back
    Vector3 upperLeftBack;
    Vector3 upperRightBack;
    Vector3 lowerLeftBack;
    Vector3 lowerRightBack;

    //Front
    Vector3 upperLeftFront;
    Vector3 upperRightFront;
    Vector3 lowerLeftFront;
    Vector3 lowerRightFront;

    VertexPositionColor[] vertices;

    BasicEffect basicEffect;
    GraphicsDevice device;
    int primitiveCount = 0;

    public Matrix View
    {
    get
    {
    return basicEffect.View;
    }
    set
    {
    basicEffect.View = value;
    }
    }

    public Matrix Projection
    {
    get
    {
    return basicEffect.Projection;
    }
    set
    {
    basicEffect.Projection = value;
    }
    }

    public Box(GraphicsDevice device, float x, float y, float z, float width, float heigth, float depth, Color color)
    {
    this.x = x;
    this.y = y;
    this.z = z;
    this.width = width;
    this.heigth = heigth;
    this.depth = depth;
    this.device = device;

    setAllColors(color);

    setVertextPositions();

    basicEffect = new BasicEffect(device, null);
    basicEffect.VertexColorEnabled = true;


    basicEffect.Projection = Matrix.CreateOrthographicOffCenter
    (0, device.Viewport.Width,
    device.Viewport.Height, 0,
    0, 1);
    }

    public void SetAllColors(Color color)
    {
    setAllColors(color);
    setVertextPositions();
    }

    private void setAllColors(Color color)
    {
    topColor = color;
    frontColor = color;
    leftColor = color;
    rightColor = color;
    bottomColor = color;
    backColor = color;
    }

    private void setVertextPositions()
    {
    float realX = x - width / 2f;
    float realY = y - heigth / 2f;
    float realZ = z - depth / 2f;
    upperLeftBack = new Vector3(realX, realY, realZ);
    upperRightBack = new Vector3(realX + width, realY, realZ);
    lowerLeftBack = new Vector3(realX, realY + heigth, realZ);
    lowerRightBack = new Vector3(realX + width, realY + heigth, realZ);

    upperLeftFront = new Vector3(realX, realY, realZ + depth);
    upperRightFront = new Vector3(realX + width, realY, realZ + depth);
    lowerLeftFront = new Vector3(realX, realY + heigth, realZ + depth);
    lowerRightFront = new Vector3(realX + width, realY + heigth, realZ + depth);

    vertices = new VertexPositionColor[]{
    //Cube
    #region front
    //new triangle
    new VertexPositionColor(upperRightFront, frontColor),
    new VertexPositionColor(upperLeftFront, frontColor),
    new VertexPositionColor(lowerLeftFront, frontColor),
    //new triangle
    new VertexPositionColor(upperRightFront, frontColor),
    new VertexPositionColor(lowerLeftFront, frontColor),
    new VertexPositionColor(lowerRightFront, frontColor),
    #endregion

    #region bottom
    //new triangle
    new VertexPositionColor(lowerRightBack, bottomColor),
    new VertexPositionColor(lowerLeftFront, bottomColor),
    new VertexPositionColor(lowerLeftBack, bottomColor),
    //new triangle
    new VertexPositionColor(lowerRightFront, bottomColor),
    new VertexPositionColor(lowerLeftFront, bottomColor),
    new VertexPositionColor(lowerRightBack, bottomColor),
    #endregion

    #region Right
    //new triangle
    new VertexPositionColor(upperRightBack, rightColor),
    new VertexPositionColor(lowerRightFront, rightColor),
    new VertexPositionColor(lowerRightBack, rightColor),
    //new triangle
    new VertexPositionColor(lowerRightFront, rightColor),
    new VertexPositionColor(upperRightBack, rightColor),
    new VertexPositionColor(upperRightFront, rightColor),
    #endregion

    #region Top
    //new triangle

    new VertexPositionColor(upperRightFront, topColor),
    new VertexPositionColor(upperRightBack, topColor),
    new VertexPositionColor(upperLeftBack, topColor),
    //new triangle

    new VertexPositionColor(upperRightFront, topColor),
    new VertexPositionColor(upperLeftBack, topColor),
    new VertexPositionColor(upperLeftFront, topColor),
    #endregion

    #region Left
    //new triangle

    new VertexPositionColor(upperLeftFront, leftColor),
    new VertexPositionColor(upperLeftBack, leftColor),
    new VertexPositionColor(lowerLeftBack, leftColor),
    //new triangle

    new VertexPositionColor(lowerLeftFront, leftColor),
    new VertexPositionColor(upperLeftFront, leftColor),
    new VertexPositionColor(lowerLeftBack, leftColor),
    #endregion

    #region Back
    //new triangle

    new VertexPositionColor(upperLeftBack, backColor),
    new VertexPositionColor(upperRightBack, backColor),
    new VertexPositionColor(lowerLeftBack, backColor),
    //new triangle

    new VertexPositionColor(upperRightBack, backColor),
    new VertexPositionColor(lowerRightBack, backColor),
    new VertexPositionColor(lowerLeftBack, backColor),
    #endregion
    };
    primitiveCount = vertices.Length / 3;


    }

    public void Draw()
    {
    //basicEffect.EnableDefaultLighting(); //If uncommented exception is thrown at device.DrawUserPrimitives..
    //basicEffect.PreferPerPixelLighting = true;

    device.RenderState.DepthBufferEnable = true;
    basicEffect.Begin();
    basicEffect.CurrentTechnique.Passes[0].Begin();
    device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, vertices, 0,
    primitiveCount);
    basicEffect.CurrentTechnique.Passes[0].End();
    basicEffect.End();


    }
    }
    }
     
  • 05-12-2008 1:15 PM In reply to

    Re: Manual Box With Light! unexpected error

    To use lighting, your vertex data must contain normal information. You will need to switch from VertexPositionColor to a vertex format that includes normal data, and fill in the normal vectors along with your box position values.
    XNA Framework Developer - blog - homepage
Page 1 of 1 (2 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG