int Shaders::Init(char * fileVertexShader, char * fileFragmentShader) { vertexShader = esLoadShader(GL_VERTEX_SHADER, fileVertexShader); if ( vertexShader == 0 ) return -1; fragmentShader = esLoadShader(GL_FRAGMENT_SHADER, fileFragmentShader); if ( fragmentShader == 0 ) { glDeleteShader( vertexShader ); return -2; } program = esLoadProgram(vertexShader, fragmentShader); //finding location of uniforms / attributes positionAttribute = glGetAttribLocation(program, "a_posL"); colorAttribute = glGetAttribLocation(program, "a_color"); matrixTransform = glGetUniformLocation(program, "u_matT"); uv = glGetAttribLocation(program, "a_uv"); textureUniform = glGetUniformLocation(program, "s_texture"); return 0; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { UserData *userData = esContext->userData; GLbyte *vShaderStr = read_data_from_file("shader.vert", 4096); GLbyte *fShaderStr = read_data_from_file("yuv2.frag", 4096); // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); // Get the sampler location userData->baseMapLoc = glGetUniformLocation ( userData->programObject, "s_baseMap" ); userData->texture_width = glGetUniformLocation(userData->programObject, "texture_width"); // set the textures glGenTextures ( 1, &userData->baseMapTexId ); glBindTexture ( GL_TEXTURE_2D, userData->baseMapTexId ); if ( userData->baseMapTexId == 0 ) return FALSE; glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); return TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { UserData *userData = esContext->userData; GLbyte vShaderStr[] = "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" " v_texCoord = a_texCoord; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_baseMap; \n" "uniform sampler2D s_lightMap; \n" "void main() \n" "{ \n" " vec4 baseColor; \n" " vec4 lightColor; \n" " \n" " baseColor = texture2D( s_baseMap, v_texCoord ); \n" " lightColor = texture2D( s_lightMap, v_texCoord ); \n" " gl_FragColor = baseColor * (lightColor + 0.25); \n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); // Get the sampler location userData->baseMapLoc = glGetUniformLocation ( userData->programObject, "s_baseMap" ); userData->lightMapLoc = glGetUniformLocation ( userData->programObject, "s_lightMap" ); // Load the textures userData->baseMapTexId = LoadTexture ( "basemap.tga" ); userData->lightMapTexId = LoadTexture ( "lightmap.tga" ); if ( userData->baseMapTexId == 0 || userData->lightMapTexId == 0 ) return FALSE; glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); return TRUE; }
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 vec4 a_color; \n" "uniform float u_offset; \n" "out vec4 v_color; \n" "void main() \n" "{ \n" " v_color = a_color; \n" " gl_Position = a_position; \n" " gl_Position.x += u_offset; \n" "}"; const char fShaderStr[] = "#version 300 es \n" "precision mediump float; \n" "in vec4 v_color; \n" "out vec4 o_fragColor; \n" "void main() \n" "{ \n" " o_fragColor = v_color; \n" "}" ; GLuint programObject; // Create the program object programObject = esLoadProgram ( vShaderStr, fShaderStr ); userData->offsetLoc = glGetUniformLocation ( programObject, "u_offset" ); if ( programObject == 0 ) { return GL_FALSE; } // Store the program object userData->programObject = programObject; userData->vboIds[0] = 0; userData->vboIds[1] = 0; glClearColor ( 1.0f, 1.0f, 1.0f, 0.0f ); return GL_TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { esContext->userData = new UserData; UserData *userData = static_cast<UserData *>(esContext->userData); char vShaderStr[] = "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "attribute mat4 Scale;\n" "attribute mat4 Rotation;\n" "attribute mat4 View;\n" "attribute mat4 Projection;\n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " mat4 MVP = Projection * View * a_position * Rotation * Scale;\n" " gl_Position = a_position; \n" " v_texCoord = vec2(a_texCoord.x, 1.0 - a_texCoord.y); \n" "} \n"; char fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); // Get the sampler location userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" ); // Load the texture userData->textureId = CreateSimpleTexture2D (); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glClearColor ( 1.0f, 0.0f, 1.0f, 1.0f ); return GL_TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { esContext->userData = malloc(sizeof(UserData)); UserData *userData = esContext->userData; GLbyte vShaderStr[] = "uniform float u_offset; \n" "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" " gl_Position.x += u_offset;\n" " v_texCoord = a_texCoord; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); // Get the sampler location userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" ); // Get the offset location userData->offsetLoc = glGetUniformLocation( userData->programObject, "u_offset" ); // Load the texture userData->textureId = CreateTexture2D (); glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f ); return GL_TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { UserData *userData = esContext->userData; GLbyte vShaderStr[] = "attribute vec4 a_position; \n" "attribute vec3 a_normal; \n" "varying vec3 v_normal; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" " v_normal = a_normal; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying vec3 v_normal; \n" "uniform samplerCube s_texture; \n" "void main() \n" "{ \n" " gl_FragColor = textureCube( s_texture, v_normal );\n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); userData->normalLoc = glGetAttribLocation ( userData->programObject, "a_normal" ); // Get the sampler locations userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" ); // Load the texture userData->textureId = CreateSimpleTextureCubemap (); // Generate the vertex data userData->numIndices = esGenSphere ( 20, 0.75f, &userData->vertices, &userData->normals, NULL, &userData->indices ); glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); return TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { UserData *userData = esContext->userData; GLbyte vShaderStr[] = "attribute vec4 a_position; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "uniform vec4 u_color; \n" "void main() \n" "{ \n" " gl_FragColor = u_color; \n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); // Get the sampler location userData->colorLoc = glGetUniformLocation ( userData->programObject, "u_color" ); // Set the clear color glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f ); // Set the stencil clear value glClearStencil ( 0x1 ); // Set the depth clear value glClearDepthf( 0.75f ); // Enable the depth and stencil tests glEnable( GL_DEPTH_TEST ); glEnable( GL_STENCIL_TEST ); return TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { UserData *userData = esContext->userData; char vShaderStr[] = "#version 300 es \n" "uniform float u_offset; \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" " gl_Position.x += u_offset; \n" " v_texCoord = a_texCoord; \n" "} \n"; 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"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the sampler location userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" ); // Get the offset location userData->offsetLoc = glGetUniformLocation ( userData->programObject, "u_offset" ); // Load the texture userData->textureId = CreateMipMappedTexture2D (); glClearColor ( 1.0f, 1.0f, 1.0f, 0.0f ); return TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { esContext->userData = malloc(sizeof(UserData)); UserData *userData = esContext->userData; GLbyte vShaderStr[] = "uniform mat4 u_mvpMatrix; \n" "attribute vec4 a_position; \n" "void main() \n" "{ \n" " gl_Position = u_mvpMatrix * a_position; \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"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); // Get the uniform locations userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatrix" ); // Generate the vertex data userData->numIndices = esGenCube( 1.0, &userData->vertices, NULL, NULL, &userData->indices ); // Starting rotation angle for the cube userData->angle = 45.0f; glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f ); return GL_TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { UserData *userData = (UserData *)esContext->userData; const char vShaderStr[] = "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" " v_texCoord = a_texCoord; \n" "} \n"; const char fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( (const char*)vShaderStr, (const char*)fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); // Get the sampler location userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" ); // Load the texture userData->textureId = CreateSimpleTexture2D (); glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); return TRUE; }
/// // Initialize the shader and program object // int htInit ( ESContext *esContext ) { HTUserData *userData = (HTUserData*)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"; GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f }; userData->programObject = esLoadProgram ( (const char*)vShaderStr, (const char*)fShaderStr ); if ( userData->programObject == 0 ) return FALSE; // Bind vPosition to attribute 0 glBindAttribLocation ( userData->programObject, 0, "vPosition" ); glGenBuffers ( 1, &userData->vbo ); glBindBuffer ( GL_ARRAY_BUFFER, userData->vbo ); glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices), NULL, GL_STATIC_DRAW ); glBufferSubData ( GL_ARRAY_BUFFER, 0, sizeof(vVertices), vVertices ); glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); return TRUE; }
/// // Initialize the shader and program object // bool setupGraphics(int screenWidth, int screenHeight) { m_glutScreenWidth = screenWidth; m_glutScreenHeight = screenHeight; glViewport ( 0, 0, screenWidth,screenHeight ); glEnable(GL_DEPTH_TEST); printGLString("Version", GL_VERSION); printGLString("Vendor", GL_VENDOR); printGLString("Renderer", GL_RENDERER); printGLString("Extensions", GL_EXTENSIONS); GLbyte vShaderStr[] = "uniform mat4 modelMatrix;\n" "uniform mat4 viewMatrix;\n" "uniform mat4 projectionMatrix;\n" "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = (projectionMatrix*viewMatrix*modelMatrix)*a_position; \n" " v_texCoord = a_texCoord; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; // for wireframe, use white color // " gl_FragColor = vec4(1.0,1.0,1.0,1.0);\n" // Load the shaders and get a linked program object #ifdef __native_client__ programObject = shader_util::CreateProgramFromVertexAndFragmentShaders((const char*)vShaderStr, (const char*)fShaderStr); #else programObject= esLoadProgram ((const char*)vShaderStr, (const char*)fShaderStr ); #endif // Get the attribute locations positionLoc = glGetAttribLocation ( programObject, "a_position" ); texCoordLoc = glGetAttribLocation ( programObject, "a_texCoord" ); // Get the sampler location samplerLoc = glGetUniformLocation ( programObject, "s_texture" ); modelMatrix = glGetUniformLocation ( programObject, "modelMatrix" ); viewMatrix = glGetUniformLocation ( programObject, "viewMatrix" ); projectionMatrix = glGetUniformLocation ( programObject, "projectionMatrix" ); // Load the texture textureId = 0;//CreateSimpleTexture2D (); glClearColor ( 1.2f, 0.2f, 0.2f, 0.2f ); createWorld(); return true; }
/// // Initialize the shader and program object // int Init () { UserData *userData = &_UserData; char vShaderStr[] = "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" " v_texCoord = a_texCoord; \n" "} \n"; char fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; memset(&_UserData, 0, sizeof(_UserData)); // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); // Get the sampler location userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" ); // Load the texture userData->textureId = CreateSimpleTexture2D (); // Setup the vertex data { GLfloat vVertices[] = { -0.5, 0.5, 0.0, // Position 0 0.0, 1.0, // TexCoord 0 -0.5, -0.5, 0.0, // Position 1 0.0, 0.0, // TexCoord 1 0.5, -0.5, 0.0, // Position 2 1.0, 0.0, // TexCoord 2 0.5, 0.5, 0.0, // Position 3 1.0, 1.0 // TexCoord 3 }; GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; glGenBuffers(1, &userData->vertexObject); glBindBuffer(GL_ARRAY_BUFFER, userData->vertexObject ); glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices), vVertices, GL_STATIC_DRAW ); glGenBuffers(1, &userData->indexObject); glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->indexObject ); glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW ); } glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f ); return GL_TRUE; }
/// // Initialize the shader and program object // static int Init ( ESContext *esContext ) { UserData *userData = esContext->userData; GLbyte vShaderStr[] = "attribute vec2 aTexel; \n" "attribute vec3 aPosition; \n" "attribute vec3 aNormal; \n" //"uniform mat4 uProjectionMatrix; \n" //"uniform mat4 uModelViewMatrix; \n" "uniform mat3 uNormalMatrix; \n" "uniform mat4 uMvpMatrix; \n" "varying vec3 vNormal; \n" "varying vec2 vTexel; \n" "void main() \n" "{ \n" "vNormal = uNormalMatrix * aNormal; \n" //"vNormal = aNormal; \n" "vTexel = aTexel; \n" //"gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aPosition, 1.0); \n" "gl_Position = uMvpMatrix * vec4(aPosition, 1.0); \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying highp vec3 vNormal; \n" "varying highp vec2 vTexel; \n" "uniform highp vec3 uDiffuse; \n" "uniform highp vec3 uSpecular; \n" "uniform sampler2D uTexture; \n" "void main() \n" "{ \n" // Material "highp vec3 ka = vec3(0.05); \n" "highp vec3 kd = uDiffuse; \n" "highp vec3 ks = uSpecular; \n" "highp float alpha = 1.0; \n" //Light "highp vec3 ia = vec3(1.0); \n" "highp vec3 id = vec3(1.0); \n" "highp vec3 is = vec3(1.0); \n" //Vectors "highp vec3 L = normalize(vec3(1.0, 1.0, 1.0)); \n" "highp vec3 N = normalize(vNormal); \n" "highp vec3 V = normalize(vec3(0.0, 0.0, 1.0)); \n" "highp vec3 R = reflect(L, N); \n" // Illumination factors "highp float df = max(0.0, dot(L, N)); \n" "highp float sf = pow(max(0.0, dot(R, V)), alpha); \n" // 3dmodel reflection equation "highp vec3 Ip = ka*ia + kd*id*df + ks*is*sf; \n" //Decal "highp vec4 decal = texture2D(uTexture, vTexel); \n" // Surface "highp vec3 surface; \n" "if(decal.a > 0.0) \n" "surface = decal.rgb; \n" "else \n" "surface = Ip; \n" "gl_FragColor = vec4(surface, 1.0); \n" "} \n"; // Load the shaders and get a linked program object userData->program = esLoadProgram ( (char *)vShaderStr, (char *)fShaderStr ); // Attributes userData->aPosition = glGetAttribLocation(userData->program, "aPosition"); userData->aNormal = glGetAttribLocation(userData->program, "aNormal"); userData->aTexel = glGetAttribLocation(userData->program, "aTexel"); // Uniforms //userData->uProjectionMatrix = glGetUniformLocation(userData->program, "uProjectionMatrix"); //userData->uModelViewMatrix = glGetUniformLocation(userData->program, "uModelViewMatrix"); userData->uMvpMatrix = glGetUniformLocation(userData->program, "uMvpMatrix"); userData->uNormalMatrix = glGetUniformLocation(userData->program, "uNormalMatrix"); userData->uDiffuse = glGetUniformLocation(userData->program, "uDiffuse"); userData->uSpecular = glGetUniformLocation(userData->program, "uSpecular"); userData->uTexture = glGetUniformLocation(userData->program, "uTexture"); userData->angle = 0.0f; // Load the textures switch (myobj) { case CUBE_OBJ: userData->texMapId = LoadTexture("/carmeter/src/bin/testfile/cube_decal.png"); break; case STARSHIP_OBJ: userData->texMapId = LoadTexture("/carmeter/src/bin/testfile/starship_decal.png"); break; case MUSHROOM_OBJ: userData->texMapId = LoadTexture("/carmeter/src/bin/testfile/mushroom.png"); break; default: break; } if ( userData->texMapId == 0 ) return FALSE; glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); return TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { UserData *userData = esContext->userData; int i; GLbyte vShaderStr[] = "uniform float u_time; \n" "uniform vec3 u_centerPosition; \n" "attribute float a_lifetime; \n" "attribute vec3 a_startPosition; \n" "attribute vec3 a_endPosition; \n" "varying float v_lifetime; \n" "void main() \n" "{ \n" " if ( u_time <= a_lifetime ) \n" " { \n" " gl_Position.xyz = a_startPosition + \n" " (u_time * a_endPosition); \n" " gl_Position.xyz += u_centerPosition; \n" " gl_Position.w = 1.0; \n" " } \n" " else \n" " gl_Position = vec4( -1000, -1000, 0, 0 ); \n" " v_lifetime = 1.0 - ( u_time / a_lifetime ); \n" " v_lifetime = clamp ( v_lifetime, 0.0, 1.0 ); \n" " gl_PointSize = ( v_lifetime * v_lifetime ) * 40.0; \n" "}"; GLbyte fShaderStr[] = "precision mediump float; \n" "uniform vec4 u_color; \n" "varying float v_lifetime; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " vec4 texColor; \n" " texColor = texture2D( s_texture, gl_PointCoord ); \n" " gl_FragColor = vec4( u_color ) * texColor; \n" " gl_FragColor.a *= v_lifetime; \n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->lifetimeLoc = glGetAttribLocation ( userData->programObject, "a_lifetime" ); userData->startPositionLoc = glGetAttribLocation ( userData->programObject, "a_startPosition" ); userData->endPositionLoc = glGetAttribLocation ( userData->programObject, "a_endPosition" ); // Get the uniform locations userData->timeLoc = glGetUniformLocation ( userData->programObject, "u_time" ); userData->centerPositionLoc = glGetUniformLocation ( userData->programObject, "u_centerPosition" ); userData->colorLoc = glGetUniformLocation ( userData->programObject, "u_color" ); userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" ); glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); // Fill in particle data array srand ( 0 ); for ( i = 0; i < NUM_PARTICLES; i++ ) { float *particleData = &userData->particleData[i * PARTICLE_SIZE]; // Lifetime of particle (*particleData++) = myrandom(); // End position of particle (*particleData++) = myrandom() * 2 - 1.0f; (*particleData++) = myrandom() * 2 - 1.0f; (*particleData++) = myrandom() * 2 - 1.0f; // Start position of particle (*particleData++) = myrandom() * 0.25 - 0.125f; (*particleData++) = myrandom() * 0.25 - 0.125f; (*particleData++) = myrandom() * 0.25 - 0.125f; } // Initialize time to cause reset on first update userData->time = 1.0f; userData->textureId = LoadTexture ( "smoke.tga" ); if ( userData->textureId <= 0 ) { return FALSE; } return TRUE; }
/// // Initialize the shader and program object // bool setupGraphics(int screenWidth, int screenHeight) { glViewport ( 0, 0, screenWidth,screenHeight ); glEnable(GL_DEPTH_TEST); printGLString("Version", GL_VERSION); printGLString("Vendor", GL_VENDOR); printGLString("Renderer", GL_RENDERER); printGLString("Extensions", GL_EXTENSIONS); GLbyte vShaderStr[] = "uniform mat4 modelMatrix;\n" "uniform mat4 viewMatrix;\n" "uniform mat4 projectionMatrix;\n" "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = (projectionMatrix*viewMatrix*modelMatrix)*a_position; \n" " v_texCoord = a_texCoord; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; // Load the shaders and get a linked program object programObject = esLoadProgram ((const char*)vShaderStr, (const char*)fShaderStr ); // Get the attribute locations positionLoc = glGetAttribLocation ( programObject, "a_position" ); texCoordLoc = glGetAttribLocation ( programObject, "a_texCoord" ); // Get the sampler location samplerLoc = glGetUniformLocation ( programObject, "s_texture" ); modelMatrix = glGetUniformLocation ( programObject, "modelMatrix" ); viewMatrix = glGetUniformLocation ( programObject, "viewMatrix" ); projectionMatrix = glGetUniformLocation ( programObject, "projectionMatrix" ); float aspect; btVector3 extents; if (screenWidth > screenHeight) { aspect = screenWidth / (float)screenHeight; extents.setValue(aspect * 1.0f, 1.0f,0); } else { aspect = screenHeight / (float)screenWidth; extents.setValue(1.0f, aspect*1.f,0); } float m_frustumZNear=1; float m_frustumZFar=1000; btCreateFrustum(-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,projMat); // Load the texture textureId = CreateSimpleTexture2D (); glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); glinitialized=true; return true; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { UserData *userData = esContext->userData; GLbyte vShaderStr[] = "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" " v_texCoord = a_texCoord; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_baseMap; \n" "uniform sampler2D s_lightMap; \n" "void main() \n" "{ \n" " vec4 baseColor; \n" " vec4 lightColor; \n" " \n" " baseColor = texture2D( s_baseMap, v_texCoord ); \n" " lightColor = texture2D( s_lightMap, v_texCoord ); \n" " gl_FragColor = baseColor * (lightColor + 0.25); \n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); // Get the sampler location userData->baseMapLoc = glGetUniformLocation ( userData->programObject, "s_baseMap" ); userData->lightMapLoc = glGetUniformLocation ( userData->programObject, "s_lightMap" ); // Load the textures userData->baseMapTexId = LoadTexture ( "basemap.tga" ); userData->lightMapTexId = LoadTexture ( "lightmap.tga" ); if ( userData->baseMapTexId == 0 || userData->lightMapTexId == 0 ) return FALSE; GLfloat vVertices[] = { -0.5, 0.5, 0.0, // Position 0 0.0, 0.0, // TexCoord 0 -0.5, -0.5, 0.0, // Position 1 0.0, 1.0, // TexCoord 1 0.5, -0.5, 0.0, // Position 2 1.0, 1.0, // TexCoord 2 0.5, 0.5, 0.0, // Position 3 1.0, 0.0 // TexCoord 3 }; GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; glGenBuffers(1, &userData->vertexObject); glBindBuffer ( GL_ARRAY_BUFFER, userData->vertexObject ); glBufferData ( GL_ARRAY_BUFFER, 5 * 4 * 4, vVertices, GL_STATIC_DRAW ); glGenBuffers(1, &userData->indexObject); glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->indexObject ); glBufferData ( GL_ELEMENT_ARRAY_BUFFER, 6 * 2, indices, GL_STATIC_DRAW ); glClearColor ( 0.0, 0.0, 0.0, 1.0 ); return TRUE; }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { Particle particleData[ NUM_PARTICLES ]; UserData *userData = ( UserData * ) esContext->userData; int i; char vShaderStr[] = "#version 300 es \n" "#define ATTRIBUTE_POSITION 0 \n" "#define ATTRIBUTE_VELOCITY 1 \n" "#define ATTRIBUTE_SIZE 2 \n" "#define ATTRIBUTE_CURTIME 3 \n" "#define ATTRIBUTE_LIFETIME 4 \n" " \n" "layout(location = ATTRIBUTE_POSITION) in vec2 a_position; \n" "layout(location = ATTRIBUTE_VELOCITY) in vec2 a_velocity; \n" "layout(location = ATTRIBUTE_SIZE) in float a_size; \n" "layout(location = ATTRIBUTE_CURTIME) in float a_curtime; \n" "layout(location = ATTRIBUTE_LIFETIME) in float a_lifetime; \n" " \n" "uniform float u_time; \n" "uniform vec2 u_acceleration; \n" " \n" "void main() \n" "{ \n" " float deltaTime = u_time - a_curtime; \n" " if ( deltaTime <= a_lifetime ) \n" " { \n" " vec2 velocity = a_velocity + deltaTime * u_acceleration; \n" " vec2 position = a_position + deltaTime * velocity; \n" " gl_Position = vec4( position, 0.0, 1.0 ); \n" " gl_PointSize = a_size * ( 1.0 - deltaTime / a_lifetime ); \n" " } \n" " else \n" " { \n" " gl_Position = vec4( -1000, -1000, 0, 0 ); \n" " gl_PointSize = 0.0; \n" " } \n" "}"; char fShaderStr[] = "#version 300 es \n" "precision mediump float; \n" "layout(location = 0) out vec4 fragColor; \n" "uniform vec4 u_color; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " vec4 texColor; \n" " texColor = texture( s_texture, gl_PointCoord ); \n" " fragColor = texColor * u_color; \n" "} \n"; InitEmitParticles ( esContext ); // Load the shaders and get a linked program object userData->drawProgramObject = esLoadProgram ( vShaderStr, fShaderStr ); // Get the uniform locations userData->drawTimeLoc = glGetUniformLocation ( userData->drawProgramObject, "u_time" ); userData->drawColorLoc = glGetUniformLocation ( userData->drawProgramObject, "u_color" ); userData->drawAccelerationLoc = glGetUniformLocation ( userData->drawProgramObject, "u_acceleration" ); userData->samplerLoc = glGetUniformLocation ( userData->drawProgramObject, "s_texture" ); userData->time = 0.0f; userData->curSrcIndex = 0; glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); userData->textureId = LoadTexture ( esContext->platformData, "smoke.tga" ); if ( userData->textureId <= 0 ) { return FALSE; } // Create a 3D noise texture for random values userData->noiseTextureId = Create3DNoiseTexture ( 128, 50.0 ); // Initialize particle data for ( i = 0; i < NUM_PARTICLES; i++ ) { Particle *particle = &particleData[i]; particle->position[0] = 0.0f; particle->position[1] = 0.0f; particle->velocity[0] = 0.0f; particle->velocity[1] = 0.0f; particle->size = 0.0f; particle->curtime = 0.0f; particle->lifetime = 0.0f; } // Create the particle VBOs glGenBuffers ( 2, &userData->particleVBOs[0] ); for ( i = 0; i < 2; i++ ) { glBindBuffer ( GL_ARRAY_BUFFER, userData->particleVBOs[i] ); glBufferData ( GL_ARRAY_BUFFER, sizeof ( Particle ) * NUM_PARTICLES, particleData, GL_DYNAMIC_COPY ); } return TRUE; }
void InitEmitParticles ( ESContext *esContext ) { UserData *userData = esContext->userData; char vShaderStr[] = "#version 300 es \n" "#define NUM_PARTICLES 200 \n" "#define ATTRIBUTE_POSITION 0 \n" "#define ATTRIBUTE_VELOCITY 1 \n" "#define ATTRIBUTE_SIZE 2 \n" "#define ATTRIBUTE_CURTIME 3 \n" "#define ATTRIBUTE_LIFETIME 4 \n" "uniform float u_time; \n" "uniform float u_emissionRate; \n" "uniform mediump sampler3D s_noiseTex; \n" " \n" "layout(location = ATTRIBUTE_POSITION) in vec2 a_position; \n" "layout(location = ATTRIBUTE_VELOCITY) in vec2 a_velocity; \n" "layout(location = ATTRIBUTE_SIZE) in float a_size; \n" "layout(location = ATTRIBUTE_CURTIME) in float a_curtime; \n" "layout(location = ATTRIBUTE_LIFETIME) in float a_lifetime; \n" " \n" "out vec2 v_position; \n" "out vec2 v_velocity; \n" "out float v_size; \n" "out float v_curtime; \n" "out float v_lifetime; \n" " \n" "float randomValue( inout float seed ) \n" "{ \n" " float vertexId = float( gl_VertexID ) / float( NUM_PARTICLES ); \n" " vec3 texCoord = vec3( u_time, vertexId, seed ); \n" " seed += 0.1; \n" " return texture( s_noiseTex, texCoord ).r; \n" "} \n" "void main() \n" "{ \n" " float seed = u_time; \n" " float lifetime = a_curtime - u_time; \n" " if( lifetime <= 0.0 && randomValue(seed) < u_emissionRate ) \n" " { \n" " v_position = vec2( 0.0, -1.0 ); \n" " v_velocity = vec2( randomValue(seed) * 2.0 - 1.00, \n" " randomValue(seed) * 1.4 + 1.0 ); \n" " v_size = randomValue(seed) * 20.0 + 60.0; \n" " v_curtime = u_time; \n" " v_lifetime = 2.0; \n" " } \n" " else \n" " { \n" " v_position = a_position; \n" " v_velocity = a_velocity; \n" " v_size = a_size; \n" " v_curtime = a_curtime; \n" " v_lifetime = a_lifetime; \n" " } \n" " gl_Position = vec4( v_position, 0.0, 1.0 ); \n" "} \n"; char fShaderStr[] = "#version 300 es \n" "precision mediump float; \n" "layout(location = 0) out vec4 fragColor; \n" "void main() \n" "{ \n" " fragColor = vec4(1.0); \n" "} \n"; userData->emitProgramObject = esLoadProgram ( vShaderStr, fShaderStr ); { const char *feedbackVaryings[5] = { "v_position", "v_velocity", "v_size", "v_curtime", "v_lifetime" }; // Set the vertex shader outputs as transform feedback varyings glTransformFeedbackVaryings ( userData->emitProgramObject, 5, feedbackVaryings, GL_INTERLEAVED_ATTRIBS ); // Link program must occur after calling glTransformFeedbackVaryings glLinkProgram ( userData->emitProgramObject ); // Get the uniform locations - this also needs to happen after glLinkProgram is called again so // that the uniforms that output to varyings are active userData->emitTimeLoc = glGetUniformLocation ( userData->emitProgramObject, "u_time" ); userData->emitEmissionRateLoc = glGetUniformLocation ( userData->emitProgramObject, "u_emissionRate" ); userData->emitNoiseSamplerLoc = glGetUniformLocation ( userData->emitProgramObject, "s_noiseTex" ); } }
void GL_Init() { assert( !SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 ) ); assert( !SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 ) ); assert( !SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 ) ); assert( !SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 ) ); //assert( !SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 0 ) ); //assert( !SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0 ) ); assert( !SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ) ); assert( !SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 ) ); assert( !SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 ) ); surface = SDL_SetVideoMode( 320, 480, 32, SDL_OPENGL); if(surface == NULL) { //systemMessage(0, "Failed to set video mode"); SDL_Quit(); exit(-1); } // setup 2D gl environment checkError(); checkError(); // Black background glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); checkError(); // Remove unnecessary operations.. glDepthFunc(GL_ALWAYS); checkError(); glDisable(GL_DEPTH_TEST); checkError(); glDisable(GL_STENCIL_TEST); checkError(); glDisable(GL_CULL_FACE); checkError(); //Enable alpha blending glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); GLbyte vShaderStr[] = "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" "varying vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" " v_texCoord = a_texCoord; \n" "} \n"; GLbyte fShaderStr[] = "precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_texture; \n" "void main() \n" "{ \n" " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; // Load the shaders and get a linked program object programObject = esLoadProgram ( ( char *)vShaderStr, (char *)fShaderStr ); checkError(); // Get the attribute locations positionLoc = glGetAttribLocation ( programObject, "a_position" ); checkError(); texCoordLoc = glGetAttribLocation ( programObject, "a_texCoord" ); checkError(); // Get the sampler location samplerLoc = glGetUniformLocation ( programObject, "s_texture" ); checkError(); }
/// // Initialize the shader and program object // int Init ( ESContext *esContext ) { GLint infoLen = 0; UserData *userData = (UserData*)esContext->userData; // // オブジェクト関係 // userData->texWidth = 8; userData->texHeight = 8; // テクスチャデータの準備 //float f=1.0f; int f=1; int i, k; //float *tmpdata[2]; int *tmpdata[2]; for(i=0; i<2; i++) { tmpdata[i] = new int[8*8*4]; //tmpdata[i] = new float[8*8*4]; for(k=8; k<8*8*4; k++) { tmpdata[i][k] = f * k; } //f /= 1000.0f; } // テクスチャ生成+データセット // 新しい 3 つのテクスチャを作成 glGenTextures(3, userData->_iTexture); for(i=0; i<3; i++) { glBindTexture(GL_TEXTURE_2D, userData->_iTexture[i]); // (set texture parameters here) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //create the texture if(i!=2) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, userData->texWidth, userData->texHeight, 0, GL_RGBA, GL_FLOAT, tmpdata[i]); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, userData->texWidth, userData->texHeight, //0, GL_RGBA, GL_UNSIGNED_BYTE, tmpdata[i]); else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, userData->texWidth, userData->texHeight, 0, GL_RGBA, GL_FLOAT, NULL); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, userData->texWidth, userData->texHeight, //0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); } // glGenFramebuffers(1, &userData->framebuffer); // // シェーダ関係 // // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, edgeFragSource ); // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); //userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); // // attributeのID?取得 // userData->_texUnit0 = glGetUniformLocation(userData->programObject, "texUnit0"); userData->_texUnit1 = glGetUniformLocation(userData->programObject, "texUnit1"); return TRUE; }