i am new to openGL. Iam using apple documentation as my major referens http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html#//apple_ref/doc/uid/TP40008793-CH107-SW6
My problem is that i am using openGL ES 1.1 and not 2 thus functions which are used in Listing 9-3 such as glVertexAttribPointer, glEnableVertexAttribArray are not recognized ... :)
I trying to make th开发者_如何学运维e optimizations which are described in this documentation: to hold indices and vertex as a struct with all of its data: position, color (Listing 9-1)
typedef struct _vertexStruct
{
GLfloat position[3];
GLubyte color[4];
} VertexStruct;
const VertexStruct vertices[] = {...};
const GLushort indices[] = {...};
and to use VBOs such as in Listing 9-2, 9-3
As i mentioned, some of the functions that are used there don't exists in openGL ES 1.1. I am wondering if there is a way to do the same in ES 1.1 maybe with some other code ?
thank, Alex
Edit according to Christians answer, tried to use glVertexPointer, glColorPointer. Here is the code, it prints the cube but no colors ... :(. Anyone, is it possible to use VBOs in such menner using ES 1.1
typedef struct {
GLubyte red;
GLubyte green;
GLubyte blue;
GLubyte alpha;
} Color3D;
typedef struct {
GLfloat x;
GLfloat y;
GLfloat z;
} Vertex3D;
typedef struct{
Vector3D position;
Color3D color;
} MeshVertex;
Cube Data:
static const MeshVertex meshVertices [] =
{
{ { 0.0, 1.0, 0.0 } , { 1.0, 0.0, 0.0 ,1.0 } },
{ { 0.0, 1.0, 1.0 } , { 0.0, 1.0, 0.0 ,1.0 } },
{ { 0.0, 0.0, 0.0 } , { 0.0, 0.0, 1.0 ,1.0 } },
{ { 0.0, 0.0, 1.0 } , { 1.0, 0.0, 0.0, 1.0 } },
{ { 1.0, 0.0, 0.0 } , { 0.0, 1.0, 0.0, 1.0 } },
{ { 1.0, 0.0, 1.0 } , { 0.0, 0.0, 1.0, 1.0 } },
{ { 1.0, 1.0, 0.0 } , { 1.0, 0.0, 0.0, 1.0 } },
{ { 1.0, 1.0, 1.0 } , { 0.0, 1.0, 0.0, 1.0 } }
};
static const GLushort meshIndices [] =
{ 0, 1, 2 ,
2, 1, 3 ,
2, 3, 4 ,
3, 5, 4 ,
0, 2, 6 ,
6, 2, 4 ,
1, 7, 3 ,
7, 5, 3 ,
0, 6, 1 ,
1, 6, 7 ,
6, 4, 7 ,
4, 5, 7
};
Function
GLuint vertexBuffer;
GLuint indexBuffer;
- (void) CreateVertexBuffers
{
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(meshVertices), meshVertices, GL_STATIC_DRAW);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(meshIndices), meshIndices, GL_STATIC_DRAW);
}
- (void) DrawModelUsingVertexBuffers
{
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexPointer(3, GL_FLOAT, sizeof(MeshVertex), (void*)offsetof(MeshVertex,position));
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(MeshVertex), (void*)offsetof(MeshVertex,color));
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(meshIndices)/sizeof(GLushort), GL_UNSIGNED_SHORT, (void*)0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
Functions like glVertexAttribPointer
and glEnableVertexAttribArray
are used for generic custom vertex attributes (which are the only supported method for submitting vertex data in OpenGL ES 2.0).
When using the fixed-function pipeline (as you have to in OpenGL ES 1.1) you just use the builtin attributes (think of the glVertex
and glColor
calls, you might have used before switching to vertex arrays). There are functions for each attribute which are called similar to their immediate mode counterparts, like glVertexPointer
or glColorPointer
(instead of glVertexAttribPointer
). These arrays are enabled/disabled by calling gl(En/Dis)ableClientState
with constants like GL_VERTEX_ARRAY
or GL_COLOR_ARRAY
(instead of gl(En/Dis)ableVertexAttribArray
).
But as a general rule you should not learn OpenGL ES 1.1 programming with 2.0 resources, as much of the information won't be of use to you (at least if you are new to OpenGL). For example some methods described on your linked site may not be supported in 1.1, like VBOs or even VAOs. But I also have to admit, that I have completely no ES experience, so am not perfectly sure about that.
EDIT: Regarding your updated code: I assume no colors means the cube is of a single color, probably white. In your first code example you used GLubyte color[4]
, and now its some Color3D
type, maybe this doesn't fit to the glColorPointer(4, GL_UNSIGNED_BYTE, ...)
call (where the first argument is the number of components and the second one the type)?
If your Color3D
type only contains 3 colors or floating point colors, I would anyway suggest you to use 4-ubyte colors, because together with your 3 floats for the position you should get a perfectly 16-byte aligned vertex, which is also and optimization they suggest in your provided link.
And by the way, the repetition of the index buffer creation in your CreateVertexBuffers
function is rather a typo, isn't it?
EDIT: Your colors contain ubytes (which range from 0 (black) to 255 (full color)) and you initialize them with floats. So your float value 1.0 (which should surely mean full color) is converted to ubyte and you get 1, which compared to the whole [0,255] range is still very small, so everything is black. When you use ubytes, then you should also initialize them with ubytes, so just replace every 0.0 with 0 and every 1.0 with 255 in the color data.
And by the way, since you're using VBOs in ES 1.1 and at least something is drawn, then ES 1.1 seems to support VBOs. I didn't know that. But I'm not sure if it also supports VAOs.
And by the way, you should call glBindBuffer(GL_ARRAY_BUFFER, 0)
and likewise for the element array buffer after you're finished with using them at the end of these two functions. Othwerwise you may get problems in other functions which assume no buffers but the buffers are still bound. Always remember that OpenGL is a state machine and every state you set stays until it's changed again or the context is destroyed.
精彩评论