Open GL V.S. OpenGL ES for triangles


OpenGL 渲染一個小角形 -


glBegin (GL_TRIANGLES);
  glVertex3f (0,1,0);
  glVertex3f (-1,0,0);
  glVertex3f (1,0,0);
glEnd();

而 OpenGL ES 若要渲染一個幾何模型, 要用 Vertex 陣列


const GLbyte KVertices []=
{
	0,1,0,
	-1,0,0,
	1,0,0
};
 
...
 
glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (3, GL_BYTE , 0, KVertices);	
glDrawArrays (GL_TRIANGLES, 0, 3);

OpenGL 与 OpenGL ES 的区别 -


数据类型:

1: i   GLint    整数型
2: f   GLfixed  定点小数
3: x   GLclampx 限定型定点小数

删除的功能:

1.glBegin/glEnd
2.glArrayElement
3.显示列表
4.求值器
5.索引色模式
6.自定义裁剪平面
7.glRect
8.图像处理(这个一般显卡也没有,FireGL/Quadro显卡有)
9.反馈缓冲
10.选择缓冲
11.累积缓冲
12.边界标志
13.glPolygonMode
14.GL_QUADS,GL_QUAD_STRIP,GL_POLYGON
15.glPushAttrib,glPopAttrib,glPushClientAttrib,glPopClientAttrib
15.TEXTURE_1D、TEXTURE_3D、TEXTURE_RECT、TEXTURE_CUBE_MAP
16.GL_COMBINE
17.自动纹理坐标生成
18.纹理边界
19.GL_CLAMP、GL_CLAMP_TO_BORDER
20.消失纹理代表
21.纹理LOD限定
22.纹理偏好限定
23.纹理自动压缩、解压缩
24.glDrawPixels,glPixelTransfer,glPixelZoom
25.glReadBuffer,glDrawBuffer,glCopyPixels

其它注意事项:

1.glDrawArrays等函数中数据必须紧密排列,即间隔为0
2.各种数据的堆栈深度较低


Hello Triangle With OpenGL ES 2.0

#include "esUtil.h"
typedef struct
{
    // Handle to a program object
   GLuint programObject;
} UserData;
///
// Create a shader object, load the shader source, and
// compile the shader.
//
GLuint LoadShader(const char *shaderSrc, GLenum type)
{
   GLuint shader;
   GLint compiled;
   // Create the shader object
    shader = glCreateShader(type);
   if(shader == 0)
       return 0;
   // Load the shader source
   glShaderSource(shader, 1, &shaderSrc, NULL);
    // Compile the shader
   glCompileShader(shader);
    // Check the compile status
   glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
   if(!compiled)
   {
       GLint infoLen = 0;
       glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
       if(infoLen > 1)
       {
          char* infoLog = malloc(sizeof(char) * infoLen);
          glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
          esLogMessage("Error compiling shader:\n%s\n", infoLog);
          free(infoLog);
       }
       glDeleteShader(shader);
       return 0;
   }
   return shader;
}
///
// Initialize the shader and program object
//
int Init(ESContext *esContext)
{
    UserData *userData = esContext->userData;
   GLbyte vShaderStr[] =
       "attribute vec4 vPosition;   \n"
       "void main()                 \n"
       "{                           \n"
       "   gl_Position = vPosition; \n"
       "}                           \n";
   GLbyte fShaderStr[] =
       "precision mediump float;                   \n"
       "void main()                                \n"
       "{                                          \n"
       " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n"
       "}                                          \n";
   GLuint vertexShader;
   GLuint fragmentShader;
   GLuint programObject;
   GLint linked;
  // Load the vertex/fragment shaders
  vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr);
  fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);
  // Create the program object
  programObject = glCreateProgram();
  if(programObject == 0)
     return 0;
  glAttachShader(programObject, vertexShader);
  glAttachShader(programObject, fragmentShader);
  // Bind vPosition to attribute 0
  glBindAttribLocation(programObject, 0, "vPosition");
  // Link the program
  glLinkProgram(programObject);
  // Check the link status
  glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
  if(!linked)
  {
     GLint infoLen = 0;
     glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
     if(infoLen > 1)
     {
        char* infoLog = malloc(sizeof(char) * infoLen);
        glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
        esLogMessage("Error linking program:\n%s\n", infoLog);
        free(infoLog);
     }
     glDeleteProgram(programObject);
     return FALSE;
  }
  // Store the program object
  userData->programObject = programObject;
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  return TRUE;
}
///
// Draw a triangle using the shader pair created in Init()
//
void Draw(ESContext *esContext)
{
    UserData *userData = esContext->userData;
   GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
                           -0.5f, -0.5f, 0.0f,
                           0.5f, -0.5f, 0.0f};
    // Set the viewport
   glViewport(0, 0, esContext->width, esContext->height);
    // Clear the color buffer
   glClear(GL_COLOR_BUFFER_BIT);
    // Use the program object
   glUseProgram(userData->programObject);
   // Load the vertex data
   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
   glEnableVertexAttribArray(0);
   glDrawArrays(GL_TRIANGLES, 0, 3);
   eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}
int main(int argc, char *argv[])
{
   ESContext esContext;
   UserData userData;
   esInitialize(&esContext);
    esContext.userData = &userData;
   esCreateWindow(&esContext, "Hello Triangle", 320, 240,
                   ES_WINDOW_RGB);
   if(!Init(&esContext))
       return 0;
   esRegisterDrawFunc(&esContext, Draw);
   esMainLoop(&esContext);
}




你可能感兴趣的:(Open GL V.S. OpenGL ES for triangles)