【发布时间】:2013-01-25 09:50:18
【问题描述】:
这是我第一次尝试学习OpenGL,我是按照书上的例子来的。我正在使用 Xcode 在 OS X 10.8 下进行操作。代码如下:
#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;
}
它编译。但是当我尝试执行它时,窗口不会出现。当我调用init() 函数时,问题就出现了。没有它,窗口就会出现,但背景为黑色。有了它,就没有窗户了。代码可以在here找到。
更新
显然程序正在GLuint program = InitShader("vertex.glsl", "fragment.glsl"); 行中退出,因为它没有找到着色器文件。我怎样才能告诉程序使用这些文件?我的意思是我将.glsl 文件与.h 和.cpp 放在同一个文件夹中,但是当Xcode 构建项目时,可执行文件与.glsl 文件不在同一个位置。如何在 Xcode 中解决这个问题?
【问题讨论】:
标签: macos opengl xcode4.5 glut