OpenGL ES 学习 1

主函数是
int esMain(Escontext *escontext){
     esCreateWindow ( esContext, "a123", 640, 480, ES_WINDOW_RGB | ES_WINDOW_DEPTH );
    esRegisterShutdownFunc ( esContext, Shutdown );
    esRegisterUpdateFunc ( esContext, Update );
    esRegisterDrawFunc ( esContext, Draw );
}
 顶点着色器
 const char vShaderStr[] =
    "#version 300 es                             \n"
    "uniform mat4 u_mvpMatrix;                   \n"
    "layout(location = 0) in vec4 a_position;   \n"
    "layout(location = 1) in vec2 a_texCoord;   \n"
    "out vec2 v_texCoord;                       \n"
    "void main()                                \n"
    "{                                          \n"
    "   gl_Position = u_mvpMatrix * a_position;  \n" 更新位置
    "   v_texCoord = a_texCoord;                \n"   为片段着色器传入纹理坐标
    "                                           \n";
      片段着色器  
    const char fShaderStr[] =
    "#version 300 es                                     \n"
    "precision mediump float;                            \n"
    "in vec2 v_texCoord;                                 \n"
    "layout(location = 0) out vec4 outColor;             \n"
    "uniform sampler2D s_texture;                        \n"
    "void main()                                         \n"
    "{                                                   \n"
    "  outColor = texture( s_texture, v_texCoord );      \n"
    "}                                                   \n";
    userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); 绑定 返回程序编号
    glGetUniformLocation(绑定程序编号,统一变量);返回绑定编号
    对应用
    glUniform***(绑定编号,数量(有的没有),值)更改;
    glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );       背景颜色
     ESMatrix modelview 声明矩阵
     esPerspective ( &perspective, 60.0f, aspect, 2.0f ,10.0f );透视矩阵 
     esMatrixLoadIdentity ( &modelview );初始化矩阵
     esTranslate ( &modelview, 0.0, 0.0, -3.0 );矩阵位移
     esRotate ( &modelview, userData->angle, 1.0, 1.0, 1.0 );矩阵旋转
     esMatrixMultiply ( &userData->mvpMatrix, &modelview, &perspective );
     glViewport ( 0, 0, esContext->width, esContext->height ); 指定窗口样式 x,y,w,h x,y左下角串口坐标 w,h宽高
     glUseProgram ( userData->programObject ); 使用程序
     glDeleteProgram ( userData->programObject );和上面对应
    为了减少每次从客户内存到图形内存的数据复制
    可以讲数据放到缓冲区中
    每次在缓冲区中取数据
    glGenBuffers 申请缓冲区对象
    glBindBuffer()绑定缓冲区
    glBufferData()缓冲区中加入数据
    纹理
     glGenTextures()生成纹理
     glDeleteTextures()删除
     glBindTexture()绑定
     glTexImage2D()加载纹理
      glTexParameteri ()指定过滤模式和纹理坐标包装
      glActiveTexture()绑定纹理单元 供采样器(sampler)加载
      在用glBindTexture()绑定
      texture()按纹理坐标输出采样器纹理    


绘制了每个面不同纹理坐标包装的立方体

#include <stdlib.h>
#include <string.h>
#include "esUtil.h"

