xml3d / xml3d.js

The WebGL/JS implementation of XML3D
Other
75 stars 25 forks source link

tristrips not working because MESH_PARAMETERS not defined for it #79

Closed j4yk closed 9 years ago

j4yk commented 9 years ago

I am currently investigating XML3D as an export option for a 3D graphics application. For prototyping I generated the following xml3d:

    <xml3d xmlns="http://www.xml3d.org/2009/xml3d">
        <view position="0 0 1"/>
        <shader id="mapShader" script="urn:xml3d:shader:flat">
            <bool name="useVertexColor">true</bool>
        </shader>
        <mesh type="tristrips" shader="#mapShader">
            <int name="index">0 6 4 5 0 <!-- etc. --></int>
            <float3 name="position">-0.5 -0.5 -0.025 -0.5 -0.5 0.025 -0.5 0.5 -0.025 -0.5 0.5 0.025 0.5 -0.5 -0.025 0.5 -0.5 0.025 0.5 0.5 -0.025 0.5 0.5 0.025 0.155431 0.156691 0.025 0.155431 0.156691 0.075 0.155431 0.357603 <!-- etc --> </float3>
            <float3 name="color">0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 <!-- etc --></float3>
        </mesh>
    </xml3d>

Without a little modification in xml3d.js this will not work in Chrome, however. For gl.TRIANGLE_STRIP no MESH_PARAMETERS are defined which leads to the following error in Chrome:

Unsupported Mesh request: GLMesh {context: GLContext, glType: 5, buffers: Object, uniformOverride: Object, minIndex: 0…} 5

...and subsequently errors of the type Cannot read property ... of null.

These lines are in xml3d.js but leave out triangle strips:

MESH_PARAMETERS[WebGLRenderingContext.TRIANGLES] = {
    attributeData: {"position": Xflow.DATA_TYPE.FLOAT3 },
    typeData: {
        "index": Xflow.DATA_TYPE.INT,
        "solid": Xflow.DATA_TYPE.BOOL,
        "vertexCount": Xflow.DATA_TYPE.INT
    },
    bboxFix: {
        "boundingBox" : Xflow.DATA_TYPE.FLOAT3
    },
    bboxCompute: {
        "position" : Xflow.DATA_TYPE.FLOAT3
    } };
MESH_PARAMETERS[WebGLRenderingContext.LINE_STRIP] = MESH_PARAMETERS[WebGLRenderingContext.TRIANGLES];
MESH_PARAMETERS[WebGLRenderingContext.LINES] = MESH_PARAMETERS[WebGLRenderingContext.TRIANGLES];
MESH_PARAMETERS[WebGLRenderingContext.POINTS] = MESH_PARAMETERS[WebGLRenderingContext.TRIANGLES];

adding this line seems to solve my problem:

MESH_PARAMETERS[WebGLRenderingContext.TRIANGLE_STRIP] = MESH_PARAMETERS[WebGLRenderingContext.TRIANGLES];

Can you tell me if triangle strips were left out there by intention or if they have just been forgotten?

Minimal example:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script src="http://www.xml3d.org/xml3d/script/xml3d.js" type="text/javascript"/>
        <script src="http://www.xml3d.org/xml3d/script/tools/camera.js" type="text/javascript"/>
    </head>
    <body>
        <xml3d xmlns="http://www.xml3d.org/2009/xml3d">
            <view position="0 0 1"/>
            <mesh type="tristrips">
                <int name="index">0 1 2 3</int>
                <float3 name="position">
                -1 -1 0
                 1 -1 0
                -1  1 0
                 1  1 0
                 </float3>
            </mesh>
        </xml3d>
    </body>
</html>

I would expect a rectangle but get the above error message.

ksons commented 9 years ago

@j4yk: No, triangles strips are not ommited intenionally. However, the solution that you skecth above would allow a single strip only and your exporter to create degenerated triangles in the strip in order to export a complex geometry in a single mesh element.

A good solution would be to have an additional (optional) count entry that triggers multiple triangle strip calls. Similar to the last parameter of glMultiDrawElements), which is not available in current WebGL but easy to emulate.

I'd suggest to add the additional line you suggested and open an enhancment issue for a way to draw multiple primitives (would also apply to other primitives).

What do you think? Do you want to create a pull requrest?

j4yk commented 9 years ago

Thank you for your reply. As my work is in a very early stage and I still have much to learn about XML3D that exporter really creates degenerated triangles at the moment.

I can certainly open a pull request later but I will have to search for the appropriate source file. Feel free to add that line yourself if you want and have time and I have not opened a request yet. ;-)

I think I do not understand your count proposal. Where should that count go (an attribute of mesh or an element of something)? Staying close to glMultiDrawElements, how would you separate the index arrays? Maybe you could also just allow multiple int subelements with the name "index". What I would have liked to have in the first place was primitive restart which is unfortunately also not available in WebGL afaik. But maybe you could even emulate that, depending on what solution for multi drawing you have in mind.