Commit e9b02245 by Geoff Lang

Port the TextureWrap sample to the new sample framework.

Refactor the generation of checkerboard textures since they're used by this sample and MipMap2D. BUG=angle:521 Change-Id: I1ba8bc4927a7944eb8efad0c8aa71eba4619e457 Reviewed-on: https://chromium-review.googlesource.com/188729Reviewed-by: 's avatarShannon Woods <shannonwoods@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent d3a4092d
...@@ -152,7 +152,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stencil_operations", "..\sa ...@@ -152,7 +152,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stencil_operations", "..\sa
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texture_wrap", "..\samples\texture_wrap.vcxproj", "{46160987-0221-9E14-3B88-80F9FCFCFFBF}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texture_wrap", "..\samples\texture_wrap.vcxproj", "{46160987-0221-9E14-3B88-80F9FCFCFFBF}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
{80E5C3D9-93C2-943E-176F-C9A903E27BC9} = {80E5C3D9-93C2-943E-176F-C9A903E27BC9} {297C46B0-9263-A9EA-82B1-EA221E7D7C7F} = {297C46B0-9263-A9EA-82B1-EA221E7D7C7F}
{FBAEE4F6-562A-588F-01F9-72DCABB3B061} = {FBAEE4F6-562A-588F-01F9-72DCABB3B061} {FBAEE4F6-562A-588F-01F9-72DCABB3B061} = {FBAEE4F6-562A-588F-01F9-72DCABB3B061}
{7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81}
EndProjectSection EndProjectSection
......
...@@ -125,7 +125,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stencil_operations", "stenc ...@@ -125,7 +125,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stencil_operations", "stenc
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texture_wrap", "texture_wrap.vcxproj", "{46160987-0221-9E14-3B88-80F9FCFCFFBF}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texture_wrap", "texture_wrap.vcxproj", "{46160987-0221-9E14-3B88-80F9FCFCFFBF}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
{80E5C3D9-93C2-943E-176F-C9A903E27BC9} = {80E5C3D9-93C2-943E-176F-C9A903E27BC9} {297C46B0-9263-A9EA-82B1-EA221E7D7C7F} = {297C46B0-9263-A9EA-82B1-EA221E7D7C7F}
{FBAEE4F6-562A-588F-01F9-72DCABB3B061} = {FBAEE4F6-562A-588F-01F9-72DCABB3B061} {FBAEE4F6-562A-588F-01F9-72DCABB3B061} = {FBAEE4F6-562A-588F-01F9-72DCABB3B061}
{7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81}
EndProjectSection EndProjectSection
......
...@@ -37,12 +37,12 @@ ...@@ -37,12 +37,12 @@
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
<AdditionalIncludeDirectories>..\..\samples\gles2_book\Common;..\..\include;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\..\include;..\..\samples\angle\sample_util;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/MP %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/MP %(AdditionalOptions)</AdditionalOptions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<BufferSecurityCheck>true</BufferSecurityCheck> <BufferSecurityCheck>true</BufferSecurityCheck>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<DisableSpecificWarnings>4100;4127;4189;4239;4244;4245;4512;4702;4530;4718;%(DisableSpecificWarnings)</DisableSpecificWarnings> <DisableSpecificWarnings>4201;4100;4127;4189;4239;4244;4245;4512;4702;4530;4718;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<MinimalRebuild>false</MinimalRebuild> <MinimalRebuild>false</MinimalRebuild>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
...@@ -67,17 +67,17 @@ ...@@ -67,17 +67,17 @@
<TargetMachine>MachineX86</TargetMachine> <TargetMachine>MachineX86</TargetMachine>
</Link> </Link>
<ResourceCompile> <ResourceCompile>
<AdditionalIncludeDirectories>..\..\samples\gles2_book\Common;..\..\include;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\..\include;..\..\samples\angle\sample_util;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Culture>0x0409</Culture> <Culture>0x0409</Culture>
</ResourceCompile> </ResourceCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile> <ClCompile>
<AdditionalIncludeDirectories>..\..\samples\gles2_book\Common;..\..\include;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\..\include;..\..\samples\angle\sample_util;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/MP %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/MP %(AdditionalOptions)</AdditionalOptions>
<BufferSecurityCheck>true</BufferSecurityCheck> <BufferSecurityCheck>true</BufferSecurityCheck>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<DisableSpecificWarnings>4100;4127;4189;4239;4244;4245;4512;4702;4530;4718;%(DisableSpecificWarnings)</DisableSpecificWarnings> <DisableSpecificWarnings>4201;4100;4127;4189;4239;4244;4245;4512;4702;4530;4718;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<MinimalRebuild>false</MinimalRebuild> <MinimalRebuild>false</MinimalRebuild>
<Optimization>MaxSpeed</Optimization> <Optimization>MaxSpeed</Optimization>
...@@ -102,7 +102,7 @@ ...@@ -102,7 +102,7 @@
<TargetMachine>MachineX86</TargetMachine> <TargetMachine>MachineX86</TargetMachine>
</Link> </Link>
<ResourceCompile> <ResourceCompile>
<AdditionalIncludeDirectories>..\..\samples\gles2_book\Common;..\..\include;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\..\include;..\..\samples\angle\sample_util;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Culture>0x0409</Culture> <Culture>0x0409</Culture>
</ResourceCompile> </ResourceCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
...@@ -110,11 +110,11 @@ ...@@ -110,11 +110,11 @@
<None Include="..\..\samples\samples.gyp"/> <None Include="..\..\samples\samples.gyp"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\samples\gles2_book\TextureWrap\TextureWrap.c"/> <ClCompile Include="..\..\samples\angle\texture_wrap\TextureWrap.cpp"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="es_util.vcxproj"> <ProjectReference Include="sample_util.vcxproj">
<Project>{80E5C3D9-93C2-943E-176F-C9A903E27BC9}</Project> <Project>{297C46B0-9263-A9EA-82B1-EA221E7D7C7F}</Project>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly> <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\src\libEGL.vcxproj"> <ProjectReference Include="..\src\libEGL.vcxproj">
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup> <ItemGroup>
<Filter Include="gles2_book"> <Filter Include="angle">
<UniqueIdentifier>{01DCD872-C931-E1FA-4F9F-EBFA79587BB6}</UniqueIdentifier> <UniqueIdentifier>{2039F76D-4C9A-B8A0-444A-4766C6F6104A}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="gles2_book\TextureWrap"> <Filter Include="angle\texture_wrap">
<UniqueIdentifier>{5F6A5AB7-398C-61AB-1A2A-396E85CA0134}</UniqueIdentifier> <UniqueIdentifier>{14D5AFA5-3272-1D58-63E6-F5DE89477808}</UniqueIdentifier>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\..\samples\samples.gyp"/> <None Include="..\..\samples\samples.gyp"/>
<ClCompile Include="..\..\samples\gles2_book\TextureWrap\TextureWrap.c"> <ClCompile Include="..\..\samples\angle\texture_wrap\TextureWrap.cpp">
<Filter>gles2_book\TextureWrap</Filter> <Filter>angle\texture_wrap</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>
...@@ -15,8 +15,7 @@ ...@@ -15,8 +15,7 @@
#include "SampleApplication.h" #include "SampleApplication.h"
#include "shader_utils.h" #include "shader_utils.h"
#include "texture_utils.h"
#include <array>
class MipMap2DSample : public SampleApplication class MipMap2DSample : public SampleApplication
{ {
...@@ -26,58 +25,6 @@ class MipMap2DSample : public SampleApplication ...@@ -26,58 +25,6 @@ class MipMap2DSample : public SampleApplication
{ {
} }
GLuint createMipMappedTexture2D()
{
// Texture object handle
const size_t width = 256;
const size_t height = 256;
std::array<GLubyte, width * height * 3> pixels;
const size_t checkerSize = 8;
for (GLsizei y = 0; y < height; y++)
{
for (GLsizei x = 0; x < width; x++)
{
GLubyte rColor = 0;
GLubyte bColor = 0;
if ((x / checkerSize) % 2 == 0)
{
rColor = 255 * ((y / checkerSize) % 2);
bColor = 255 * (1 - ((y / checkerSize) % 2));
}
else
{
bColor = 255 * ((y / checkerSize) % 2);
rColor = 255 * (1 - ((y / checkerSize) % 2));
}
pixels[(y * height + x) * 3] = rColor;
pixels[(y * height + x) * 3 + 1] = 0;
pixels[(y * height + x) * 3 + 2] = bColor;
}
}
// Generate a texture object
GLuint texture;
glGenTextures(1, &texture);
// Bind the texture object
glBindTexture(GL_TEXTURE_2D, texture);
// Load mipmap level 0
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels.data());
// Generate mipmaps
glGenerateMipmap(GL_TEXTURE_2D);
// Set the filtering mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return texture;
}
virtual bool initialize() virtual bool initialize()
{ {
const std::string vs = SHADER_SOURCE const std::string vs = SHADER_SOURCE
...@@ -122,7 +69,7 @@ class MipMap2DSample : public SampleApplication ...@@ -122,7 +69,7 @@ class MipMap2DSample : public SampleApplication
mOffsetLoc = glGetUniformLocation(mProgram, "u_offset"); mOffsetLoc = glGetUniformLocation(mProgram, "u_offset");
// Load the texture // Load the texture
mTextureID = createMipMappedTexture2D(); mTextureID = CreateMipMappedTexture2D();
// Check Anisotropy limits // Check Anisotropy limits
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mMaxAnisotropy); glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mMaxAnisotropy);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
// //
#include "texture_utils.h" #include "texture_utils.h"
#include <array>
GLuint CreateSimpleTexture2D() GLuint CreateSimpleTexture2D()
{ {
...@@ -74,3 +75,55 @@ GLuint CreateSimpleTextureCubemap() ...@@ -74,3 +75,55 @@ GLuint CreateSimpleTextureCubemap()
return texture; return texture;
} }
GLuint CreateMipMappedTexture2D()
{
// Texture object handle
const size_t width = 256;
const size_t height = 256;
std::array<GLubyte, width * height * 3> pixels;
const size_t checkerSize = 8;
for (GLsizei y = 0; y < height; y++)
{
for (GLsizei x = 0; x < width; x++)
{
GLubyte rColor = 0;
GLubyte bColor = 0;
if ((x / checkerSize) % 2 == 0)
{
rColor = 255 * ((y / checkerSize) % 2);
bColor = 255 * (1 - ((y / checkerSize) % 2));
}
else
{
bColor = 255 * ((y / checkerSize) % 2);
rColor = 255 * (1 - ((y / checkerSize) % 2));
}
pixels[(y * height + x) * 3] = rColor;
pixels[(y * height + x) * 3 + 1] = 0;
pixels[(y * height + x) * 3 + 2] = bColor;
}
}
// Generate a texture object
GLuint texture;
glGenTextures(1, &texture);
// Bind the texture object
glBindTexture(GL_TEXTURE_2D, texture);
// Load mipmap level 0
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels.data());
// Generate mipmaps
glGenerateMipmap(GL_TEXTURE_2D);
// Set the filtering mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return texture;
}
...@@ -12,4 +12,6 @@ ...@@ -12,4 +12,6 @@
GLuint CreateSimpleTexture2D(); GLuint CreateSimpleTexture2D();
GLuint CreateSimpleTextureCubemap(); GLuint CreateSimpleTextureCubemap();
GLuint CreateMipMappedTexture2D();
#endif // SAMPLE_UTIL_TEXTURE_UTILS_H #endif // SAMPLE_UTIL_TEXTURE_UTILS_H
//
// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Based on TextureWrap.c from
// Book: OpenGL(R) ES 2.0 Programming Guide
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
// ISBN-10: 0321502795
// ISBN-13: 9780321502797
// Publisher: Addison-Wesley Professional
// URLs: http://safari.informit.com/9780321563835
// http://www.opengles-book.com
#include "SampleApplication.h"
#include "shader_utils.h"
#include "texture_utils.h"
class TextureWrapSample : public SampleApplication
{
public:
TextureWrapSample::TextureWrapSample()
: SampleApplication("TextureWrap", 1280, 720)
{
}
virtual bool initialize()
{
const std::string vs = SHADER_SOURCE
(
uniform float u_offset;
attribute vec4 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
void main()
{
gl_Position = a_position;
gl_Position.x += u_offset;
v_texCoord = a_texCoord;
}
);
const std::string fs = SHADER_SOURCE
(
precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main()
{
gl_FragColor = texture2D(s_texture, v_texCoord);
}
);
mProgram = CompileProgram(vs, fs);
if (!mProgram)
{
return false;
}
// Get the attribute locations
mPositionLoc = glGetAttribLocation(mProgram, "a_position");
mTexCoordLoc = glGetAttribLocation(mProgram, "a_texCoord");
// Get the sampler location
mSamplerLoc = glGetUniformLocation(mProgram, "s_texture");
// Get the offset location
mOffsetLoc = glGetUniformLocation(mProgram, "u_offset");
// Load the texture
mTexture = CreateMipMappedTexture2D();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
return true;
}
virtual void destroy()
{
glDeleteProgram(mProgram);
}
virtual void draw()
{
GLfloat vertices[] =
{
-0.3f, 0.3f, 0.0f, 1.0f, // Position 0
-1.0f, -1.0f, // TexCoord 0
-0.3f, -0.3f, 0.0f, 1.0f, // Position 1
-1.0f, 2.0f, // TexCoord 1
0.3f, -0.3f, 0.0f, 1.0f, // Position 2
2.0f, 2.0f, // TexCoord 2
0.3f, 0.3f, 0.0f, 1.0f, // Position 3
2.0f, -1.0f // TexCoord 3
};
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
// Set the viewport
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(mProgram);
// Load the vertex position
glVertexAttribPointer(mPositionLoc, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), vertices);
glEnableVertexAttribArray(mPositionLoc);
// Load the texture coordinate
glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), vertices + 4);
glEnableVertexAttribArray(mTexCoordLoc);
// Set the sampler texture unit to 0
glUniform1i(mSamplerLoc, 0);
// Draw quad with repeat wrap mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glUniform1f(mOffsetLoc, -0.7f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
// Draw quad with clamp to edge wrap mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1f(mOffsetLoc, 0.0f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
// Draw quad with mirrored repeat
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glUniform1f(mOffsetLoc, 0.7f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}
private:
// Handle to a program object
GLuint mProgram;
// Attribute locations
GLint mPositionLoc;
GLint mTexCoordLoc;
// Sampler location
GLint mSamplerLoc;
// Offset location
GLint mOffsetLoc;
// Texture handle
GLuint mTexture;
};
int main(int argc, char **argv)
{
TextureWrapSample app;
return app.run();
}
//
// Book: OpenGL(R) ES 2.0 Programming Guide
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
// ISBN-10: 0321502795
// ISBN-13: 9780321502797
// Publisher: Addison-Wesley Professional
// URLs: http://safari.informit.com/9780321563835
// http://www.opengles-book.com
//
// TextureWrap.c
//
// This is an example that demonstrates the three texture
// wrap modes available on 2D textures
//
#include <stdlib.h>
#include "esUtil.h"
typedef struct
{
// Handle to a program object
GLuint programObject;
// Attribute locations
GLint positionLoc;
GLint texCoordLoc;
// Sampler location
GLint samplerLoc;
// Offset location
GLint offsetLoc;
// Texture handle
GLuint textureId;
} UserData;
///
// Generate an RGB8 checkerboard image
//
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 * height + x) * 3] = rColor;
pixels[(y * height + x) * 3 + 1] = 0;
pixels[(y * height + x) * 3 + 2] = bColor;
}
return pixels;
}
///
// Create a mipmapped 2D texture image
//
GLuint CreateTexture2D( )
{
// Texture object handle
GLuint textureId;
int width = 256,
height = 256;
GLubyte *pixels;
pixels = GenCheckImage( width, height, 64 );
if ( pixels == NULL )
return 0;
// Generate a texture object
glGenTextures ( 1, &textureId );
// Bind the texture object
glBindTexture ( GL_TEXTURE_2D, textureId );
// Load mipmap level 0
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, pixels );
// Set the filtering mode
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
return textureId;
}
///
// Initialize the shader and program object
//
int Init ( ESContext *esContext )
{
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, 0.0f );
return TRUE;
}
///
// Draw a triangle using the shader pair created in Init()
//
void Draw ( ESContext *esContext )
{
UserData *userData = esContext->userData;
GLfloat vVertices[] = { -0.3f, 0.3f, 0.0f, 1.0f, // Position 0
-1.0f, -1.0f, // TexCoord 0
-0.3f, -0.3f, 0.0f, 1.0f, // Position 1
-1.0f, 2.0f, // TexCoord 1
0.3f, -0.3f, 0.0f, 1.0f, // Position 2
2.0f, 2.0f, // TexCoord 2
0.3f, 0.3f, 0.0f, 1.0f, // Position 3
2.0f, -1.0f // TexCoord 3
};
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
// 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 position
glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT,
GL_FALSE, 6 * sizeof(GLfloat), vVertices );
// Load the texture coordinate
glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] );
glEnableVertexAttribArray ( userData->positionLoc );
glEnableVertexAttribArray ( userData->texCoordLoc );
// Bind the texture
glActiveTexture ( GL_TEXTURE0 );
glBindTexture ( GL_TEXTURE_2D, userData->textureId );
// Set the sampler texture unit to 0
glUniform1i ( userData->samplerLoc, 0 );
// Draw quad with repeat wrap mode
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glUniform1f ( userData->offsetLoc, -0.7f );
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
// Draw quad with clamp to edge wrap mode
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glUniform1f ( userData->offsetLoc, 0.0f );
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
// Draw quad with mirrored repeat
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
glUniform1f ( userData->offsetLoc, 0.7f );
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
}
///
// Cleanup
//
void ShutDown ( ESContext *esContext )
{
UserData *userData = esContext->userData;
// Delete texture object
glDeleteTextures ( 1, &userData->textureId );
// Delete program object
glDeleteProgram ( userData->programObject );
}
int main ( int argc, char *argv[] )
{
ESContext esContext;
UserData userData;
esInitContext ( &esContext );
esContext.userData = &userData;
esCreateWindow ( &esContext, TEXT("Texture Wrap"), 640, 480, ES_WINDOW_RGB );
if ( !Init ( &esContext ) )
return 0;
esRegisterDrawFunc ( &esContext, Draw );
esMainLoop ( &esContext );
ShutDown ( &esContext );
}
...@@ -193,8 +193,8 @@ ...@@ -193,8 +193,8 @@
{ {
'target_name': 'texture_wrap', 'target_name': 'texture_wrap',
'type': 'executable', 'type': 'executable',
'dependencies': [ 'es_util' ], 'dependencies': [ 'sample_util' ],
'sources': [ '<!@(python <(angle_path)/enumerate_files.py gles2_book/TextureWrap -types *.c *.h)' ], 'sources': [ '<!@(python <(angle_path)/enumerate_files.py angle/texture_wrap -types *.cpp *.h)' ],
}, },
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment