gpt4 book ai didi

macos - GLSL 示例无法在 Mac 上运行

转载 作者:行者123 更新时间:2023-12-03 16:43:05 27 4
gpt4 key购买 nike

我正在尝试在 Mac 上创建一个 GLSL 示例。我正在尝试为每个顶点设置一种颜色属性。但是,当程序运行时,我只是看到紫色屏幕(紫色来自 glClearColor)。我发布了相关的代码片段。

-(void)drawRect:(NSRect)dirtyRect
{
// get program ID for shader program
GLuint programID = [self loadShaders];

// get new dimensions
NSSize dim = [self frame].size;

// clear the background with color
glClearColor(0.4f, 0.1f, 0.7f, 1.0f);
glDepthRange(1.0, -1.0);
glViewport(0, 0, dim.width, dim.height);
glClear(GL_COLOR_BUFFER_BIT);

// generate a buffer for our triangle
glGenBuffers(2, vertexBuffer);

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);

glUseProgram(programID);

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
glEnableVertexAttribArray(0);
glVertexAttribPointer(VERTEX_POS_INDEX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, VERTEX_POS_SIZE*sizeof(vertices), vertices);

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]);
glEnableVertexAttribArray(1);
glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, VERTEX_POS_SIZE*sizeof(colors), colors);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glUseProgram(0);

// flush buffer
glFlush();
[[self openGLContext] flushBuffer];
}

然后是加载阴影代码:

-(GLuint)loadShaders
{
printf("GLSL version %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
printf("GL Version: %s", glGetString(GL_VERSION));

// Create the shaders
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

// get handle for app bundle
NSBundle *appBundle = [NSBundle mainBundle];

// get the path for the vertex shader file
NSString *vertexFilePath = [appBundle pathForResource:vertexShaderFile ofType:nil];
// get the path for the fragment shader file
NSString *fragmentFilePath = [appBundle pathForResource:fragmentShaderFile ofType:nil];

// get the contents of the vertex shader file into a string
NSString *vertexFileContents = [NSString stringWithContentsOfFile:vertexFilePath encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"%@", vertexFileContents);
// get the contents of the fragment shader file into a string
NSString *fragmentFileContents = [NSString stringWithContentsOfFile:fragmentFilePath encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"%@", fragmentFileContents);

GLint Result = GL_FALSE;
int infoLogLength;

// get a pointer the vertex shader program source, compile shader program
const char *vertexSourcePointer = [vertexFileContents UTF8String];
glShaderSource(vertexShaderID, 1, &vertexSourcePointer, NULL);
glCompileShader(vertexShaderID);
// check the vertex shader
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
char vertexShaderErrorMessage[infoLogLength];
glGetShaderInfoLog(vertexShaderID, infoLogLength, NULL, vertexShaderErrorMessage);
// print error message
NSLog(@"%@", [NSString stringWithUTF8String:vertexShaderErrorMessage]);

// get a pointer to the fragment shader program source, compile shader program
const char *fragmentSourcePointer = [fragmentFileContents UTF8String];
glShaderSource(fragmentShaderID, 1, &fragmentSourcePointer, NULL);
glCompileShader(fragmentShaderID);
// check the fragment shader
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
char fragmentShaderErrorMessage[infoLogLength];
glGetShaderInfoLog(fragmentShaderID, infoLogLength, NULL, fragmentShaderErrorMessage);
// print error message
NSLog(@"%@", [NSString stringWithUTF8String:fragmentShaderErrorMessage]);

// link the program
NSLog(@"Linking program...");
GLuint programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);

glBindAttribLocation(programID, 0, "position");
glBindAttribLocation(programID, 1, "inColor");

glLinkProgram(programID);

// check the program
glGetProgramiv(programID, GL_LINK_STATUS, &Result);
glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLogLength);
char shaderProgramErrorMessage[max(infoLogLength, (int)1)];
glGetProgramInfoLog(programID, infoLogLength, NULL, shaderProgramErrorMessage);
// pring error message
NSLog(@"%@", [NSString stringWithUTF8String:shaderProgramErrorMessage]);

// delete shaders
glDeleteShader(vertexShaderID);
glDeleteShader(fragmentShaderID);

return programID;

}

最后是着色器:

#version 120

attribute vec4 position;
attribute vec4 inColor;
varying vec4 outColor;
void main()
{
gl_Position = position;
outColor = inColor;
}

#version 120

varying vec4 outColor;
void main()
{
gl_FragColor = outColor;
}

最佳答案

看起来相当不错,但我确实发现了一个错误。

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
glEnableVertexAttribArray(0);
glVertexAttribPointer(VERTEX_POS_INDEX,
VERTEX_POS_SIZE,
GL_FLOAT,
GL_FALSE,
VERTEX_POS_SIZE*sizeof(vertices),
vertices);

如果您使用的是 VBO,则 glVertexAttribPointer 的最终属性应该是距当前绑定(bind)缓冲区开头的偏移

如果您使用顶点数组(无 glBindBuffer),那么您将使用 glVertexAttribPointer 的最后一个参数指向顶点。

但是,由于您已经将顶点上传到缓冲区并将其绑定(bind),因此 glVertexAttribPointer 的最终值应该是距顶点缓冲区开头的偏移,在你的情况下,应该是0

颜色缓冲区也有同样的错误。

关于macos - GLSL 示例无法在 Mac 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com