зеркало из https://github.com/AvaloniaUI/angle.git
Port the MipMap2D sample to the new sample framework.
BUG=angle:521 Change-Id: I3b1d3a02214850bf52c1d5c468f5696076065b88 Reviewed-on: https://chromium-review.googlesource.com/188193 Reviewed-by: Shannon Woods <shannonwoods@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Tested-by: Geoff Lang <geofflang@chromium.org>
This commit is contained in:
Родитель
916e769a5a
Коммит
9adef9ba7a
|
@ -72,7 +72,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libGLESv2", "..\src\libGLES
|
|||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mip_map_2d", "..\samples\mip_map_2d.vcxproj", "{A6E86EB3-561F-9FAB-670F-EF23556344BE}"
|
||||
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}
|
||||
{7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81}
|
||||
EndProjectSection
|
||||
|
|
|
@ -37,12 +37,12 @@
|
|||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<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>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
<BufferSecurityCheck>true</BufferSecurityCheck>
|
||||
<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>
|
||||
<MinimalRebuild>false</MinimalRebuild>
|
||||
<Optimization>Disabled</Optimization>
|
||||
|
@ -67,17 +67,17 @@
|
|||
<TargetMachine>MachineX86</TargetMachine>
|
||||
</Link>
|
||||
<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>
|
||||
</ResourceCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<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>
|
||||
<BufferSecurityCheck>true</BufferSecurityCheck>
|
||||
<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>
|
||||
<MinimalRebuild>false</MinimalRebuild>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
|
@ -102,7 +102,7 @@
|
|||
<TargetMachine>MachineX86</TargetMachine>
|
||||
</Link>
|
||||
<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>
|
||||
</ResourceCompile>
|
||||
</ItemDefinitionGroup>
|
||||
|
@ -110,11 +110,11 @@
|
|||
<None Include="..\..\samples\samples.gyp"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\samples\gles2_book\MipMap2D\MipMap2D.c"/>
|
||||
<ClCompile Include="..\..\samples\angle\mip_map_2d\MipMap2D.cpp"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="es_util.vcxproj">
|
||||
<Project>{80E5C3D9-93C2-943E-176F-C9A903E27BC9}</Project>
|
||||
<ProjectReference Include="sample_util.vcxproj">
|
||||
<Project>{297C46B0-9263-A9EA-82B1-EA221E7D7C7F}</Project>
|
||||
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\src\libEGL.vcxproj">
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="gles2_book">
|
||||
<UniqueIdentifier>{01DCD872-C931-E1FA-4F9F-EBFA79587BB6}</UniqueIdentifier>
|
||||
<Filter Include="angle">
|
||||
<UniqueIdentifier>{2039F76D-4C9A-B8A0-444A-4766C6F6104A}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="gles2_book\MipMap2D">
|
||||
<UniqueIdentifier>{FE8C9EA3-68C6-B350-A0FB-AA34D954630D}</UniqueIdentifier>
|
||||
<Filter Include="angle\mip_map_2d">
|
||||
<UniqueIdentifier>{63DD2738-D3D7-E13E-A38C-0177BCB49114}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\samples\samples.gyp"/>
|
||||
<ClCompile Include="..\..\samples\gles2_book\MipMap2D\MipMap2D.c">
|
||||
<Filter>gles2_book\MipMap2D</Filter>
|
||||
<ClCompile Include="..\..\samples\angle\mip_map_2d\MipMap2D.cpp">
|
||||
<Filter>angle\mip_map_2d</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
@ -47,7 +47,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libGLESv2", "..\src\libGLES
|
|||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mip_map_2d", "mip_map_2d.vcxproj", "{A6E86EB3-561F-9FAB-670F-EF23556344BE}"
|
||||
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}
|
||||
{7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81}
|
||||
EndProjectSection
|
||||
|
|
|
@ -0,0 +1,222 @@
|
|||
//
|
||||
// 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 MipMap2D.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 <array>
|
||||
|
||||
class MipMap2DSample : public SampleApplication
|
||||
{
|
||||
public:
|
||||
MipMap2DSample::MipMap2DSample()
|
||||
: SampleApplication("MipMap2D", 1280, 720)
|
||||
{
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
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
|
||||
mTextureID = createMipMappedTexture2D();
|
||||
|
||||
// Check Anisotropy limits
|
||||
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mMaxAnisotropy);
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void destroy()
|
||||
{
|
||||
glDeleteProgram(mProgram);
|
||||
glDeleteTextures(1, &mTextureID);
|
||||
}
|
||||
|
||||
virtual void draw()
|
||||
{
|
||||
const GLfloat vertices[] =
|
||||
{
|
||||
-0.25f, 0.5f, 0.0f, 5.0f, // Position 0
|
||||
0.0f, 0.0f, // TexCoord 0
|
||||
-0.25f, -0.5f, 0.0f, 1.0f, // Position 1
|
||||
0.0f, 1.0f, // TexCoord 1
|
||||
0.25f, -0.5f, 0.0f, 1.0f, // Position 2
|
||||
1.0f, 1.0f, // TexCoord 2
|
||||
0.25f, 0.5f, 0.0f, 5.0f, // Position 3
|
||||
1.0f, 0.0f // TexCoord 3
|
||||
};
|
||||
const 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);
|
||||
// Load the texture coordinate
|
||||
glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), vertices + 4);
|
||||
|
||||
glEnableVertexAttribArray(mPositionLoc);
|
||||
glEnableVertexAttribArray(mTexCoordLoc);
|
||||
|
||||
// Bind the texture
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, mTextureID);
|
||||
|
||||
// Set the sampler texture unit to 0
|
||||
glUniform1i(mSamplerLoc, 0);
|
||||
|
||||
// Draw quad with nearest sampling
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glUniform1f(mOffsetLoc, -0.6f);
|
||||
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
|
||||
|
||||
// Draw quad with trilinear filtering
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glUniform1f(mOffsetLoc, 0.0f);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
|
||||
|
||||
// Draw quad with anisotropic filtering
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, mMaxAnisotropy);
|
||||
glUniform1f(mOffsetLoc, 0.6f);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
|
||||
}
|
||||
|
||||
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 mTextureID;
|
||||
|
||||
float mMaxAnisotropy;
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
MipMap2DSample app;
|
||||
return app.run();
|
||||
}
|
|
@ -1,346 +0,0 @@
|
|||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
// MipMap2D.c
|
||||
//
|
||||
// This is a simple example that demonstrates generating a mipmap chain
|
||||
// and rendering with it
|
||||
//
|
||||
#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;
|
||||
|
||||
|
||||
///
|
||||
// From an RGB8 source image, generate the next level mipmap
|
||||
//
|
||||
GLboolean GenMipMap2D( GLubyte *src, GLubyte **dst, int srcWidth, int srcHeight, int *dstWidth, int *dstHeight )
|
||||
{
|
||||
int x,
|
||||
y;
|
||||
int texelSize = 3;
|
||||
|
||||
*dstWidth = srcWidth / 2;
|
||||
if ( *dstWidth <= 0 )
|
||||
*dstWidth = 1;
|
||||
|
||||
*dstHeight = srcHeight / 2;
|
||||
if ( *dstHeight <= 0 )
|
||||
*dstHeight = 1;
|
||||
|
||||
*dst = malloc ( sizeof(GLubyte) * texelSize * (*dstWidth) * (*dstHeight) );
|
||||
if ( *dst == NULL )
|
||||
return GL_FALSE;
|
||||
|
||||
for ( y = 0; y < *dstHeight; y++ )
|
||||
{
|
||||
for( x = 0; x < *dstWidth; x++ )
|
||||
{
|
||||
int srcIndex[4];
|
||||
float r = 0.0f,
|
||||
g = 0.0f,
|
||||
b = 0.0f;
|
||||
int sample;
|
||||
|
||||
// Compute the offsets for 2x2 grid of pixels in previous
|
||||
// image to perform box filter
|
||||
srcIndex[0] =
|
||||
(((y * 2) * srcWidth) + (x * 2)) * texelSize;
|
||||
srcIndex[1] =
|
||||
(((y * 2) * srcWidth) + (x * 2 + 1)) * texelSize;
|
||||
srcIndex[2] =
|
||||
((((y * 2) + 1) * srcWidth) + (x * 2)) * texelSize;
|
||||
srcIndex[3] =
|
||||
((((y * 2) + 1) * srcWidth) + (x * 2 + 1)) * texelSize;
|
||||
|
||||
// Sum all pixels
|
||||
for ( sample = 0; sample < 4; sample++ )
|
||||
{
|
||||
r += src[srcIndex[sample]];
|
||||
g += src[srcIndex[sample] + 1];
|
||||
b += src[srcIndex[sample] + 2];
|
||||
}
|
||||
|
||||
// Average results
|
||||
r /= 4.0;
|
||||
g /= 4.0;
|
||||
b /= 4.0;
|
||||
|
||||
// Store resulting pixels
|
||||
(*dst)[ ( y * (*dstWidth) + x ) * texelSize ] = (GLubyte)( r );
|
||||
(*dst)[ ( y * (*dstWidth) + x ) * texelSize + 1] = (GLubyte)( g );
|
||||
(*dst)[ ( y * (*dstWidth) + x ) * texelSize + 2] = (GLubyte)( b );
|
||||
}
|
||||
}
|
||||
|
||||
return GL_TRUE;
|
||||
}
|
||||
|
||||
///
|
||||
// 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 CreateMipMappedTexture2D( )
|
||||
{
|
||||
// Texture object handle
|
||||
GLuint textureId;
|
||||
int width = 256,
|
||||
height = 256;
|
||||
int level;
|
||||
GLubyte *pixels;
|
||||
GLubyte *prevImage;
|
||||
GLubyte *newImage = NULL;
|
||||
|
||||
pixels = GenCheckImage( width, height, 8 );
|
||||
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 );
|
||||
|
||||
level = 1;
|
||||
prevImage = &pixels[0];
|
||||
|
||||
while ( width > 1 && height > 1 )
|
||||
{
|
||||
int newWidth,
|
||||
newHeight;
|
||||
|
||||
// Generate the next mipmap level
|
||||
GenMipMap2D( prevImage, &newImage, width, height,
|
||||
&newWidth, &newHeight );
|
||||
|
||||
// Load the mipmap level
|
||||
glTexImage2D( GL_TEXTURE_2D, level, GL_RGB,
|
||||
newWidth, newHeight, 0, GL_RGB,
|
||||
GL_UNSIGNED_BYTE, newImage );
|
||||
|
||||
// Free the previous image
|
||||
free ( prevImage );
|
||||
|
||||
// Set the previous image for the next iteration
|
||||
prevImage = newImage;
|
||||
level++;
|
||||
|
||||
// Half the width and height
|
||||
width = newWidth;
|
||||
height = newHeight;
|
||||
}
|
||||
|
||||
free ( newImage );
|
||||
|
||||
// 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 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 = CreateMipMappedTexture2D ();
|
||||
|
||||
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.5f, 0.5f, 0.0f, 1.5f, // Position 0
|
||||
0.0f, 0.0f, // TexCoord 0
|
||||
-0.5f, -0.5f, 0.0f, 0.75f, // Position 1
|
||||
0.0f, 1.0f, // TexCoord 1
|
||||
0.5f, -0.5f, 0.0f, 0.75f, // Position 2
|
||||
1.0f, 1.0f, // TexCoord 2
|
||||
0.5f, 0.5f, 0.0f, 1.5f, // Position 3
|
||||
1.0f, 0.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 nearest sampling
|
||||
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
||||
glUniform1f ( userData->offsetLoc, -0.6f );
|
||||
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
|
||||
|
||||
// Draw quad with trilinear filtering
|
||||
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
|
||||
glUniform1f ( userData->offsetLoc, 0.6f );
|
||||
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("MipMap 2D"), 320, 240, ES_WINDOW_RGB );
|
||||
|
||||
if ( !Init ( &esContext ) )
|
||||
return 0;
|
||||
|
||||
esRegisterDrawFunc ( &esContext, Draw );
|
||||
|
||||
esMainLoop ( &esContext );
|
||||
|
||||
ShutDown ( &esContext );
|
||||
}
|
|
@ -109,8 +109,8 @@
|
|||
{
|
||||
'target_name': 'mip_map_2d',
|
||||
'type': 'executable',
|
||||
'dependencies': [ 'es_util' ],
|
||||
'sources': [ '<!@(python <(angle_path)/enumerate_files.py gles2_book/MipMap2D -types *.c *.h)' ],
|
||||
'dependencies': [ 'sample_util' ],
|
||||
'sources': [ '<!@(python <(angle_path)/enumerate_files.py angle/mip_map_2d -types *.cpp *.h)' ],
|
||||
},
|
||||
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче