Example #1
0
/*
 * Initalize OpenGL ES
 */
int S2D_gles_init(int width, int height, int s_width, int s_height) {
  
  // Enable transparency
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  
  // Set the viewport
  glViewport(0, 0, width, height);
  
  // Vertex shader source string
  GLchar vertexSource[] =
    // uniforms used by the vertex shader
    "uniform mat4 u_mvpMatrix;"  // model view and projection matrix
    
    // attributes input to the vertex shader
    "attribute vec4 a_position;"  // position value
    "attribute vec4 a_color;"     // input vertex color
    "attribute vec2 a_texcoord;"  // input texture
    
    // varying variables, input to the fragment shader
    "varying vec4 v_color;"     // output vertex color
    "varying vec2 v_texcoord;"  // output texture
    
    // main
    "void main()"
    "{"
    "  v_color = a_color;"
    "  v_texcoord = a_texcoord;"
    "  gl_Position = u_mvpMatrix * vec4(a_position.xyz, 1.0);"
    "}";
  
  // Fragment shader source string
  GLchar fragmentSource[] =
    "precision mediump float;"
    // input vertex color from vertex shader
    "varying vec4 v_color;"
    "void main()"
    "{"
    "  gl_FragColor = v_color;"
    "}";
  
  // Fragment shader source string for textures
  GLchar texFragmentSource[] =
    "precision mediump float;"
    // input vertex color from vertex shader
    "varying vec4 v_color;"
    "varying vec2 v_texcoord;"
    "uniform sampler2D texture;"
    "void main()"
    "{"
    "  gl_FragColor = texture(texture, v_texcoord) * v_color;"
    "}";
  
  // Load the vertex and fragment shaders
  GLuint vertexShader      = S2D_GL_LoadShader(  GL_VERTEX_SHADER,      vertexSource, "GLES Vertex");
  GLuint fragmentShader    = S2D_GL_LoadShader(GL_FRAGMENT_SHADER,    fragmentSource, "GLES Fragment");
  GLuint texFragmentShader = S2D_GL_LoadShader(GL_FRAGMENT_SHADER, texFragmentSource, "GLES Texture Fragment");
  
  // Create the shader program object
  shaderProgram = glCreateProgram();
  
  // Check if program was created successfully
  if (shaderProgram == 0) {
    S2D_GL_PrintError("Failed to create shader program");
    return GL_FALSE;
  }
  
  // Create the texture shader program object
  texShaderProgram = glCreateProgram();
  
  // Check if program was created successfully
  if (texShaderProgram == 0) {
    S2D_GL_PrintError("Failed to create shader program");
    return GL_FALSE;
  }
  
  // Attach the shader objects to the program object
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glAttachShader(texShaderProgram, vertexShader);
  glAttachShader(texShaderProgram, texFragmentShader);
  
  // Associate user-defined attribute variables in the
  // program object with the vertex attribute index
  glBindAttribLocation(shaderProgram, 0, "a_position");
  glBindAttribLocation(texShaderProgram, 0, "a_position");
  
  // Link the shader program
  glLinkProgram(shaderProgram);
  glLinkProgram(texShaderProgram);
  
  // Check if linked
  S2D_gles_check_linked(shaderProgram, "shaderProgram");
  S2D_gles_check_linked(texShaderProgram, "texShaderProgram");
  
  // Compute scaling factors, if necessary
  GLfloat scale_x = 1.0f;
  GLfloat scale_y = 1.0f;
  
  if (s_width != width) {
    scale_x = (GLfloat)s_width / (GLfloat)width;
  }
  
  if (s_height != height) {
    scale_y = (GLfloat)s_height / (GLfloat)height;
  }
  
  // Set orthographic projection matrix
  S2D_GL_orthoMatrix[0] = 2.0f / ((GLfloat)width * scale_x);
  S2D_GL_orthoMatrix[5] = -2.0f / ((GLfloat)height * scale_y);
  
  // Use the program object
  glUseProgram(shaderProgram);
  
  GLuint mMvpLocation = glGetUniformLocation(shaderProgram, "u_mvpMatrix");
  glUniformMatrix4fv(mMvpLocation, 1, GL_FALSE, S2D_GL_orthoMatrix);
  
  // Use the texture program object
  glUseProgram(texShaderProgram);
  
  GLuint texmMvpLocation = glGetUniformLocation(texShaderProgram, "u_mvpMatrix");
  glUniformMatrix4fv(texmMvpLocation, 1, GL_FALSE, S2D_GL_orthoMatrix);
  
  // Clean up
  glDeleteShader(vertexShader);
  glDeleteShader(fragmentShader);
  glDeleteShader(texFragmentShader);
  
  return GL_TRUE;
}
Example #2
0
/*
 * Initalize OpenGL ES
 */
