Herramientas de usuario

Herramientas del sitio


wiki2:ogl

¡Esta es una revisión vieja del documento!


OpenGL & GLSL

Render

glDrawArrays & glDrawElements

glDrawArrays is basically “draw this contiguous range of vertices, using the data I gave you earlier”.

  • Good:
    • You don't need to build an index buffer
  • Bad:
    • If you organise your data into GL_TRIANGLES, you will have duplicate vertex data for adjacent triangles. This is obviously wasteful.
    • If you use GL_TRIANGLE_STRIP and GL_TRIANGLE_FAN to try and avoid duplicating data: it isn't terribly effective and you'd have to make a rendering call for each strip and fan. OpenGL calls are expensive and should be avoided where possible.

With glDrawElements, you pass in buffer containing the indices of the vertices you want to draw.

  • Good
    • No duplicate vertex data - you just index the same data for different triangles
    • You can just use GL_TRIANGLES and rely on the vertex cache to avoid processing the same data twice - no need to re-organise your geometry data or split rendering over multiple calls
  • Bad
    • Memory overhead of index buffer

Texturas

Creating textures is very similar to creating vertex buffers : Create a texture, bind it, fill it, and configure it.

Remember to use power of two textures (…, 128×128, 256×256, 1024×1024…). However, in standard OpenGL ES 2.0, textures don’t have to be square, but each dimension should be a power of two ( POT ). This means that each dimension should be a number like 128, 256, 512, and so on, but there is also a maximum texture size that varies from implementation to implementation but is usually something large, like 2048 x 2048.

In glTexImage2D, the GL_RGB indicates that we are talking about a 3-component color, and GL_BGR says how exactly it is represented in RAM. As a matter of fact, BMP does not store Red→Green→Blue but Blue→Green→Red, so we have to tell it to OpenGL. So imagine that we stored a BMP in a unsigned char* data variable, and unsigned int widht, height variables have the size:

// Create one OpenGL texture
GLuint textureID;
glGenTextures(1, &textureID);
 
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, textureID);
 
// Give the image to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
 
// Configure the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

Lenguaje GLSL

Reserverd variables

  • vec4 gl_Position: Reserved variable within the vertex shader that holds the final transformed vertex to be displayed on the screen
  • vec4 gl_FragColor: Reserved variable within the fragment shader that holds the color of the vertex that has just been processed by the vertex shader

Data types

  • void: No function return value
  • bool: Boolean
  • int: Signed integer
  • float: Floating scalar
  • vec2, vec3, vec4: 2, 3, and 4 component floating-point vector
  • bvec2, bvec3, bvec4: 2, 3, and 4 component Boolean vector
  • ivec2, ivec3, ivec4: 2, 3, and 4 component signed integer vector
  • mat2, mat3, mat4: 2-by-2, 3-by-3, and 4-by-4 float matrices
  • sampler2D: Used to represent and access a 2D texture
  • samplerCube: Used to represent and access a cube mapped texture
  • float floatarray[3]: One-dimensional arrays; can be of types such as floats, vectors, and integers

You can address components in a vec4 type in the following ways:

  • {x, y, z, w}: You can use this representation when accessing vectors that represent points or normals.
  • {r, g, b, a}: You can use this representation when accessing vectors that represent colors.
  • {s, t, p, q}: You can use this representation when accessing vectors that represent texture coordinates.
vec3 position;
position.x = 1.0f;
 
vec4 color;
color.r = 1.0f;
 
vec2 texcoord;
texcoord.s = 1.0f;

Operators and flow control

There are these operators:

++ -- + - ! * / < > <= >= == !== && ^^ || = += -= *= /=

And the next flow control statements:

for(Initial counter value;
Expression to be evaluated;
Counter increment/decrement value)
if (Expression to evaluate)
{
// Statement to execute if expression is true
}
else
{
// Statement to execute if expression is false
}
while( Expression to evaluate )
{
// Statement to be executed
}
if (Expression to evaluate )
{
// Statements to execute
}

Built-in functions

  • float radians(float degrees): Converts degrees to radians and returns radians
  • float degrees(float radians): Converts radians to degrees and returns degrees
  • float sin(float angle): Returns the sine of an angle measured in radians
  • float cos(float angle): Returns the cosine of an angle measured in radians
  • float tan(float angle): Returns the tangent of an angle measured in radians
  • float asin(float x): Returns the angle whose sine is x
  • float acos(float x): Returns the angle whose cosine is x
  • float atan(float y, float x): Returns the angle whose tangent is specified by the slope y/x
  • float atan(float slope): Returns the angle whose tangent is slope
  • float abs(float x): Returns the absolute value of x
  • float length(vec3 x): Returns the length of vector x
  • float distance(vec3 point0, vec3 point1): Returns the distance between point0 and point1
  • float dot(vec3 x, vec3 y): Returns the dot product between two vectors x and y
  • vec3 cross(vec3 x, vec3 y): Returns the cross product of two vectors x and y
  • vec3 normalize(vec3 x): Normalizes the vector to a length of 1 and then returns it
  • float pow(float x, float y): Calculates x to the power of y and return it
  • float min(float x, float y): Returns the minimum value between x and y
  • float max(float x, float y): Returns the maximum value between x and y

Storage qualifiers

  • Const: The const qualifier specifies a compile time constant or a read-only function parameter.
  • Attribute: The attribute qualifier specifies a linkage between a vertex shader and the main OpenGL ES 2.0 program for per-vertex data. Some examples of the types of variables where the attribute qualifier can be used are vertex position, vertex textures, and vertex normal.
  • Uniform: The uniform qualifier specifies that the value does not change across the primitive being processed. Uniform variables form the linkage between a vertex or fragment shader and the main OpenGL ES 2.0 application. Some examples of variables where the uniform qualifier would be appropriate are lighting values, material values, and matrices.
  • Varying: The varying qualifier specifies a variable that occurs both in the vertex shader and fragment shader. This creates the linkage between a vertex shader and fragment shader for interpolated data. These are usually values for the diffuse and the specular lighting that will be passed from the vertex shader to the fragment shader. Texture coordinates, if present, are also varying. The values of the diffuse and specular lighting, as well as textures, are interpolated or “varying” across the object the shader is rendering. Some examples of varying variables are the vertex texture coordinate, the diffuse color of the vertex, and the specular color of the vertex.
Const int NumberLights = 3;
attribute vec3 aPosition;
attribute vec2 aTextureCoord;
attribute vec3 aNormal;
uniform vec3 uLightAmbient;
uniform vec3 uLightDiffuse;
uniform vec3 uLightSpecular;
varying vec2 vTextureCoord;
varying float vDiffuse;
varying float vSpecular;

Otras funciones

  • The color from the currently active texture that matches the texture coordinates in the texture coordinate is found through the texture2D() function.
vec4 color = texture2D(sTexture, vTextureCoord);

Shaders en general

Vertex Shader

Cambiar el tamaño de los puntos dibujados

gl_PointSize = 10.0;

Fragment Shader

Asignar un color fijo en el Vertex Shader

gl_FragColor = vec4(1,0,0,1);

Otros

Código ejemplo iOS

wiki2/ogl.1448712456.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)