Skip to content

Instantly share code, notes, and snippets.

@taoeffect
Last active December 16, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taoeffect/5479568 to your computer and use it in GitHub Desktop.
Save taoeffect/5479568 to your computer and use it in GitHub Desktop.
creates an extruded pentagon VBO with indexing and VAO
void CreatePentagon() {
float step = 2.0*M_PI/5, t=0;
Vertex Vertices[7+7+12];
// pentagon[0] = vec3(0);
// bottom
vec4 color(1,0,0,1);
Vertices[0].normal = vec3(0,-1,0);
Vertices[0].color = color;
for (int i=1; i<7; i++, t-=step) {
Vertices[i].pos = vec3(cosf(t),0,sinf(t));
Vertices[i].color = color;
Vertices[i].normal = vec3(0,-1,0);
}
color = vec4(0,1,0,1);
// top
for (int i=7; i<14; i++) {
Vertices[i].pos = Vertices[(7+(7-i))%7].pos + vec3(0,1,0);
Vertices[i].color = color;
Vertices[i].normal = vec3(0,1,0);
}
color = vec4(0,0,1,1);
// walls
for (int i=14; i<26; i+=2, t+=step) {
Vertices[i+1].pos = vec3(cosf(t), 0, sinf(t));
Vertices[i+1].color = color;
Vertices[i+1].normal = Vertices[i].pos;
Vertices[i].pos = vec3(cosf(t), 1, sinf(t));
Vertices[i].color = color;
Vertices[i].normal = Vertices[i].pos; // yes, cause it must be normalized
}
// convert to this silly orientation, you can remove this (just for the assignment)
for (int i=0; i<26; i++) {
float t = Vertices[i].pos.y;
Vertices[i].pos.y = Vertices[i].pos.z;
Vertices[i].pos.z = t;
t = Vertices[i].normal.y;
Vertices[i].normal.y = Vertices[i].normal.x;
Vertices[i].normal.x = t;
}
GLushort Indices[] = {
0,1,2,
0,2,3,
0,3,4,
0,4,5,
0,5,6, // bottom done
7,8,9,
7,9,10,
7,10,11,
7,11,12,
7,12,13, // top done
14,15,16, // walls start
15,17,16,
16,17,18,
17,19,18,
18,19,20,
19,21,20,
20,21,22,
21,23,22,
22,23,24,
23,25,24,
25,0,1
};
// Create Vertex Array Object
genVAO(1, &VaoId[VAO_PENT]);
bindVAO(VaoId[VAO_PENT]);
// Create Buffer for vertex data
glGenBuffers(1, &BufferId[BUF_PENT_VERTS]);
glBindBuffer(GL_ARRAY_BUFFER, BufferId[BUF_PENT_VERTS]);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
// Assign vertex attributes
glVertexAttribPointer(rootNode.shader.in_Position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(rootNode.shader.in_Color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)myOffset(Vertex,color));
if (rootNode.shader.in_Normal != -1) {
glVertexAttribPointer(rootNode.shader.in_Normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)myOffset(Vertex,normal));
glEnableVertexAttribArray(rootNode.shader.in_Normal);
}
glEnableVertexAttribArray(rootNode.shader.in_Position);
glEnableVertexAttribArray(rootNode.shader.in_Color);
// Vreate Buffer for indics
glGenBuffers(1, &BufferId[BUF_PENT_IDX]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferId[BUF_PENT_IDX]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
GLenum ErrorCheckValue = glGetError();
if (ErrorCheckValue != GL_NO_ERROR) {
fprintf(stderr,"ERROR: Could not create a VBO (%d): %s \n", __LINE__, gluErrorString(ErrorCheckValue));
exit(-1);
}
bindVAO(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment