Commit f6139359 by Geoff Lang

Port the Simple_VertexShader sample to the new sample framework.

Add a Matrix type and cube gemetry generation helper functions. BUG=angle:521 Change-Id: Ia50be1790d6f440499eea052f427762637f3c876 Reviewed-on: https://chromium-review.googlesource.com/188727Reviewed-by: 's avatarShannon Woods <shannonwoods@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent 705073f7
......@@ -138,7 +138,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "simple_texture_cubemap", ".
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "simple_vertex_shader", "..\samples\simple_vertex_shader.vcxproj", "{459E5678-892C-4EF9-6ED8-0D9E5272B4FF}"
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
......
......@@ -111,10 +111,11 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\samples\angle\sample_util\Window.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\path_utils.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\Vector.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\keyboard.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\shader_utils.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\path_utils.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\Matrix.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\Timer.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\mouse.h"/>
<ClInclude Include="..\..\samples\angle\sample_util\geometry_utils.h"/>
......@@ -135,6 +136,7 @@
<ClCompile Include="..\..\samples\angle\sample_util\geometry_utils.cpp"/>
<ClCompile Include="..\..\samples\angle\sample_util\tga_utils.cpp"/>
<ClCompile Include="..\..\samples\angle\sample_util\random_utils.cpp"/>
<ClCompile Include="..\..\samples\angle\sample_util\Matrix.cpp"/>
<ClCompile Include="..\..\samples\angle\sample_util\win32\Win32Timer.cpp"/>
<ClCompile Include="..\..\samples\angle\sample_util\win32\Win32Window.cpp"/>
<ClCompile Include="..\..\samples\angle\sample_util\win32\Win32_path_utils.cpp"/>
......
......@@ -22,27 +22,30 @@
<ClCompile Include="..\..\samples\angle\sample_util\texture_utils.cpp">
<Filter>angle\sample_util</Filter>
</ClCompile>
<ClInclude Include="..\..\samples\angle\sample_util\Vector.h">
<ClInclude Include="..\..\samples\angle\sample_util\path_utils.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
<ClCompile Include="..\..\samples\angle\sample_util\Vector.cpp">
<Filter>angle\sample_util</Filter>
</ClCompile>
<ClInclude Include="..\..\samples\angle\sample_util\Vector.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
<ClInclude Include="..\..\samples\angle\sample_util\keyboard.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
<ClInclude Include="..\..\samples\angle\sample_util\shader_utils.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
<ClInclude Include="..\..\samples\angle\sample_util\path_utils.h">
<ClCompile Include="..\..\samples\angle\sample_util\shader_utils.cpp">
<Filter>angle\sample_util</Filter>
</ClCompile>
<ClInclude Include="..\..\samples\angle\sample_util\Matrix.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
<ClInclude Include="..\..\samples\angle\sample_util\Timer.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
<ClCompile Include="..\..\samples\angle\sample_util\shader_utils.cpp">
<Filter>angle\sample_util</Filter>
</ClCompile>
<ClInclude Include="..\..\samples\angle\sample_util\mouse.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
......@@ -73,6 +76,9 @@
<ClInclude Include="..\..\samples\angle\sample_util\SampleApplication.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
<ClCompile Include="..\..\samples\angle\sample_util\Matrix.cpp">
<Filter>angle\sample_util</Filter>
</ClCompile>
<ClInclude Include="..\..\samples\angle\sample_util\texture_utils.h">
<Filter>angle\sample_util</Filter>
</ClInclude>
......
......@@ -111,7 +111,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "simple_texture_cubemap", "s
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "simple_vertex_shader", "simple_vertex_shader.vcxproj", "{459E5678-892C-4EF9-6ED8-0D9E5272B4FF}"
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\Simple_VertexShader\Simple_VertexShader.c"/>
<ClCompile Include="..\..\samples\angle\simple_vertex_shader\SimpleVertexShader.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">
......
<?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\Simple_VertexShader">
<UniqueIdentifier>{13D4600F-2070-2CFD-75DC-23AE9927F5C9}</UniqueIdentifier>
<Filter Include="angle\simple_vertex_shader">
<UniqueIdentifier>{B50155D0-892A-4C65-4754-9C5D58289F2E}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="..\..\samples\samples.gyp"/>
<ClCompile Include="..\..\samples\gles2_book\Simple_VertexShader\Simple_VertexShader.c">
<Filter>gles2_book\Simple_VertexShader</Filter>
<ClCompile Include="..\..\samples\angle\simple_vertex_shader\SimpleVertexShader.cpp">
<Filter>angle\simple_vertex_shader</Filter>
</ClCompile>
</ItemGroup>
</Project>
//
// 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.
//
#include "Matrix.h"
#define _USE_MATH_DEFINES
#include <math.h>
Matrix4::Matrix4()
{
data[ 0] = 1.0f; data[ 4] = 0.0f; data[ 8] = 0.0f; data[12] = 0.0f;
data[ 1] = 0.0f; data[ 5] = 1.0f; data[ 9] = 0.0f; data[13] = 0.0f;
data[ 2] = 0.0f; data[ 6] = 0.0f; data[10] = 1.0f; data[14] = 0.0f;
data[ 3] = 0.0f; data[ 7] = 0.0f; data[11] = 0.0f; data[15] = 1.0f;
}
Matrix4::Matrix4(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33)
{
data[ 0] = m00; data[ 4] = m01; data[ 8] = m02; data[12] = m03;
data[ 1] = m10; data[ 5] = m11; data[ 9] = m12; data[13] = m13;
data[ 2] = m20; data[ 6] = m21; data[10] = m22; data[14] = m23;
data[ 3] = m30; data[ 7] = m31; data[11] = m32; data[15] = m33;
}
Matrix4 Matrix4::identity()
{
return Matrix4(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::rotate(float angle, const Vector3 &p)
{
Vector3 u = Vector3::normalize(p);
float theta = angle * (M_PI / 180.0f);
float cos_t = cosf(theta);
float sin_t = sinf(theta);
return Matrix4( cos_t + (u.x * u.x * (1.0f - cos_t)), (u.x * u.y * (1.0f - cos_t)) - (u.z * sin_t), (u.x * u.z * (1.0f - cos_t)) + (u.y * sin_t), 0.0f,
(u.y * u.x * (1.0f - cos_t)) + (u.z * sin_t), cos_t + (u.y * u.y * (1.0f - cos_t)), (u.y * u.z * (1.0f - cos_t)) - (u.x * sin_t), 0.0f,
(u.z * u.x * (1.0f - cos_t)) - (u.y * sin_t), (u.z * u.y * (1.0f - cos_t)) + (u.x * sin_t), cos_t + (u.z * u.z * (1.0f - cos_t)), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::translate(const Vector3 &t)
{
return Matrix4(1.0f, 0.0f, 0.0f, t.x,
0.0f, 1.0f, 0.0f, t.y,
0.0f, 0.0f, 1.0f, t.z,
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::scale(const Vector3 &s)
{
return Matrix4( s.x, 0.0f, 0.0f, 0.0f,
0.0f, s.y, 0.0f, 0.0f,
0.0f, 0.0f, s.z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::frustum(float l, float r, float b, float t, float n, float f)
{
return Matrix4((2.0f * n) / (r - l), 0.0f, (r + l) / (r - l), 0.0f,
0.0f, (2.0f * n) / (t - b), (t + b) / (t - b), 0.0f,
0.0f, 0.0f, -(f + n) / (f - n), -(2.0f * f * n) / (f - n),
0.0f, 0.0f, -1.0f, 0.0f);
}
Matrix4 Matrix4::perspective(float fovY, float aspectRatio, float nearZ, float farZ)
{
const float frustumHeight = tanf(fovY / 360.0f * M_PI) * nearZ;
const float frustumWidth = frustumHeight * aspectRatio;
return frustum(-frustumWidth, frustumWidth, -frustumHeight, frustumHeight, nearZ, farZ);
}
Matrix4 Matrix4::ortho(float l, float r, float b, float t, float n, float f)
{
return Matrix4(2.0f / (r - l), 0.0f, 0.0f, -(r + l) / (r - l),
0.0f, 2.0f / (t - b), 0.0f, -(t + b) / (t - b),
0.0f, 0.0f, -2.0f / (f - n), -(f + n) / (f - n),
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::rollPitchYaw(float roll, float pitch, float yaw)
{
return rotate(yaw, Vector3(0, 0, 1)) *
rotate(pitch, Vector3(0, 1, 0)) *
rotate(roll, Vector3(1, 0, 0));
}
Matrix4 Matrix4::invert(const Matrix4 &mat)
{
Matrix4 inverted( mat.data[ 5] * mat.data[10] * mat.data[15] - mat.data[5] * mat.data[11] * mat.data[14] - mat.data[ 9] * mat.data[ 6] * mat.data[15] + mat.data[ 9] * mat.data[ 7] * mat.data[14] + mat.data[13] * mat.data[ 6] * mat.data[11] - mat.data[13] * mat.data[ 7] * mat.data[10],
-mat.data[ 4] * mat.data[10] * mat.data[15] + mat.data[4] * mat.data[11] * mat.data[14] + mat.data[ 8] * mat.data[ 6] * mat.data[15] - mat.data[ 8] * mat.data[ 7] * mat.data[14] - mat.data[12] * mat.data[ 6] * mat.data[11] + mat.data[12] * mat.data[ 7] * mat.data[10],
mat.data[ 4] * mat.data[ 9] * mat.data[15] - mat.data[4] * mat.data[11] * mat.data[13] - mat.data[ 8] * mat.data[ 5] * mat.data[15] + mat.data[ 8] * mat.data[ 7] * mat.data[13] + mat.data[12] * mat.data[ 5] * mat.data[11] - mat.data[12] * mat.data[ 7] * mat.data[ 9],
-mat.data[ 4] * mat.data[ 9] * mat.data[14] + mat.data[4] * mat.data[10] * mat.data[13] + mat.data[ 8] * mat.data[ 5] * mat.data[14] - mat.data[ 8] * mat.data[ 6] * mat.data[13] - mat.data[12] * mat.data[ 5] * mat.data[10] + mat.data[12] * mat.data[ 6] * mat.data[ 9],
-mat.data[ 1] * mat.data[10] * mat.data[15] + mat.data[1] * mat.data[11] * mat.data[14] + mat.data[ 9] * mat.data[ 2] * mat.data[15] - mat.data[ 9] * mat.data[ 3] * mat.data[14] - mat.data[13] * mat.data[ 2] * mat.data[11] + mat.data[13] * mat.data[ 3] * mat.data[10],
mat.data[ 0] * mat.data[10] * mat.data[15] - mat.data[0] * mat.data[11] * mat.data[14] - mat.data[ 8] * mat.data[ 2] * mat.data[15] + mat.data[ 8] * mat.data[ 3] * mat.data[14] + mat.data[12] * mat.data[ 2] * mat.data[11] - mat.data[12] * mat.data[ 3] * mat.data[10],
-mat.data[ 0] * mat.data[ 9] * mat.data[15] + mat.data[0] * mat.data[11] * mat.data[13] + mat.data[ 8] * mat.data[ 1] * mat.data[15] - mat.data[ 8] * mat.data[ 3] * mat.data[13] - mat.data[12] * mat.data[ 1] * mat.data[11] + mat.data[12] * mat.data[ 3] * mat.data[ 9],
mat.data[ 0] * mat.data[ 9] * mat.data[14] - mat.data[0] * mat.data[10] * mat.data[13] - mat.data[ 8] * mat.data[ 1] * mat.data[14] + mat.data[ 8] * mat.data[ 2] * mat.data[13] + mat.data[12] * mat.data[ 1] * mat.data[10] - mat.data[12] * mat.data[ 2] * mat.data[ 9],
mat.data[ 1] * mat.data[ 6] * mat.data[15] - mat.data[1] * mat.data[ 7] * mat.data[14] - mat.data[ 5] * mat.data[ 2] * mat.data[15] + mat.data[ 5] * mat.data[ 3] * mat.data[14] + mat.data[13] * mat.data[ 2] * mat.data[ 7] - mat.data[13] * mat.data[ 3] * mat.data[ 6],
-mat.data[ 0] * mat.data[ 6] * mat.data[15] + mat.data[0] * mat.data[ 7] * mat.data[14] + mat.data[ 4] * mat.data[ 2] * mat.data[15] - mat.data[ 4] * mat.data[ 3] * mat.data[14] - mat.data[12] * mat.data[ 2] * mat.data[ 7] + mat.data[12] * mat.data[ 3] * mat.data[ 6],
mat.data[ 0] * mat.data[ 5] * mat.data[15] - mat.data[0] * mat.data[ 7] * mat.data[13] - mat.data[ 4] * mat.data[ 1] * mat.data[15] + mat.data[ 4] * mat.data[ 3] * mat.data[13] + mat.data[12] * mat.data[ 1] * mat.data[ 7] - mat.data[12] * mat.data[ 3] * mat.data[ 5],
-mat.data[ 0] * mat.data[ 5] * mat.data[14] + mat.data[0] * mat.data[ 6] * mat.data[13] + mat.data[ 4] * mat.data[ 1] * mat.data[14] - mat.data[ 4] * mat.data[ 2] * mat.data[13] - mat.data[12] * mat.data[ 1] * mat.data[ 6] + mat.data[12] * mat.data[ 2] * mat.data[ 5],
-mat.data[ 1] * mat.data[ 6] * mat.data[11] + mat.data[1] * mat.data[ 7] * mat.data[10] + mat.data[ 5] * mat.data[ 2] * mat.data[11] - mat.data[ 5] * mat.data[ 3] * mat.data[10] - mat.data[ 9] * mat.data[ 2] * mat.data[ 7] + mat.data[ 9] * mat.data[ 3] * mat.data[ 6],
mat.data[ 0] * mat.data[ 6] * mat.data[11] - mat.data[0] * mat.data[ 7] * mat.data[10] - mat.data[ 4] * mat.data[ 2] * mat.data[11] + mat.data[ 4] * mat.data[ 3] * mat.data[10] + mat.data[ 8] * mat.data[ 2] * mat.data[ 7] - mat.data[ 8] * mat.data[ 3] * mat.data[ 6],
-mat.data[ 0] * mat.data[ 5] * mat.data[11] + mat.data[0] * mat.data[ 7] * mat.data[ 9] + mat.data[ 4] * mat.data[ 1] * mat.data[11] - mat.data[ 4] * mat.data[ 3] * mat.data[ 9] - mat.data[ 8] * mat.data[ 1] * mat.data[ 7] + mat.data[ 8] * mat.data[ 3] * mat.data[ 5],
mat.data[ 0] * mat.data[ 5] * mat.data[10] - mat.data[0] * mat.data[ 6] * mat.data[ 9] - mat.data[ 4] * mat.data[ 1] * mat.data[10] + mat.data[ 4] * mat.data[ 2] * mat.data[ 9] + mat.data[ 8] * mat.data[ 1] * mat.data[ 6] - mat.data[ 8] * mat.data[ 2] * mat.data[ 5]);
float determinant = mat.data[0] * inverted.data[0] + mat.data[1] * inverted.data[4] + mat.data[2] * inverted.data[8] + mat.data[3] * inverted.data[12];
if (determinant != 0.0f)
{
inverted *= 1.0f / determinant;
}
else
{
inverted = identity();
}
return inverted;
}
Matrix4 Matrix4::transpose(const Matrix4 &mat)
{
return Matrix4(mat.data[ 0], mat.data[ 1], mat.data[ 2], mat.data[ 3],
mat.data[ 4], mat.data[ 5], mat.data[ 6], mat.data[ 7],
mat.data[ 8], mat.data[ 9], mat.data[10], mat.data[11],
mat.data[12], mat.data[13], mat.data[14], mat.data[15]);
}
Vector3 Matrix4::transform(const Matrix4 &mat, const Vector3 &pt)
{
Vector4 transformed = Vector4::normalize(mat * Vector4(pt.x, pt.y, pt.z, 1.0f));
return Vector3(transformed.x, transformed.y, transformed.z);
}
Vector3 Matrix4::transform(const Matrix4 &mat, const Vector4 &pt)
{
Vector4 transformed = Vector4::normalize(mat * pt);
return Vector3(transformed.x, transformed.y, transformed.z);
}
Matrix4 operator*(const Matrix4 &a, const Matrix4 &b)
{
return Matrix4(a.data[ 0] * b.data[ 0] + a.data[ 4] * b.data[ 1] + a.data[ 8] * b.data[ 2] + a.data[12] * b.data[ 3],
a.data[ 0] * b.data[ 4] + a.data[ 4] * b.data[ 5] + a.data[ 8] * b.data[ 6] + a.data[12] * b.data[ 7],
a.data[ 0] * b.data[ 8] + a.data[ 4] * b.data[ 9] + a.data[ 8] * b.data[10] + a.data[12] * b.data[11],
a.data[ 0] * b.data[12] + a.data[ 4] * b.data[13] + a.data[ 8] * b.data[14] + a.data[12] * b.data[15],
a.data[ 1] * b.data[ 0] + a.data[ 5] * b.data[ 1] + a.data[ 9] * b.data[ 2] + a.data[13] * b.data[ 3],
a.data[ 1] * b.data[ 4] + a.data[ 5] * b.data[ 5] + a.data[ 9] * b.data[ 6] + a.data[13] * b.data[ 7],
a.data[ 1] * b.data[ 8] + a.data[ 5] * b.data[ 9] + a.data[ 9] * b.data[10] + a.data[13] * b.data[11],
a.data[ 1] * b.data[12] + a.data[ 5] * b.data[13] + a.data[ 9] * b.data[14] + a.data[13] * b.data[15],
a.data[ 2] * b.data[ 0] + a.data[ 6] * b.data[ 1] + a.data[10] * b.data[ 2] + a.data[14] * b.data[ 3],
a.data[ 2] * b.data[ 4] + a.data[ 6] * b.data[ 5] + a.data[10] * b.data[ 6] + a.data[14] * b.data[ 7],
a.data[ 2] * b.data[ 8] + a.data[ 6] * b.data[ 9] + a.data[10] * b.data[10] + a.data[14] * b.data[11],
a.data[ 2] * b.data[12] + a.data[ 6] * b.data[13] + a.data[10] * b.data[14] + a.data[14] * b.data[15],
a.data[ 3] * b.data[ 0] + a.data[ 7] * b.data[ 1] + a.data[11] * b.data[ 2] + a.data[15] * b.data[ 3],
a.data[ 3] * b.data[ 4] + a.data[ 7] * b.data[ 5] + a.data[11] * b.data[ 6] + a.data[15] * b.data[ 7],
a.data[ 3] * b.data[ 8] + a.data[ 7] * b.data[ 9] + a.data[11] * b.data[10] + a.data[15] * b.data[11],
a.data[ 3] * b.data[12] + a.data[ 7] * b.data[13] + a.data[11] * b.data[14] + a.data[15] * b.data[15]);
}
Matrix4 &operator*=(Matrix4 &a, const Matrix4 &b)
{
a = a * b;
return a;
}
Matrix4 operator*(const Matrix4 &a, float b)
{
Matrix4 ret(a);
for (size_t i = 0; i < 16; i++)
{
ret.data[i] *= b;
}
return ret;
}
Matrix4 &operator*=(Matrix4 &a, float b)
{
for (size_t i = 0; i < 16; i++)
{
a.data[i] *= b;
}
return a;
}
Vector4 operator*(const Matrix4 &a, const Vector4 &b)
{
return Vector4(a.data[ 0] * b.x + a.data[ 4] * b.y + a.data[ 8] * b.z + a.data[12] * b.w,
a.data[ 1] * b.x + a.data[ 5] * b.y + a.data[ 9] * b.z + a.data[13] * b.w,
a.data[ 2] * b.x + a.data[ 6] * b.y + a.data[10] * b.z + a.data[14] * b.w,
a.data[ 3] * b.x + a.data[ 7] * b.y + a.data[11] * b.z + a.data[15] * b.w);
}
bool operator==(const Matrix4 &a, const Matrix4 &b)
{
for (size_t i = 0; i < 16; i++)
{
if (a.data[i] != b.data[i])
{
return false;
}
}
return true;
}
bool operator!=(const Matrix4 &a, const Matrix4 &b)
{
return !(a == b);
}
//
// 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.
//
#ifndef SAMPLE_UTIL_MATRIX_H
#define SAMPLE_UTIL_MATRIX_H
#include "Vector.h"
struct Matrix4
{
float data[16];
Matrix4();
Matrix4(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33);
static Matrix4 identity();
static Matrix4 rotate(float angle, const Vector3 &p);
static Matrix4 translate(const Vector3 &t);
static Matrix4 scale(const Vector3 &s);
static Matrix4 frustum(float l, float r, float b, float t, float n, float f);
static Matrix4 perspective(float fov, float aspectRatio, float n, float f);
static Matrix4 ortho(float l, float r, float b, float t, float n, float f);
static Matrix4 rollPitchYaw(float roll, float pitch, float yaw);
static Matrix4 invert(const Matrix4 &mat);
static Matrix4 transpose(const Matrix4 &mat);
static Vector3 transform(const Matrix4 &mat, const Vector3 &pt);
static Vector3 transform(const Matrix4 &mat, const Vector4 &pt);
};
Matrix4 operator*(const Matrix4 &a, const Matrix4 &b);
Matrix4 &operator*=(Matrix4 &a, const Matrix4 &b);
Matrix4 operator*(const Matrix4 &a, float b);
Matrix4 &operator*=(Matrix4 &a, float b);
Vector4 operator*(const Matrix4 &a, const Vector4 &b);
bool operator==(const Matrix4 &a, const Matrix4 &b);
bool operator!=(const Matrix4 &a, const Matrix4 &b);
#endif // SAMPLE_UTIL_MATRIX_H
......@@ -48,3 +48,98 @@ void CreateSphereGeometry(size_t sliceCount, float radius, SphereGeometry *resul
}
}
}
void GenerateCubeGeometry(float radius, CubeGeometry *result)
{
result->positions.resize(24);
result->positions[ 0] = Vector3(-radius, -radius, -radius);
result->positions[ 1] = Vector3(-radius, -radius, radius);
result->positions[ 2] = Vector3( radius, -radius, radius);
result->positions[ 3] = Vector3( radius, -radius, -radius);
result->positions[ 4] = Vector3(-radius, radius, -radius);
result->positions[ 5] = Vector3(-radius, radius, radius);
result->positions[ 6] = Vector3( radius, radius, radius);
result->positions[ 7] = Vector3( radius, radius, -radius);
result->positions[ 8] = Vector3(-radius, -radius, -radius);
result->positions[ 9] = Vector3(-radius, radius, -radius);
result->positions[10] = Vector3( radius, radius, -radius);
result->positions[11] = Vector3( radius, -radius, -radius);
result->positions[12] = Vector3(-radius, -radius, radius);
result->positions[13] = Vector3(-radius, radius, radius);
result->positions[14] = Vector3( radius, radius, radius);
result->positions[15] = Vector3( radius, -radius, radius);
result->positions[16] = Vector3(-radius, -radius, -radius);
result->positions[17] = Vector3(-radius, -radius, radius);
result->positions[18] = Vector3(-radius, radius, radius);
result->positions[19] = Vector3(-radius, radius, -radius);
result->positions[20] = Vector3( radius, -radius, -radius);
result->positions[21] = Vector3( radius, -radius, radius);
result->positions[22] = Vector3( radius, radius, radius);
result->positions[23] = Vector3( radius, radius, -radius);
result->normals.resize(24);
result->normals[ 0] = Vector3( 0.0f, -1.0f, 0.0f);
result->normals[ 1] = Vector3( 0.0f, -1.0f, 0.0f);
result->normals[ 2] = Vector3( 0.0f, -1.0f, 0.0f);
result->normals[ 3] = Vector3( 0.0f, -1.0f, 0.0f);
result->normals[ 4] = Vector3( 0.0f, 1.0f, 0.0f);
result->normals[ 5] = Vector3( 0.0f, 1.0f, 0.0f);
result->normals[ 6] = Vector3( 0.0f, 1.0f, 0.0f);
result->normals[ 7] = Vector3( 0.0f, 1.0f, 0.0f);
result->normals[ 8] = Vector3( 0.0f, 0.0f, -1.0f);
result->normals[ 9] = Vector3( 0.0f, 0.0f, -1.0f);
result->normals[10] = Vector3( 0.0f, 0.0f, -1.0f);
result->normals[11] = Vector3( 0.0f, 0.0f, -1.0f);
result->normals[12] = Vector3( 0.0f, 0.0f, 1.0f);
result->normals[13] = Vector3( 0.0f, 0.0f, 1.0f);
result->normals[14] = Vector3( 0.0f, 0.0f, 1.0f);
result->normals[15] = Vector3( 0.0f, 0.0f, 1.0f);
result->normals[16] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[17] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[18] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[19] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[20] = Vector3( 1.0f, 0.0f, 0.0f);
result->normals[21] = Vector3( 1.0f, 0.0f, 0.0f);
result->normals[22] = Vector3( 1.0f, 0.0f, 0.0f);
result->normals[23] = Vector3( 1.0f, 0.0f, 0.0f);
result->texcoords.resize(24);
result->texcoords[ 0] = Vector2(0.0f, 0.0f);
result->texcoords[ 1] = Vector2(0.0f, 1.0f);
result->texcoords[ 2] = Vector2(1.0f, 1.0f);
result->texcoords[ 3] = Vector2(1.0f, 0.0f);
result->texcoords[ 4] = Vector2(1.0f, 0.0f);
result->texcoords[ 5] = Vector2(1.0f, 1.0f);
result->texcoords[ 6] = Vector2(0.0f, 1.0f);
result->texcoords[ 7] = Vector2(0.0f, 0.0f);
result->texcoords[ 8] = Vector2(0.0f, 0.0f);
result->texcoords[ 9] = Vector2(0.0f, 1.0f);
result->texcoords[10] = Vector2(1.0f, 1.0f);
result->texcoords[11] = Vector2(1.0f, 0.0f);
result->texcoords[12] = Vector2(0.0f, 0.0f);
result->texcoords[13] = Vector2(0.0f, 1.0f);
result->texcoords[14] = Vector2(1.0f, 1.0f);
result->texcoords[15] = Vector2(1.0f, 0.0f);
result->texcoords[16] = Vector2(0.0f, 0.0f);
result->texcoords[17] = Vector2(0.0f, 1.0f);
result->texcoords[18] = Vector2(1.0f, 1.0f);
result->texcoords[19] = Vector2(1.0f, 0.0f);
result->texcoords[20] = Vector2(0.0f, 0.0f);
result->texcoords[21] = Vector2(0.0f, 1.0f);
result->texcoords[22] = Vector2(1.0f, 1.0f);
result->texcoords[23] = Vector2(1.0f, 0.0f);
result->indices.resize(36);
result->indices[ 0] = 0; result->indices[ 1] = 2; result->indices[ 2] = 1;
result->indices[ 3] = 0; result->indices[ 4] = 3; result->indices[ 5] = 2;
result->indices[ 6] = 4; result->indices[ 7] = 5; result->indices[ 8] = 6;
result->indices[ 9] = 4; result->indices[10] = 6; result->indices[11] = 7;
result->indices[12] = 8; result->indices[13] = 9; result->indices[14] = 10;
result->indices[15] = 8; result->indices[16] = 10; result->indices[17] = 11;
result->indices[18] = 12; result->indices[19] = 15; result->indices[20] = 14;
result->indices[21] = 12; result->indices[22] = 14; result->indices[23] = 13;
result->indices[24] = 16; result->indices[25] = 17; result->indices[26] = 18;
result->indices[27] = 16; result->indices[28] = 18; result->indices[29] = 19;
result->indices[30] = 20; result->indices[31] = 23; result->indices[32] = 22;
result->indices[33] = 20; result->indices[34] = 22; result->indices[35] = 21;
}
......@@ -21,4 +21,14 @@ struct SphereGeometry
void CreateSphereGeometry(size_t sliceCount, float radius, SphereGeometry *result);
struct CubeGeometry
{
std::vector<Vector3> positions;
std::vector<Vector3> normals;
std::vector<Vector2> texcoords;
std::vector<GLushort> indices;
};
void GenerateCubeGeometry(float radius, CubeGeometry *result);
#endif // SAMPLE_UTIL_GEOMETRY_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 Simple_VertexShader.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"
#include "geometry_utils.h"
#include "Vector.h"
#include "Matrix.h"
class SimpleVertexShaderSample : public SampleApplication
{
public:
SimpleVertexShaderSample::SimpleVertexShaderSample()
: SampleApplication("SimpleVertexShader", 1280, 720)
{
}
virtual bool initialize()
{
const std::string vs = SHADER_SOURCE
(
uniform mat4 u_mvpMatrix;
attribute vec4 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
void main()
{
gl_Position = u_mvpMatrix * a_position;
v_texcoord = a_texcoord;
}
);
const std::string fs = SHADER_SOURCE
(
precision mediump float;
varying vec2 v_texcoord;
void main()
{
gl_FragColor = vec4(v_texcoord.x, v_texcoord.y, 1.0, 1.0);
}
);
mProgram = CompileProgram(vs, fs);
if (!mProgram)
{
return false;
}
// Get the attribute locations
mPositionLoc = glGetAttribLocation(mProgram, "a_position");
mTexcoordLoc = glGetAttribLocation(mProgram, "a_texcoord");
// Get the uniform locations
mMVPMatrixLoc = glGetUniformLocation(mProgram, "u_mvpMatrix");
// Generate the geometry data
GenerateCubeGeometry(0.5f, &mCube);
// Set an initial rotation
mRotation = 45.0f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
return true;
}
virtual void destroy()
{
glDeleteProgram(mProgram);
}
virtual void step(float dt, double totalTime)
{
mRotation = fmod(mRotation + (dt * 40.0f), 360.0f);
Matrix4 perspectiveMatrix = Matrix4::perspective(60.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(),
1.0f, 20.0f);
Matrix4 modelMatrix = Matrix4::translate(Vector3(0.0f, 0.0f, -2.0f)) *
Matrix4::rotate(mRotation, Vector3(1.0f, 0.0f, 1.0f));
Matrix4 viewMatrix = Matrix4::identity();
Matrix4 mvpMatrix = perspectiveMatrix * viewMatrix * modelMatrix;
// Load the matrices
glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);
}
virtual void draw()
{
// 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, 3, GL_FLOAT, GL_FALSE, 0, mCube.positions.data());
glEnableVertexAttribArray(mPositionLoc);
// Load the texcoord data
glVertexAttribPointer(mTexcoordLoc, 2, GL_FLOAT, GL_FALSE, 0, mCube.texcoords.data());
glEnableVertexAttribArray(mTexcoordLoc);
// Draw the cube
glDrawElements(GL_TRIANGLES, mCube.indices.size(), GL_UNSIGNED_SHORT, mCube.indices.data());
}
private:
// Handle to a program object
GLuint mProgram;
// Attribute locations
GLint mPositionLoc;
GLint mTexcoordLoc;
// Uniform locations
GLuint mMVPMatrixLoc;
// Current rotation
float mRotation;
// Geometry data
CubeGeometry mCube;
};
int main(int argc, char **argv)
{
SimpleVertexShaderSample 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
//
// Simple_VertexShader.c
//
// This is a simple example that draws a rotating cube in perspective
// using a vertex shader to transform the object
//
#include <stdlib.h>
#include "esUtil.h"
typedef struct
{
// Handle to a program object
GLuint programObject;
// Attribute locations
GLint positionLoc;
// Uniform locations
GLint mvpLoc;
// Vertex daata
GLfloat *vertices;
GLushort *indices;
int numIndices;
// Rotation angle
GLfloat angle;
// MVP matrix
ESMatrix mvpMatrix;
} UserData;
///
// Initialize the shader and program object
//
int Init ( ESContext *esContext )
{
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, 0.0f );
return TRUE;
}
///
// Update MVP matrix based on time
//
void Update ( ESContext *esContext, float deltaTime )
{
UserData *userData = (UserData*) esContext->userData;
ESMatrix perspective;
ESMatrix modelview;
float aspect;
// Compute a rotation angle based on time to rotate the cube
userData->angle += ( deltaTime * 40.0f );
if( userData->angle >= 360.0f )
userData->angle -= 360.0f;
// Compute the window aspect ratio
aspect = (GLfloat) esContext->width / (GLfloat) esContext->height;
// Generate a perspective matrix with a 60 degree FOV
esMatrixLoadIdentity( &perspective );
esPerspective( &perspective, 60.0f, aspect, 1.0f, 20.0f );
// Generate a model view matrix to rotate/translate the cube
esMatrixLoadIdentity( &modelview );
// Translate away from the viewer
esTranslate( &modelview, 0.0, 0.0, -2.0 );
// Rotate the cube
esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 );
// Compute the final MVP by multiplying the
// modevleiw and perspective matrices together
esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective );
}
///
// Draw a triangle using the shader pair created in Init()
//
void Draw ( ESContext *esContext )
{
UserData *userData = esContext->userData;
// 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, 3, GL_FLOAT,
GL_FALSE, 3 * sizeof(GLfloat), userData->vertices );
glEnableVertexAttribArray ( userData->positionLoc );
// Load the MVP matrix
glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpMatrix.m[0][0] );
// Draw the cube
glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_SHORT, userData->indices );
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
}
///
// Cleanup
//
void ShutDown ( ESContext *esContext )
{
UserData *userData = esContext->userData;
if ( userData->vertices != NULL )
{
free ( userData->vertices );
}
if ( userData->indices != NULL )
{
free ( userData->indices );
}
// Delete program object
glDeleteProgram ( userData->programObject );
}
int main ( int argc, char *argv[] )
{
ESContext esContext;
UserData userData;
esInitContext ( &esContext );
esContext.userData = &userData;
esCreateWindow ( &esContext, TEXT("Simple Vertex Shader"), 320, 240, ES_WINDOW_RGB );
if ( !Init ( &esContext ) )
return 0;
esRegisterDrawFunc ( &esContext, Draw );
esRegisterUpdateFunc ( &esContext, Update );
esMainLoop ( &esContext );
ShutDown ( &esContext );
}
......@@ -179,8 +179,8 @@
{
'target_name': 'simple_vertex_shader',
'type': 'executable',
'dependencies': [ 'es_util' ],
'sources': [ '<!@(python <(angle_path)/enumerate_files.py gles2_book/Simple_VertexShader -types *.c *.h)' ],
'dependencies': [ 'sample_util' ],
'sources': [ '<!@(python <(angle_path)/enumerate_files.py angle/simple_vertex_shader -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