Anfänger in OpenGL ES: Problem bei der Darstellung eines Objektes im GLKViewController

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Anfänger in OpenGL ES: Problem bei der Darstellung eines Objektes im GLKViewController

    Hallo erstmal :)

    Ich kämpfe schon seit geraumer Zeit mit OpenGL, und wollte nun ein Objekt (exportiert aus Blender / ohne Textur) in den
    GLKview vom GLKViewController bringen. Jedoch wird anscheinend nichts projiziert? Ich weiß einfach nicht an was das liegen kann.. Ist das Objekt aus dem Sichtfeld? Kann ich mir aber iwie nicht vorstellen ^^

    wäre toll wenn sich jemand mit OpenGL auskennt und mir weiter helfen könnte. :D

    mfg Seibold

    Quellcode

    1. #import "FlascheGLKViewController.h"
    2. int H = 1024;
    3. int W = 768;
    4. @interface FlascheGLKViewController ()
    5. {
    6. float _curRed;
    7. BOOL _increasing;
    8. }
    9. @end
    10. @implementation FlascheGLKViewController
    11. @synthesize context;
    12. @synthesize effect;
    13. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    14. {
    15. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    16. if (self) {
    17. // Custom initialization
    18. }
    19. return self;
    20. }
    21. - (void)viewDidLoad
    22. {
    23. [super viewDidLoad];
    24. self.context = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES1];
    25. if (!self.context) {
    26. NSLog(@"Failed to create ES context");
    27. }
    28. GLKView *view = (GLKView*)self.view;
    29. view.context = self.context;
    30. view.drawableDepthFormat = GLKViewDrawableDepthFormat16;
    31. [self setUpGL];
    32. // Do any additional setup after loading the view.
    33. }
    34. - (void)didReceiveMemoryWarning
    35. {
    36. NSLog(@"ich bin der Fehler");
    37. [super didReceiveMemoryWarning];
    38. if ([self isViewLoaded] && ([[self view] window] == nil))
    39. {
    40. self.view = nil;
    41. [self tearDownGL];
    42. if ([EAGLContext currentContext] == self.context)
    43. {
    44. [EAGLContext setCurrentContext:nil];
    45. }
    46. self.context = nil;
    47. }
    48. }
    49. - (void) tearDownGL
    50. {
    51. // [EAGLContext setCurrentContext:self.context];
    52. // glDeleteBuffers(1, &vertexBuffer);
    53. // self.effect = nil;
    54. }
    55. -(void) glkView:(GLKView *)view drawInRect:(CGRect)rect {
    56. glClearColor(0.6, 0.1, 0.0, 1.0); // RGB und Alpha
    57. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    58. glLoadIdentity();
    59. GLKMatrix4 gml = GLKMatrix4MakeLookAt(0, 0, 1, //Kamera Position
    60. 0, 0.3, 0.5, //Look At
    61. 0, 1, 0); //"Stehend"
    62. glMultMatrixf(gml.m);
    63. [self drawOGLCube];
    64. }
    65. -(void) update{
    66. // float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
    67. glLoadIdentity();
    68. H = self.view.bounds.size.width;
    69. W = self.view.bounds.size.width;
    70. int w = W;
    71. int h = H;
    72. glMatrixMode(GL_PROJECTION);
    73. float zNear = 0.1;
    74. float zFar = 2500;
    75. float fieldOfViewAngle = 10;
    76. float top = zNear * tan(M_PI * fieldOfViewAngle / w);
    77. float bottom = -top;
    78. float left = bottom * w / h;
    79. float right = top * w / h;
    80. glFrustumf(left, right, bottom, top, zNear, zFar); //3D-Perspektive
    81. //Enable Modelview: Auf das Rendern von Vertex-Arrays umschalten
    82. glMatrixMode(GL_MODELVIEW);
    83. glEnableClientState(GL_VERTEX_ARRAY);
    84. glEnable(GL_DEPTH_TEST);
    85. glDepthFunc(GL_LESS);
    86. //Light
    87. glEnable(GL_LIGHTING);
    88. glEnable(GL_COLOR_MATERIAL);
    89. glEnable(GL_LIGHT0);
    90. GLfloat color0[ ] = {1, 1, 1, 1};
    91. glLightfv(GL_LIGHT0, GL_DIFFUSE, color0);
    92. GLfloat light_position0[ ] = {0, 1, 0, 0};
    93. glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
    94. /* GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(50), aspect, 0.1, 100);
    95. // static __inline__ GLKMatrix4 GLKMatrix4MakePerspective(float fovyRadians, float aspect, float nearZ, float farZ)
    96. self.effect.transform.projectionMatrix = projectionMatrix;
    97. GLKMatrix4 modelMatrix = GLKMatrix4MakeTranslation(0, 0, -7);
    98. modelMatrix = GLKMatrix4RotateY(modelMatrix, rotation);
    99. self.effect.transform.modelviewMatrix = modelMatrix;
    100. */
    101. // rotation += self.timeSinceLastUpdate;
    102. }
    103. - (void) setUpGL {
    104. [EAGLContext setCurrentContext:self.context];
    105. /*
    106. self.effect = [[GLKBaseEffect alloc]init];
    107. self.effect.light0.enabled = GL_TRUE;
    108. self.effect.light0.diffuseColor = GLKVector4Make(1, 0.8, 1.5, 1.0);
    109. */
    110. //Modelview: Auf das Rendern von Vertex Arrays umschalten
    111. /*
    112. glEnable(GL_DEPTH_TEST);
    113. glMatrixMode(GL_MODELVIEW);
    114. glEnableClientState(GL_VERTEX_ARRAY);
    115. glDepthFunc(GL_LESS);
    116. */
    117. }
    118. - (void) drawOGLCube {
    119. NSLog(@"drawOGLCube");
    120. GLfloat vertices[ ] = {
    121. -0.157871, 0.952064, -0.988728, 0.037220, 0.952064, -0.969513, 0.224813, 0.952064, -0.912607, 0.397700, 0.952064, -0.820197, 0.549236, 0.952064, -0.695835, 0.673599, 0.952064, -0.544298, 0.766009, 0.952064, -0.371411, 0.822915, 0.952064, -0.183818, 0.842129, 0.952064, 0.011272, -0.157871, 2.952064, 0.011272, 0.822915, 0.952064, 0.206362, 0.766009, 0.952064, 0.393956, 0.673599, 0.952064, 0.566842, 0.549236, 0.952064, 0.718379, 0.397700, 0.952064, 0.842742, 0.224813, 0.952064, 0.935152, 0.037219, 0.952064, 0.992058, -0.157871, 0.952064, 1.011272, -0.352961, 0.952064, 0.992057, -0.540555, 0.952064, 0.935152, -0.713441, 0.952064, 0.842742, -0.864978, 0.952064, 0.718379, -0.989341, 0.952064, 0.566842, -1.081751, 0.952064, 0.393955, -1.138656, 0.952064, 0.206362, -1.157871, 0.952064, 0.011271, -1.138656, 0.952064, -0.183819, -1.081750, 0.952064, -0.371412, -0.989340, 0.952064, -0.544299, -0.864977, 0.952064, -0.695835, -0.713440, 0.952064, -0.820198, -0.540553, 0.952064, -0.912608, -0.352959, 0.952064, -0.969513
    122. };
    123. GLfloat normals[ ] = {
    124. -0.259887, 0.445488, -0.856737, 0.087754, 0.445488, -0.890977, -0.087753, 0.445488, -0.890977, 0.259888, 0.445488, -0.856737, -0.422035, 0.445488, -0.789574, -0.567964, 0.445488, -0.692067, -0.692066, 0.445488, -0.567966, -0.789573, 0.445488, -0.422036, -0.856737, 0.445488, -0.259889, -0.890977, 0.445488, -0.087755, -0.890977, 0.445488, 0.087753, -0.856737, 0.445488, 0.259888, -0.789574, 0.445488, 0.422035, -0.692067, 0.445488, 0.567964, -0.567965, 0.445488, 0.692066, -0.422036, 0.445488, 0.789573, -0.259889, 0.445488, 0.856737, -0.087754, 0.445488, 0.890977, 0.087753, 0.445488, 0.890977, 0.259888, 0.445488, 0.856737, 0.422036, 0.445488, 0.789573, 0.567965, 0.445488, 0.692067, 0.692067, 0.445488, 0.567965, 0.789573, 0.445488, 0.422035, 0.856737, 0.445488, 0.259888, 0.890977, 0.445488, 0.087753, 0.890977, 0.445488, -0.087754, 0.856737, 0.445488, -0.259888, 0.789573, 0.445488, -0.422036, 0.692067, 0.445488, -0.567965, 0.567965, 0.445488, -0.692067, 0.422036, 0.445488, -0.789573, 0.000000, -1.000000, -0.000000
    125. };
    126. GLushort faces[ ] = { //12 Faces = 6 Wuerfelseiten mit je zwei Dreiecken
    127. 31, 9, 32, 0, 9, 1, 9, 0, 32, 1, 9, 2, 30, 9, 31, 29, 9, 30, 28, 9, 29, 27, 9, 28, 26, 9, 27, 25, 9, 26, 24, 9, 25, 23, 9, 24, 22, 9, 23, 21, 9, 22, 20, 9, 21, 19, 9, 20, 18, 9, 19, 17, 9, 18, 16, 9, 17, 15, 9, 16, 14, 9, 15, 13, 9, 14, 12, 9, 13, 11, 9, 12, 10, 9, 11, 8, 9, 10, 7, 9, 8, 6, 9, 7, 5, 9, 6, 4, 9, 5, 3, 9, 4, 2, 9, 3, 0, 1, 32, 1, 31, 32, 1, 2, 31, 2, 30, 31, 2, 3, 30, 3, 29, 30, 3, 4, 29, 4, 28, 29, 4, 5, 28, 5, 27, 28, 5, 6, 27, 6, 26, 27, 6, 7, 26, 7, 25, 26, 7, 8, 25, 8, 24, 25, 8, 10, 24, 10, 23, 24, 10, 11, 23, 11, 22, 23, 11, 12, 22, 12, 21, 22, 12, 13, 21, 13, 20, 21, 13, 14, 20, 14, 19, 20, 14, 15, 19, 15, 18, 19, 15, 16, 18, 16, 17, 18
    128. };
    129. //Anzahl der Array-Elemente
    130. //int verticesLength = sizeof(vertices) / sizeof(GLfloat);
    131. int facesLength = sizeof(faces) / sizeof(GLushort);
    132. //Render obj-Modell
    133. glEnableClientState(GL_NORMAL_ARRAY); //normal arrays aktivieren
    134. glEnable(GL_NORMALIZE);
    135. glEnable(GL_CULL_FACE); //Nicht sichtbare Flaechen ignorieren
    136. glCullFace(GL_BACK);
    137. glVertexPointer(3, GL_FLOAT, 0, vertices);
    138. glNormalPointer(GL_FLOAT, 0, normals);
    139. glColor4f(1, 1, 0, 1); //Einfaerben
    140. //Matrix-Operations
    141. glPushMatrix();
    142. glTranslatef(0, 0, -7);
    143. static int angle = 0; angle += 2;
    144. glRotatef(angle, 0, 1, 0);
    145. //glDrawArrays(GL_TRIANGLE_STRIP, 0, verticesLength/3);
    146. glDrawElements(GL_TRIANGLES, facesLength, GL_UNSIGNED_SHORT, faces);
    147. glPopMatrix();
    148. //Cleanup
    149. // glDisableClientState(GL_NORMAL_ARRAY);
    150. glDisable(GL_NORMALIZE);
    151. glDisable(GL_CULL_FACE);
    152. //glDrawArrays(GL_TRIANGLE_STRIP, 0, verticesLength/3); //-> obj liefert faces
    153. }
    154. -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    155. self.paused = !self.paused ;
    156. }
    157. @end
    Alles anzeigen