º£²ó¤ÏOpenGLES2¤ÇÈĥݥꥴ¥ó¤òÉÁ²è¤·¤Þ¤¹ º£²ó¤Î¥×¥í¥°¥é¥à°ì¼°¤Ï¤³¤Á¤é º£²ó°Ê¹ß¤«¤é º£²ó¤Ï¤ä¤ë¤³¤ÈÀ¹¤ê¤À¤¯¤µ¤ó¤Ç¤¹¡£ ÉÁ²è¤Îή¤ì¤ÏDrawQuadView.m¤Ë¤Þ¤È¤á¤Æ¤¢¤ë¤Î¤Ç ¡ÄºÅÀ¹½Â¤¤ÎÄêµÁ // ĺÅÀ¹½Â¤ typedef struct { float Position[3]; // ĺÅÀºÂɸ float TexCoord[2]; // ¥Æ¥¯¥¹¥Á¥ãºÂɸ } Vertex; #define TEX_COORD_MAX 1 const Vertex Vertices[] = { {{1, -1, 1}, {TEX_COORD_MAX, 0}}, {{1, 1, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, -1, 1},{0, 0}}, }; // ĺÅÀ¥¤¥ó¥Ç¥Ã¥¯¥¹ const GLubyte Indices[] = { 2, 1, 0, 0, 3, 2, }; º£²ó¤ÏĺÅÀ¥¤¥ó¥Ç¥Ã¥¯¥¹¤Ç»ØÄꤷ¤¿ÄºÅÀ¤«¤éÀ®¤ë £³DÉÁ²è¤Î´ðËÜñ°Ì¤Ï»°³Ñ·Á¤Ç¤¹ ¢¥Æ¥¯¥¹¥Á¥ã¡Ê²èÁü¡Ë¤ÎÆɤ߹þ¤ß _texture = [[Texture alloc] initWithFile:[NSString stringWithUTF8String:"tile_floor.png"]]; £ÄºÅÀ¥Ð¥Ã¥Õ¥¡¥ª¥Ö¥¸¥§¥¯¥È¡ÊVBO¡Ë¤ÎºîÀ® // ĺÅÀ¥Ð¥Ã¥Õ¥¡ºîÀ® glGenBuffers(1, &_vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); // ¥¤¥ó¥Ç¥Ã¥¯¥¹¥Ð¥Ã¥Õ¥¡ºîÀ® glGenBuffers(1, &_indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); ¤ÄºÅÀ¥·¥§¡¼¥À¡¢¥Õ¥é¥°¥á¥ó¥È¥·¥§¡¼¥À¤ÎºîÀ®¤ª¤è¤Ó¥³¥ó¥Ñ¥¤¥ë¡õ¥ê¥ó¥¯ ¥·¥§¡¼¥À¤Ï£²¼ïÎढ¤ê¡¢ ÉÁ²è»þ¤Ë¤ÏĺÅÀñ°Ì¤Ë½èÍý¤ò¤µ¤ì¤¿¸å¡¢ Vertex.glsl¤¬ÄºÅÀ¥·¥§¡¼¥À¤Ç¤¹¡£ // input Vertex data attribute vec3 aPosition; attribute vec2 aTexCoord; // Uniform data uniform mat4 uProjectionMatrix; uniform mat4 uModelViewMatrix; // Output Vertex data varying vec2 vTexCoord; void main(void) { gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aPosition,1.0); vTexCoord = aTexCoord; } attribute°À¤ò»ý¤ÄÊÑ¿ô¤ÏĺÅÀñ°Ì¤ÇÁ÷¤é¤ì¤Æ¤¯¤ë¥Ç¡¼¥¿¤Ç¤¹¡£ Fragment.glsl¤¬¥Õ¥é¥°¥á¥ó¥È¥·¥§¡¼¥À¤Ç¤¹¡£ varying highp vec2 vTexCoord; uniform sampler2D ColorTextureSampler; void main(void) { gl_FragColor = texture2D(ColorTextureSampler, vTexCoord); } varying°À¤ò»ý¤ÄÊÑ¿ô¤Ï¥Õ¥é¥°¥á¥ó¥È¥·¥§¡¼¥À¤ËÁ÷¤é¤ì¤Æ¤¯¤ë¥Ç¡¼¥¿¤Ç¤¹¡£ ºîÀ®¤·¤¿¥·¥§¡¼¥À¤Î¥³¥ó¥Ñ¥¤¥ë¡õ¥ê¥ó¥¯¤Ï // ¥×¥í¥°¥é¥à¥Ö¥ë¥·¥§¡¼¥À¡¼¤Î¥³¥ó¥Ñ¥¤¥ë - (void)compileShaders { shader = [[Shader alloc] init]; [shader compileShaders:@"Vertex.glsl" fragmentShader:@"Fragment.glsl"]; ¥³¥ó¥Ñ¥¤¥ë¡õ¥ê¥ó¥¯¤Î¼ÂÁõ¤Ï ¥·¥§¡¼¥À¡¼¤Î¥³¥ó¥Ñ¥¤¥ë¡õ¥ê¥ó¥¯¤¬À®¸ù¤·¤¿¤é // ¥·¥§¡¼¥ÀÊÑ¿ô¤Î¥Ï¥ó¥É¥ë¼èÆÀ _positionSlot = glGetAttribLocation(shader.programHandle, "Position"); _texCoordSlot = glGetAttribLocation(shader.programHandle, "TexCoordIn"); _modelViewUniform = glGetUniformLocation(shader.programHandle, "Modelview"); _projectionUniform = glGetUniformLocation(shader.programHandle, "Projection"); _textureUniform = glGetUniformLocation(shader.programHandle, "ColorTexture"); ¥¥·¥§¡¼¥ÀÊÑ¿ô¤ÎÀßÄê GLKMatrix4 modelMatrix = GLKMatrix4Make(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1); // ¥Ó¥å¡¼ÊÑ´¹ GLKMatrix4 viewMatrix = [_transformations getViewMatrix]; GLKMatrix4 modelViewMatrix = GLKMatrix4Multiply(viewMatrix,modelMatrix); glUniformMatrix4fv(_modelViewUniform, 1, 0, modelViewMatrix.m); // ¼Í±ÆÊÑ´¹¹ÔÎó const GLfloat aspectRatio = (GLfloat)(self.bounds.size.width) / (GLfloat)(self.bounds.size.height); const GLfloat fieldView = GLKMathDegreesToRadians(60.0f); const GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(fieldView, aspectRatio, 0.1f, 100.0f); glUniformMatrix4fv(_projectionUniform, 1, 0, projectionMatrix.m); // ¥Ó¥å¡¼¥Ý¡¼¥ÈÊÑ´¹¤ÎÀßÄê glViewport(0, 0, self.frame.size.width, self.frame.size.height); // ÉÁ²è»þ¤Ë»ÈÍѤ¹¤ë¥·¥§¡¼¥À¤ÎÀßÄê glUseProgram(shader.programHandle); // VBO¤ÎÀßÄê glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); // ĺÅÀ¥Ç¡¼¥¿¹½Â¤¤ÎÀßÄê glEnableVertexAttribArray(_positionSlot); glEnableVertexAttribArray(_texCoordSlot); glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3)); // ¥Æ¥¯¥¹¥Á¥ã¤ÎÀßÄê glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, _texture.handle); glUniform1i(_textureUniform, 0); ¦ÄºÅÀ¥Ð¥Ã¥Õ¥¡¥ª¥Ö¥¸¥§¥¯¥È¤ÎÉÁ²è // ÉÁ²è glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0); Ê䡧 ¥¸¥§¥¹¥Á¥ã¡¼ÁàºîÉôʬ¤Ï¤½¤Î¤Þ¤Þ¥Ñ¥¯¤Ã¤Æ¤¤¿¤â¤Î¤Ê¤Î¤Ç¾ÜºÙ¤Ï¥ê¥ó¥¯Àè¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤ DrawQuadView.m¤Î - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // Begin transformations [_transformations start]; } // ¥Ñ¥ó - (IBAction)pan:(UIPanGestureRecognizer *)sender { // Pan (1 Finger) if((sender.numberOfTouches == 1) && ((_transformations.state == S_NEW) || (_transformations.state == S_TRANSLATION))) { // Ê¿¹Ô°ÜÆ° CGPoint translation = [sender translationInView:sender.view]; float x = translation.x/sender.view.frame.size.width; float y = translation.y/sender.view.frame.size.height; [_transformations translate:GLKVector2Make(x, y) withMultiplier:5.0f]; } // Pan (2 Fingers) else if((sender.numberOfTouches == 2) && ((_transformations.state == S_NEW) || (_transformations.state == S_ROTATION))) { // ²óž const float m = GLKMathDegreesToRadians(0.5f); CGPoint rotation = [sender translationInView:sender.view]; [_transformations rotate:GLKVector3Make(rotation.x, rotation.y, 0.0f) withMultiplier:m]; } } // ¥Ô¥ó¥Á¥¤¥ó¡¦¥¢¥¦¥È - (IBAction)pinch:(UIPinchGestureRecognizer *)sender { // Pinch if((_transformations.state == S_NEW) || (_transformations.state == S_SCALE)) { float scale = [sender scale]; [_transformations scale:scale]; } } // ¥í¡¼¥Æ¡¼¥·¥ç¥ó - (IBAction)rotation:(UIRotationGestureRecognizer *)sender { // Rotation if((_transformations.state == S_NEW) || (_transformations.state == S_ROTATION)) { float rotation = [sender rotation]; [_transformations rotate:GLKVector3Make(0.0f, 0.0f, rotation) withMultiplier:1.0f]; } } ¾åµ¤Î ¼¡¡§iPhone3D¹ÖºÂ3²ó
|