gpt4 book ai didi

objective-c - 在 GLKView 中绘图

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:37 25 4
gpt4 key购买 nike

我是 OpenGL ES 的新手。作为测试,我试图在 GLKView 上绘制一个简单的三角形。没有编译或运行时错误,也没有 OpenGL 错误。它只是简单地不绘制三角形。它设置了清晰的颜色 OK,这意味着 OpenGL 正在工作,但它只是不绘制三角形,我做错了什么......

绘图回调代码

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{
const GLfloat triangleVertices[] = {
0.0, 2.0, 3.0, // Triangle top centre
-2.0, -2.0, 3.0, // bottom left
2.0, -2.0, 3.0 // bottom right
};



glViewport(0, 0, 12, 12);
[self checkError];

glMatrixMode(GL_MODELVIEW);

// Our new drawing code goes here
glLoadIdentity();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

[self checkError];


// Red
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

glVertexPointer(3, GL_FLOAT, 0, &triangleVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 3);

[self checkError];
}

整个文件代码

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (nonatomic, retain) EAGLContext *context;

@end

@implementation ViewController

@synthesize context = _context;
@synthesize Zoom = _Zoom;

- (void)dealloc
{
[_context release];

[super dealloc];
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1] autorelease];

if (!_context) {
NSLog(@"Failed to create ES context");
}

_Zoom = 1;

GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

[EAGLContext setCurrentContext:self.context];

[self setupGL];

[self checkError];
}

- (void)viewDidUnload
{
[super viewDidUnload];

if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
self.context = nil;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}

- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];

CGFloat w = self.view.bounds.size.width;
CGFloat h = self.view.bounds.size.height;

glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glClearColor(0.65f, 0.65f, 1.00f, 1.0f);


double mLat = (((double)h / 2) * (_Zoom / 60));
double mLon = (((double)w / 2) * (_Zoom / 60));

glOrthof((float)-mLon, (float)mLon, (float)-mLat, (float)mLat, -5.0f, 5.0f);
glViewport(0, 0, w, h);
}

-(void) checkError
{
GLenum error = glGetError();

if (error == GL_NO_ERROR)
return;

switch (error)
{
case GL_INVALID_ENUM:
NSLog(@"Invalid Enum");
break;
}
}


#pragma mark - GLKView and GLKViewController delegate methods

- (void)update
{

}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
const GLfloat triangleVertices[] = {
0.0, 2.0, 3.0, // Triangle top centre
-2.0, -2.0, 3.0, // bottom left
2.0, -2.0, 3.0 // bottom right
};



glViewport(0, 0, 12, 12);
[self checkError];

glMatrixMode(GL_MODELVIEW);

// Our new drawing code goes here
glLoadIdentity();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

[self checkError];


// Red
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

glVertexPointer(3, GL_FLOAT, 0, &triangleVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 3);

[self checkError];
}

@end

最佳答案

您对 glViewport(12,12) 的调用(在 -glkView:drawInRect: 中)告诉系统您正在尝试绘制 12 x 12 像素像素区域。那是你要的吗?那个好像很小

关于objective-c - 在 GLKView 中绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9960115/

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