the*_*tic 7 iphone opengl-es objective-c
我有一个GLKView,几乎显示我想要的形状,但有一些东西仍然没有按预期工作.
3_______________________2
|\ /|
| \_ _ _ _ _ _ _ _ _ _/ |
| /4 5\ |
|/_____________________\|
0 1
Run Code Online (Sandbox Code Playgroud)
纹理映射正在正面工作,另外两个面孔并没有真正起作用,还有三角形缺乏.
当我使用颜色时,面看起来像所希望的那样,但是背面不会在由点4和5形成的边缘处连接(请看我上面提供的图).
我想(如果你从侧面看)这三个面形成一个等边三角形.
我已暂时注释掉代码的纹理映射部分,以便您可以测试它如果您愿意.
在这里我设置了我的视图(注意我的视图的尺寸.坐标系工作正常,没有摆弄低值.面部看起来很好,他们只是没有,正如我所说的在后面加入):
EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
OpenGLShape *view = [[OpenGLShape alloc] initWithFrame:CGRectMake(0, 200, 320, 80) context:context];
view.delegate = view;
[view setupGL];
[self.view addSubview:view];
- (void)setupGL {
[EAGLContext setCurrentContext:self.myContext];
//glEnable(GL_CULL_FACE);
self.effect = [[GLKBaseEffect alloc] init];
BOOL useTexture = NO;
// Create default framebuffer object.
glGenFramebuffers(1, &defaultFrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, defaultFrameBuffer);
if(useTexture){
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,
nil];
NSError * error;
NSString *path = [[NSBundle mainBundle] pathForResource:@"blogcell@2x" ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;
glGenBuffers(1, &texArray);
glBindBuffer(GL_ARRAY_BUFFER, texArray);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0,0);
glBufferData(GL_ARRAY_BUFFER, sizeof(TexCoords), TexCoords, GL_STATIC_DRAW);
}else{
glGenBuffers(1, &colArray);
glBindBuffer(GL_ARRAY_BUFFER, colArray);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBufferData(GL_ARRAY_BUFFER, sizeof(Color), Color, GL_STATIC_DRAW);
}
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 320.0f, 80.0f);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
glEnable(GL_DEPTH_TEST);
glGenBuffers(1, &vertexArray);
glBindBuffer(GL_ARRAY_BUFFER, vertexArray);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,3,GL_FLOAT,GL_FALSE,0,0);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
self.effect.transform.projectionMatrix = GLKMatrix4MakePerspective(51.4f,4.0f, 0.1f, 10.75f);
rotMatrix = GLKMatrix4Translate(self.effect.transform.modelviewMatrix,0, 0, -3);
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
self.opaque = NO;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[self.effect prepareToDraw];
glDrawArrays(GL_TRIANGLES, 0, sizeof(Vertices));
}
Run Code Online (Sandbox Code Playgroud)
红色=正面(由点0123组成)
蓝色=背面(顶部 - 由点4523组成)
绿色=背面(底部 - 由点0154组成)
白色=背景
为了看看它们是否混合/剔除等,我还使面部半透明(alpha = 0.5).



rra*_*lvv 10
在OpenGL中,相机始终位于{0,0,0},更改位置的是3D世界.
对于正交投影,投影矩阵由下式给出:
self.effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(left, right, bottom, top, front, back);
Run Code Online (Sandbox Code Playgroud)

正交投影中的物体在靠近或远离相机时看起来大小相同.
透视投影中的物体在远离相机时看起来较小.

对于透视投影,投影矩阵由下式给出:
self.effect.transform.projectionMatrix = GLKMatrix4 GLKMatrix4MakePerspective (fov, aspect, front, back);
);
Run Code Online (Sandbox Code Playgroud)
对于透视投影,视场(fov)的工作方式类似于真实相机上的视场,对于较小的值,视角几乎不可察觉,对于较大的值,由于透视投影引起的失真很大.
该fov角是弧度,并且可以进行调整,直至场景看起来适合您的应用
aspect 是水平和垂直可视区域之间的屏幕纵横比.
出于实际原因,fov以度为单位,因此可以如下使用
GLKMatrix4MakePerspective(fov * M_PI / 180.0f,
screenWidth / screenHeight,
front, back);
Run Code Online (Sandbox Code Playgroud)
在透视投影中,近剪裁平面需要大于零,因此您需要向模型视图矩阵添加平移,以便将世界与camara稍微分开.
此转换在模型视图矩阵中指定
self.effect.transform.modelviewMatrix = GLKMatrix4MakeTranslation(0, 0, -frontClippingPlane);
Run Code Online (Sandbox Code Playgroud)
更新
剪切体积需要包括所有顶点以避免与前剪切平面进行z-战斗,在这种情况下,透视投影中的最终平移需要更进一步
self.effect.transform.modelviewMatrix = GLKMatrix4MakeTranslation(0, 0, -frontClippingPlane - 0.1f);
Run Code Online (Sandbox Code Playgroud)
要旋转模型并将其转换为最终位置,您需要将两个转换组合如下:
GLKMatrix4 rotation = GLKMatrix4MakeRotation(angle*M_PI/180, 1, 0, 0); // angle in degrees and x,y, and z coordinates for the rotation axis
GLKMatrix4 translation = GLKMatrix4MakeTranslation( 0, 0, -front - 0.1f);
self.effect.transform.modelviewMatrix = GLKMatrix4Multiply(rotation, translation); // the order matters
Run Code Online (Sandbox Code Playgroud)
以下是OpenGL的旋转轴和方向,右手

这是最后的效果

| 归档时间: |
|
| 查看次数: |
1308 次 |
| 最近记录: |