mario-deluna / php-glfw

🪐A fully-featured OpenGL and GLFW extension for PHP. 🔋Batteries included (Math Functions, Texture Loaders, etc..)
http://phpgl.net/
Other
391 stars 14 forks source link

Element array buffer support #15

Open medilies opened 1 year ago

medilies commented 1 year ago

Hi, can I currently use GL_ELEMENT_ARRAY_BUFFER and glDrawElements? because I tried some hours ago and got an error (I do not remember its detail but I'll reproduce it if needed)

If not, are there any plans to add support?

mario-deluna commented 1 year ago

Hey @medilies, element drawing is currently not supported. I haven't prioritized element drawing yet, as its benefits truly shine in larger scenes with highly complex geometry.

The obj. file loader included with php-glfw produces non-indexed vertex arrays. I plan to support element drawing in the future, as I aim to cover the entirety of the OpenGL API. But for now almost everything should be solvable with normal vertex arrays.

Does your use case greatly benefit from indexed vertices? Are you implementing skinning or something similar?

medilies commented 1 year ago

Nah, I'm too noob to play with skinning and similar things. I'm still tinkering with the included examples and trying to make a pong game.

I just didn't like how verbose it is to write a vertex buffer without indexing. And I also noticed that the instancing example was consuming 100% of my GPU.

mario-deluna commented 1 year ago

The instancing example renders 125,000 cubes, so it requires a significant amount of GPU power. Additionally, there's considerable overdraw in the example. You can view it as a simple benchmark ;)

In VISU there is a simple abstraction around vertex arrays/buffers, which reduces some of the verbosity. Here's an example of instanced rendering of several planes to render images, sprites, rectangles, etc.

$vertexLayout = [
    2, // position
    2, // tex coords
];

$instanceLayout = [
    4, // mat4x0
    4, // mat4x1
    4, // mat4x2
    4, // mat4x3
];

$vertexArray = new BasicInstancedVertexArray($gl, $vertexLayout, $instanceLayout);
$vertexArray->uploadVertexData(new FloatBuffer([
    // vertex positions
    // this makes up the quad
    // position  // tex coords
    -1.0, -1.0,  0.0, -1.0,
     1.0, -1.0,  1.0, -1.0,
    -1.0,  1.0,  0.0,  0.0,
     1.0,  1.0,  1.0,  0.0,
]));

Then to render:

// allocate a buffer to hold all instance matrices
$buffer = new FloatBuffer();

// create a transform for item A
$itemA = new Transform();
$itemA->position->x = 20;

// and B..
$itemB = new Transform();
$itemB->position->x = 40;

// push the matrices into our buffer
$buffer->pushMat4($transform->getLocalMatrix());
$buffer->pushMat4($transform->getLocalMatrix());

// upload the buffer and draw the vertex array in TRAINGLE_STRIP mode.
$this->vertexArray->uploadInstanceData($buffer);
$this->vertexArray->drawAll(GL_TRIANGLE_STRIP);
medilies commented 1 year ago

Thank you for the tip 🙇🏼, I'll keep a note to try it when I use VISU.

Edit: I think I shouldn't keep this to myself, but I find it a little bit hard to understand the VISU folder structure and dev flow, and I couldn't find any doc.

Edit 2: Maybe I find it hard because I'm alien to game dev jargon (scene, node, signals ...)