主函数是
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;
}