Skip to content

Commit

Permalink
New Object Picking example.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Jun 23, 2015
1 parent 76e91b4 commit f45a301
Show file tree
Hide file tree
Showing 16 changed files with 517 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cmake_dependent_option(WITH_CUBEMAP "Build CubeMap example (requires JpegImporte
cmake_dependent_option(WITH_FRAMEBUFFER "Build Framebuffer example (requires TgaImporter plugin)" OFF "NOT MAGNUM_TARGET_GLES" OFF)
cmake_dependent_option(WITH_MOTIONBLUR "Build MotionBlur example" OFF "NOT MAGNUM_TARGET_GLES" OFF)
option(WITH_OVR "Build OVR example" OFF)
option(WITH_PICKING "Build Picking example" OFF)
option(WITH_PRIMITIVES "Build Primitives example" OFF)
option(WITH_TEXT "Build Text example (requires FreeTypeFont plugin)" OFF)
cmake_dependent_option(WITH_TEXTUREDTRIANGLE "Build TexturedTriangle example (requires TgaImporter plugin)" OFF "NOT MAGNUM_TARGET_GLES" OFF)
Expand Down
1 change: 1 addition & 0 deletions doc/building-examples.dox
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ for more information.
@ref Trade::TgaImporter "TgaImporter" plugin, not available in OpenGL ES.
- `WITH_MOTIONBLUR` -- Build Motion Blur example. Not available on OpenGL
ES.
- `WITH_PICKING` -- Build Object Picking example.
- `WITH_PRIMITIVES` -- Build @ref examples-primitives "Primitives" example.
- `WITH_TEXT` -- Build Text example. Requires @ref Text::FreeTypeFont "FreeTypeFont"
plugin.
Expand Down
1 change: 1 addition & 0 deletions package/archlinux/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ build() {
-DWITH_FRAMEBUFFER=ON \
-DWITH_MOTIONBLUR=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_PICKING=ON \
-DWITH_TEXT=ON \
-DWITH_TEXTUREDTRIANGLE=ON \
-DWITH_TRIANGLE=ON \
Expand Down
1 change: 1 addition & 0 deletions package/archlinux/magnum-examples-git/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ build() {
-DWITH_FRAMEBUFFER=ON \
-DWITH_MOTIONBLUR=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_PICKING=ON \
-DWITH_TEXT=ON \
-DWITH_TEXTUREDTRIANGLE=ON \
-DWITH_TRIANGLE=ON \
Expand Down
1 change: 1 addition & 0 deletions package/ci/jenkins-mingw-w64.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ cmake .. \
-DWITH_CUBEMAP=ON \
-DWITH_FRAMEBUFFER=ON \
-DWITH_MOTIONBLUR=ON \
-DWITH_PICKING=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_TEXT=ON \
-DWITH_TEXTUREDTRIANGLE=ON \
Expand Down
1 change: 1 addition & 0 deletions package/ci/jenkins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ cmake .. \
-DWITH_CUBEMAP=${desktop_flag} \
-DWITH_FRAMEBUFFER=${desktop_flag} \
-DWITH_MOTIONBLUR=${desktop_flag} \
-DWITH_PICKING=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_TEXT=${desktop_flag} \
-DWITH_TEXTUREDTRIANGLE=${desktop_flag} \
Expand Down
1 change: 1 addition & 0 deletions package/debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ override_dh_auto_configure:
-DWITH_CUBEMAP=ON \
-DWITH_FRAMEBUFFER=ON \
-DWITH_MOTIONBLUR=ON \
-DWITH_PICKING=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_TEXT=ON \
-DWITH_TEXTUREDTRIANGLE=ON \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ src_configure() {
-DWITH_CUBEMAP=ON
-DWITH_FRAMEBUFFER=ON
-DWITH_MOTIONBLUR=ON
-DWITH_PICKING=ON
-DWITH_PRIMITIVES=ON
-DWITH_TEXT=ON
-DWITH_TEXTUREDTRIANGLE=ON
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ if(WITH_OVR)
add_subdirectory(ovr)
endif()

if(WITH_PICKING)
add_subdirectory(picking)
endif()

if(WITH_PRIMITIVES)
add_subdirectory(primitives)
endif()
Expand Down
51 changes: 51 additions & 0 deletions src/picking/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# This file is part of Magnum.
#
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015
# Vladimír Vondruš <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#

cmake_minimum_required(VERSION 2.8.9)
project(MagnumPickingExample)

find_package(Magnum REQUIRED
MeshTools
Primitives
SceneGraph
Sdl2Application)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CORRADE_CXX_FLAGS}")
include_directories(${MAGNUM_INCLUDE_DIRS} ${MAGNUM_APPLICATION_INCLUDE_DIRS})

corrade_add_resource(PickingData resources.conf)

add_executable(magnum-picking
PickingExample.cpp
${PickingData})
target_link_libraries(magnum-picking
${MAGNUM_LIBRARIES}
${MAGNUM_MESHTOOLS_LIBRARIES}
${MAGNUM_PRIMITIVES_LIBRARIES}
${MAGNUM_SCENEGRAPH_LIBRARIES}
${MAGNUM_APPLICATION_LIBRARIES})

install(TARGETS magnum-picking DESTINATION ${MAGNUM_BINARY_INSTALL_DIR})
install(FILES README.md DESTINATION ${MAGNUM_DATA_INSTALL_DIR}/examples RENAME README-picking.md)
58 changes: 58 additions & 0 deletions src/picking/PhongId.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015
Vladimír Vondruš <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

uniform lowp vec3 ambientColor;
uniform lowp vec3 color;
uniform lowp int objectId;

in mediump vec3 transformedNormal;
in highp vec3 lightDirection;
in highp vec3 cameraDirection;

layout(location = 0) out lowp vec4 fragmentColor;
layout(location = 1) out lowp int fragmentObjectId;

void main() {
mediump vec3 normalizedTransformedNormal = normalize(transformedNormal);
highp vec3 normalizedLightDirection = normalize(lightDirection);

/* Add ambient color */
fragmentColor.rgb = ambientColor;

/* Add diffuse color */
lowp float intensity = max(0.0, dot(normalizedTransformedNormal, normalizedLightDirection));
fragmentColor.rgb += color*intensity;

/* Add specular color, if needed */
if(intensity > 0.001) {
highp vec3 reflection = reflect(-normalizedLightDirection, normalizedTransformedNormal);
mediump float specularity = pow(max(0.0, dot(normalize(cameraDirection), reflection)), 80.0);
fragmentColor.rgb += vec3(1.0)*specularity;
}

/* Force alpha to 1 */
fragmentColor.a = 1.0;
fragmentObjectId = objectId;
}
54 changes: 54 additions & 0 deletions src/picking/PhongId.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015
Vladimír Vondruš <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

uniform highp mat4 transformationMatrix;
uniform highp mat4 projectionMatrix;
uniform mediump mat3 normalMatrix;
uniform highp vec3 light;

layout(location = 0) in highp vec4 position;
layout(location = 1) in mediump vec3 normal;

out mediump vec3 transformedNormal;
out highp vec3 lightDirection;
out highp vec3 cameraDirection;

void main() {
/* Transformed vertex position */
highp vec4 transformedPosition4 = transformationMatrix*position;
highp vec3 transformedPosition = transformedPosition4.xyz/transformedPosition4.w;

/* Transformed normal vector */
transformedNormal = normalMatrix*normal;

/* Direction to the light */
lightDirection = normalize(light - transformedPosition);

/* Direction to the camera */
cameraDirection = -transformedPosition;

/* Transform the position */
gl_Position = projectionMatrix*transformedPosition4;
}
Loading

0 comments on commit f45a301

Please sign in to comment.