2D drawing in OpenGL ES 2.0 (iOS)

I am learning how to use OpenGL ES 2.0 on iOS. Now I just want to make a basic 2D animation (for example, move the rectangle around the screen and resize it). I started with a project template for OpenGL ES provided by Apple in Xcode. My drawing code is as follows:

static GLfloat squareVertices[] = {
    -0.5f, -0.33f,
    0.5f, -0.33f,
    -0.5f,  0.33f,
    0.5f,  0.33f
 };

// Update attribute values.
glVertexAttribPointer(VERTEX_ATTR, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(VERTEX_ATTR);

glVertexAttribPointer(COLOR_ATTR, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors);
glEnableVertexAttribArray(COLOR_ATTR);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 5);

Now it will draw a beautiful rectangle in the middle of the screen. But if I start changing the rectangle by adding the following code, it will start to look funky :

squareVertices[5] -= .001;
squareVertices[7] -= .001;

. OpenGL ES, , . , , OpenGL ES 3D- , 2D-. : 2D- OpenGL ES 2.0? OpenGL ES 1.1, . 2D- OpenGL ES 2.0 - 2D-?

.

+3
4

@macinjosh: , . , 10 , !

OpenGL 3D, 2D. , 4D, w, , 1.0 .

- 3D-, "z" , .

+6

OpenGL, 3 float, ax: X, Y, Z?

, :

( -0.50f, -0.33f, 0.50f) 
( -0.33f, -0.50f, 0.33f)
(  0.50f,  0.33f, ?????)

:

( -0.50f, -0.33f, 0.00f )
(  0.50f, -0.33f, 0.00f )
( -0.50f,  0.33f, 0.00f )
(  0.50f,  0.33f, 0.00f )

, ...:)

, OpenGL, :

http://iphonedevelopment.blogspot.com/2010/10/opengl-es-20-for-ios-chapter-3.html

, ...

+2

"3" glVertexAttribPointer. , 2, GL. , z "0" ( , !). , , 4 4- ( "w" ) , gubbins.

, ( , - ), , . , ,

glInterleavedArrays(format_code, stride, data)

may be more efficient, as the device may have optimized code paths for any "code_format" that you decide to go with.

+2
source

After some playback, I changed the drawing code to the following:

static GLfloat squareVertices[12] = {
    -0.5f, -0.33f, 0.0,
    0.5f, -0.33f, 0.0,
    -0.5f, 0.33f, 0.0,
    0.5f, 0.33f, 0.0
};

// Update attribute values.
glVertexAttribPointer(VERTEX_ATTR, 3, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(VERTEX_ATTR);

glVertexAttribPointer(COLOR_ATTR, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors);
glEnableVertexAttribArray(COLOR_ATTR);

squareVertices[7] -= .001;
squareVertices[10] -= .001;

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Adding a third 0.0 float to each vertex seemed to do the trick. I do not understand why this is so, if someone can shed some light, I would appreciate it.

+1
source

Source: https://habr.com/ru/post/1782185/


All Articles