双缓冲区就是两个贞缓冲区来回切换
一个在前面现实渲染好的图片,另一个在后台渲染下一贞,渲染好了再切换。
颜色缓冲区,模版缓冲区和深度缓冲区在程序起的的时候申请内存,大小固定。
15日shader 正常shader没有其他渲染操作
#include <stdio.h>
#include <stdlib.h>
#include "esUtil.h"
typedef struct{
GLuint id;
GLint sampler2D;
GLuint texture;
}UserData;
GLuint LoadTexture(void *context , char *filename){
int width,height;
char *buffer = esLoadTGA( context, filename, &width, &height );
if ( buffer == NULL )
{
esLogMessage ( "Error loading (%s) image.\n", filename );
return 0;
}
GLuint texture;
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
free(buffer);
return texture;
}
//GLuint LoadTexture1 ( void *ioContext, char *fileName )
//{
// int width,
// height;
//
// char *buffer = esLoadTGA ( ioContext, fileName, &width, &height );
// GLuint texId;
//
// if ( buffer == NULL )
// {
// esLogMessage ( "Error loading (%s) image.\n", fileName );
// return 0;
// }
//
// glGenTextures ( 1, &texId );
// glBindTexture ( GL_TEXTURE_2D, texId );
//
// glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer );
// glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
// glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
// glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
//
// free ( buffer );
//
// return texId;
//}
int init(ESContext *esContext){
UserData *userData = esContext->userData;
const char vShaderStr[] =
"#version 300 es \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 = 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"
"uniform sampler2D s_texture; \n"
"out vec4 OutColor; \n"
"void main() \n"
"{ \n"
" OutColor = texture(s_texture , v_texCoord); \n"
"} \n";
userData->id = esLoadProgram ( vShaderStr, fShaderStr );
userData->sampler2D = glGetUniformLocation( userData->id , "s_texture" );
userData->texture = LoadTexture ( esContext->platformData, "basemap.tga" );
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
if( userData->texture == 0 )
return 0;
else
return 1;
}
GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0
0.0f, 0.0f, // TexCoord 0
-0.5f, -0.5f, 0.0f, // Position 1
0.0f, 1.0f, // TexCoord 1
0.5f, -0.5f, 0.0f, // Position 2
1.0f, 1.0f, // TexCoord 2
0.5f, 0.5f, 0.0f, // Position 3
1.0f, 0.0f // TexCoord 3
};
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
void Draw(ESContext *esContext){
UserData *userData = esContext->userData;
glClear( GL_COLOR_BUFFER_BIT );
glBindTexture( GL_TEXTURE_2D, userData->texture );
glUseProgram( userData->id );
glViewport ( 0, 0, esContext->width, esContext->height );
//glVertexAttrib1f(<#GLuint indx#>, <#GLfloat x#>)常属性
//glEnableVertexAttribArray(<#GLuint index#>)启用顶点数组
//glDisableVertexAttribArray(<#GLuint index#>)禁用顶点数组如果索引下还有顶点常量则启用
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vVertices );
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );
glEnableVertexAttribArray( 0 );
glEnableVertexAttribArray( 1 );
glActiveTexture( GL_TEXTURE0 );
glUniform1i( userData->sampler2D, 0 );
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
}
int esMain ( ESContext *esContext )
{
UserData *userData = esContext->userData;
esContext->userData = malloc(sizeof(userData));
esCreateWindow( esContext, "123", 320, 240, ES_WINDOW_RGB | ES_WINDOW_DEPTH );
if(!init(esContext)){
return GL_FALSE;
}
esRegisterDrawFunc( esContext, Draw );
return GL_TRUE;
}