最简单,简约的opengl 3.2可可项目

最简单,简约的opengl 3.2可可项目,第1张

概述多年来我一直使用传统的openGL和 cocoa,但我现在正努力转向openGL 3.2.互联网上有几个例子,但它们都太复杂了(很多甚至在XCode 5.1下都不再编译).有人可以写一个最简单,最简单的最小可可代码的例子,只是为了在NSOpenGLView中绘制一个读三角形吗? (没有花哨的着色器,没有displayCallbacks,代码行越少越好). https://stackoverflow 多年来我一直使用传统的openGL和 cocoa,但我现在正努力转向openGL 3.2.互联网上有几个例子,但它们都太复杂了(很多甚至在XCode 5.1下都不再编译).有人可以写一个最简单,最简单的最小可可代码的例子,只是为了在NSOpenGLVIEw中绘制一个读三角形吗? (没有花哨的着色器,没有displayCallbacks,代码行越少越好).解决方法 https://stackoverflow.com/a/22502999/4946861

在xcode 6.3.2中,我得到了在替换之后运行的示例

(ID)initWithFrame:方法(的NSRect)帧

(空隙)awakeFromNib

和更换

self = [super initWithFrame:frame pixelFormat:pixelFormat];

super.pixelFormat =像素格式;

和删除
回归自我;

或详细写出:

#import "MyOpenGLVIEw.h"@implementation MyOpenGLVIEw- (voID)awakeFromNib{    NSLog(@"...was here");    // 1. Create a context with opengl pixel format    NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =    {        NSOpenGLPFAOpenGLProfile,NSOpenGLProfiLeversion3_2Core,NSOpenGLPFAcolorSize,24,NSOpenGLPFAAlphaSize,8,NSOpenGLPFADoubleBuffer,NSOpenGLPFAAccelerated,NSOpenGLPFAnorecovery,0    };    NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];    super.pixelFormat=pixelFormat;    // 2. Make the context current    [[self openGLContext] makeCurrentContext];    // 3. define and compile vertex and fragment shaders    gluint  vs;    gluint  fs;    const char    *vss="#version 150\n\    uniform vec2 p;\    in vec4 position;\    in vec4 colour;\    out vec4 colourV;\    voID main (voID)\    {\    colourV = colour;\    gl_position = vec4(p,0.0,0.0) + position;\    }";    const char    *fss="#version 150\n\    in vec4 colourV;\    out vec4 fragColour;\    voID main(voID)\    {\    fragColour = colourV;\    }";    vs = glCreateShader(GL_VERTEX_SHADER);    glShaderSource(vs,1,&vss,NulL);    glCompileShader(vs);    fs = glCreateShader(GL_FRAGMENT_SHADER);    glShaderSource(fs,&fss,NulL);    glCompileShader(fs);    printf("vs: %i,fs: %i\n",vs,fs);    // 4. Attach the shaders    shaderProgram = glCreateProgram();    glAttachShader(shaderProgram,vs);    glAttachShader(shaderProgram,fs);    glBindFragDataLocation(shaderProgram,"fragColour");    gllinkProgram(shaderProgram);    // 5. Get pointers to uniforms and attributes    positionUniform = glGetUniformlocation(shaderProgram,"p");    colourAttribute = glGetAttribLocation(shaderProgram,"colour");    positionAttribute = glGetAttribLocation(shaderProgram,"position");    glDeleteShader(vs);    glDeleteShader(fs);    printf("positionUniform: %i,colourAttribute: %i,positionAttribute: %i\n",positionUniform,colourAttribute,positionAttribute);    // 6. Upload vertices (1st four values in a row) and colours (following four values)    GLfloat vertexData[]= { -0.5,-0.5,1.0,0.5,1.0};    glGenVertexArrays(1,&vertexArrayObject);    glBindVertexArray(vertexArrayObject);    glGenBuffers(1,&vertexBuffer);    glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);    glBufferData(GL_ARRAY_BUFFER,4*8*sizeof(GLfloat),vertexData,GL_STATIC_DRAW);    glEnabLevertexAttribarray((gluint)positionAttribute);    glEnabLevertexAttribarray((gluint)colourAttribute  );    glVertexAttribPointer((gluint)positionAttribute,4,GL_float,GL_FALSE,8*sizeof(GLfloat),0);    glVertexAttribPointer((gluint)colourAttribute,(char*)0+4*sizeof(GLfloat));}- (voID)drawRect:(NSRect)dirtyRect {    [super drawRect:dirtyRect];    // Drawing code here.    glClearcolor(0.0,1.0);    glClear(GL_color_BUFFER_BIT);    gluseProgram(shaderProgram);    GLfloat p[]={0,0};    gluniform2fv(positionUniform,(const GLfloat *)&p);    glDrawArrays(GL_TRIANGLE_FAN,4);    [[self openGLContext] flushBuffer];}@end
总结

以上是内存溢出为你收集整理的最简单,简约的opengl 3.2可可项目全部内容,希望文章能够帮你解决最简单,简约的opengl 3.2可可项目所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1025311.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存