typedef struct
{
    GLuint programObject;
    GLint  mvpLoc;
    GLfloat  *cubetextures;
    GLfloat   angle;
    GLint  samplerLoc;
    GLint id;
    ESMatrix  mvpMatrix;
} UserData;
GLubyte *GenCheckImage ( int width, int height, int checkSize )
{
    int x,
    y;
    GLubyte *pixels = malloc ( width * height * 3 );

    if ( pixels == NULL )
    {
        return NULL;
    }

    for ( y = 0; y < height; y++ )
        for ( x = 0; x < width; x++ )
        {
            GLubyte rColor = 0;
            GLubyte bColor = 0;

            if ( ( x / checkSize ) % 2 == 0 )
            {
                rColor = 255 * ( ( y / checkSize ) % 2 );
                bColor = 255 * ( 1 - ( ( y / checkSize ) % 2 ) );
            }
            else
            {
                bColor = 255 * ( ( y / checkSize ) % 2 );
                rColor = 255 * ( 1 - ( ( y / checkSize ) % 2 ) );
            }

            pixels[ ( y * width + x ) * 3] = rColor;
            pixels[ ( y * width + x ) * 3 + 1] = 0;
            pixels[ ( y * width + x ) * 3 + 2] = bColor;
        }

    return pixels;
}
GLuint CreateSimpleTextureCubemap( int val)
{
    GLuint textureId;
    int    width = 256,
    height = 256;
    GLubyte *cubePixels;
        cubePixels = GenCheckImage(width, height,val);
    glGenTextures ( 1, &textureId );
    glBindTexture ( GL_TEXTURE_2D, textureId );
    glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0,
                  GL_RGB, GL_UNSIGNED_BYTE, cubePixels );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    return textureId;

}
int Init ( ESContext *esContext )
{
    UserData *userData = esContext->userData;
    const char vShaderStr[] =
    "#version 300 es                             \n"
    "uniform mat4 u_mvpMatrix;                   \n"
    "layout(location = 0) in vec4 a_position;   \n"
    "layout(location = 1) in vec2 a_texCoord;   \n"
    "out vec2 v_texCoord;                       \n"
    "void main()                                \n"
    "{                                          \n"
    "   gl_Position = u_mvpMatrix * a_position;  \n"
    "   v_texCoord = a_texCoord;                \n"
    "}                                          \n";
    const char fShaderStr[] =
    "#version 300 es                                     \n"
    "precision mediump float;                            \n"
    "in vec2 v_texCoord;                                 \n"
    "layout(location = 0) out vec4 outColor;             \n"
    "uniform sampler2D s_texture;                        \n"
    "void main()                                         \n"
    "{                                                   \n"
    "  outColor = texture( s_texture, v_texCoord );      \n"
    "}                                                   \n";
     userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
    userData->mvpLoc = glGetUniformLocation ( userData->programObject, "u_mvpMatrix" );
    userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
    userData->angle = 0.0f;
    glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );
    return GL_TRUE;
}
GLfloat vVertices[] = {
    // 前
    -0.5, -0.5, 0.5,   1.0f,
    -0.5, 0.5, 0.5,     1.0f,
    0.5, 0.5, 0.5,    1.0f,
    0.5, -0.5, 0.5,     1.0f,
    // 后
    -0.5, -0.5, -0.5,   1.0f,
    -0.5, 0.5, -0.5,     1.0f,
    0.5, 0.5, -0.5,    1.0f,
    0.5, -0.5, -0.5,     1.0f,
    //上
    -0.5, 0.5, -0.5,1.0f,
     0.5, 0.5, -0.5, 1.0f,
    0.5, 0.5, 0.5,1.0f,
    -0.5, 0.5, 0.5, 1.0f,
    // 下
    -0.5, -0.5, -0.5,1.0f,
    -0.5, -0.5, 0.5, 1.0f,
    0.5, -0.5, 0.5,1.0f,
     0.5, -0.5, -0.5, 1.0f,

    // 右
    0.5, -0.5, -0.5,   1.0f,
    0.5, 0.5, -0.5,    1.0f,
    0.5, 0.5, 0.5,1.0f,
    0.5, -0.5, 0.5,  1.0f,
    // 左
     -0.5, -0.5, -0.5,   1.0f,
    -0.5, 0.5, -0.5,    1.0f,
    -0.5, 0.5, 0.5,   1.0f,
    -0.5, -0.5, 0.5,  1.0f,
};
GLfloat Vertices[] = {
    0, 0,
   0, 0.5,
    0.5, 0.5,
    0.5, 0,
    // 后
    0, 0,
    0, 0.5,
    0.5, 0.5,
    0.5, 0,
    //上
    0,  0,
    2.0, 0,
    2.0, 2.0,
    0,  2.0,
    // 下
    0,  0,
    0, 0.5,
    0.5,  0.5,
    0.5,  0,
    
    // 右
    0, 0,
     0.5, 0,
     0.5, 0.5,
     0, 0.5,
    // 左
    0, 0,
     2.0, 0,
     2.0, 2.0,
     0, 2.0,
};
GLushort indices[] = {
    0, 2, 1,
    0, 3, 2,
    4, 5, 6,
    4, 6, 7,
    8, 10, 9,
    8, 11, 10,
    12, 14, 13,
    12, 15, 14,
    16, 17, 18,
    16, 18, 19,
    20, 22, 21,
    20, 23, 22
};
void Update ( ESContext *esContext, float deltaTime )
{
    UserData *userData = esContext->userData;
    ESMatrix perspective;
    ESMatrix modelview;
    float    aspect;
   userData->angle += ( deltaTime * 40.0f );
    if ( userData->angle >= 360.0f ){
        userData->angle -= 360.0f;
    }
    aspect = ( GLfloat ) esContext->width / ( GLfloat ) esContext->height;
    esMatrixLoadIdentity ( &perspective );
    esPerspective ( &perspective, 60.0f, aspect, 2.0f ,10.0f );
    esMatrixLoadIdentity ( &modelview );
    esTranslate ( &modelview, 0.0, 0.0, -3.0 );
    esRotate ( &modelview, userData->angle, 1.0, 1.0, 1.0 );
    esMatrixMultiply ( &userData->mvpMatrix, &modelview, &perspective );
}
void draw(UserData *userData,int val,int vval){
    userData->id=CreateSimpleTextureCubemap(vval);
    glActiveTexture ( GL_TEXTURE0 );
    glBindTexture ( GL_TEXTURE_2D, userData->id );
    glUniform1i ( userData->samplerLoc, 0 );
}
void Draw ( ESContext *esContext )
{   UserData *userData = esContext->userData;
    glViewport ( 0, 0, esContext->width, esContext->height );
    glClear ( GL_COLOR_BUFFER_BIT  );
    glUseProgram ( userData->programObject );
    glVertexAttribPointer ( 0, 3, GL_FLOAT,GL_FALSE, 4 * sizeof ( GLfloat ), vVertices );
    glVertexAttribPointer ( 1, 2, GL_FLOAT,GL_TRUE,2 * sizeof ( GLfloat ), Vertices);
    glEnableVertexAttribArray ( 1 );
    glEnableVertexAttribArray ( 0 );
    glUniformMatrix4fv ( userData->mvpLoc, 1, GL_FALSE, ( GLfloat * ) &userData->mvpMatrix.m[0][0]);
    draw(userData,0,64);
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   glDrawElements ( GL_TRIANGLES,12, GL_UNSIGNED_SHORT, &indices[0] );
    draw(userData,32,64);
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    glDrawElements ( GL_TRIANGLES,12, GL_UNSIGNED_SHORT, &indices[12] );
    draw(userData,64,32);
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_MIRRORED_REPEAT );
    glDrawElements ( GL_TRIANGLES,12, GL_UNSIGNED_SHORT, &indices[24] );
}
void Shutdown ( ESContext *esContext )
{
    UserData *userData = esContext->userData;
    glDeleteProgram ( userData->programObject );
}
int esMain ( ESContext *esContext )
{
    esContext->userData = malloc ( sizeof ( UserData ) );
    
    esCreateWindow ( esContext, "a123", 640, 480, ES_WINDOW_RGB | ES_WINDOW_DEPTH );
    if ( !Init ( esContext ) )
    {
        return GL_FALSE;
    }
    glEnable ( GL_CULL_FACE );
    esRegisterShutdownFunc ( esContext, Shutdown );
    esRegisterUpdateFunc ( esContext, Update );
    esRegisterDrawFunc ( esContext, Draw );
    return GL_TRUE;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenGL ES 中的投影矩阵可以使用透视投影和正交投影两种方式进行设置。 透视投影是一种模拟人眼观察物体的方式,近处的物体看起来比远处的物体大,而且在远处的物体看起来比在近处的物体更小。在 OpenGL ES 中,透视投影的矩阵通常由 glFrustum() 或者 gluPerspective() 函数生成。这些函数所需要的参数包括视角、画面宽高比、近裁剪面和远裁剪面的距离。 正交投影则是一种无论远近都能保持物体大小一致的投影方式。在 OpenGL ES 中,正交投影的矩阵通常由 glOrtho() 函数生成。这个函数需要指定可视空间的左、右、上、下、近、远六个面。 下面是一个简单的 OpenGL ES 代码示例,使用透视投影和正交投影来绘制一个立方体: ```c++ // 使用透视投影绘制立方体 glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1, 1, -1, 1, 1, 10); // 绘制立方体 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0, 0, -5); glRotatef(angle, 1, 1, 1); glColor3f(1, 0, 0); glutWireCube(2); // 使用正交投影绘制立方体 glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2, 2, -2, 2, 1, 10); // 绘制立方体 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0, 0, -5); glRotatef(angle, 1, 1, 1); glColor3f(0, 0, 1); glutWireCube(2); ``` 其中,glFrustum() 函数生成的透视投影矩阵,可以让离观察者比较近的物体显得比较大,而离观察者比较远的物体显得比较小。glOrtho() 函数生成的正交投影矩阵,则可以让物体无论离观察者多远,都保持一致的大小。 需要注意的是,在使用不同的投影方式时,需要先将当前矩阵模式设置为投影矩阵模式,然后再进行矩阵的设置和变换。最后,需要将矩阵模式设置回模型视图矩阵模式,以便绘制物体。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值