Reputation: 3867
I am new to OpenGL ES. As a test I am trying to draw a simple triangle on a GLKView. There are no compilation or runtime errors and no OpenGL errors either. It just simply does not draw the triangle. It sets the clear color OK, which means that OpenGL is working, but it just doesnt draw the triangle, what am I doing wrong...
The drawing callback code
- (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];
}
The whole file code
#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
Upvotes: 2
Views: 4064
Reputation: 26325
Your call to glViewport(12,12)
(in -glkView:drawInRect:
) tells the system that you're trying to draw in a 12 pixel by 12 pixel area. Is that what you want? That seems small.
Upvotes: 1