gpt4 book ai didi

objective-c - 苹果机,OpenGL 4 : drawing on FrameBuffer does not work

转载 作者:行者123 更新时间:2023-11-30 14:51:14 25 4
gpt4 key购买 nike

我尝试将简单的 OpenGL 绘图实现为 Framebuffer 对象,如 described here 。我的代码编译得很好,但是没有绘制几个 2D 三角形的形状,并且输出图片没有从片段着色器获取颜色。我花了很多时间,但没有找到解决方案。请帮助我。

执行期间没有异常或 OpenGL 错误。使用断点,我看到缓冲区、着色器程序、属性的所有索引都获得了适当的值。当我更改 glClearColor 时,我会得到具有指定大小和背景颜色的输出图像。我完全不知道问题出在哪里......我的代码在这里。

一般变量初始化:

GLuint framebuffer, renderbuffer;
GLuint program, uiVBO[1], uiVAO[1];
float ver[8];

_width = 512; _height = 512;
framebuffer = 0; renderbuffer = 0; program = 0;
uiVAO[0] = 0; uiVBO[0] = 0;

float pos = 1.0f; float neg = -1.0f;
ver[0] = pos; ver[1] = neg;
ver[2] = neg; ver[3] = neg;
ver[4] = pos; ver[5] = pos;
ver[6] = neg; ver[7] = pos;

创建帧和渲染缓冲区:

glGenFramebuffersEXT(1, &framebuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
glGenRenderbuffersEXT(1, &renderbuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, _width, _height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, renderbuffer);

if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) {
NSLog(@"Bad Framebuffer status!");
assert(0);
}

绘图本身:

glClearColor(0.3, 0.3, 0.3, 0);
glClear(GL_COLOR_BUFFER_BIT);
GetError();

glUseProgram(program);

GLint atr = glGetAttribLocation(program, "inPosition");
glBindVertexArray(uiVAO[0]);
glEnableVertexAttribArray(atr);
GetError();

glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
glBufferData(GL_ARRAY_BUFFER, 4*2*sizeof(float), ver, GL_STATIC_DRAW);
glVertexAttribPointer(atr, 2, GL_FLOAT, GL_FALSE, 0, 0);
GetError();

// I also tried GL_ELEMENT_ARRAY_BUFFER and glDrawElements
// But no result is visible anyway...
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
GetError();

glDisableVertexAttribArray(atr);
glBindVertexArray(0);

// Is it needed?
glFlush();
GetError();

// It just calls "glReadPixels"
[self saveResult];

其他东西,不是那么重要或只是明显正确:

/// Setting up OpenGL Context

CGLContextObj context = NULL;
CGLPixelFormatAttribute attributes[13] = {
kCGLPFAOpenGLProfile,
(CGLPixelFormatAttribute) kCGLOGLPVersion_GL4_Core,
kCGLPFAAccelerated,
kCGLPFAColorSize, (CGLPixelFormatAttribute)24,
kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8,
kCGLPFADoubleBuffer,
kCGLPFASampleBuffers, (CGLPixelFormatAttribute)1,
kCGLPFASamples, (CGLPixelFormatAttribute)4,
(CGLPixelFormatAttribute) 0
};
CGLPixelFormatObj pix; CGLError errorCode; GLint num;
errorCode = CGLChoosePixelFormat( attributes, &pix, &num );
errorCode = CGLCreateContext(pix, NULL, &context);
CGLDestroyPixelFormat( pix );
errorCode = CGLSetCurrentContext( context );



/// Shaders creation

GLuint vertexShader = makeShader(GL_VERTEX_SHADER, loadFile(@"main", @"vsh"));
GLuint fragmentShader = makeShader(GL_FRAGMENT_SHADER, loadFile(@"main", @"fsh"));

program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (0 == status) {
NSLog(@"Failed to link shader program!\n");
assert( 0 );
}


/// VAO, VBO creation

glGenVertexArrays(1, &uiVAO[0]);
glGenBuffers(1, &uiVBO[0]);
GetError();



/// Vertex shader:

#version 410

layout (location = 1) in vec2 inPosition;

void main() {
gl_Position = vec4(inPosition, 0.0, 1.0);
}

/// Fragment shader:

#version 410

out vec4 outputColor;

void main() {
outputColor = vec4(1.0, 1.0, 0.5, 1.0);
}



/// Extracts OpenGL error if it exists

#define GetError( )\
{\
for ( GLenum Error = glGetError( ); ( GL_NO_ERROR != Error ); Error = glGetError( ) )\
{\
switch ( Error )\
{\
case GL_INVALID_ENUM: printf( "\n%s\n\n", "GL_INVALID_ENUM" ); assert( 0 ); break;\
case GL_INVALID_VALUE: printf( "\n%s\n\n", "GL_INVALID_VALUE" ); assert( 0 ); break;\
case GL_INVALID_OPERATION: printf( "\n%s\n\n", "GL_INVALID_OPERATION" ); assert( 0 ); break;\
case GL_OUT_OF_MEMORY: printf( "\n%s\n\n", "GL_OUT_OF_MEMORY" ); assert( 0 ); break;\
default: break;\
}\
}\
}


/// Creates shader by string

GLuint makeShader(GLenum type, NSString *cont) {
const GLchar *source = (GLchar *)[cont cStringUsingEncoding:NSASCIIStringEncoding];
GLuint shader;

shader = glCreateShader(type);
GetError();
glShaderSource(shader, 1, &source, NULL);
GetError();
glCompileShader(shader);
GetError();

GLint logLength;

glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
GetError();
if (logLength > 0) {
GLchar *log = (GLchar *)malloc((size_t)logLength);
glGetShaderInfoLog(shader, logLength, &logLength, log);
GetError();
NSLog(@"Shader compilation failed with error:\n%s", log);
free(log);
}

GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
GetError();
if (0 == status) {
glDeleteShader(shader);
GetError();
NSLog(@"Shader compilation failed from code!");
assert(0);
}

return shader;
}


/// Loads file to string

NSString* loadFile(NSString *name, NSString *ext) {
NSString *absPath = [[NSBundle mainBundle] pathForResource:name ofType:ext];
return [NSString stringWithContentsOfFile:absPath encoding:NSASCIIStringEncoding error:nil];
}

最佳答案

我会检查 glViewport 是否设置正确。

关于objective-c - 苹果机,OpenGL 4 : drawing on FrameBuffer does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48446093/

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