Skip to content
Advertisement

WebGL gl.triangle make square

JavaScript

This is some portion of the code ( the code is too long to fit ) so I will put these part where I’m curious about why it didnt draw a square but still a triangle? I know that I used gl.Triangle but I want to try using gl.Triangle to draw a square which I’m not sure which part of this is wrong and I have searched about it but no one do the same thing as I do ( the one where I put position and vertices in the same array )

There’s also this part where count is 3 which I’m not sure what it does ( this code is given by my professor to let me make it a square and colored by changing a few setting so I do not know how to code opengl yet )

JavaScript

below is the full code

JavaScript

I would like some hint instead of answer if that is ok because I was trying to learn but I cant find this method anywhere on the internet

Advertisement

Answer

In your vertex specification, the coordinate (0.9, 0.9) is duplicated, however, that’s not the only problem.

See Triangle primitives. The primitive type gl.TRIANGLES renders, as the name suggests, triangles. For 2 triangles you need 6 verticals (2*3). Each triangle consists of 3 vertices, and the triangles are completely independent and have no common vertices. e.g.:

JavaScript

However you can use the primitive type gl.TRIANGLE_STRIP to draw a single quad:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement