gpt4 book ai didi

macos - OpenGL 立方体未显示,可能有近平面和远平面吗?

转载 作者:行者123 更新时间:2023-12-03 17:24:02 25 4
gpt4 key购买 nike

尝试进行手动透视转换后,我无法显示我的立方体我的代码如下。我怀疑这可能是我的近处和远处的飞机号码。

-(void)drawRect:(NSRect)dirtyRect
{
// get the dimensions of the window
NSSize dim = [self frame].size;

// clear the background with color
glClearColor(0.0, 0.0, 0.0, 0.4);
glViewport(0, 0, dim.width, dim.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// variables
GLfloat cubeHeight = 90.0;
GLfloat cubeWidth = 90.0;
GLfloat cubeLength = 200.0;
//GLfloat alpha = 0.0;
//GLfloat beta = 0.0;
//GLfloat gamma = 0.0;

// cube position data
GLfloat cubePositionData[] = {0.0, 0.0, 0.0,
0.0, 0.0, cubeLength,
cubeWidth, 0.0, cubeLength,
cubeWidth, 0.0, 0.0,
0.0, cubeHeight, 0.0,
0.0, cubeHeight, cubeLength,
cubeWidth, cubeHeight, cubeLength,
cubeWidth, cubeHeight, 0.0};

// cube indices data
GLubyte cubeIndices[] = {0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
1, 2, 6, 1, 6, 5,
2, 3, 7, 2, 7, 6,
3, 0, 4, 3, 4, 7,
0, 1, 5, 0, 5, 4};

// cube color data
GLfloat cubeColorData[] = {0.0, 0.3, 0.8, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.3, 0.8, 1.0,
0.0, 0.3, 0.8, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.3, 0.8, 1.0};

// array to hold buffer IDs
GLuint vertexBuffers[2];

// bind each array of data to separate buffers
// bind cube position data to the first buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositionData), cubePositionData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// bind the cube color data to the second buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeColorData), cubeColorData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// enable the shader program
GLuint programID = [self loadShaders];
glUseProgram(programID);

// enable vertex attributes
// enable cube position attributes
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
glEnableVertexAttribArray(VERTEX_POS_INDEX);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// enable cube color attributes
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
glEnableVertexAttribArray(VERTEX_COLOR_INDEX);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// point to the enabled attribute data
glVertexAttribPointer(VERTEX_POS_INDEX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, 0, cubePositionData);
glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, 0, cubeColorData);

GLfloat offset[] = {0.5, 0.5};
GLint offsetUnif = glGetUniformLocation(programID, "offset");
GLint zNearUnif = glGetUniformLocation(programID, "zNear");
GLint zFarUnif = glGetUniformLocation(programID, "zFar");
GLint frustumScaleUnif = glGetUniformLocation(programID, "frustumScale");

glUniform2fv(offsetUnif, 1, offset);
glUniform1f(frustumScaleUnif, 1.0f);
glUniform1f(zNearUnif, 1.0);
glUniform1f(zFarUnif, 3.0);

// draw the elements
glDrawElements(GL_TRIANGLES, sizeof(cubeIndices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, cubeIndices);

glDisableVertexAttribArray(VERTEX_POS_INDEX);
glDisableVertexAttribArray(VERTEX_COLOR_INDEX);
glUseProgram(0);

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

}

问题可能是什么?哦,还有着色器:

#version 120

attribute vec3 position;
attribute vec4 inColor;

uniform vec2 offset;
uniform float zNear;
uniform float zFar;
uniform float frustumScale;

varying vec4 outColor;

void main()
{
vec4 cameraPos = vec4(position, 1.0) + vec4(offset.x, offset.y, 0.0, 0.0);
vec4 clipPos;

clipPos.xy = cameraPos.xy * frustumScale;

clipPos.z = cameraPos.z * (zNear + zFar) / (zNear - zFar);
clipPos.z += 2 * zNear * zFar / (zNear - zFar);

clipPos.w = -cameraPos.z;

gl_Position = clipPos;
outColor = inColor;
}

#version 120

varying vec4 outColor;

void main()
{
gl_FragColor = outColor;
}
<小时/>

好的,将我的代码部分更改为如下所示:

GLfloat offset[] = {-2.0, -2.0};
GLint offsetUnif = glGetUniformLocation(programID, "offset");
GLint zNearUnif = glGetUniformLocation(programID, "zNear");
GLint zFarUnif = glGetUniformLocation(programID, "zFar");
GLint frustumScaleUnif = glGetUniformLocation(programID, "frustumScale");

glUniform2fv(offsetUnif, 1, offset);
glUniform1f(frustumScaleUnif, 1.0);
glUniform1f(zNearUnif, 1.0);
glUniform1f(zFarUnif, 25.0);

我明白了:

Program result

后端(绿色)从远处看不应该看起来更小而不是更大,就像梯形底座一样?

最佳答案

看来您只使用了缓冲区和着色器。在这种情况下,您应该使用 OpenGL3.2 和 GLSL 1.5。您必须通过在 NSOpenGLView(或 Controller )中手动更改上下文来手动更改为核心 opengl。

NSOpenGLPixelFormatAttribute attribute[] = 
{
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
0
};

NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribute];
NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
[self setPixelFormat:pixelFormat];
[self setOpenGLContext:context];

但是您必须更改一些着色器代码,例如输入/输出的属性/变化。

那么,针对你的问题,需要你创建并绑定(bind)一个VAO。只需添加

int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

在绑定(bind)缓冲区和顶点属性指针之前。就是这样。而已。

另外,我认为你的顶点可能有一些问题。由于您正在查看 -Z 位置,因此您的立方体在您身后。因此,您必须将偏移量更改为 vec3,并将 Z 偏移量设置为 -100,看看您的立方体有多大。

关于macos - OpenGL 立方体未显示,可能有近平面和远平面吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12126660/

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