现在我正在尝试使用Xcode运行以下代码,但到目前为止还不可能.代码是我在网上找到的一个简单的教程,代码应该只是使用OpenGL和VBO绘制一个三角形.如果我使用Visual Studio尝试代码,我实际上得到了预期的结果,没有任何问题.但是,当我尝试使用Xcode运行代码时,我只得到一个黑屏.
要在Xcode中设置项目,我使用MacPorts安装了GLEW和Freeglut,然后安装了XQuartz 2.7.5.然后我在xcode中创建了一个新项目作为命令行工具,在构建设置中,我在Other Linker Flags部分添加了-lGLEW和-lgluT.另外,我修改了库搜索路径以包含/ opt / local / lib /和/ opt / X11 / lib /,并修改了用户标题搜索路径以包含/ opt / local / include /和/ opt / X11 / include /.最后,在Build Phases部分中,我在Link Binary With Libraries部分添加了OpenGL.framework.
我错过了什么?如果代码在visual studio上为我工作,那么在尝试配置Xcode时我一定搞砸了.
编辑:如果我用GL_POINTS更改GL_TRIANGLES,由于某种原因,有时它只会在屏幕中间绘制一个点.如果我为着色器添加代码,则此单个点实际上将具有与着色器中指定的颜色相同的颜色.
Edit2:对于那些感兴趣的人,我正在关注的教程是:http://ogldev.atspace.co.uk
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "math_3d.h"
gluint VBO;
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnabLevertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0);
glDrawArrays(GL_TRIANGLES,3);
gldisabLevertexAttribArray(0);
glutSwapBuffers();
}
static void InitializeglutCallbacks()
{
glutdisplayFunc(RenderSceneCB);
}
static void CreateVertexBuffer()
{
Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f,-1.0f,0.0f);
Vertices[1] = Vector3f(1.0f,0.0f);
Vertices[2] = Vector3f(0.0f,1.0f,0.0f);
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices),Vertices,GL_STATIC_DRAW);
}
int main(int argc,char** argv)
{
glutinit(&argc,argv);
glutinitdisplayMode(gluT_DOUBLE|gluT_RGBA);
glutinitwindowSize(1024,768);
glutinitwindowPosition(100,100);
glutCreateWindow("Tutorial 03");
InitializeglutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr,"Error: '%s'\n",glewGetErrorString(res));
return 1;
}
glClearColor(0.0f,0.0f,0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
解决方法
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY); // or GL_norMAL_ARRAY,etc.
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glVertexPointer(3,0); // or glnormalPointer(),etc.
glDrawArrays(GL_TRIANGLES,3);
gldisableClientState(GL_VERTEX_ARRAY);
glutSwapBuffers();
}
请注意,您的代码实际上可能在某些GPU上工作,因为它们会将通用属性映射到与命名属性相同的“槽”,然后将零解释为位置(对于这些问题,通常NVidia(错误地)不那么严格我的GTX 680提供的代码确实显示了一个白色三角形).
你可以使用MiniShader,只需在CreateVertexBuffer()之后输入以下代码:
minish::InitializeGLExts(); // initialize shading extensions
const char *vs =
"layout(location = 0) in vec3 pos;\n"
// the layout specifier binds this variable to vertex attribute 0
"void main()\n"
"{\n"
" gl_Position = gl_ModelViewProjectionMatrix * vec4(pos,1.0);\n"
"}\n";
const char *fs = "void main() { gl_FragColor=vec4(.0,1.0,.0,1.0); }"; // green
minish::CompileShader(vs,fs);
请注意,像这样写顶点着色器可能会导致问题,因为没有指定版本.布局限定符仅在GLSL 330中可用,但在GLSL 120之后不推荐使用gl_ModelViewProjectionMatrix.因此我们需要在顶点着色器的开头添加类似这样的内容:
"#version 140\n" // need at least 140 for GL_ARB_explicit_attrib_location
"#extension GL_ARB_explicit_attrib_location : require\n"
// required for layout(location)
"#extension GL_ARB_compatibility : require\n"
// still need gl_ModelViewProjectionMatrix
// (which is otherwise deprecated after version 120)
// also,GL_ARB_compatibility is not supported in version 330 (and above)
或者,我们可以使用GLSL 330(或更高版本),但是模型视图 – 投影矩阵需要通过统一传递(也需要在cpu代码中正确设置):
"#version 330\n"
"uniform mat4 MVP;\n"
"layout(location = 0) in vec3 pos;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(pos,1.0);\n"
"}\n";
请注意,这个答案是我对Triangle doesn’t render的回答的扩展副本.