int S2D_GLES_Init() {

  // Enable transparency
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  // Vertex shader source string
  GLchar vertexSource[] =
    // uniforms used by the vertex shader
    "uniform mat4 u_mvpMatrix;"   // projection matrix

    // attributes input to the vertex shader
    "attribute vec4 a_position;"  // position value
    "attribute vec4 a_color;"     // input vertex color
    "attribute vec2 a_texcoord;"  // input texture

    // varying variables, input to the fragment shader
    "varying vec4 v_color;"       // output vertex color
    "varying vec2 v_texcoord;"    // output texture

    "void main()"
    "{"
    "  v_color = a_color;"
    "  v_texcoord = a_texcoord;"
    "  gl_Position = u_mvpMatrix * a_position;"
    "}";

  // Fragment shader source string
  GLchar fragmentSource[] =
    "precision mediump float;"
    // input vertex color from vertex shader
    "varying vec4 v_color;"

    "void main()"
    "{"
    "  gl_FragColor = v_color;"
    "}";

  // Fragment shader source string for textures
  GLchar texFragmentSource[] =
    "precision mediump float;"
    // input vertex color from vertex shader
    "varying vec4 v_color;"
    "varying vec2 v_texcoord;"
    "uniform sampler2D s_texture;"

    "void main()"
    "{"
    "  gl_FragColor = texture2D(s_texture, v_texcoord) * v_color;"
    "}";

  // Load the vertex and fragment shaders
  GLuint vertexShader      = S2D_GL_LoadShader(  GL_VERTEX_SHADER,      vertexSource, "GLES Vertex");
  GLuint fragmentShader    = S2D_GL_LoadShader(GL_FRAGMENT_SHADER,    fragmentSource, "GLES Fragment");
  GLuint texFragmentShader = S2D_GL_LoadShader(GL_FRAGMENT_SHADER, texFragmentSource, "GLES Texture Fragment");

  // Triangle Shader //

  // Create the shader program object
  shaderProgram = glCreateProgram();

  // Check if program was created successfully
  if (shaderProgram == 0) {
    S2D_GL_PrintError("Failed to create shader program");
    return GL_FALSE;
  }

  // Attach the shader objects to the program object
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);

  // Link the shader program
  glLinkProgram(shaderProgram);

  // Check if linked
  S2D_GL_CheckLinked(shaderProgram, "GLES shader");

  // Get the attribute locations
  positionLocation = glGetAttribLocation(shaderProgram, "a_position");
  colorLocation    = glGetAttribLocation(shaderProgram, "a_color");

  // Texture Shader //

  // Create the texture shader program object
  texShaderProgram = glCreateProgram();

  // Check if program was created successfully
  if (texShaderProgram == 0) {
    S2D_GL_PrintError("Failed to create shader program");
    return GL_FALSE;
  }

  // Attach the shader objects to the program object
  glAttachShader(texShaderProgram, vertexShader);
  glAttachShader(texShaderProgram, texFragmentShader);

  // Link the shader program
  glLinkProgram(texShaderProgram);

  // Check if linked
  S2D_GL_CheckLinked(texShaderProgram, "GLES texture shader");

  // Get the attribute locations
  texPositionLocation = glGetAttribLocation(texShaderProgram, "a_position");
  texColorLocation    = glGetAttribLocation(texShaderProgram, "a_color");
  texCoordLocation    = glGetAttribLocation(texShaderProgram, "a_texcoord");

  // Get the sampler location
  samplerLocation = glGetUniformLocation(texShaderProgram, "s_texture");

  // Clean up
  glDeleteShader(vertexShader);
  glDeleteShader(fragmentShader);
  glDeleteShader(texFragmentShader);

  return GL_TRUE;
}