Question

I've been learning about stage3D, and am trying to render something for the first time. No run-time errors appear during the course of this program, but I am not seeing any output... my code is as follows (with comments where necessary)

import flash.events.Event;
import flash.display.*;
import flash.display3D.*;
import flash.display3D.textures.Texture;
import stillicidium.rendering.MeshMaker;
import stillicidium.rendering.AGALMiniAssembler;

stage.scaleMode = 'noBorder';

var context0:Context3D;
var vBuff:VertexBuffer3D;
var iBuff:IndexBuffer3D;
var tex:Texture;

//MeshMaker is designed to make a stage-fitted n-by-n mesh with
//corresponding uv coords - form is like: (x, y, 0 ,u, v)
var mm:MeshMaker = new MeshMaker(this.stage, 20);
var vBuff_vec:Vector.<Number> = mm.GetVertexBuffer();
var iBuff_vec:Vector.<uint> = mm.GetIndexBuffer();
mm = null;

var vShader:AGALMiniAssembler = new AGALMiniAssembler();
var pShader:AGALMiniAssembler = new AGALMiniAssembler();
var program:Program3D;

//Create a shape for drawing
var square:Shape = new Shape();
square.graphics.lineStyle(3,0xffffff,1,true);
square.graphics.beginFill(0xaa00ff,1);
square.graphics.moveTo(100,100);
square.graphics.lineTo(stage.stageWidth-100,100);
square.graphics.lineTo(stage.stageWidth-100,stage.stageHeight-100);
square.graphics.lineTo(100,stage.stageHeight-100);
square.graphics.lineTo(100,100);

//Draw shape as BitmapData to use as texture
var bmpdata:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,0);
bmpdata.draw(square);

//Initialize stage3D
stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, initStage3D);
stage.stage3Ds[0].requestContext3D();

function initStage3D(e:Event):void {
    context0 = stage.stage3Ds[0].context3D;
    context0.configureBackBuffer(stage.stageWidth, stage.stageHeight, 4, true);

    vBuff = context0.createVertexBuffer(0.2 * vBuff_vec.length, 5);
    vBuff.uploadFromVector(vBuff_vec, 0, 0.2 * vBuff_vec.length);
    vBuff_vec = null;

    iBuff = context0.createIndexBuffer(iBuff_vec.length);
    iBuff.uploadFromVector(iBuff_vec, 0, iBuff_vec.length);
    iBuff_vec = null;

    tex = context0.createTexture(bmpdata.width,bmpdata.height,'bgra',false);
    tex.uploadFromBitmapData(bmpdata,0);

    //Passes vertex buffer 1 to fragment shader
    //Outputs vertex buffer 0
    vShader.assemble('vertex', 'mov v0 va1 \n'+'mov op va0');
    //Textures using data from vertex shader and sends to temporary register
    //Outputs temporary register
    pShader.assemble('fragment', 'tex ft0 v0 fs0 <2d,linear,nomip> \n'+'mov oc ft0');

    program = context0.createProgram();
    program.upload(vShader.agalcode, pShader.agalcode);

    addEventListener(Event.ENTER_FRAME, render);
}

function render(e:Event):void {
    if (!context0) { return; }
    context0.clear();

    //(x,y,0) to vertex buffer 0, (u,v) to vertex buffer 1
    context0.setVertexBufferAt(0, vBuff, 0, 'float3');
    context0.setVertexBufferAt(1, vBuff, 3, 'float2');
    context0.setTextureAt(0, tex);
    context0.setProgram(program);

    context0.drawTriangles(iBuff);
    context0.present();
}

I've compared this to several online examples, and have found no discrepancies. I'm thinking that there might be some setting I'm overlooking, or requirement I'm missing... I'm lost though. Thoughts?

Was it helpful?

Solution

The mesh size wasn't correct - the tutorials I read failed to mention that x, y, and z values used as vertex shader output are between -1 and 1. Hazarding a guess: my mesh fell outside the zone being rendered, and all the polygons were lost during the stage of rendering where the viewport is clipped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top