Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:oglpipeline [2015/11/21 20:36] alfred [Color] |
wiki2:oglpipeline [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 257: | Línea 257: | ||
| {{:wiki2:ogl:color.zip|Example}} | {{:wiki2:ogl:color.zip|Example}} | ||
| - | ===== Transformaciones ===== | + | ===== Transformaciones y proyecciones ===== |
| + | |||
| + | * {{:wiki2:ogl:tutorial_3_matrices.pdf|Explicacion de matrices y proyecciones}} | ||
| + | * {{:wiki2:ogl:objectoriented.matrix.zip|Ejemplo de matrices y orientación a objetos en OpenGL ES}} | ||
| ===== Texturas ===== | ===== Texturas ===== | ||
| + | ==== Cargar texturas ==== | ||
| + | |||
| + | - Read the texture. | ||
| + | - Bind the texture. | ||
| + | - Set filtering: a default must be set, or the texture will be black. | ||
| + | - Load the bitmap into the bound texture with ''texImage2D''. | ||
| + | - Unbind from the texture with ''glBindTexture(GL_TEXTURE_2D, 0)''. | ||
| + | |||
| + | <code java> | ||
| + | public static int loadTexture(Context context, int resourceId) { | ||
| + | final int[] textureObjectIds = new int[1]; | ||
| + | glGenTextures(1, textureObjectIds, 0); | ||
| + | if (textureObjectIds[0] == 0) { | ||
| + | Log.w(TAG, "Could not generate a new OpenGL texture object."); | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | final BitmapFactory.Options options = new BitmapFactory.Options(); | ||
| + | options.inScaled = false; | ||
| + | final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options); | ||
| + | if (bitmap == null) { | ||
| + | Log.w(TAG, "Resource ID " + resourceId + " could not be decoded."); | ||
| + | glDeleteTextures(1, textureObjectIds, 0); | ||
| + | return 0; | ||
| + | } | ||
| + | glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]); | ||
| + | |||
| + | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
| + | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
| + | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | ||
| + | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | ||
| + | |||
| + | texImage2D(GL_TEXTURE_2D, 0, bitmap, 0); | ||
| + | glBindTexture(GL_TEXTURE_2D, 0); | ||
| + | bitmap.recycle(); | ||
| + | |||
| + | Log.w(TAG, "Texture loaded"); | ||
| + | |||
| + | return textureObjectIds[0]; | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | Podríamos cargar mipmaps: | ||
| + | <code java> | ||
| + | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); | ||
| + | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
| + | glGenerateMipmap(GL_TEXTURE_2D); | ||
| + | </code> | ||
| + | |||
| + | ==== Shaders ==== | ||
| + | === Vertex === | ||
| + | <code> | ||
| + | attribute vec4 a_Position; | ||
| + | attribute vec2 a_TextureCoordinates; | ||
| + | varying vec2 v_TextureCoordinates; | ||
| + | |||
| + | void main() | ||
| + | { | ||
| + | v_TextureCoordinates = a_TextureCoordinates; | ||
| + | gl_Position = a_Position; | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | === Fragment === | ||
| + | <code> | ||
| + | precision mediump float; | ||
| + | uniform sampler2D u_TextureUnit; | ||
| + | varying vec2 v_TextureCoordinates; | ||
| + | |||
| + | void main() | ||
| + | { | ||
| + | gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates); | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ==== When render ==== | ||
| + | <code java> | ||
| + | vertexData.position(0); | ||
| + | indexArray.position(0); | ||
| + | |||
| + | glClear(GL_COLOR_BUFFER_BIT); | ||
| + | |||
| + | glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT, | ||
| + | false, STRIDE, vertexData); | ||
| + | |||
| + | vertexData.position(POSITION_COMPONENT_COUNT); | ||
| + | glVertexAttribPointer(aTextCoordLocation, TEXTURE_COORDINATES_COMPONENT_COUNT, GL_FLOAT, | ||
| + | false, STRIDE, vertexData); | ||
| + | |||
| + | glEnableVertexAttribArray(aPositionLocation); | ||
| + | glEnableVertexAttribArray(aTextCoordLocation); | ||
| + | |||
| + | glActiveTexture(GL_TEXTURE0); | ||
| + | glBindTexture(GL_TEXTURE_2D, texture); | ||
| + | glUniform1i(uTextureUnitLocation, 0); | ||
| + | |||
| + | glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, indexArray); | ||
| + | |||
| + | glDisableVertexAttribArray(aPositionLocation); | ||
| + | glDisableVertexAttribArray(aTextCoordLocation); | ||
| + | </code> | ||
| + | |||
| + | Setting these values: | ||
| + | <code java> | ||
| + | float[] tableVerticesWithTriangles = { | ||
| + | // x, y s,t | ||
| + | 0, 0.5f, 0.5f,0, | ||
| + | -0.5f, 0, 0,1, | ||
| + | 0.5f, 0, 1,1 | ||
| + | }; | ||
| + | </code> | ||
| + | |||
| + | And these constants: | ||
| + | <code java> | ||
| + | private static final int BYTES_PER_FLOAT = 4; | ||
| + | private static final int POSITION_COMPONENT_COUNT = 2; | ||
| + | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; | ||
| + | private static final int STRIDE = (POSITION_COMPONENT_COUNT + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; | ||
| + | </code> | ||
| + | ==== Code ==== | ||
| + | * {{:wiki2:ogl:textures.zip|}} | ||
| ===== Luces ===== | ===== Luces ===== | ||