问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/14404779/using-glkit-and-opengl-es-how-to-draw-two-triangles-one-textured-another-soli