Search code examples
macosopenglxcode4.5glut

Xcode executable cannot find glsl files


This is the first time I try to learn OpenGL, I'm following the examples of a book. I'm doing it under OS X 10.8 with Xcode. The code is the following:

#include "Angel.h"

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);
    glutInitWindowSize(640, 480);

    glutCreateWindow("Sierpinski Gasket");

    init();
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

It compiles. But when I try to execute it the window does not appear. The problem arises when I call the init() function. Without it the window appears but with a black background. With it, there's no window. The code can be found here.

UPDATE

Apparently the program is exiting in the line GLuint program = InitShader("vertex.glsl", "fragment.glsl"); because it's not finding the shader-files. How can I tell the program to use the files? I mean I have the .glsl files in the same folder as the .h and .cpp but when Xcode builds the project the executable is not in the same place as the .glsl files. How to solve this within Xcode?


Solution

  • Follow Below Step:

    Select the project on the left panel.

    Select the target and then select Build Phases

    There you should fin a button called Add Build Phase

    There will appear a box where you have to select the files (there's a little +sign). And be sure you selected Destination: Products directory

    Build the project, run it and now it should work !!