HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在XCODE中使用VBO使OpenGL绘制内容?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
晚上好,

现在我正在尝试使用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;
}

解决方法

由于您没有着色器,OpenGL不知道如何解释您的顶点属性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的回答的扩展副本.

大佬总结

以上是大佬教程为你收集整理的如何在XCODE中使用VBO使OpenGL绘制内容?全部内容,希望文章能够帮你解决如何在XCODE中使用VBO使OpenGL绘制内容?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: