简体   繁体   中英

Using GLKit and OpenGL-ES, How To Draw Two Triangles, One Textured, Another Solid

I'm new to OpenGL-ES and have started working my way through Buck's Learning OpenGL ES for iOS book.

Chapter 2 covers drawing triangles with a solid color, and chapter 3 moves on to use textures.

As a personal learning exercise, I'm trying to alter the sample code to draw two triangles; one with the texture, the other solid.

Seems like a very basic exercise, but one that - as an OpenGL-ES beginner - is proving tricky.

So far I've tried a combination of things.

First, I prepare to draw with GLKVertexAttribPosition , then GLKVertexAttribTexCoord0 . I then draw my vertex buffer with triangle 1.

Next, I've tried a couple of things, including:

    glDisableVertexAttribArray(GLKVertexAttribTexCoord0);

and

    GLuint name = self.textureInfo0.name;
    glDeleteTextures(1, &name);

prior to then drawing the colour attributes:

[self.vertexBuffer prepareToDrawWithAttrib:GLKVertexAttribColor
                       numberOfCoordinates:3
                              attribOffset:offsetof(SceneVertex, colours)
                              shouldEnable:YES];

You can see in the screenshot that the bottom left triangle has the texture blended with the vertex color attributes. The right triangle - that makes the square - is completely black.

Note that the prepareToDrawWithAttrib method calls are Buck's utility classes that principally call the glVertexAttribPointer method.

It's important to note that I'm using GLKit here, so no shaders or any of that stuff.

Here's the screenshot:

在此处输入图片说明

Any thoughts much appreciated.

There can be many reasons why things don't render in OpenGL. For example, I wonder if you are setting up your second triangle with the wrong winding (clockwise vs counterclockwise).

I would try switching the color / texture between the two triangles, and see if you can get the texture on the second triangle.

Otherwise, without more code in your example, it is tough to say.

One of my advice is to avoid library (like Buck's utility class) if you begin openGLES. You must know well what you're doing. Don't waste your time with GLKBaseEffect.

Start an Xcode opengles project. Remove all the GLKBaseEffect part and start using shaders, it's not hard while you do simple lighting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM