I’m trying to learn OpenGL. I tried some code I found in this book but the output of the program is just a white screen, the output should be the Sierpinski triangle.
What could be wrong? I’m developing on Mac OS X 10.8 and Xcode 4.5
#include "Angel.h"
#include <iostream>
const int numPoints = 5000;
typedef vec2 point2;
void init(){
point2 points[numPoints];
point2 vertices[3] = {
point2(-1.0, -1.0), point2(0.0, 1.0), point2(1.0, -1.0)
};
points[0] = point2(0.25, 0.5);
for (int k = 1; k < numPoints; k++) {
int j = rand()%3;
points[k] = (points[k-1]+vertices[j])/2.0;
}
GLuint program = InitShader("vertex.glsl", "fragment.glsl");
glUseProgram(program);
GLuint abuffer;
glGenVertexArraysAPPLE(1, &abuffer);
glBindVertexArrayAPPLE(abuffer);
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
GLuint location = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(location);
glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, numPoints);
glFlush();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_3_2_CORE_PROFILE);
glutInitWindowSize(640, 480);
glutCreateWindow("Sierpinski Gasket");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Finally !
The code is complete, the shaders are attached as they should be.
There’s a problem with a
.hfiles included inAngel.h, instead of having<OpenGL/OpenGL.h>it should be<OpenGL/gl3.h>. The second thing to change is to remove theAPPLEsuffix to the functionsglGenVertexArrayAPPLEandglBindVertexArrayAPPLE.Apparently there’s a problem with Apple and their implementation of OpenGL, probably not all the stuff is compatible with what they have, but I let that topic to the experts, if there’s someone out there please clarify this.