diff --git a/examples/ControlACs.js b/examples/ControlACs.js index 43eb3290a4..1b60a9e848 100644 --- a/examples/ControlACs.js +++ b/examples/ControlACs.js @@ -11,7 +11,7 @@ // Set the following variables to the right value var NUM_AC = 3; // This is the number of AC. Their ID need to be unique and between 0 (included) and NUM_AC (excluded) -var NAMES = new Array("Arnold", "Jeff"); // ACs names ordered by IDs (Default name is "ACx", x = ID + 1)) +var NAMES = new Array("Craig", "Clement", "Jeff"); // ACs names ordered by IDs (Default name is "ACx", x = ID + 1)) // Those variables MUST be common to every scripts var controlVoxelSize = 0.25; @@ -40,9 +40,10 @@ var windowDimensions = Controller.getViewportDimensions(); var TOOL_ICON_URL = "http://s3-us-west-1.amazonaws.com/highfidelity-public/images/tools/"; var ALPHA_ON = 1.0; var ALPHA_OFF = 0.7; -var COLOR_TOOL_BAR = { red: 128, green: 128, blue: 128 }; -var COLOR_MASTER = { red: 200, green: 200, blue: 200 }; -var TEXT_HEIGHT = 10; +var COLOR_TOOL_BAR = { red: 0, green: 0, blue: 0 }; +var COLOR_MASTER = { red: 0, green: 0, blue: 0 }; +var TEXT_HEIGHT = 12; +var TEXT_MARGIN = 3; var toolBars = new Array(); var nameOverlays = new Array(); @@ -52,76 +53,87 @@ var playLoopIcon = new Array(); var stopIcon = new Array(); setupToolBars(); + function setupToolBars() { if (toolBars.length > 0) { print("Multiple calls to Recorder.js:setupToolBars()"); return; } - + Tool.IMAGE_HEIGHT /= 2; + Tool.IMAGE_WIDTH /= 2; + for (i = 0; i <= NUM_AC; i++) { toolBars.push(new ToolBar(0, 0, ToolBar.HORIZONTAL)); - nameOverlays.push(Overlays.addOverlay("text", { - font: { size: TEXT_HEIGHT }, - text: (i === NUM_AC) ? "Master" : - ((i < NAMES.length) ? NAMES[i] : - "AC" + (i + 1)), - x: 0, y: 0, - width: 0, - height: 0, - alpha: 1.0, - visible: true - })); - + toolBars[i].setBack((i === NUM_AC) ? COLOR_MASTER : COLOR_TOOL_BAR, ALPHA_OFF); + onOffIcon.push(toolBars[i].addTool({ - imageURL: TOOL_ICON_URL + "models-tool.svg", - subImage: { x: 0, y: Tool.IMAGE_WIDTH, width: Tool.IMAGE_WIDTH, height: Tool.IMAGE_HEIGHT }, - width: Tool.IMAGE_WIDTH, - height: Tool.IMAGE_HEIGHT, - alpha: ALPHA_ON, - visible: true - }, true, false)); - playIcon.push(null); - playLoopIcon.push(null); - stopIcon.push(null); + imageURL: TOOL_ICON_URL + "ac-on-off.svg", + subImage: { x: 0, y: 0, width: Tool.IMAGE_WIDTH, height: Tool.IMAGE_HEIGHT }, + x: 0, y: 0, + width: Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: ALPHA_ON, + visible: true + }, true, true)); + + playIcon[i] = toolBars[i].addTool({ + imageURL: TOOL_ICON_URL + "play.svg", + subImage: { x: 0, y: 0, width: Tool.IMAGE_WIDTH, height: Tool.IMAGE_HEIGHT }, + width: Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: ALPHA_OFF, + visible: true + }, false); + + var playLoopWidthFactor = 1.65; + playLoopIcon[i] = toolBars[i].addTool({ + imageURL: TOOL_ICON_URL + "play-and-loop.svg", + subImage: { x: 0, y: 0, width: playLoopWidthFactor * Tool.IMAGE_WIDTH, height: Tool.IMAGE_HEIGHT }, + width: playLoopWidthFactor * Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: ALPHA_OFF, + visible: true + }, false); + + stopIcon[i] = toolBars[i].addTool({ + imageURL: TOOL_ICON_URL + "recording-stop.svg", + width: Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: ALPHA_OFF, + visible: true + }, false); + + nameOverlays.push(Overlays.addOverlay("text", { + backgroundColor: { red: 0, green: 0, blue: 0 }, + font: { size: TEXT_HEIGHT }, + text: (i === NUM_AC) ? "Master" : i + ". " + + ((i < NAMES.length) ? NAMES[i] : + "AC" + i), + x: 0, y: 0, + width: toolBars[i].width + ToolBar.SPACING, + height: TEXT_HEIGHT + TEXT_MARGIN, + leftMargin: TEXT_MARGIN, + topMargin: TEXT_MARGIN, + alpha: ALPHA_OFF, + visible: true + })); } } function sendCommand(id, action) { - if (action === SHOW && toolBars[id].numberOfTools() === 1) { - toolBars[id].selectTool(onOffIcon[id], true); - - playIcon[id] = toolBars[id].addTool({ - imageURL: TOOL_ICON_URL + "play.svg", - width: Tool.IMAGE_WIDTH, - height: Tool.IMAGE_HEIGHT, - alpha: ALPHA_ON, - visible: true - }, false); - - playLoopIcon[id] = toolBars[id].addTool({ - imageURL: TOOL_ICON_URL + "play-loop.svg", - width: Tool.IMAGE_WIDTH, - height: Tool.IMAGE_HEIGHT, - alpha: ALPHA_ON, - visible: true - }, false); - - stopIcon[id] = toolBars[id].addTool({ - imageURL: TOOL_ICON_URL + "stop.svg", - width: Tool.IMAGE_WIDTH, - height: Tool.IMAGE_HEIGHT, - alpha: ALPHA_ON, - visible: true - }, false); - - toolBars[id].setBack((id === NUM_AC) ? COLOR_MASTER : COLOR_TOOL_BAR, ALPHA_OFF); - } else if (action === HIDE && toolBars[id].numberOfTools() != 1) { + if (action === SHOW) { toolBars[id].selectTool(onOffIcon[id], false); - toolBars[id].removeLastTool(); - toolBars[id].removeLastTool(); - toolBars[id].removeLastTool(); - toolBars[id].setBack(null); - } + toolBars[id].setAlpha(ALPHA_ON, playIcon[id]); + toolBars[id].setAlpha(ALPHA_ON, playLoopIcon[id]); + toolBars[id].setAlpha(ALPHA_ON, stopIcon[id]); + } else if (action === HIDE) { + toolBars[id].selectTool(onOffIcon[id], true); + toolBars[id].setAlpha(ALPHA_OFF, playIcon[id]); + toolBars[id].setAlpha(ALPHA_OFF, playLoopIcon[id]); + toolBars[id].setAlpha(ALPHA_OFF, stopIcon[id]); + } else if (toolBars[id].toolSelected(onOffIcon[id])) { + return; + } if (id === toolBars.length - 1) { for (i = 0; i < NUM_AC; i++) { @@ -141,55 +153,56 @@ function sendCommand(id, action) { function mousePressEvent(event) { clickedOverlay = Overlays.getOverlayAtPoint({ x: event.x, y: event.y }); - + // Check master control var i = toolBars.length - 1; - if (onOffIcon[i] === toolBars[i].clicked(clickedOverlay)) { - if (toolBars[i].toolSelected(onOffIcon[i])) { - sendCommand(i, SHOW); - } else { - sendCommand(i, HIDE); - } - } else if (playIcon[i] === toolBars[i].clicked(clickedOverlay)) { - sendCommand(i, PLAY); - } else if (playLoopIcon[i] === toolBars[i].clicked(clickedOverlay)) { - sendCommand(i, PLAY_LOOP); - } else if (stopIcon[i] === toolBars[i].clicked(clickedOverlay)) { - sendCommand(i, STOP); - } else { - // Check individual controls + if (onOffIcon[i] === toolBars[i].clicked(clickedOverlay, false)) { + if (toolBars[i].toolSelected(onOffIcon[i])) { + sendCommand(i, SHOW); + } else { + sendCommand(i, HIDE); + } + } else if (playIcon[i] === toolBars[i].clicked(clickedOverlay, false)) { + sendCommand(i, PLAY); + } else if (playLoopIcon[i] === toolBars[i].clicked(clickedOverlay, false)) { + sendCommand(i, PLAY_LOOP); + } else if (stopIcon[i] === toolBars[i].clicked(clickedOverlay, false)) { + sendCommand(i, STOP); + } else { + // Check individual controls for (i = 0; i < NUM_AC; i++) { - if (onOffIcon[i] === toolBars[i].clicked(clickedOverlay)) { - if (toolBars[i].toolSelected(onOffIcon[i])) { - sendCommand(i, SHOW); - } else { - sendCommand(i, HIDE); - } - } else if (playIcon[i] === toolBars[i].clicked(clickedOverlay)) { - sendCommand(i, PLAY); - } else if (playLoopIcon[i] === toolBars[i].clicked(clickedOverlay)) { - sendCommand(i, PLAY_LOOP); - } else if (stopIcon[i] === toolBars[i].clicked(clickedOverlay)) { - sendCommand(i, STOP); - } else { - - } + if (onOffIcon[i] === toolBars[i].clicked(clickedOverlay, false)) { + if (toolBars[i].toolSelected(onOffIcon[i], false)) { + sendCommand(i, SHOW); + } else { + sendCommand(i, HIDE); + } + } else if (playIcon[i] === toolBars[i].clicked(clickedOverlay, false)) { + sendCommand(i, PLAY); + } else if (playLoopIcon[i] === toolBars[i].clicked(clickedOverlay, false)) { + sendCommand(i, PLAY_LOOP); + } else if (stopIcon[i] === toolBars[i].clicked(clickedOverlay, false)) { + sendCommand(i, STOP); + } else { + + } } - } + } } function moveUI() { - var relative = { x: 70, y: 400 }; + var textSize = TEXT_HEIGHT + 2 * TEXT_MARGIN; + var relative = { x: 70, y: 75 + (NUM_AC) * (Tool.IMAGE_HEIGHT + ToolBar.SPACING + textSize) }; + for (i = 0; i <= NUM_AC; i++) { toolBars[i].move(relative.x, - windowDimensions.y - relative.y + - i * (Tool.IMAGE_HEIGHT + 2 * ToolBar.SPACING + TEXT_HEIGHT)); + windowDimensions.y - relative.y + + i * (Tool.IMAGE_HEIGHT + ToolBar.SPACING + textSize)); + Overlays.editOverlay(nameOverlays[i], { - x: relative.x, - y: windowDimensions.y - relative.y + - i * (Tool.IMAGE_HEIGHT + 2 * ToolBar.SPACING + TEXT_HEIGHT) - - ToolBar.SPACING - 2 * TEXT_HEIGHT - }); + x: toolBars[i].x - ToolBar.SPACING, + y: toolBars[i].y - textSize + }); } } diff --git a/examples/Recorder.js b/examples/Recorder.js index 7ecf0e2640..b3a4c7a4c3 100644 --- a/examples/Recorder.js +++ b/examples/Recorder.js @@ -13,7 +13,6 @@ Script.include("toolBars.js"); var recordingFile = "recording.rec"; var playFromCurrentLocation = true; -var loop = true; var windowDimensions = Controller.getViewportDimensions(); var TOOL_ICON_URL = "http://s3-us-west-1.amazonaws.com/highfidelity-public/images/tools/"; @@ -21,69 +20,91 @@ var ALPHA_ON = 1.0; var ALPHA_OFF = 0.7; var COLOR_ON = { red: 128, green: 0, blue: 0 }; var COLOR_OFF = { red: 128, green: 128, blue: 128 }; -Tool.IMAGE_WIDTH *= 0.7; -Tool.IMAGE_HEIGHT *= 0.7; +var COLOR_TOOL_BAR = { red: 0, green: 0, blue: 0 }; var toolBar = null; var recordIcon; var playIcon; +var playLoopIcon; var saveIcon; var loadIcon; +var spacing; +var timerOffset; setupToolBar(); var timer = null; setupTimer(); +var watchStop = false; + function setupToolBar() { if (toolBar != null) { print("Multiple calls to Recorder.js:setupToolBar()"); return; } - + Tool.IMAGE_HEIGHT /= 2; + Tool.IMAGE_WIDTH /= 2; + toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL); - toolBar.setBack(COLOR_OFF, ALPHA_OFF); - - recordIcon = toolBar.addTool({ - imageURL: TOOL_ICON_URL + "record.svg", - width: Tool.IMAGE_WIDTH, - height: Tool.IMAGE_HEIGHT, - alpha: ALPHA_ON, - visible: true - }, false); - - playIcon = toolBar.addTool({ - imageURL: TOOL_ICON_URL + "play.svg", - width: Tool.IMAGE_WIDTH, - height: Tool.IMAGE_HEIGHT, - alpha: ALPHA_ON, - visible: true - }, false, false); - - saveIcon = toolBar.addTool({ - imageURL: TOOL_ICON_URL + "save.svg", - width: Tool.IMAGE_WIDTH, - height: Tool.IMAGE_HEIGHT, - alpha: ALPHA_ON, - visible: true - }, false, false); - - loadIcon = toolBar.addTool({ - imageURL: TOOL_ICON_URL + "load.svg", - width: Tool.IMAGE_WIDTH, - height: Tool.IMAGE_HEIGHT, - alpha: ALPHA_ON, - visible: true - }, false, false); + + toolBar.setBack(COLOR_TOOL_BAR, ALPHA_OFF); + + recordIcon = toolBar.addTool({ + imageURL: TOOL_ICON_URL + "recording-record.svg", + subImage: { x: 0, y: 0, width: Tool.IMAGE_WIDTH, height: Tool.IMAGE_HEIGHT }, + x: 0, y: 0, + width: Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: MyAvatar.isPlaying() ? ALPHA_OFF : ALPHA_ON, + visible: true + }, true, !MyAvatar.isRecording()); + + var playLoopWidthFactor = 1.65; + playIcon = toolBar.addTool({ + imageURL: TOOL_ICON_URL + "play-pause.svg", + width: playLoopWidthFactor * Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: (MyAvatar.isRecording() || MyAvatar.playerLength() === 0) ? ALPHA_OFF : ALPHA_ON, + visible: true + }, false); + + playLoopIcon = toolBar.addTool({ + imageURL: TOOL_ICON_URL + "play-and-loop.svg", + subImage: { x: 0, y: 0, width: playLoopWidthFactor * Tool.IMAGE_WIDTH, height: Tool.IMAGE_HEIGHT }, + width: playLoopWidthFactor * Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: (MyAvatar.isRecording() || MyAvatar.playerLength() === 0) ? ALPHA_OFF : ALPHA_ON, + visible: true + }, false); + + timerOffset = toolBar.width; + spacing = toolBar.addSpacing(0); + + saveIcon = toolBar.addTool({ + imageURL: TOOL_ICON_URL + "recording-save.svg", + width: Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: (MyAvatar.isRecording() || MyAvatar.isPlaying() || MyAvatar.playerLength() === 0) ? ALPHA_OFF : ALPHA_ON, + visible: true + }, false); + + loadIcon = toolBar.addTool({ + imageURL: TOOL_ICON_URL + "recording-upload.svg", + width: Tool.IMAGE_WIDTH, + height: Tool.IMAGE_HEIGHT, + alpha: (MyAvatar.isRecording() || MyAvatar.isPlaying()) ? ALPHA_OFF : ALPHA_ON, + visible: true + }, false); } function setupTimer() { timer = Overlays.addOverlay("text", { - font: { size: 20 }, + font: { size: 15 }, text: (0.00).toFixed(3), backgroundColor: COLOR_OFF, x: 0, y: 0, - width: 100, - height: 100, + width: 0, + height: 0, alpha: 1.0, visible: true }); @@ -92,7 +113,8 @@ function setupTimer() { function updateTimer() { var text = ""; if (MyAvatar.isRecording()) { - text = formatTime(MyAvatar.recorderElapsed()) + text = formatTime(MyAvatar.recorderElapsed()); + } else { text = formatTime(MyAvatar.playerElapsed()) + " / " + formatTime(MyAvatar.playerLength()); @@ -101,6 +123,7 @@ function updateTimer() { Overlays.editOverlay(timer, { text: text }) + toolBar.changeSpacing(text.length * 8 + ((MyAvatar.isRecording()) ? 15 : 0), spacing); } function formatTime(time) { @@ -127,54 +150,86 @@ function formatTime(time) { } function moveUI() { - var relative = { x: 30, y: 90 }; + var relative = { x: 70, y: 40 }; toolBar.move(relative.x, windowDimensions.y - relative.y); Overlays.editOverlay(timer, { - x: relative.x - 10, - y: windowDimensions.y - relative.y - 35, - width: 0, - height: 0 + x: relative.x + timerOffset - ToolBar.SPACING, + y: windowDimensions.y - relative.y - ToolBar.SPACING }); } function mousePressEvent(event) { clickedOverlay = Overlays.getOverlayAtPoint({ x: event.x, y: event.y }); - - if (recordIcon === toolBar.clicked(clickedOverlay) && !MyAvatar.isPlaying()) { - if (!MyAvatar.isRecording()) { - MyAvatar.startRecording(); - toolBar.setBack(COLOR_ON, ALPHA_ON); - } else { - MyAvatar.stopRecording(); - MyAvatar.loadLastRecording(); - toolBar.setBack(COLOR_OFF, ALPHA_OFF); - } - } else if (playIcon === toolBar.clicked(clickedOverlay) && !MyAvatar.isRecording()) { - if (MyAvatar.isPlaying()) { - MyAvatar.stopPlaying(); - } else { - MyAvatar.setPlayFromCurrentLocation(playFromCurrentLocation); - MyAvatar.setPlayerLoop(loop); - MyAvatar.startPlaying(true); - } - } else if (saveIcon === toolBar.clicked(clickedOverlay)) { - if (!MyAvatar.isRecording()) { - recordingFile = Window.save("Save recording to file", ".", "*.rec"); - if (recordingFile != null) { + + if (recordIcon === toolBar.clicked(clickedOverlay, false) && !MyAvatar.isPlaying()) { + if (!MyAvatar.isRecording()) { + MyAvatar.startRecording(); + toolBar.selectTool(recordIcon, false); + toolBar.setAlpha(ALPHA_OFF, playIcon); + toolBar.setAlpha(ALPHA_OFF, playLoopIcon); + toolBar.setAlpha(ALPHA_OFF, saveIcon); + toolBar.setAlpha(ALPHA_OFF, loadIcon); + } else { + MyAvatar.stopRecording(); + toolBar.selectTool(recordIcon, true ); + MyAvatar.loadLastRecording(); + toolBar.setAlpha(ALPHA_ON, playIcon); + toolBar.setAlpha(ALPHA_ON, playLoopIcon); + toolBar.setAlpha(ALPHA_ON, saveIcon); + toolBar.setAlpha(ALPHA_ON, loadIcon); + } + } else if (playIcon === toolBar.clicked(clickedOverlay) && !MyAvatar.isRecording()) { + if (MyAvatar.isPlaying()) { + MyAvatar.stopPlaying(); + toolBar.setAlpha(ALPHA_ON, recordIcon); + toolBar.setAlpha(ALPHA_ON, saveIcon); + toolBar.setAlpha(ALPHA_ON, loadIcon); + } else if (MyAvatar.playerLength() > 0) { + MyAvatar.setPlayFromCurrentLocation(playFromCurrentLocation); + MyAvatar.setPlayerLoop(false); + MyAvatar.startPlaying(); + toolBar.setAlpha(ALPHA_OFF, recordIcon); + toolBar.setAlpha(ALPHA_OFF, saveIcon); + toolBar.setAlpha(ALPHA_OFF, loadIcon); + watchStop = true; + } + } else if (playLoopIcon === toolBar.clicked(clickedOverlay) && !MyAvatar.isRecording()) { + if (MyAvatar.isPlaying()) { + MyAvatar.stopPlaying(); + toolBar.setAlpha(ALPHA_ON, recordIcon); + toolBar.setAlpha(ALPHA_ON, saveIcon); + toolBar.setAlpha(ALPHA_ON, loadIcon); + } else if (MyAvatar.playerLength() > 0) { + MyAvatar.setPlayFromCurrentLocation(playFromCurrentLocation); + MyAvatar.setPlayerLoop(true); + MyAvatar.startPlaying(); + toolBar.setAlpha(ALPHA_OFF, recordIcon); + toolBar.setAlpha(ALPHA_OFF, saveIcon); + toolBar.setAlpha(ALPHA_OFF, loadIcon); + } + } else if (saveIcon === toolBar.clicked(clickedOverlay)) { + if (!MyAvatar.isRecording() && !MyAvatar.isPlaying() && MyAvatar.playerLength() != 0) { + recordingFile = Window.save("Save recording to file", ".", "*.rec"); + if (!(recordingFile === "null" || recordingFile === null || recordingFile === "")) { MyAvatar.saveRecording(recordingFile); - } - } - } else if (loadIcon === toolBar.clicked(clickedOverlay)) { - if (!MyAvatar.isRecording()) { - recordingFile = Window.browse("Load recorcding from file", ".", "*.rec"); - if (recordingFile != "null") { - MyAvatar.loadRecording(recordingFile); - } - } - } else { - - } + } + } + } else if (loadIcon === toolBar.clicked(clickedOverlay)) { + if (!MyAvatar.isRecording() && !MyAvatar.isPlaying()) { + recordingFile = Window.browse("Load recorcding from file", ".", "*.rec"); + if (!(recordingFile === "null" || recordingFile === null || recordingFile === "")) { + MyAvatar.loadRecording(recordingFile); + } + if (MyAvatar.playerLength() > 0) { + toolBar.setAlpha(ALPHA_ON, playIcon); + toolBar.setAlpha(ALPHA_ON, playLoopIcon); + toolBar.setAlpha(ALPHA_ON, saveIcon); + } + } + } else { + + } } function update() { @@ -186,6 +241,13 @@ function update() { } updateTimer(); + + if (watchStop && !MyAvatar.isPlaying()) { + watchStop = false; + toolBar.setAlpha(ALPHA_ON, recordIcon); + toolBar.setAlpha(ALPHA_ON, saveIcon); + toolBar.setAlpha(ALPHA_ON, loadIcon); + } } function scriptEnding() { diff --git a/examples/toolBars.js b/examples/toolBars.js index 092ab5992e..dc17f02869 100644 --- a/examples/toolBars.js +++ b/examples/toolBars.js @@ -61,11 +61,10 @@ Overlay2D = function(properties, overlay) { // overlay is an optionnal variable } this.clicked = function(clickedOverlay) { - return (overlay == clickedOverlay ? true : false); + return overlay === clickedOverlay; } this.cleanup = function() { - print("Cleanup"); Overlays.deleteOverlay(overlay); } } @@ -112,9 +111,9 @@ Tool = function(properties, selectable, selected) { // selectable and selected a this.select(selected); this.baseClicked = this.clicked; - this.clicked = function(clickedOverlay) { + this.clicked = function(clickedOverlay, update) { if (this.baseClicked(clickedOverlay)) { - if (selectable) { + if (selectable && update) { this.toggle(); } return true; @@ -141,6 +140,7 @@ ToolBar = function(x, y, direction) { alpha: 1.0, visible: false }); + this.spacing = []; this.addTool = function(properties, selectable, selected) { if (direction == ToolBar.HORIZONTAL) { @@ -154,16 +154,56 @@ ToolBar = function(x, y, direction) { this.width = Math.max(properties.width, this.width); this.height += properties.height + ToolBar.SPACING; } + if (this.back != null) { Overlays.editOverlay(this.back, { - width: this.width + 2 * ToolBar.SPACING, - height: this.height + 2 * ToolBar.SPACING + width: this.width + + ((direction == ToolBar.HORIZONTAL) ? 1 : 2) * ToolBar.SPACING, + height: this.height + + ((direction == ToolBar.VERTICAL) ? 1 : 2) * ToolBar.SPACING, }); } this.tools.push(new Tool(properties, selectable, selected)); return ((this.tools.length) - 1); } + + this.addSpacing = function(size) { + if (direction == ToolBar.HORIZONTAL) { + this.width += size; + } else { + this.height += size; + } + this.spacing[this.tools.length] = size; + + return (this.tools.length); + } + + this.changeSpacing = function(size, id) { + if (this.spacing[id] === null) { + this.spacing[id] = 0; + } + var diff = size - this.spacing[id]; + this.spacing[id] = size; + + var dx = (direction == ToolBar.HORIZONTAL) ? diff : 0; + var dy = (direction == ToolBar.VERTICAL) ? diff : 0; + this.width += dx; + this.height += dy; + + for(i = id; i < this.tools.length; i++) { + this.tools[i].move(this.tools[i].x() + dx, + this.tools[i].y() + dy); + } + if (this.back != null) { + Overlays.editOverlay(this.back, { + width: this.width + + ((direction == ToolBar.HORIZONTAL) ? 1 : 2) * ToolBar.SPACING, + height: this.height + + ((direction == ToolBar.VERTICAL) ? 1 : 2) * ToolBar.SPACING, + }); + } + } this.removeLastTool = function() { this.tools.pop().cleanup(); @@ -209,18 +249,22 @@ ToolBar = function(x, y, direction) { this.tools[tool].setAlpha(alpha); } } - + this.setBack = function(color, alpha) { if (color == null) { Overlays.editOverlay(this.back, { - visible: false - }); + visible: false + }); } else { Overlays.editOverlay(this.back, { - visible: true, - backgroundColor: color, - alpha: alpha - }) + width: this.width + + ((direction == ToolBar.HORIZONTAL) ? 1 : 2) * ToolBar.SPACING, + height: this.height + + ((direction == ToolBar.VERTICAL) ? 1 : 2) * ToolBar.SPACING, + visible: true, + backgroundColor: color, + alpha: alpha + }); } } @@ -233,9 +277,13 @@ ToolBar = function(x, y, direction) { } } - this.clicked = function(clickedOverlay) { + this.clicked = function(clickedOverlay, update) { + if(typeof(update) === 'undefined') { + update = true; + } + for(var tool in this.tools) { - if (this.tools[tool].visible() && this.tools[tool].clicked(clickedOverlay)) { + if (this.tools[tool].visible() && this.tools[tool].clicked(clickedOverlay, update)) { return parseInt(tool); } } diff --git a/interface/resources/shaders/directional_light.frag b/interface/resources/shaders/directional_light.frag new file mode 100644 index 0000000000..7fb5d83719 --- /dev/null +++ b/interface/resources/shaders/directional_light.frag @@ -0,0 +1,26 @@ +#version 120 + +// +// directional_light.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the diffuse texture +uniform sampler2D diffuseMap; + +// the normal texture +uniform sampler2D normalMap; + +void main(void) { + // compute the base color based on OpenGL lighting model + vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); + gl_FragColor = vec4((texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightModelProduct.sceneColor + + gl_FrontLightProduct[0].ambient + gl_FrontLightProduct[0].diffuse * max(0.0, dot(normal * 2.0 - + vec4(1.0, 1.0, 1.0, 2.0), gl_LightSource[0].position)))).rgb, normal.a); +} diff --git a/interface/resources/shaders/metavoxel_heightfield_cascaded_shadow_map.frag b/interface/resources/shaders/directional_light_cascaded_shadow_map.frag similarity index 50% rename from interface/resources/shaders/metavoxel_heightfield_cascaded_shadow_map.frag rename to interface/resources/shaders/directional_light_cascaded_shadow_map.frag index 059a4e0296..d16467520e 100644 --- a/interface/resources/shaders/metavoxel_heightfield_cascaded_shadow_map.frag +++ b/interface/resources/shaders/directional_light_cascaded_shadow_map.frag @@ -1,10 +1,10 @@ #version 120 // -// metavoxel_heightfield.frag +// directional_light.frag // fragment shader // -// Created by Andrzej Kapolka on 7/28/14. +// Created by Andrzej Kapolka on 9/3/14. // Copyright 2014 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. @@ -14,6 +14,12 @@ // the diffuse texture uniform sampler2D diffuseMap; +// the normal texture +uniform sampler2D normalMap; + +// the depth texture +uniform sampler2D depthMap; + // the shadow texture uniform sampler2DShadow shadowMap; @@ -21,28 +27,39 @@ uniform sampler2DShadow shadowMap; uniform vec3 shadowDistances; // the inverse of the size of the shadow map -const float shadowScale = 1.0 / 2048.0; +uniform float shadowScale; -// the interpolated position -varying vec4 position; +// the distance to the near clip plane +uniform float near; -// the interpolated normal -varying vec4 normal; +// scale factor for depth: (far - near) / far +uniform float depthScale; + +// offset for depth texture coordinates +uniform vec2 depthTexCoordOffset; + +// scale for depth texture coordinates +uniform vec2 depthTexCoordScale; void main(void) { + // compute the view space position using the depth + float z = near / (texture2D(depthMap, gl_TexCoord[0].st).r * depthScale - 1.0); + vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); + // compute the index of the cascade to use and the corresponding texture coordinates int shadowIndex = int(dot(step(vec3(position.z), shadowDistances), vec3(1.0, 1.0, 1.0))); vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[shadowIndex], position), dot(gl_EyePlaneT[shadowIndex], position), dot(gl_EyePlaneR[shadowIndex], position)); - // compute the base color based on OpenGL lighting model - float diffuse = dot(normalize(normal), gl_LightSource[0].position); + // compute the color based on OpenGL lighting model, use the alpha from the normal map + vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); + float diffuse = dot(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0), gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); - vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); - gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st); + vec4 baseColor = texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightModelProduct.sceneColor + + gl_FrontLightProduct[0].ambient + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FragColor = vec4(baseColor.rgb, normal.a); } diff --git a/interface/resources/shaders/directional_light_shadow_map.frag b/interface/resources/shaders/directional_light_shadow_map.frag new file mode 100644 index 0000000000..d4ac254fc5 --- /dev/null +++ b/interface/resources/shaders/directional_light_shadow_map.frag @@ -0,0 +1,60 @@ +#version 120 + +// +// directional_light.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the diffuse texture +uniform sampler2D diffuseMap; + +// the normal texture +uniform sampler2D normalMap; + +// the depth texture +uniform sampler2D depthMap; + +// the shadow texture +uniform sampler2DShadow shadowMap; + +// the inverse of the size of the shadow map +uniform float shadowScale; + +// the distance to the near clip plane +uniform float near; + +// scale factor for depth: (far - near) / far +uniform float depthScale; + +// offset for depth texture coordinates +uniform vec2 depthTexCoordOffset; + +// scale for depth texture coordinates +uniform vec2 depthTexCoordScale; + +void main(void) { + // compute the view space position using the depth + float z = near / (texture2D(depthMap, gl_TexCoord[0].st).r * depthScale - 1.0); + vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); + + // compute the corresponding texture coordinates + vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[0], position), dot(gl_EyePlaneT[0], position), dot(gl_EyePlaneR[0], position)); + + // compute the color based on OpenGL lighting model, use the alpha from the normal map + vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); + float diffuse = dot(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0), gl_LightSource[0].position); + float facingLight = step(0.0, diffuse) * 0.25 * + (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); + vec4 baseColor = texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightModelProduct.sceneColor + + gl_FrontLightProduct[0].ambient + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FragColor = vec4(baseColor.rgb, normal.a); +} diff --git a/interface/resources/shaders/metavoxel_heightfield.frag b/interface/resources/shaders/metavoxel_heightfield.frag deleted file mode 100644 index f99a0a6403..0000000000 --- a/interface/resources/shaders/metavoxel_heightfield.frag +++ /dev/null @@ -1,25 +0,0 @@ -#version 120 - -// -// metavoxel_heightfield.frag -// fragment shader -// -// Created by Andrzej Kapolka on 7/28/14. -// Copyright 2014 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// the diffuse texture -uniform sampler2D diffuseMap; - -// the interpolated normal -varying vec4 normal; - -void main(void) { - // compute the base color based on OpenGL lighting model - vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * max(0.0, dot(normalize(normal), gl_LightSource[0].position))); - gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st); -} diff --git a/interface/resources/shaders/metavoxel_heightfield.vert b/interface/resources/shaders/metavoxel_heightfield.vert deleted file mode 100644 index 70cf3f9419..0000000000 --- a/interface/resources/shaders/metavoxel_heightfield.vert +++ /dev/null @@ -1,51 +0,0 @@ -#version 120 - -// -// metavoxel_heighfield.vert -// vertex shader -// -// Created by Andrzej Kapolka on 7/28/14. -// Copyright 2014 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// the height texture -uniform sampler2D heightMap; - -// the distance between height points in texture space -uniform float heightScale; - -// the scale between height and color textures -uniform float colorScale; - -// the interpolated position -varying vec4 position; - -// the interpolated normal -varying vec4 normal; - -void main(void) { - // transform and store the normal for interpolation - vec2 heightCoord = gl_MultiTexCoord0.st; - float deltaX = texture2D(heightMap, heightCoord - vec2(heightScale, 0.0)).r - - texture2D(heightMap, heightCoord + vec2(heightScale, 0.0)).r; - float deltaZ = texture2D(heightMap, heightCoord - vec2(0.0, heightScale)).r - - texture2D(heightMap, heightCoord + vec2(0.0, heightScale)).r; - normal = normalize(gl_ModelViewMatrix * vec4(deltaX, heightScale, deltaZ, 0.0)); - - // add the height to the position - float height = texture2D(heightMap, heightCoord).r; - position = gl_ModelViewMatrix * (gl_Vertex + vec4(0.0, height, 0.0, 0.0)); - gl_Position = gl_ProjectionMatrix * position; - - // the zero height should be invisible - gl_FrontColor = vec4(1.0, 1.0, 1.0, step(height, 0.0)); - - // pass along the scaled/offset texture coordinates - gl_TexCoord[0] = (gl_MultiTexCoord0 - vec4(heightScale, heightScale, 0.0, 0.0)) * colorScale; - - // and the shadow texture coordinates - gl_TexCoord[1] = vec4(dot(gl_EyePlaneS[0], position), dot(gl_EyePlaneT[0], position), dot(gl_EyePlaneR[0], position), 1.0); -} diff --git a/interface/resources/shaders/metavoxel_heightfield_base.frag b/interface/resources/shaders/metavoxel_heightfield_base.frag index 9b64a59e6f..a4523ced23 100644 --- a/interface/resources/shaders/metavoxel_heightfield_base.frag +++ b/interface/resources/shaders/metavoxel_heightfield_base.frag @@ -14,7 +14,11 @@ // the diffuse texture uniform sampler2D diffuseMap; +// the interpolated normal +varying vec4 normal; + void main(void) { // compute the base color based on OpenGL lighting model - gl_FragColor = gl_Color * texture2D(diffuseMap, gl_TexCoord[0].st); + gl_FragData[0] = gl_Color * texture2D(diffuseMap, gl_TexCoord[0].st); + gl_FragData[1] = normalize(normal) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); } diff --git a/interface/resources/shaders/metavoxel_heightfield_base.vert b/interface/resources/shaders/metavoxel_heightfield_base.vert index 3e4b081d6f..f097426e13 100644 --- a/interface/resources/shaders/metavoxel_heightfield_base.vert +++ b/interface/resources/shaders/metavoxel_heightfield_base.vert @@ -20,9 +20,20 @@ uniform float heightScale; // the scale between height and color textures uniform float colorScale; +// the interpolated normal +varying vec4 normal; + void main(void) { + // transform and store the normal for interpolation + vec2 heightCoord = gl_MultiTexCoord0.st; + float deltaX = texture2D(heightMap, heightCoord - vec2(heightScale, 0.0)).r - + texture2D(heightMap, heightCoord + vec2(heightScale, 0.0)).r; + float deltaZ = texture2D(heightMap, heightCoord - vec2(0.0, heightScale)).r - + texture2D(heightMap, heightCoord + vec2(0.0, heightScale)).r; + normal = normalize(gl_ModelViewMatrix * vec4(deltaX, heightScale, deltaZ, 0.0)); + // add the height to the position - float height = texture2D(heightMap, gl_MultiTexCoord0.st).r; + float height = texture2D(heightMap, heightCoord).r; gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + vec4(0.0, height, 0.0, 0.0)); // the zero height should be invisible diff --git a/interface/resources/shaders/metavoxel_heightfield_light.frag b/interface/resources/shaders/metavoxel_heightfield_light.frag deleted file mode 100644 index ce3f23e142..0000000000 --- a/interface/resources/shaders/metavoxel_heightfield_light.frag +++ /dev/null @@ -1,21 +0,0 @@ -#version 120 - -// -// metavoxel_heightfield_light.frag -// fragment shader -// -// Created by Andrzej Kapolka on 8/20/14. -// Copyright 2014 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// the interpolated normal -varying vec4 normal; - -void main(void) { - // compute the base color based on OpenGL lighting model - gl_FragColor = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * max(0.0, dot(normalize(normal), gl_LightSource[0].position))); -} diff --git a/interface/resources/shaders/metavoxel_heightfield_light.vert b/interface/resources/shaders/metavoxel_heightfield_light.vert deleted file mode 100644 index 228d575b81..0000000000 --- a/interface/resources/shaders/metavoxel_heightfield_light.vert +++ /dev/null @@ -1,45 +0,0 @@ -#version 120 - -// -// metavoxel_heighfield_light.vert -// vertex shader -// -// Created by Andrzej Kapolka on 8/20/14. -// Copyright 2014 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// the height texture -uniform sampler2D heightMap; - -// the distance between height points in texture space -uniform float heightScale; - -// the interpolated position -varying vec4 position; - -// the interpolated normal -varying vec4 normal; - -void main(void) { - // transform and store the normal for interpolation - vec2 heightCoord = gl_MultiTexCoord0.st; - float deltaX = texture2D(heightMap, heightCoord - vec2(heightScale, 0.0)).r - - texture2D(heightMap, heightCoord + vec2(heightScale, 0.0)).r; - float deltaZ = texture2D(heightMap, heightCoord - vec2(0.0, heightScale)).r - - texture2D(heightMap, heightCoord + vec2(0.0, heightScale)).r; - normal = normalize(gl_ModelViewMatrix * vec4(deltaX, heightScale, deltaZ, 0.0)); - - // add the height to the position - float height = texture2D(heightMap, heightCoord).r; - position = gl_ModelViewMatrix * (gl_Vertex + vec4(0.0, height, 0.0, 0.0)); - gl_Position = gl_ProjectionMatrix * position; - - // the zero height should be invisible - gl_FrontColor = vec4(1.0, 1.0, 1.0, step(height, 0.0)); - - // and the shadow texture coordinates - gl_TexCoord[1] = vec4(dot(gl_EyePlaneS[0], position), dot(gl_EyePlaneT[0], position), dot(gl_EyePlaneR[0], position), 1.0); -} diff --git a/interface/resources/shaders/metavoxel_heightfield_light_cascaded_shadow_map.frag b/interface/resources/shaders/metavoxel_heightfield_light_cascaded_shadow_map.frag deleted file mode 100644 index 73382eb83c..0000000000 --- a/interface/resources/shaders/metavoxel_heightfield_light_cascaded_shadow_map.frag +++ /dev/null @@ -1,44 +0,0 @@ -#version 120 - -// -// metavoxel_heightfield_light_cascaded_shadow_map.frag -// fragment shader -// -// Created by Andrzej Kapolka on 8/20/14. -// Copyright 2014 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// the shadow texture -uniform sampler2DShadow shadowMap; - -// the distances to the cascade sections -uniform vec3 shadowDistances; - -// the inverse of the size of the shadow map -const float shadowScale = 1.0 / 2048.0; - -// the interpolated position -varying vec4 position; - -// the interpolated normal -varying vec4 normal; - -void main(void) { - // compute the index of the cascade to use and the corresponding texture coordinates - int shadowIndex = int(dot(step(vec3(position.z), shadowDistances), vec3(1.0, 1.0, 1.0))); - vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[shadowIndex], position), dot(gl_EyePlaneT[shadowIndex], position), - dot(gl_EyePlaneR[shadowIndex], position)); - - // compute the base color based on OpenGL lighting model - float diffuse = dot(normalize(normal), gl_LightSource[0].position); - float facingLight = step(0.0, diffuse) * 0.25 * - (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + - shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, shadowScale, 0.0)).r + - shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + - shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); - gl_FragColor = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); -} diff --git a/interface/resources/shaders/metavoxel_heightfield_light_shadow_map.frag b/interface/resources/shaders/metavoxel_heightfield_light_shadow_map.frag deleted file mode 100644 index 4f2df8958b..0000000000 --- a/interface/resources/shaders/metavoxel_heightfield_light_shadow_map.frag +++ /dev/null @@ -1,33 +0,0 @@ -#version 120 - -// -// metavoxel_heightfield_light_shadow_map.frag -// fragment shader -// -// Created by Andrzej Kapolka on 8/20/14. -// Copyright 2014 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// the shadow texture -uniform sampler2DShadow shadowMap; - -// the inverse of the size of the shadow map -const float shadowScale = 1.0 / 2048.0; - -// the interpolated normal -varying vec4 normal; - -void main(void) { - // compute the base color based on OpenGL lighting model - float diffuse = dot(normalize(normal), gl_LightSource[0].position); - float facingLight = step(0.0, diffuse) * 0.25 * - (shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, -shadowScale, 0.0)).r + - shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, shadowScale, 0.0)).r + - shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, -shadowScale, 0.0)).r + - shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, shadowScale, 0.0)).r); - gl_FragColor = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); -} diff --git a/interface/resources/shaders/metavoxel_heightfield_shadow_map.frag b/interface/resources/shaders/metavoxel_heightfield_shadow_map.frag deleted file mode 100644 index bf319462d3..0000000000 --- a/interface/resources/shaders/metavoxel_heightfield_shadow_map.frag +++ /dev/null @@ -1,37 +0,0 @@ -#version 120 - -// -// metavoxel_heightfield.frag -// fragment shader -// -// Created by Andrzej Kapolka on 7/28/14. -// Copyright 2014 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// the diffuse texture -uniform sampler2D diffuseMap; - -// the shadow texture -uniform sampler2DShadow shadowMap; - -// the inverse of the size of the shadow map -const float shadowScale = 1.0 / 2048.0; - -// the interpolated normal -varying vec4 normal; - -void main(void) { - // compute the base color based on OpenGL lighting model - float diffuse = dot(normalize(normal), gl_LightSource[0].position); - float facingLight = step(0.0, diffuse) * 0.25 * - (shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, -shadowScale, 0.0)).r + - shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, shadowScale, 0.0)).r + - shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, -shadowScale, 0.0)).r + - shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, shadowScale, 0.0)).r); - vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); - gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st); -} diff --git a/interface/resources/shaders/metavoxel_voxel_base.frag b/interface/resources/shaders/metavoxel_voxel_base.frag new file mode 100644 index 0000000000..72eca6b25d --- /dev/null +++ b/interface/resources/shaders/metavoxel_voxel_base.frag @@ -0,0 +1,21 @@ +#version 120 + +// +// metavoxel_voxel_base.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/4/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the interpolated normal +varying vec4 normal; + +void main(void) { + // store the interpolated color and normal + gl_FragData[0] = gl_Color; + gl_FragData[1] = normalize(normal) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); +} diff --git a/interface/resources/shaders/metavoxel_voxel_base.vert b/interface/resources/shaders/metavoxel_voxel_base.vert new file mode 100644 index 0000000000..4fe5802462 --- /dev/null +++ b/interface/resources/shaders/metavoxel_voxel_base.vert @@ -0,0 +1,26 @@ +#version 120 + +// +// metavoxel_voxel_base.vert +// vertex shader +// +// Created by Andrzej Kapolka on 9/4/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the interpolated normal +varying vec4 normal; + +void main(void) { + // transform and store the normal for interpolation + normal = vec4(normalize(gl_NormalMatrix * gl_Normal), 0.0); + + // use the fixed-function position + gl_Position = ftransform(); + + // copy the color for interpolation + gl_FrontColor = vec4(gl_Color.rgb, 0.0); +} diff --git a/interface/resources/shaders/metavoxel_voxel_splat.frag b/interface/resources/shaders/metavoxel_voxel_splat.frag new file mode 100644 index 0000000000..66ce2721ac --- /dev/null +++ b/interface/resources/shaders/metavoxel_voxel_splat.frag @@ -0,0 +1,29 @@ +#version 120 + +// +// metavoxel_voxel_splat.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/4/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the number of splats per pass +const int SPLAT_COUNT = 4; + +// the splat textures +uniform sampler2D diffuseMaps[SPLAT_COUNT]; + +// alpha values for the four splat textures +varying vec4 alphaValues; + +void main(void) { + // blend the splat textures + gl_FragColor = (texture2D(diffuseMaps[0], gl_TexCoord[0].st) * alphaValues.x + + texture2D(diffuseMaps[1], gl_TexCoord[1].st) * alphaValues.y + + texture2D(diffuseMaps[2], gl_TexCoord[2].st) * alphaValues.z + + texture2D(diffuseMaps[3], gl_TexCoord[3].st) * alphaValues.w); +} diff --git a/interface/resources/shaders/metavoxel_voxel_splat.vert b/interface/resources/shaders/metavoxel_voxel_splat.vert new file mode 100644 index 0000000000..150a9e7d2e --- /dev/null +++ b/interface/resources/shaders/metavoxel_voxel_splat.vert @@ -0,0 +1,62 @@ +#version 120 + +// +// metavoxel_voxel_splat.vert +// vertex shader +// +// Created by Andrzej Kapolka on 9/4/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the splat textures scales on the S axis +uniform vec4 splatTextureScalesS; + +// the splat texture scales on the T axis +uniform vec4 splatTextureScalesT; + +// the lower bounds of the values corresponding to the splat textures +uniform vec4 textureValueMinima; + +// the upper bounds of the values corresponding to the splat textures +uniform vec4 textureValueMaxima; + +// the materials to apply to the vertex +attribute vec4 materials; + +// the weights of each material +attribute vec4 materialWeights; + +// alpha values for the four splat textures +varying vec4 alphaValues; + +void main(void) { + // use the fixed-function position + gl_Position = ftransform(); + + // pass along the scaled/offset texture coordinates + vec4 textureSpacePosition = vec4(gl_Vertex.xz, 0.0, 1.0); + gl_TexCoord[0] = textureSpacePosition * vec4(splatTextureScalesS[0], splatTextureScalesT[0], 0.0, 1.0); + gl_TexCoord[1] = textureSpacePosition * vec4(splatTextureScalesS[1], splatTextureScalesT[1], 0.0, 1.0); + gl_TexCoord[2] = textureSpacePosition * vec4(splatTextureScalesS[2], splatTextureScalesT[2], 0.0, 1.0); + gl_TexCoord[3] = textureSpacePosition * vec4(splatTextureScalesS[3], splatTextureScalesT[3], 0.0, 1.0); + + // compute the alpha values for each texture + float value = materials[0]; + vec4 valueVector = vec4(value, value, value, value); + alphaValues = step(textureValueMinima, valueVector) * step(valueVector, textureValueMaxima) * materialWeights[0]; + + value = materials[1]; + valueVector = vec4(value, value, value, value); + alphaValues += step(textureValueMinima, valueVector) * step(valueVector, textureValueMaxima) * materialWeights[1]; + + value = materials[2]; + valueVector = vec4(value, value, value, value); + alphaValues += step(textureValueMinima, valueVector) * step(valueVector, textureValueMaxima) * materialWeights[2]; + + value = materials[3]; + valueVector = vec4(value, value, value, value); + alphaValues += step(textureValueMinima, valueVector) * step(valueVector, textureValueMaxima) * materialWeights[3]; +} diff --git a/interface/src/MetavoxelSystem.cpp b/interface/src/MetavoxelSystem.cpp index 85287d03b5..892efe7211 100644 --- a/interface/src/MetavoxelSystem.cpp +++ b/interface/src/MetavoxelSystem.cpp @@ -9,7 +9,11 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// include this before QOpenGLFramebufferObject, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + #include +#include #include #include #include @@ -24,6 +28,7 @@ #include "Application.h" #include "MetavoxelSystem.h" #include "renderer/Model.h" +#include "renderer/RenderUtil.h" REGISTER_META_OBJECT(DefaultMetavoxelRendererImplementation) REGISTER_META_OBJECT(SphereRenderer) @@ -34,12 +39,24 @@ static int bufferPointVectorMetaTypeId = qRegisterMetaType(); void MetavoxelSystem::init() { MetavoxelClientManager::init(); DefaultMetavoxelRendererImplementation::init(); - _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new BufferDataAttribute("pointBuffer")); - _heightfieldBufferAttribute = AttributeRegistry::getInstance()->registerAttribute( - new BufferDataAttribute("heightfieldBuffer")); + _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new BufferDataAttribute("pointBuffer")); + + _heightfieldBufferAttribute = AttributeRegistry::getInstance()->registerAttribute( + new BufferDataAttribute("heightfieldBuffer")); _heightfieldBufferAttribute->setLODThresholdMultiplier( AttributeRegistry::getInstance()->getHeightfieldAttribute()->getLODThresholdMultiplier()); + + _voxelBufferAttribute = AttributeRegistry::getInstance()->registerAttribute( + new BufferDataAttribute("voxelBuffer")); + _voxelBufferAttribute->setLODThresholdMultiplier( + AttributeRegistry::getInstance()->getVoxelColorAttribute()->getLODThresholdMultiplier()); + + loadLightProgram("shaders/directional_light.frag", _directionalLight, _directionalLightLocations); + loadLightProgram("shaders/directional_light_shadow_map.frag", _directionalLightShadowMap, + _directionalLightShadowMapLocations); + loadLightProgram("shaders/directional_light_cascaded_shadow_map.frag", _directionalLightCascadedShadowMap, + _directionalLightCascadedShadowMapLocations); } MetavoxelLOD MetavoxelSystem::getLOD() { @@ -110,6 +127,10 @@ int RenderVisitor::visit(MetavoxelInfo& info) { return STOP_RECURSION; } +const GLenum COLOR_DRAW_BUFFERS[] = { GL_COLOR_ATTACHMENT0 }; +const GLenum NORMAL_DRAW_BUFFERS[] = { GL_COLOR_ATTACHMENT1 }; +const GLenum COLOR_NORMAL_DRAW_BUFFERS[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }; + void MetavoxelSystem::render() { // update the frustum ViewFrustum* viewFrustum = Application::getInstance()->getViewFrustum(); @@ -117,8 +138,128 @@ void MetavoxelSystem::render() { viewFrustum->getFarBottomRight(), viewFrustum->getNearTopLeft(), viewFrustum->getNearTopRight(), viewFrustum->getNearBottomLeft(), viewFrustum->getNearBottomRight()); + _needToLight = false; + + // clear the normal buffer + glDrawBuffers(sizeof(NORMAL_DRAW_BUFFERS) / sizeof(NORMAL_DRAW_BUFFERS[0]), NORMAL_DRAW_BUFFERS); + glClear(GL_COLOR_BUFFER_BIT); + + glDrawBuffers(sizeof(COLOR_DRAW_BUFFERS) / sizeof(COLOR_DRAW_BUFFERS[0]), COLOR_DRAW_BUFFERS); + RenderVisitor renderVisitor(getLOD()); guideToAugmented(renderVisitor, true); + + // give external parties a chance to join in + emit rendering(); + + if (!_needToLight) { + return; // skip lighting if not needed + } + + // perform deferred lighting, rendering to free fbo + glPushMatrix(); + glLoadIdentity(); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + glDisable(GL_BLEND); + glDisable(GL_LIGHTING); + glDisable(GL_DEPTH_TEST); + glDepthMask(false); + + QOpenGLFramebufferObject* primaryFBO = Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject(); + primaryFBO->release(); + + QOpenGLFramebufferObject* freeFBO = Application::getInstance()->getGlowEffect()->getFreeFramebufferObject(); + freeFBO->bind(); + glClear(GL_COLOR_BUFFER_BIT); + + glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPrimaryNormalTextureID()); + + if (Menu::getInstance()->getShadowsEnabled()) { + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPrimaryDepthTextureID()); + + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getShadowDepthTextureID()); + + ProgramObject* program = &_directionalLightShadowMap; + const LightLocations* locations = &_directionalLightShadowMapLocations; + if (Menu::getInstance()->isOptionChecked(MenuOption::CascadedShadows)) { + program = &_directionalLightCascadedShadowMap; + locations = &_directionalLightCascadedShadowMapLocations; + _directionalLightCascadedShadowMap.bind(); + _directionalLightCascadedShadowMap.setUniform(locations->shadowDistances, + Application::getInstance()->getShadowDistances()); + + } else { + program->bind(); + } + program->setUniformValue(locations->shadowScale, + 1.0f / Application::getInstance()->getTextureCache()->getShadowFramebufferObject()->width()); + + float left, right, bottom, top, nearVal, farVal; + glm::vec4 nearClipPlane, farClipPlane; + Application::getInstance()->computeOffAxisFrustum( + left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); + program->setUniformValue(locations->nearLocation, nearVal); + program->setUniformValue(locations->depthScale, (farVal - nearVal) / farVal); + float nearScale = -1.0f / nearVal; + program->setUniformValue(locations->depthTexCoordOffset, left * nearScale, bottom * nearScale); + program->setUniformValue(locations->depthTexCoordScale, (right - left) * nearScale, (top - bottom) * nearScale); + + renderFullscreenQuad(); + + program->release(); + + glBindTexture(GL_TEXTURE_2D, 0); + + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, 0); + + glActiveTexture(GL_TEXTURE1); + + } else { + _directionalLight.bind(); + renderFullscreenQuad(); + _directionalLight.release(); + } + + glBindTexture(GL_TEXTURE_2D, 0); + glActiveTexture(GL_TEXTURE0); + + freeFBO->release(); + + // now transfer the lit region to the primary fbo + glEnable(GL_BLEND); + + primaryFBO->bind(); + + glBindTexture(GL_TEXTURE_2D, freeFBO->texture()); + glEnable(GL_TEXTURE_2D); + + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + + renderFullscreenQuad(); + + glBindTexture(GL_TEXTURE_2D, 0); + glDisable(GL_TEXTURE_2D); + + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); + glDepthMask(true); + + glDisable(GL_ALPHA_TEST); + + glPopMatrix(); + + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); } class RayHeightfieldIntersectionVisitor : public RayIntersectionVisitor { @@ -138,7 +279,8 @@ RayHeightfieldIntersectionVisitor::RayHeightfieldIntersectionVisitor(const glm:: intersectionDistance(FLT_MAX) { } -static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / 255.0f; +static const int EIGHT_BIT_MAXIMUM = 255; +static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / EIGHT_BIT_MAXIMUM; int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance) { if (!info.isLeaf) { @@ -477,6 +619,24 @@ void MetavoxelSystem::guideToAugmented(MetavoxelVisitor& visitor, bool render) { } } +void MetavoxelSystem::loadLightProgram(const char* name, ProgramObject& program, LightLocations& locations) { + program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + name); + program.link(); + + program.bind(); + program.setUniformValue("diffuseMap", 0); + program.setUniformValue("normalMap", 1); + program.setUniformValue("depthMap", 2); + program.setUniformValue("shadowMap", 3); + locations.shadowDistances = program.uniformLocation("shadowDistances"); + locations.shadowScale = program.uniformLocation("shadowScale"); + locations.nearLocation = program.uniformLocation("near"); + locations.depthScale = program.uniformLocation("depthScale"); + locations.depthTexCoordOffset = program.uniformLocation("depthTexCoordOffset"); + locations.depthTexCoordScale = program.uniformLocation("depthTexCoordScale"); + program.release(); +} + MetavoxelSystemClient::MetavoxelSystemClient(const SharedNodePointer& node, MetavoxelUpdater* updater) : MetavoxelClient(node, updater) { } @@ -608,22 +768,22 @@ const int HeightfieldBuffer::SHARED_EDGE = 1; const int HeightfieldBuffer::HEIGHT_EXTENSION = 2 * HeightfieldBuffer::HEIGHT_BORDER + HeightfieldBuffer::SHARED_EDGE; HeightfieldBuffer::HeightfieldBuffer(const glm::vec3& translation, float scale, - const QByteArray& height, const QByteArray& color, const QByteArray& texture, - const QVector& textures) : + const QByteArray& height, const QByteArray& color, const QByteArray& material, + const QVector& materials) : _translation(translation), _scale(scale), _heightBounds(translation, translation + glm::vec3(scale, scale, scale)), _colorBounds(_heightBounds), _height(height), _color(color), - _texture(texture), - _textures(textures), + _material(material), + _materials(materials), _heightTextureID(0), _colorTextureID(0), - _textureTextureID(0), + _materialTextureID(0), _heightSize(glm::sqrt(height.size())), _heightIncrement(scale / (_heightSize - HEIGHT_EXTENSION)), - _colorSize(glm::sqrt(color.size() / HeightfieldData::COLOR_BYTES)), + _colorSize(glm::sqrt(color.size() / DataBlock::COLOR_BYTES)), _colorIncrement(scale / (_colorSize - SHARED_EDGE)) { _heightBounds.minimum.x -= _heightIncrement * HEIGHT_BORDER; @@ -639,11 +799,11 @@ HeightfieldBuffer::~HeightfieldBuffer() { // the textures have to be deleted on the main thread (for its opengl context) if (QThread::currentThread() != Application::getInstance()->thread()) { QMetaObject::invokeMethod(Application::getInstance()->getMetavoxels(), "deleteTextures", - Q_ARG(int, _heightTextureID), Q_ARG(int, _colorTextureID), Q_ARG(int, _textureTextureID)); + Q_ARG(int, _heightTextureID), Q_ARG(int, _colorTextureID), Q_ARG(int, _materialTextureID)); } else { glDeleteTextures(1, &_heightTextureID); glDeleteTextures(1, &_colorTextureID); - glDeleteTextures(1, &_textureTextureID); + glDeleteTextures(1, &_materialTextureID); } } @@ -660,13 +820,13 @@ QByteArray HeightfieldBuffer::getUnextendedHeight() const { } QByteArray HeightfieldBuffer::getUnextendedColor() const { - int srcSize = glm::sqrt(_color.size() / HeightfieldData::COLOR_BYTES); + int srcSize = glm::sqrt(_color.size() / DataBlock::COLOR_BYTES); int destSize = srcSize - 1; - QByteArray unextended(destSize * destSize * HeightfieldData::COLOR_BYTES, 0); + QByteArray unextended(destSize * destSize * DataBlock::COLOR_BYTES, 0); const char* src = _color.constData(); - int srcStride = srcSize * HeightfieldData::COLOR_BYTES; + int srcStride = srcSize * DataBlock::COLOR_BYTES; char* dest = unextended.data(); - int destStride = destSize * HeightfieldData::COLOR_BYTES; + int destStride = destSize * DataBlock::COLOR_BYTES; for (int z = 0; z < destSize; z++, src += srcStride, dest += destStride) { memcpy(dest, src, destStride); } @@ -705,27 +865,27 @@ void HeightfieldBuffer::render(bool cursor) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR); } else { - int colorSize = glm::sqrt(_color.size() / HeightfieldData::COLOR_BYTES); + int colorSize = glm::sqrt(_color.size() / DataBlock::COLOR_BYTES); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, colorSize, colorSize, 0, GL_RGB, GL_UNSIGNED_BYTE, _color.constData()); } - if (!_texture.isEmpty()) { - glGenTextures(1, &_textureTextureID); - glBindTexture(GL_TEXTURE_2D, _textureTextureID); + if (!_material.isEmpty()) { + glGenTextures(1, &_materialTextureID); + glBindTexture(GL_TEXTURE_2D, _materialTextureID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - int textureSize = glm::sqrt(_texture.size()); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, textureSize, textureSize, 0, - GL_LUMINANCE, GL_UNSIGNED_BYTE, _texture.constData()); + int materialSize = glm::sqrt(_material.size()); + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, materialSize, materialSize, 0, + GL_LUMINANCE, GL_UNSIGNED_BYTE, _material.constData()); - _networkTextures.resize(_textures.size()); - for (int i = 0; i < _textures.size(); i++) { - const SharedObjectPointer texture = _textures.at(i); - if (texture) { + _networkTextures.resize(_materials.size()); + for (int i = 0; i < _materials.size(); i++) { + const SharedObjectPointer material = _materials.at(i); + if (material) { _networkTextures[i] = Application::getInstance()->getTextureCache()->getTexture( - static_cast(texture.data())->getURL(), SPLAT_TEXTURE); + static_cast(material.data())->getDiffuse(), SPLAT_TEXTURE); } } } @@ -799,8 +959,7 @@ void HeightfieldBuffer::render(bool cursor) { if (cursor) { glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); - } else if (!_textures.isEmpty()) { - DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().bind(); + } else if (!_materials.isEmpty()) { DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue( DefaultMetavoxelRendererImplementation::getBaseHeightScaleLocation(), 1.0f / _heightSize); DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue( @@ -810,6 +969,8 @@ void HeightfieldBuffer::render(bool cursor) { glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); + glDrawBuffers(sizeof(COLOR_DRAW_BUFFERS) / sizeof(COLOR_DRAW_BUFFERS[0]), COLOR_DRAW_BUFFERS); + glDepthFunc(GL_LEQUAL); glDepthMask(false); glEnable(GL_BLEND); @@ -818,18 +979,18 @@ void HeightfieldBuffer::render(bool cursor) { glPolygonOffset(-1.0f, -1.0f); DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().bind(); + const DefaultMetavoxelRendererImplementation::SplatLocations& locations = + DefaultMetavoxelRendererImplementation::getSplatHeightfieldLocations(); DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getSplatHeightScaleLocation(), 1.0f / _heightSize); + locations.heightScale, 1.0f / _heightSize); DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getSplatTextureScaleLocation(), (float)_heightSize / innerSize); + locations.textureScale, (float)_heightSize / innerSize); DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getSplatTextureOffsetLocation(), - _translation.x / _scale, _translation.z / _scale); + locations.splatTextureOffset, _translation.x / _scale, _translation.z / _scale); - glBindTexture(GL_TEXTURE_2D, _textureTextureID); + glBindTexture(GL_TEXTURE_2D, _materialTextureID); - const int TEXTURES_PER_SPLAT = 4; - for (int i = 0; i < _textures.size(); i += TEXTURES_PER_SPLAT) { + for (int i = 0; i < _materials.size(); i += SPLAT_COUNT) { QVector4D scalesS, scalesT; for (int j = 0; j < SPLAT_COUNT; j++) { @@ -838,9 +999,9 @@ void HeightfieldBuffer::render(bool cursor) { if (index < _networkTextures.size()) { const NetworkTexturePointer& texture = _networkTextures.at(index); if (texture) { - HeightfieldTexture* heightfieldTexture = static_cast(_textures.at(index).data()); - scalesS[j] = _scale / heightfieldTexture->getScaleS(); - scalesT[j] = _scale / heightfieldTexture->getScaleT(); + MaterialObject* material = static_cast(_materials.at(index).data()); + scalesS[j] = _scale / material->getScaleS(); + scalesT[j] = _scale / material->getScaleT(); glBindTexture(GL_TEXTURE_2D, texture->getID()); } else { glBindTexture(GL_TEXTURE_2D, 0); @@ -851,23 +1012,20 @@ void HeightfieldBuffer::render(bool cursor) { } const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL; DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getSplatTextureScalesSLocation(), scalesS); + locations.splatTextureScalesS, scalesS); DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getSplatTextureScalesTLocation(), scalesT); + locations.splatTextureScalesT, scalesT); DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getSplatTextureValueMinimaLocation(), + locations.textureValueMinima, (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP); DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getSplatTextureValueMaximaLocation(), + locations.textureValueMaxima, (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP); glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); } - glEnable(GL_ALPHA_TEST); - glBlendFunc(GL_DST_COLOR, GL_ZERO); - for (int i = 0; i < SPLAT_COUNT; i++) { glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]); glBindTexture(GL_TEXTURE_2D, 0); @@ -875,53 +1033,24 @@ void HeightfieldBuffer::render(bool cursor) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); - - if (Menu::getInstance()->isOptionChecked(MenuOption::SimpleShadows)) { - DefaultMetavoxelRendererImplementation::getShadowLightHeightfieldProgram().bind(); - DefaultMetavoxelRendererImplementation::getShadowLightHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getShadowLightHeightScaleLocation(), 1.0f / _heightSize); - glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); - DefaultMetavoxelRendererImplementation::getShadowMapHeightfieldProgram().bind(); - - } else if (Menu::getInstance()->isOptionChecked(MenuOption::CascadedShadows)) { - DefaultMetavoxelRendererImplementation::getCascadedShadowLightHeightfieldProgram().bind(); - DefaultMetavoxelRendererImplementation::getCascadedShadowLightHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getCascadedShadowLightHeightScaleLocation(), 1.0f / _heightSize); - glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); - DefaultMetavoxelRendererImplementation::getCascadedShadowMapHeightfieldProgram().bind(); - - } else { - DefaultMetavoxelRendererImplementation::getLightHeightfieldProgram().bind(); - DefaultMetavoxelRendererImplementation::getLightHeightfieldProgram().setUniformValue( - DefaultMetavoxelRendererImplementation::getLightHeightScaleLocation(), 1.0f / _heightSize); - glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); - DefaultMetavoxelRendererImplementation::getHeightfieldProgram().bind(); - } - - glDisable(GL_POLYGON_OFFSET_FILL); - glDisable(GL_BLEND); - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); - glDepthFunc(GL_LESS); - glDepthMask(true); glActiveTexture(GL_TEXTURE0); + glDisable(GL_POLYGON_OFFSET_FILL); + glEnable(GL_ALPHA_TEST); + glDisable(GL_BLEND); + glDepthMask(true); + glDepthFunc(GL_LESS); + + glDrawBuffers(sizeof(COLOR_NORMAL_DRAW_BUFFERS) / sizeof(COLOR_NORMAL_DRAW_BUFFERS[0]), COLOR_NORMAL_DRAW_BUFFERS); + + DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().bind(); + } else { - int heightScaleLocation = DefaultMetavoxelRendererImplementation::getHeightScaleLocation(); - int colorScaleLocation = DefaultMetavoxelRendererImplementation::getColorScaleLocation(); - ProgramObject* program = &DefaultMetavoxelRendererImplementation::getHeightfieldProgram(); - if (Menu::getInstance()->isOptionChecked(MenuOption::SimpleShadows)) { - heightScaleLocation = DefaultMetavoxelRendererImplementation::getShadowMapHeightScaleLocation(); - colorScaleLocation = DefaultMetavoxelRendererImplementation::getShadowMapColorScaleLocation(); - program = &DefaultMetavoxelRendererImplementation::getShadowMapHeightfieldProgram(); - - } else if (Menu::getInstance()->isOptionChecked(MenuOption::CascadedShadows)) { - heightScaleLocation = DefaultMetavoxelRendererImplementation::getCascadedShadowMapHeightScaleLocation(); - colorScaleLocation = DefaultMetavoxelRendererImplementation::getCascadedShadowMapColorScaleLocation(); - program = &DefaultMetavoxelRendererImplementation::getCascadedShadowMapHeightfieldProgram(); - } - program->setUniformValue(heightScaleLocation, 1.0f / _heightSize); - program->setUniformValue(colorScaleLocation, (float)_heightSize / innerSize); + DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue( + DefaultMetavoxelRendererImplementation::getBaseHeightScaleLocation(), 1.0f / _heightSize); + DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue( + DefaultMetavoxelRendererImplementation::getBaseColorScaleLocation(), (float)_heightSize / innerSize); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, _colorTextureID); @@ -937,11 +1066,15 @@ void HeightfieldBuffer::render(bool cursor) { bufferPair.first.release(); bufferPair.second.release(); + + Application::getInstance()->getMetavoxels()->noteNeedToLight(); } QHash HeightfieldBuffer::_bufferPairs; void HeightfieldPreview::render(const glm::vec3& translation, float scale) const { + glDrawBuffers(sizeof(COLOR_NORMAL_DRAW_BUFFERS) / sizeof(COLOR_NORMAL_DRAW_BUFFERS[0]), COLOR_NORMAL_DRAW_BUFFERS); + glDisable(GL_BLEND); glEnable(GL_CULL_FACE); glEnable(GL_ALPHA_TEST); @@ -952,7 +1085,7 @@ void HeightfieldPreview::render(const glm::vec3& translation, float scale) const glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); - DefaultMetavoxelRendererImplementation::getHeightfieldProgram().bind(); + DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().bind(); glPushMatrix(); glTranslatef(translation.x, translation.y, translation.z); @@ -964,7 +1097,7 @@ void HeightfieldPreview::render(const glm::vec3& translation, float scale) const glPopMatrix(); - DefaultMetavoxelRendererImplementation::getHeightfieldProgram().release(); + DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().release(); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); @@ -972,6 +1105,137 @@ void HeightfieldPreview::render(const glm::vec3& translation, float scale) const glDisable(GL_ALPHA_TEST); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); + + glDrawBuffers(sizeof(COLOR_DRAW_BUFFERS) / sizeof(COLOR_DRAW_BUFFERS[0]), COLOR_DRAW_BUFFERS); +} + +VoxelBuffer::VoxelBuffer(const QVector& vertices, const QVector& indices, + const QVector& materials) : + _vertices(vertices), + _indices(indices), + _vertexCount(vertices.size()), + _indexCount(indices.size()), + _indexBuffer(QOpenGLBuffer::IndexBuffer), + _materials(materials) { +} + +void VoxelBuffer::render(bool cursor) { + if (!_vertexBuffer.isCreated()) { + _vertexBuffer.create(); + _vertexBuffer.bind(); + _vertexBuffer.allocate(_vertices.constData(), _vertices.size() * sizeof(VoxelPoint)); + _vertices.clear(); + + _indexBuffer.create(); + _indexBuffer.bind(); + _indexBuffer.allocate(_indices.constData(), _indices.size() * sizeof(int)); + _indices.clear(); + + if (!_materials.isEmpty()) { + _networkTextures.resize(_materials.size()); + for (int i = 0; i < _materials.size(); i++) { + const SharedObjectPointer material = _materials.at(i); + if (material) { + _networkTextures[i] = Application::getInstance()->getTextureCache()->getTexture( + static_cast(material.data())->getDiffuse(), SPLAT_TEXTURE); + } + } + } + } else { + _vertexBuffer.bind(); + _indexBuffer.bind(); + } + + VoxelPoint* point = 0; + glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex); + glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color); + glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal); + + glDrawRangeElements(GL_QUADS, 0, _vertexCount - 1, _indexCount, GL_UNSIGNED_INT, 0); + + if (!_materials.isEmpty()) { + glDrawBuffers(sizeof(COLOR_DRAW_BUFFERS) / sizeof(COLOR_DRAW_BUFFERS[0]), COLOR_DRAW_BUFFERS); + + glDepthFunc(GL_LEQUAL); + glDepthMask(false); + glEnable(GL_BLEND); + glDisable(GL_ALPHA_TEST); + glEnable(GL_POLYGON_OFFSET_FILL); + glPolygonOffset(-1.0f, -1.0f); + + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().bind(); + const DefaultMetavoxelRendererImplementation::SplatLocations& locations = + DefaultMetavoxelRendererImplementation::getSplatVoxelLocations(); + + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setAttributeBuffer(locations.materials, + GL_UNSIGNED_BYTE, (qint64)&point->materials, SPLAT_COUNT, sizeof(VoxelPoint)); + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().enableAttributeArray(locations.materials); + + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setAttributeBuffer(locations.materialWeights, + GL_UNSIGNED_BYTE, (qint64)&point->materialWeights, SPLAT_COUNT, sizeof(VoxelPoint)); + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().enableAttributeArray(locations.materialWeights); + + for (int i = 0; i < _materials.size(); i += SPLAT_COUNT) { + QVector4D scalesS, scalesT; + + for (int j = 0; j < SPLAT_COUNT; j++) { + glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[j]); + int index = i + j; + if (index < _networkTextures.size()) { + const NetworkTexturePointer& texture = _networkTextures.at(index); + if (texture) { + MaterialObject* material = static_cast(_materials.at(index).data()); + scalesS[j] = 1.0f / material->getScaleS(); + scalesT[j] = 1.0f / material->getScaleT(); + glBindTexture(GL_TEXTURE_2D, texture->getID()); + } else { + glBindTexture(GL_TEXTURE_2D, 0); + } + } else { + glBindTexture(GL_TEXTURE_2D, 0); + } + } + const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL; + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue( + locations.splatTextureScalesS, scalesS); + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue( + locations.splatTextureScalesT, scalesT); + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue( + locations.textureValueMinima, + (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, + (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP); + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue( + locations.textureValueMaxima, + (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, + (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP); + glDrawRangeElements(GL_QUADS, 0, _vertexCount - 1, _indexCount, GL_UNSIGNED_INT, 0); + } + + for (int i = 0; i < SPLAT_COUNT; i++) { + glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]); + glBindTexture(GL_TEXTURE_2D, 0); + } + + glActiveTexture(GL_TEXTURE0); + + glDisable(GL_POLYGON_OFFSET_FILL); + glEnable(GL_ALPHA_TEST); + glDisable(GL_BLEND); + glDepthMask(true); + glDepthFunc(GL_LESS); + + glDrawBuffers(sizeof(COLOR_NORMAL_DRAW_BUFFERS) / sizeof(COLOR_NORMAL_DRAW_BUFFERS[0]), COLOR_NORMAL_DRAW_BUFFERS); + + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().disableAttributeArray(locations.materials); + DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().disableAttributeArray(locations.materialWeights); + + DefaultMetavoxelRendererImplementation::getBaseVoxelProgram().bind(); + } + + _vertexBuffer.release(); + _indexBuffer.release(); + + Application::getInstance()->getMetavoxels()->noteNeedToLight(); } BufferDataAttribute::BufferDataAttribute(const QString& name) : @@ -1001,48 +1265,6 @@ void DefaultMetavoxelRendererImplementation::init() { _pointScaleLocation = _pointProgram.uniformLocation("pointScale"); _pointProgram.release(); - _heightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + - "shaders/metavoxel_heightfield.vert"); - _heightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + - "shaders/metavoxel_heightfield.frag"); - _heightfieldProgram.link(); - - _heightfieldProgram.bind(); - _heightfieldProgram.setUniformValue("heightMap", 0); - _heightfieldProgram.setUniformValue("diffuseMap", 1); - _heightScaleLocation = _heightfieldProgram.uniformLocation("heightScale"); - _colorScaleLocation = _heightfieldProgram.uniformLocation("colorScale"); - _heightfieldProgram.release(); - - _shadowMapHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + - "shaders/metavoxel_heightfield.vert"); - _shadowMapHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + - "shaders/metavoxel_heightfield_shadow_map.frag"); - _shadowMapHeightfieldProgram.link(); - - _shadowMapHeightfieldProgram.bind(); - _shadowMapHeightfieldProgram.setUniformValue("heightMap", 0); - _shadowMapHeightfieldProgram.setUniformValue("diffuseMap", 1); - _shadowMapHeightfieldProgram.setUniformValue("shadowMap", 2); - _shadowMapHeightScaleLocation = _shadowMapHeightfieldProgram.uniformLocation("heightScale"); - _shadowMapColorScaleLocation = _shadowMapHeightfieldProgram.uniformLocation("colorScale"); - _shadowMapHeightfieldProgram.release(); - - _cascadedShadowMapHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + - "shaders/metavoxel_heightfield.vert"); - _cascadedShadowMapHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + - "shaders/metavoxel_heightfield_cascaded_shadow_map.frag"); - _cascadedShadowMapHeightfieldProgram.link(); - - _cascadedShadowMapHeightfieldProgram.bind(); - _cascadedShadowMapHeightfieldProgram.setUniformValue("heightMap", 0); - _cascadedShadowMapHeightfieldProgram.setUniformValue("diffuseMap", 1); - _cascadedShadowMapHeightfieldProgram.setUniformValue("shadowMap", 2); - _cascadedShadowMapHeightScaleLocation = _cascadedShadowMapHeightfieldProgram.uniformLocation("heightScale"); - _cascadedShadowMapColorScaleLocation = _cascadedShadowMapHeightfieldProgram.uniformLocation("colorScale"); - _shadowDistancesLocation = _cascadedShadowMapHeightfieldProgram.uniformLocation("shadowDistances"); - _cascadedShadowMapHeightfieldProgram.release(); - _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_heightfield_base.vert"); _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + @@ -1056,60 +1278,7 @@ void DefaultMetavoxelRendererImplementation::init() { _baseColorScaleLocation = _baseHeightfieldProgram.uniformLocation("colorScale"); _baseHeightfieldProgram.release(); - _splatHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + - "shaders/metavoxel_heightfield_splat.vert"); - _splatHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + - "shaders/metavoxel_heightfield_splat.frag"); - _splatHeightfieldProgram.link(); - - _splatHeightfieldProgram.bind(); - _splatHeightfieldProgram.setUniformValue("heightMap", 0); - _splatHeightfieldProgram.setUniformValue("textureMap", 1); - _splatHeightfieldProgram.setUniformValueArray("diffuseMaps", SPLAT_TEXTURE_UNITS, SPLAT_COUNT); - _splatHeightScaleLocation = _splatHeightfieldProgram.uniformLocation("heightScale"); - _splatTextureScaleLocation = _splatHeightfieldProgram.uniformLocation("textureScale"); - _splatTextureOffsetLocation = _splatHeightfieldProgram.uniformLocation("splatTextureOffset"); - _splatTextureScalesSLocation = _splatHeightfieldProgram.uniformLocation("splatTextureScalesS"); - _splatTextureScalesTLocation = _splatHeightfieldProgram.uniformLocation("splatTextureScalesT"); - _splatTextureValueMinimaLocation = _splatHeightfieldProgram.uniformLocation("textureValueMinima"); - _splatTextureValueMaximaLocation = _splatHeightfieldProgram.uniformLocation("textureValueMaxima"); - _splatHeightfieldProgram.release(); - - _lightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + - "shaders/metavoxel_heightfield_light.vert"); - _lightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + - "shaders/metavoxel_heightfield_light.frag"); - _lightHeightfieldProgram.link(); - - _lightHeightfieldProgram.bind(); - _lightHeightfieldProgram.setUniformValue("heightMap", 0); - _lightHeightScaleLocation = _lightHeightfieldProgram.uniformLocation("heightScale"); - _lightHeightfieldProgram.release(); - - _shadowLightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + - "shaders/metavoxel_heightfield_light.vert"); - _shadowLightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + - "shaders/metavoxel_heightfield_light_shadow_map.frag"); - _shadowLightHeightfieldProgram.link(); - - _shadowLightHeightfieldProgram.bind(); - _shadowLightHeightfieldProgram.setUniformValue("heightMap", 0); - _shadowLightHeightfieldProgram.setUniformValue("shadowMap", 2); - _shadowLightHeightScaleLocation = _shadowLightHeightfieldProgram.uniformLocation("heightScale"); - _shadowLightHeightfieldProgram.release(); - - _cascadedShadowLightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + - "shaders/metavoxel_heightfield_light.vert"); - _cascadedShadowLightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + - "shaders/metavoxel_heightfield_light_cascaded_shadow_map.frag"); - _cascadedShadowLightHeightfieldProgram.link(); - - _cascadedShadowLightHeightfieldProgram.bind(); - _cascadedShadowLightHeightfieldProgram.setUniformValue("heightMap", 0); - _cascadedShadowLightHeightfieldProgram.setUniformValue("shadowMap", 2); - _cascadedShadowLightHeightScaleLocation = _cascadedShadowLightHeightfieldProgram.uniformLocation("heightScale"); - _shadowLightDistancesLocation = _cascadedShadowLightHeightfieldProgram.uniformLocation("shadowDistances"); - _cascadedShadowLightHeightfieldProgram.release(); + loadSplatProgram("heightfield", _splatHeightfieldProgram, _splatHeightfieldLocations); _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_heightfield_cursor.vert"); @@ -1120,6 +1289,14 @@ void DefaultMetavoxelRendererImplementation::init() { _heightfieldCursorProgram.bind(); _heightfieldCursorProgram.setUniformValue("heightMap", 0); _heightfieldCursorProgram.release(); + + _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + + "shaders/metavoxel_voxel_base.vert"); + _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + + "shaders/metavoxel_voxel_base.frag"); + _baseVoxelProgram.link(); + + loadSplatProgram("voxel", _splatVoxelProgram, _splatVoxelLocations); } } @@ -1265,7 +1442,6 @@ int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) { shift++; size *= 2.0f; } - const int EIGHT_BIT_MAXIMUM = 255; int subtract = (_buffer->getTranslation().y - info.minimum.y) * EIGHT_BIT_MAXIMUM / _buffer->getScale(); for (int y = 0; y < destHeight; y++, dest += heightSize, srcY += srcAdvance) { const uchar* src = (const uchar*)srcHeight.constData() + (int)srcY * srcSize; @@ -1291,13 +1467,13 @@ int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) { destY = (overlap.minimum.z - colorBounds.minimum.z) / colorIncrement; destWidth = glm::ceil((overlap.maximum.x - overlap.minimum.x) / colorIncrement); destHeight = glm::ceil((overlap.maximum.z - overlap.minimum.z) / colorIncrement); - dest = _buffer->getColor().data() + (destY * colorSize + destX) * HeightfieldData::COLOR_BYTES; - int destStride = colorSize * HeightfieldData::COLOR_BYTES; - int destBytes = destWidth * HeightfieldData::COLOR_BYTES; + dest = _buffer->getColor().data() + (destY * colorSize + destX) * DataBlock::COLOR_BYTES; + int destStride = colorSize * DataBlock::COLOR_BYTES; + int destBytes = destWidth * DataBlock::COLOR_BYTES; const QByteArray& srcColor = color->getContents(); - srcSize = glm::sqrt(srcColor.size() / HeightfieldData::COLOR_BYTES); - int srcStride = srcSize * HeightfieldData::COLOR_BYTES; + srcSize = glm::sqrt(srcColor.size() / DataBlock::COLOR_BYTES); + int srcStride = srcSize * DataBlock::COLOR_BYTES; srcIncrement = info.size / srcSize; if (srcIncrement == colorIncrement) { @@ -1305,7 +1481,7 @@ int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) { int srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement; int srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement; - const char* src = srcColor.constData() + (srcY * srcSize + srcX) * HeightfieldData::COLOR_BYTES; + const char* src = srcColor.constData() + (srcY * srcSize + srcX) * DataBlock::COLOR_BYTES; for (int y = 0; y < destHeight; y++, src += srcStride, dest += destStride) { memcpy(dest, src, destBytes); } @@ -1317,9 +1493,9 @@ int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) { for (int y = 0; y < destHeight; y++, dest += destStride, srcY += srcAdvance) { const char* src = srcColor.constData() + (int)srcY * srcStride; float lineSrcX = srcX; - for (char* lineDest = dest, *end = dest + destBytes; lineDest != end; lineDest += HeightfieldData::COLOR_BYTES, + for (char* lineDest = dest, *end = dest + destBytes; lineDest != end; lineDest += DataBlock::COLOR_BYTES, lineSrcX += srcAdvance) { - const char* lineSrc = src + (int)lineSrcX * HeightfieldData::COLOR_BYTES; + const char* lineSrc = src + (int)lineSrcX * DataBlock::COLOR_BYTES; lineDest[0] = lineSrc[0]; lineDest[1] = lineSrc[1]; lineDest[2] = lineSrc[2]; @@ -1351,7 +1527,7 @@ private: HeightfieldRegionVisitor::HeightfieldRegionVisitor(const MetavoxelLOD& lod) : MetavoxelVisitor(QVector() << AttributeRegistry::getInstance()->getHeightfieldAttribute() << AttributeRegistry::getInstance()->getHeightfieldColorAttribute() << - AttributeRegistry::getInstance()->getHeightfieldTextureAttribute() << + AttributeRegistry::getInstance()->getHeightfieldMaterialAttribute() << Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), QVector() << Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod), regionBounds(glm::vec3(FLT_MAX, FLT_MAX, FLT_MAX), glm::vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX)), @@ -1374,17 +1550,17 @@ int HeightfieldRegionVisitor::visit(MetavoxelInfo& info) { int colorContentsSize = 0; if (color) { const QByteArray& colorContents = color->getContents(); - int colorSize = glm::sqrt(colorContents.size() / HeightfieldData::COLOR_BYTES); + int colorSize = glm::sqrt(colorContents.size() / DataBlock::COLOR_BYTES); int extendedColorSize = colorSize + HeightfieldBuffer::SHARED_EDGE; - colorContentsSize = extendedColorSize * extendedColorSize * HeightfieldData::COLOR_BYTES; + colorContentsSize = extendedColorSize * extendedColorSize * DataBlock::COLOR_BYTES; } - HeightfieldTextureDataPointer texture = info.inputValues.at(2).getInlineValue(); - QByteArray textureContents; - QVector textures; - if (texture) { - textureContents = texture->getContents(); - textures = texture->getTextures(); + HeightfieldMaterialDataPointer material = info.inputValues.at(2).getInlineValue(); + QByteArray materialContents; + QVector materials; + if (material) { + materialContents = material->getContents(); + materials = material->getMaterials(); } const HeightfieldBuffer* existingBuffer = static_cast( @@ -1395,12 +1571,12 @@ int HeightfieldRegionVisitor::visit(MetavoxelInfo& info) { // we already have a buffer of the correct resolution addRegion(bounds, existingBuffer->getHeightBounds()); buffer = new HeightfieldBuffer(info.minimum, info.size, existingBuffer->getHeight(), - existingBuffer->getColor(), textureContents, textures); + existingBuffer->getColor(), materialContents, materials); } else { // we must create a new buffer and update its borders buffer = new HeightfieldBuffer(info.minimum, info.size, QByteArray(heightContentsSize, 0), - QByteArray(colorContentsSize, 0), textureContents, textures); + QByteArray(colorContentsSize, 0), materialContents, materials); const Box& heightBounds = buffer->getHeightBounds(); addRegion(bounds, heightBounds); @@ -1474,13 +1650,445 @@ int HeightfieldUpdateVisitor::visit(MetavoxelInfo& info) { return STOP_RECURSION; } HeightfieldBuffer* newBuffer = new HeightfieldBuffer(info.minimum, info.size, - buffer->getHeight(), buffer->getColor(), buffer->getTexture(), buffer->getTextures()); + buffer->getHeight(), buffer->getColor(), buffer->getMaterial(), buffer->getMaterials()); _fetchVisitor.init(newBuffer); _data->guide(_fetchVisitor); info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(newBuffer))); return STOP_RECURSION; } +class VoxelAugmentVisitor : public MetavoxelVisitor { +public: + + VoxelAugmentVisitor(const MetavoxelLOD& lod); + + virtual int visit(MetavoxelInfo& info); +}; + +VoxelAugmentVisitor::VoxelAugmentVisitor(const MetavoxelLOD& lod) : + MetavoxelVisitor(QVector() << AttributeRegistry::getInstance()->getVoxelColorAttribute() << + AttributeRegistry::getInstance()->getVoxelMaterialAttribute() << + AttributeRegistry::getInstance()->getVoxelHermiteAttribute(), QVector() << + Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), lod) { +} + +class EdgeCrossing { +public: + glm::vec3 point; + glm::vec3 normal; + QRgb color; + char material; +}; + +int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { + if (!info.isLeaf) { + return DEFAULT_ORDER; + } + VoxelBuffer* buffer = NULL; + VoxelColorDataPointer color = info.inputValues.at(0).getInlineValue(); + VoxelMaterialDataPointer material = info.inputValues.at(1).getInlineValue(); + VoxelHermiteDataPointer hermite = info.inputValues.at(2).getInlineValue(); + if (color && material && hermite) { + QVector vertices; + QVector indices; + + // see http://www.frankpetterson.com/publications/dualcontour/dualcontour.pdf for a description of the + // dual contour algorithm for generating meshes from voxel data using Hermite-tagged edges + const QVector& colorContents = color->getContents(); + const QByteArray& materialContents = material->getContents(); + const QVector& hermiteContents = hermite->getContents(); + int size = color->getSize(); + int area = size * size; + + // number variables such as offset3 and alpha0 in this function correspond to cube corners, where the x, y, and z + // components are represented as bits in the 0, 1, and 2 position, respectively; hence, alpha0 is the value at + // the minimum x, y, and z corner and alpha7 is the value at the maximum x, y, and z + int offset3 = size + 1; + int offset5 = area + 1; + int offset6 = area + size; + int offset7 = area + size + 1; + + const QRgb* colorZ = colorContents.constData(); + const QRgb* hermiteData = hermiteContents.constData(); + int hermiteStride = hermite->getSize() * VoxelHermiteData::EDGE_COUNT; + int hermiteArea = hermiteStride * hermite->getSize(); + + const char* materialData = materialContents.constData(); + + // as we scan down the cube generating vertices between grid points, we remember the indices of the last + // (element, line, section--x, y, z) so that we can connect generated vertices as quads + int expanded = size + 1; + QVector lineIndices(expanded, -1); + QVector lastLineIndices(expanded, -1); + QVector planeIndices(expanded * expanded, -1); + QVector lastPlaneIndices(expanded * expanded, -1); + + const int EDGES_PER_CUBE = 12; + EdgeCrossing crossings[EDGES_PER_CUBE]; + + float highest = size - 1.0f; + float scale = info.size / highest; + const int ALPHA_OFFSET = 24; + for (int z = 0; z < expanded; z++) { + const QRgb* colorY = colorZ; + for (int y = 0; y < expanded; y++) { + int lastIndex; + const QRgb* colorX = colorY; + for (int x = 0; x < expanded; x++) { + int alpha0 = colorX[0] >> ALPHA_OFFSET; + int alpha1 = alpha0, alpha2 = alpha0, alpha4 = alpha0; + int alphaTotal = alpha0; + int possibleTotal = EIGHT_BIT_MAXIMUM; + + // cubes on the edge are two-dimensional: this ensures that their vertices will be shared between + // neighboring blocks, which share only one layer of points + bool middleX = (x != 0 && x != size); + bool middleY = (y != 0 && y != size); + bool middleZ = (z != 0 && z != size); + if (middleZ) { + alphaTotal += (alpha4 = colorX[area] >> ALPHA_OFFSET); + possibleTotal += EIGHT_BIT_MAXIMUM; + } + + int alpha5 = alpha4, alpha6 = alpha4; + if (middleY) { + alphaTotal += (alpha2 = colorX[size] >> ALPHA_OFFSET); + possibleTotal += EIGHT_BIT_MAXIMUM; + + if (middleZ) { + alphaTotal += (alpha6 = colorX[offset6] >> ALPHA_OFFSET); + possibleTotal += EIGHT_BIT_MAXIMUM; + } + } + + int alpha3 = alpha2, alpha7 = alpha6; + if (middleX) { + alphaTotal += (alpha1 = colorX[1] >> ALPHA_OFFSET); + possibleTotal += EIGHT_BIT_MAXIMUM; + + if (middleY) { + alphaTotal += (alpha3 = colorX[offset3] >> ALPHA_OFFSET); + possibleTotal += EIGHT_BIT_MAXIMUM; + + if (middleZ) { + alphaTotal += (alpha7 = colorX[offset7] >> ALPHA_OFFSET); + possibleTotal += EIGHT_BIT_MAXIMUM; + } + } + if (middleZ) { + alphaTotal += (alpha5 = colorX[offset5] >> ALPHA_OFFSET); + possibleTotal += EIGHT_BIT_MAXIMUM; + } + } + if (alphaTotal == 0 || alphaTotal == possibleTotal) { + if (x != 0) { + colorX++; + } + continue; // no corners set/all corners set + } + // the terrifying conditional code that follows checks each cube edge for a crossing, gathering + // its properties (color, material, normal) if one is present; as before, boundary edges are excluded + int clampedX = qMax(x - 1, 0), clampedY = qMax(y - 1, 0), clampedZ = qMax(z - 1, 0); + const QRgb* hermiteBase = hermiteData + clampedZ * hermiteArea + clampedY * hermiteStride + + clampedX * VoxelHermiteData::EDGE_COUNT; + const char* materialBase = materialData + clampedZ * area + clampedY * size + clampedX; + int crossingCount = 0; + if (middleX) { + if (alpha0 != alpha1) { + QRgb hermite = hermiteBase[0]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha0 == 0) { + crossing.color = colorX[1]; + crossing.material = materialBase[1]; + } else { + crossing.color = colorX[0]; + crossing.material = materialBase[0]; + } + crossing.point = glm::vec3(qAlpha(hermite), 0.0f, 0.0f); + } + if (middleY) { + if (alpha1 != alpha3) { + QRgb hermite = hermiteBase[VoxelHermiteData::EDGE_COUNT + 1]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha1 == 0) { + crossing.color = colorX[offset3]; + crossing.material = materialBase[offset3]; + } else { + crossing.color = colorX[1]; + crossing.material = materialBase[1]; + } + crossing.point = glm::vec3(EIGHT_BIT_MAXIMUM, qAlpha(hermite), 0.0f); + } + if (alpha2 != alpha3) { + QRgb hermite = hermiteBase[hermiteStride]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha2 == 0) { + crossing.color = colorX[offset3]; + crossing.material = materialBase[offset3]; + } else { + crossing.color = colorX[size]; + crossing.material = materialBase[size]; + } + crossing.point = glm::vec3(qAlpha(hermite), EIGHT_BIT_MAXIMUM, 0.0f); + } + if (middleZ) { + if (alpha3 != alpha7) { + QRgb hermite = hermiteBase[hermiteStride + VoxelHermiteData::EDGE_COUNT + 2]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha3 == 0) { + crossing.color = colorX[offset7]; + crossing.material = materialBase[offset7]; + } else { + crossing.color = colorX[offset3]; + crossing.material = materialBase[offset3]; + } + crossing.point = glm::vec3(EIGHT_BIT_MAXIMUM, EIGHT_BIT_MAXIMUM, qAlpha(hermite)); + } + if (alpha5 != alpha7) { + QRgb hermite = hermiteBase[hermiteArea + VoxelHermiteData::EDGE_COUNT + 1]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha5 == 0) { + crossing.color = colorX[offset7]; + crossing.material = materialBase[offset7]; + } else { + crossing.color = colorX[offset5]; + crossing.material = materialBase[offset5]; + } + crossing.point = glm::vec3(EIGHT_BIT_MAXIMUM, qAlpha(hermite), EIGHT_BIT_MAXIMUM); + } + if (alpha6 != alpha7) { + QRgb hermite = hermiteBase[hermiteArea + hermiteStride]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha6 == 0) { + crossing.color = colorX[offset7]; + crossing.material = materialBase[offset7]; + } else { + crossing.color = colorX[offset6]; + crossing.material = materialBase[offset6]; + } + crossing.point = glm::vec3(qAlpha(hermite), EIGHT_BIT_MAXIMUM, EIGHT_BIT_MAXIMUM); + } + } + } + if (middleZ) { + if (alpha1 != alpha5) { + QRgb hermite = hermiteBase[VoxelHermiteData::EDGE_COUNT + 2]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha1 == 0) { + crossing.color = colorX[offset5]; + crossing.material = materialBase[offset5]; + } else { + crossing.color = colorX[1]; + crossing.material = materialBase[1]; + } + crossing.point = glm::vec3(EIGHT_BIT_MAXIMUM, 0.0f, qAlpha(hermite)); + } + if (alpha4 != alpha5) { + QRgb hermite = hermiteBase[hermiteArea]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha4 == 0) { + crossing.color = colorX[offset5]; + crossing.material = materialBase[offset5]; + } else { + crossing.color = colorX[area]; + crossing.material = materialBase[area]; + } + crossing.point = glm::vec3(qAlpha(hermite), 0.0f, EIGHT_BIT_MAXIMUM); + } + } + } + if (middleY) { + if (alpha0 != alpha2) { + QRgb hermite = hermiteBase[1]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha0 == 0) { + crossing.color = colorX[size]; + crossing.material = materialBase[size]; + } else { + crossing.color = colorX[0]; + crossing.material = materialBase[0]; + } + crossing.point = glm::vec3(0.0f, qAlpha(hermite), 0.0f); + } + if (middleZ) { + if (alpha2 != alpha6) { + QRgb hermite = hermiteBase[hermiteStride + 2]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha2 == 0) { + crossing.color = colorX[offset6]; + crossing.material = materialBase[offset6]; + } else { + crossing.color = colorX[size]; + crossing.material = materialBase[size]; + } + crossing.point = glm::vec3(0.0f, EIGHT_BIT_MAXIMUM, qAlpha(hermite)); + } + if (alpha4 != alpha6) { + QRgb hermite = hermiteBase[hermiteArea + 1]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha4 == 0) { + crossing.color = colorX[offset6]; + crossing.material = materialBase[offset6]; + } else { + crossing.color = colorX[area]; + crossing.material = materialBase[area]; + } + crossing.point = glm::vec3(0.0f, qAlpha(hermite), EIGHT_BIT_MAXIMUM); + } + } + } + if (middleZ && alpha0 != alpha4) { + QRgb hermite = hermiteBase[2]; + EdgeCrossing& crossing = crossings[crossingCount++]; + crossing.normal = unpackNormal(hermite); + if (alpha0 == 0) { + crossing.color = colorX[area]; + crossing.material = materialBase[area]; + } else { + crossing.color = colorX[0]; + crossing.material = materialBase[0]; + } + crossing.point = glm::vec3(0.0f, 0.0f, qAlpha(hermite)); + } + // at present, we simply average the properties of each crossing as opposed to finding the vertex that + // minimizes the quadratic error function as described in the reference paper + glm::vec3 center; + glm::vec3 normal; + const int MAX_MATERIALS_PER_VERTEX = 4; + quint8 materials[] = { 0, 0, 0, 0 }; + glm::vec4 materialWeights; + float totalWeight = 0.0f; + int red = 0, green = 0, blue = 0; + for (int i = 0; i < crossingCount; i++) { + const EdgeCrossing& crossing = crossings[i]; + center += crossing.point; + normal += crossing.normal; + red += qRed(crossing.color); + green += qGreen(crossing.color); + blue += qBlue(crossing.color); + + // when assigning a material, search for its presence and, if not found, + // place it in the first empty slot + if (crossing.material != 0) { + for (int j = 0; j < MAX_MATERIALS_PER_VERTEX; j++) { + if (materials[j] == crossing.material) { + materialWeights[j] += 1.0f; + totalWeight += 1.0f; + break; + + } else if (materials[j] == 0) { + materials[j] = crossing.material; + materialWeights[j] = 1.0f; + totalWeight += 1.0f; + break; + } + } + } + } + normal = glm::normalize(normal); + center /= crossingCount; + if (totalWeight > 0.0f) { + materialWeights *= (EIGHT_BIT_MAXIMUM / totalWeight); + } + VoxelPoint point = { info.minimum + (glm::vec3(clampedX, clampedY, clampedZ) + + center * EIGHT_BIT_MAXIMUM_RECIPROCAL) * scale, + { (quint8)(red / crossingCount), (quint8)(green / crossingCount), (quint8)(blue / crossingCount) }, + { (char)(normal.x * 127.0f), (char)(normal.y * 127.0f), (char)(normal.z * 127.0f) }, + { materials[0], materials[1], materials[2], materials[3] }, + { (quint8)materialWeights[0], (quint8)materialWeights[1], (quint8)materialWeights[2], + (quint8)materialWeights[3] } }; + int index = vertices.size(); + vertices.append(point); + + // the first x, y, and z are repeated for the boundary edge; past that, we consider generating + // quads for each edge that includes a transition, using indices of previously generated vertices + if (x != 0 && y != 0 && z != 0) { + if (alpha0 != alpha1) { + indices.append(index); + int index1 = lastLineIndices.at(x); + int index2 = lastPlaneIndices.at((y - 1) * expanded + x); + int index3 = lastPlaneIndices.at(y * expanded + x); + if (alpha0 == 0) { // quad faces negative x + indices.append(index3); + indices.append(index2); + indices.append(index1); + } else { // quad faces positive x + indices.append(index1); + indices.append(index2); + indices.append(index3); + } + } + + if (alpha0 != alpha2) { + indices.append(index); + int index1 = lastIndex; + int index2 = lastPlaneIndices.at(y * expanded + x - 1); + int index3 = lastPlaneIndices.at(y * expanded + x); + if (alpha0 == 0) { // quad faces negative y + indices.append(index1); + indices.append(index2); + indices.append(index3); + } else { // quad faces positive y + indices.append(index3); + indices.append(index2); + indices.append(index1); + } + } + + if (alpha0 != alpha4) { + indices.append(index); + int index1 = lastIndex; + int index2 = lastLineIndices.at(x - 1); + int index3 = lastLineIndices.at(x); + if (alpha0 == 0) { // quad faces negative z + indices.append(index3); + indices.append(index2); + indices.append(index1); + } else { // quad faces positive z + indices.append(index1); + indices.append(index2); + indices.append(index3); + } + } + } + lastIndex = index; + lineIndices[x] = index; + planeIndices[y * expanded + x] = index; + + if (x != 0) { + colorX++; + } + } + lineIndices.swap(lastLineIndices); + + if (y != 0) { + colorY += size; + } + } + planeIndices.swap(lastPlaneIndices); + + if (z != 0) { + colorZ += area; + } + } + + buffer = new VoxelBuffer(vertices, indices, material->getMaterials()); + } + info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(buffer))); + return STOP_RECURSION; +} + void DefaultMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous, MetavoxelInfo& info, const MetavoxelLOD& lod) { // copy the previous buffers @@ -1501,6 +2109,13 @@ void DefaultMetavoxelRendererImplementation::augment(MetavoxelData& data, const data.setRoot(heightfieldBufferAttribute, root); root->incrementReferenceCount(); } + const AttributePointer& voxelBufferAttribute = + Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(); + root = expandedPrevious.getRoot(voxelBufferAttribute); + if (root) { + data.setRoot(voxelBufferAttribute, root); + root->incrementReferenceCount(); + } PointAugmentVisitor pointAugmentVisitor(lod); data.guideToDifferent(expandedPrevious, pointAugmentVisitor); @@ -1511,6 +2126,9 @@ void DefaultMetavoxelRendererImplementation::augment(MetavoxelData& data, const HeightfieldUpdateVisitor heightfieldUpdateVisitor(lod, heightfieldRegionVisitor.regions, heightfieldRegionVisitor.regionBounds); data.guide(heightfieldUpdateVisitor); + + VoxelAugmentVisitor voxelAugmentVisitor(lod); + data.guideToDifferent(expandedPrevious, voxelAugmentVisitor); } class SpannerSimulateVisitor : public SpannerVisitor { @@ -1653,38 +2271,22 @@ void DefaultMetavoxelRendererImplementation::render(MetavoxelData& data, Metavox _pointProgram.release(); + glDrawBuffers(sizeof(COLOR_NORMAL_DRAW_BUFFERS) / sizeof(COLOR_NORMAL_DRAW_BUFFERS[0]), COLOR_NORMAL_DRAW_BUFFERS); + glEnable(GL_CULL_FACE); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_EQUAL, 0.0f); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - ProgramObject* program = &_heightfieldProgram; - if (Menu::getInstance()->getShadowsEnabled()) { - if (Menu::getInstance()->isOptionChecked(MenuOption::CascadedShadows)) { - _cascadedShadowLightHeightfieldProgram.bind(); - _cascadedShadowLightHeightfieldProgram.setUniform(_shadowLightDistancesLocation, - Application::getInstance()->getShadowDistances()); - program = &_cascadedShadowMapHeightfieldProgram; - program->bind(); - program->setUniform(_shadowDistancesLocation, Application::getInstance()->getShadowDistances()); - - } else { - program = &_shadowMapHeightfieldProgram; - } - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getShadowDepthTextureID()); - glActiveTexture(GL_TEXTURE0); - } - - program->bind(); + _baseHeightfieldProgram.bind(); glEnableClientState(GL_TEXTURE_COORD_ARRAY); BufferRenderVisitor heightfieldRenderVisitor(Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute()); data.guide(heightfieldRenderVisitor); - program->release(); + _baseHeightfieldProgram.release(); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, 0); @@ -1693,42 +2295,63 @@ void DefaultMetavoxelRendererImplementation::render(MetavoxelData& data, Metavox glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + + _baseVoxelProgram.bind(); + + BufferRenderVisitor voxelRenderVisitor(Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute()); + data.guide(voxelRenderVisitor); + + _baseVoxelProgram.release(); + glDisable(GL_ALPHA_TEST); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + + glDrawBuffers(sizeof(COLOR_DRAW_BUFFERS) / sizeof(COLOR_DRAW_BUFFERS[0]), COLOR_DRAW_BUFFERS); +} + +void DefaultMetavoxelRendererImplementation::loadSplatProgram(const char* type, + ProgramObject& program, SplatLocations& locations) { + program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + + "shaders/metavoxel_" + type + "_splat.vert"); + program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + + "shaders/metavoxel_" + type + "_splat.frag"); + program.link(); + + program.bind(); + program.setUniformValue("heightMap", 0); + program.setUniformValue("textureMap", 1); + program.setUniformValueArray("diffuseMaps", SPLAT_TEXTURE_UNITS, SPLAT_COUNT); + locations.heightScale = program.uniformLocation("heightScale"); + locations.textureScale = program.uniformLocation("textureScale"); + locations.splatTextureOffset = program.uniformLocation("splatTextureOffset"); + locations.splatTextureScalesS = program.uniformLocation("splatTextureScalesS"); + locations.splatTextureScalesT = program.uniformLocation("splatTextureScalesT"); + locations.textureValueMinima = program.uniformLocation("textureValueMinima"); + locations.textureValueMaxima = program.uniformLocation("textureValueMaxima"); + locations.materials = program.attributeLocation("materials"); + locations.materialWeights = program.attributeLocation("materialWeights"); + program.release(); } ProgramObject DefaultMetavoxelRendererImplementation::_pointProgram; int DefaultMetavoxelRendererImplementation::_pointScaleLocation; -ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldProgram; -int DefaultMetavoxelRendererImplementation::_heightScaleLocation; -int DefaultMetavoxelRendererImplementation::_colorScaleLocation; -ProgramObject DefaultMetavoxelRendererImplementation::_shadowMapHeightfieldProgram; -int DefaultMetavoxelRendererImplementation::_shadowMapHeightScaleLocation; -int DefaultMetavoxelRendererImplementation::_shadowMapColorScaleLocation; -ProgramObject DefaultMetavoxelRendererImplementation::_cascadedShadowMapHeightfieldProgram; -int DefaultMetavoxelRendererImplementation::_cascadedShadowMapHeightScaleLocation; -int DefaultMetavoxelRendererImplementation::_cascadedShadowMapColorScaleLocation; -int DefaultMetavoxelRendererImplementation::_shadowDistancesLocation; ProgramObject DefaultMetavoxelRendererImplementation::_baseHeightfieldProgram; int DefaultMetavoxelRendererImplementation::_baseHeightScaleLocation; int DefaultMetavoxelRendererImplementation::_baseColorScaleLocation; ProgramObject DefaultMetavoxelRendererImplementation::_splatHeightfieldProgram; -int DefaultMetavoxelRendererImplementation::_splatHeightScaleLocation; -int DefaultMetavoxelRendererImplementation::_splatTextureScaleLocation; -int DefaultMetavoxelRendererImplementation::_splatTextureOffsetLocation; -int DefaultMetavoxelRendererImplementation::_splatTextureScalesSLocation; -int DefaultMetavoxelRendererImplementation::_splatTextureScalesTLocation; -int DefaultMetavoxelRendererImplementation::_splatTextureValueMinimaLocation; -int DefaultMetavoxelRendererImplementation::_splatTextureValueMaximaLocation; -ProgramObject DefaultMetavoxelRendererImplementation::_lightHeightfieldProgram; -int DefaultMetavoxelRendererImplementation::_lightHeightScaleLocation; -ProgramObject DefaultMetavoxelRendererImplementation::_shadowLightHeightfieldProgram; -int DefaultMetavoxelRendererImplementation::_shadowLightHeightScaleLocation; -ProgramObject DefaultMetavoxelRendererImplementation::_cascadedShadowLightHeightfieldProgram; -int DefaultMetavoxelRendererImplementation::_cascadedShadowLightHeightScaleLocation; -int DefaultMetavoxelRendererImplementation::_shadowLightDistancesLocation; +DefaultMetavoxelRendererImplementation::SplatLocations DefaultMetavoxelRendererImplementation::_splatHeightfieldLocations; ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldCursorProgram; +ProgramObject DefaultMetavoxelRendererImplementation::_baseVoxelProgram; +ProgramObject DefaultMetavoxelRendererImplementation::_splatVoxelProgram; +DefaultMetavoxelRendererImplementation::SplatLocations DefaultMetavoxelRendererImplementation::_splatVoxelLocations; static void enableClipPlane(GLenum plane, float x, float y, float z, float w) { GLdouble coefficients[] = { x, y, z, w }; diff --git a/interface/src/MetavoxelSystem.h b/interface/src/MetavoxelSystem.h index b1ddcf0bff..f3b9a02117 100644 --- a/interface/src/MetavoxelSystem.h +++ b/interface/src/MetavoxelSystem.h @@ -39,6 +39,7 @@ public: const AttributePointer& getPointBufferAttribute() { return _pointBufferAttribute; } const AttributePointer& getHeightfieldBufferAttribute() { return _heightfieldBufferAttribute; } + const AttributePointer& getVoxelBufferAttribute() { return _voxelBufferAttribute; } void simulate(float deltaTime); void render(); @@ -51,6 +52,12 @@ public: Q_INVOKABLE void deleteTextures(int heightID, int colorID, int textureID); + void noteNeedToLight() { _needToLight = true; } + +signals: + + void rendering(); + protected: virtual MetavoxelClient* createClient(const SharedNodePointer& node); @@ -59,12 +66,33 @@ private: void guideToAugmented(MetavoxelVisitor& visitor, bool render = false); + class LightLocations { + public: + int shadowDistances; + int shadowScale; + int nearLocation; + int depthScale; + int depthTexCoordOffset; + int depthTexCoordScale; + }; + + static void loadLightProgram(const char* name, ProgramObject& program, LightLocations& locations); + AttributePointer _pointBufferAttribute; AttributePointer _heightfieldBufferAttribute; + AttributePointer _voxelBufferAttribute; MetavoxelLOD _lod; QReadWriteLock _lodLock; Frustum _frustum; + bool _needToLight; + + ProgramObject _directionalLight; + LightLocations _directionalLightLocations; + ProgramObject _directionalLightShadowMap; + LightLocations _directionalLightShadowMapLocations; + ProgramObject _directionalLightCascadedShadowMap; + LightLocations _directionalLightCascadedShadowMapLocations; }; /// Describes contents of a point in a point buffer. @@ -143,8 +171,8 @@ public: static const int HEIGHT_EXTENSION; HeightfieldBuffer(const glm::vec3& translation, float scale, const QByteArray& height, - const QByteArray& color, const QByteArray& texture = QByteArray(), - const QVector& textures = QVector()); + const QByteArray& color, const QByteArray& material = QByteArray(), + const QVector& materials = QVector()); ~HeightfieldBuffer(); const glm::vec3& getTranslation() const { return _translation; } @@ -159,10 +187,10 @@ public: QByteArray& getColor() { return _color; } const QByteArray& getColor() const { return _color; } - QByteArray& getTexture() { return _texture; } - const QByteArray& getTexture() const { return _texture; } + QByteArray& getMaterial() { return _material; } + const QByteArray& getMaterial() const { return _material; } - const QVector& getTextures() const { return _textures; } + const QVector& getMaterials() const { return _materials; } QByteArray getUnextendedHeight() const; QByteArray getUnextendedColor() const; @@ -183,11 +211,11 @@ private: Box _colorBounds; QByteArray _height; QByteArray _color; - QByteArray _texture; - QVector _textures; + QByteArray _material; + QVector _materials; GLuint _heightTextureID; GLuint _colorTextureID; - GLuint _textureTextureID; + GLuint _materialTextureID; QVector _networkTextures; int _heightSize; float _heightIncrement; @@ -212,6 +240,37 @@ private: QVector _buffers; }; +/// Describes contents of a vertex in a voxel buffer. +class VoxelPoint { +public: + glm::vec3 vertex; + quint8 color[3]; + char normal[3]; + quint8 materials[4]; + quint8 materialWeights[4]; +}; + +/// Contains the information necessary to render a voxel block. +class VoxelBuffer : public BufferData { +public: + + VoxelBuffer(const QVector& vertices, const QVector& indices, + const QVector& materials = QVector()); + + virtual void render(bool cursor = false); + +private: + + QVector _vertices; + QVector _indices; + int _vertexCount; + int _indexCount; + QOpenGLBuffer _vertexBuffer; + QOpenGLBuffer _indexBuffer; + QVector _materials; + QVector _networkTextures; +}; + /// A client-side attribute that stores renderable buffers. class BufferDataAttribute : public InlineAttribute { Q_OBJECT @@ -233,42 +292,33 @@ public: static void init(); - static ProgramObject& getHeightfieldProgram() { return _heightfieldProgram; } - static int getHeightScaleLocation() { return _heightScaleLocation; } - static int getColorScaleLocation() { return _colorScaleLocation; } - - static ProgramObject& getShadowMapHeightfieldProgram() { return _shadowMapHeightfieldProgram; } - static int getShadowMapHeightScaleLocation() { return _shadowMapHeightScaleLocation; } - static int getShadowMapColorScaleLocation() { return _shadowMapColorScaleLocation; } - - static ProgramObject& getCascadedShadowMapHeightfieldProgram() { return _cascadedShadowMapHeightfieldProgram; } - static int getCascadedShadowMapHeightScaleLocation() { return _cascadedShadowMapHeightScaleLocation; } - static int getCascadedShadowMapColorScaleLocation() { return _cascadedShadowMapColorScaleLocation; } - static ProgramObject& getBaseHeightfieldProgram() { return _baseHeightfieldProgram; } static int getBaseHeightScaleLocation() { return _baseHeightScaleLocation; } static int getBaseColorScaleLocation() { return _baseColorScaleLocation; } + class SplatLocations { + public: + int heightScale; + int textureScale; + int splatTextureOffset; + int splatTextureScalesS; + int splatTextureScalesT; + int textureValueMinima; + int textureValueMaxima; + int materials; + int materialWeights; + }; + static ProgramObject& getSplatHeightfieldProgram() { return _splatHeightfieldProgram; } - static int getSplatHeightScaleLocation() { return _splatHeightScaleLocation; } - static int getSplatTextureScaleLocation() { return _splatTextureScaleLocation; } - static int getSplatTextureOffsetLocation() { return _splatTextureOffsetLocation; } - static int getSplatTextureScalesSLocation() { return _splatTextureScalesSLocation; } - static int getSplatTextureScalesTLocation() { return _splatTextureScalesTLocation; } - static int getSplatTextureValueMinimaLocation() { return _splatTextureValueMinimaLocation; } - static int getSplatTextureValueMaximaLocation() { return _splatTextureValueMaximaLocation; } - - static ProgramObject& getLightHeightfieldProgram() { return _lightHeightfieldProgram; } - static int getLightHeightScaleLocation() { return _lightHeightScaleLocation; } - - static ProgramObject& getShadowLightHeightfieldProgram() { return _shadowLightHeightfieldProgram; } - static int getShadowLightHeightScaleLocation() { return _shadowLightHeightScaleLocation; } - - static ProgramObject& getCascadedShadowLightHeightfieldProgram() { return _cascadedShadowLightHeightfieldProgram; } - static int getCascadedShadowLightHeightScaleLocation() { return _cascadedShadowLightHeightScaleLocation; } + static const SplatLocations& getSplatHeightfieldLocations() { return _splatHeightfieldLocations; } static ProgramObject& getHeightfieldCursorProgram() { return _heightfieldCursorProgram; } + static ProgramObject& getBaseVoxelProgram() { return _baseVoxelProgram; } + + static ProgramObject& getSplatVoxelProgram() { return _splatVoxelProgram; } + static const SplatLocations& getSplatVoxelLocations() { return _splatVoxelLocations; } + Q_INVOKABLE DefaultMetavoxelRendererImplementation(); virtual void augment(MetavoxelData& data, const MetavoxelData& previous, MetavoxelInfo& info, const MetavoxelLOD& lod); @@ -277,27 +327,18 @@ public: private: + static void loadSplatProgram(const char* type, ProgramObject& program, SplatLocations& locations); + static ProgramObject _pointProgram; static int _pointScaleLocation; - static ProgramObject _heightfieldProgram; - static int _heightScaleLocation; - static int _colorScaleLocation; - - static ProgramObject _shadowMapHeightfieldProgram; - static int _shadowMapHeightScaleLocation; - static int _shadowMapColorScaleLocation; - - static ProgramObject _cascadedShadowMapHeightfieldProgram; - static int _cascadedShadowMapHeightScaleLocation; - static int _cascadedShadowMapColorScaleLocation; - static int _shadowDistancesLocation; - static ProgramObject _baseHeightfieldProgram; static int _baseHeightScaleLocation; static int _baseColorScaleLocation; static ProgramObject _splatHeightfieldProgram; + static SplatLocations _splatHeightfieldLocations; + static int _splatHeightScaleLocation; static int _splatTextureScaleLocation; static int _splatTextureOffsetLocation; @@ -306,17 +347,11 @@ private: static int _splatTextureValueMinimaLocation; static int _splatTextureValueMaximaLocation; - static ProgramObject _lightHeightfieldProgram; - static int _lightHeightScaleLocation; - - static ProgramObject _shadowLightHeightfieldProgram; - static int _shadowLightHeightScaleLocation; - - static ProgramObject _cascadedShadowLightHeightfieldProgram; - static int _cascadedShadowLightHeightScaleLocation; - static int _shadowLightDistancesLocation; - static ProgramObject _heightfieldCursorProgram; + + static ProgramObject _baseVoxelProgram; + static ProgramObject _splatVoxelProgram; + static SplatLocations _splatVoxelLocations; }; /// Base class for spanner renderers; provides clipping. diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 43d7c65dc5..b3468a9214 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -78,7 +78,8 @@ MyAvatar::MyAvatar() : _lookAtTargetAvatar(), _shouldRender(true), _billboardValid(false), - _physicsSimulation() + _physicsSimulation(), + _voxelShapeManager() { ShapeCollider::initDispatchTable(); for (int i = 0; i < MAX_DRIVE_KEYS; i++) { @@ -89,11 +90,11 @@ MyAvatar::MyAvatar() : _skeletonModel.setEnableShapes(true); Ragdoll* ragdoll = _skeletonModel.buildRagdoll(); _physicsSimulation.setRagdoll(ragdoll); + _physicsSimulation.addEntity(&_voxelShapeManager); } MyAvatar::~MyAvatar() { - _physicsSimulation.setRagdoll(NULL); - _physicsSimulation.setEntity(NULL); + _physicsSimulation.clear(); _lookAtTargetAvatar.clear(); } @@ -1366,112 +1367,125 @@ void MyAvatar::updateCollisionWithEnvironment(float deltaTime, float radius) { static CollisionList myCollisions(64); void MyAvatar::updateCollisionWithVoxels(float deltaTime, float radius) { - const float MAX_VOXEL_COLLISION_SPEED = 100.0f; - float speed = glm::length(_velocity); - if (speed > MAX_VOXEL_COLLISION_SPEED) { - // don't even bother to try to collide against voxles when moving very fast - _trapDuration = 0.0f; - return; - } - bool isTrapped = false; - myCollisions.clear(); - const CapsuleShape& boundingShape = _skeletonModel.getBoundingShape(); - if (Application::getInstance()->getVoxelTree()->findShapeCollisions(&boundingShape, myCollisions, Octree::TryLock)) { - const float VOXEL_ELASTICITY = 0.0f; - const float VOXEL_DAMPING = 0.0f; - float capsuleRadius = boundingShape.getRadius(); - float capsuleHalfHeight = boundingShape.getHalfHeight(); - const float MAX_STEP_HEIGHT = capsuleRadius + capsuleHalfHeight; - const float MIN_STEP_HEIGHT = 0.0f; - glm::vec3 footBase = boundingShape.getTranslation() - (capsuleRadius + capsuleHalfHeight) * _worldUpDirection; - float highestStep = 0.0f; - float lowestStep = MAX_STEP_HEIGHT; - glm::vec3 floorPoint; - glm::vec3 stepPenetration(0.0f); - glm::vec3 totalPenetration(0.0f); + if (Menu::getInstance()->isOptionChecked(MenuOption::CollideAsRagdoll)) { + // We use a multiple of the avatar's boundingRadius as the size of the cube of interest. + float cubeScale = 4.0f * getBoundingRadius(); + glm::vec3 corner = getPosition() - glm::vec3(0.5f * cubeScale); + AACube boundingCube(corner, cubeScale); - for (int i = 0; i < myCollisions.size(); ++i) { - CollisionInfo* collision = myCollisions[i]; - glm::vec3 cubeCenter = collision->_vecData; - float cubeSide = collision->_floatData; - float verticalDepth = glm::dot(collision->_penetration, _worldUpDirection); - float horizontalDepth = glm::length(collision->_penetration - verticalDepth * _worldUpDirection); - const float MAX_TRAP_PERIOD = 0.125f; - if (horizontalDepth > capsuleRadius || fabsf(verticalDepth) > MAX_STEP_HEIGHT) { - isTrapped = true; - if (_trapDuration > MAX_TRAP_PERIOD) { - float distance = glm::dot(boundingShape.getTranslation() - cubeCenter, _worldUpDirection); - if (distance < 0.0f) { - distance = fabsf(distance) + 0.5f * cubeSide; - } - distance += capsuleRadius + capsuleHalfHeight; - totalPenetration = addPenetrations(totalPenetration, - distance * _worldUpDirection); - continue; - } - } else if (_trapDuration > MAX_TRAP_PERIOD) { - // we're trapped, ignore this collision - continue; - } - totalPenetration = addPenetrations(totalPenetration, collision->_penetration); - if (glm::dot(collision->_penetration, _velocity) >= 0.0f) { - glm::vec3 cubeTop = cubeCenter + (0.5f * cubeSide) * _worldUpDirection; - float stepHeight = glm::dot(_worldUpDirection, cubeTop - footBase); - if (stepHeight > highestStep) { - highestStep = stepHeight; - stepPenetration = collision->_penetration; - } - if (stepHeight < lowestStep) { - lowestStep = stepHeight; - floorPoint = collision->_contactPoint - collision->_penetration; - } - } + // query the VoxelTree for cubes that touch avatar's boundingCube + CubeList cubes; + if (Application::getInstance()->getVoxelTree()->findContentInCube(boundingCube, cubes)) { + _voxelShapeManager.updateVoxels(cubes); } - if (lowestStep < MAX_STEP_HEIGHT) { - _lastFloorContactPoint = floorPoint; - } - - float penetrationLength = glm::length(totalPenetration); - if (penetrationLength < EPSILON) { + } else { + const float MAX_VOXEL_COLLISION_SPEED = 100.0f; + float speed = glm::length(_velocity); + if (speed > MAX_VOXEL_COLLISION_SPEED) { + // don't even bother to try to collide against voxles when moving very fast _trapDuration = 0.0f; return; } - float verticalPenetration = glm::dot(totalPenetration, _worldUpDirection); - if (highestStep > MIN_STEP_HEIGHT && highestStep < MAX_STEP_HEIGHT && verticalPenetration <= 0.0f) { - // we're colliding against an edge - glm::vec3 targetVelocity = _motorVelocity; - if (_motionBehaviors & AVATAR_MOTION_MOTOR_USE_LOCAL_FRAME) { - // rotate _motorVelocity into world frame - glm::quat rotation = getHead()->getCameraOrientation(); - targetVelocity = rotation * _motorVelocity; - } - if (_wasPushing && glm::dot(targetVelocity, totalPenetration) > EPSILON) { - // we're puhing into the edge, so we want to lift - - // remove unhelpful horizontal component of the step's penetration - totalPenetration -= stepPenetration - (glm::dot(stepPenetration, _worldUpDirection) * _worldUpDirection); - - // further adjust penetration to help lift - float liftSpeed = glm::max(MAX_WALKING_SPEED, speed); - float thisStep = glm::min(liftSpeed * deltaTime, highestStep); - float extraStep = glm::dot(totalPenetration, _worldUpDirection) + thisStep; - if (extraStep > 0.0f) { - totalPenetration -= extraStep * _worldUpDirection; + bool isTrapped = false; + myCollisions.clear(); + const CapsuleShape& boundingShape = _skeletonModel.getBoundingShape(); + if (Application::getInstance()->getVoxelTree()->findShapeCollisions(&boundingShape, myCollisions, Octree::TryLock)) { + const float VOXEL_ELASTICITY = 0.0f; + const float VOXEL_DAMPING = 0.0f; + float capsuleRadius = boundingShape.getRadius(); + float capsuleHalfHeight = boundingShape.getHalfHeight(); + const float MAX_STEP_HEIGHT = capsuleRadius + capsuleHalfHeight; + const float MIN_STEP_HEIGHT = 0.0f; + glm::vec3 footBase = boundingShape.getTranslation() - (capsuleRadius + capsuleHalfHeight) * _worldUpDirection; + float highestStep = 0.0f; + float lowestStep = MAX_STEP_HEIGHT; + glm::vec3 floorPoint; + glm::vec3 stepPenetration(0.0f); + glm::vec3 totalPenetration(0.0f); + + for (int i = 0; i < myCollisions.size(); ++i) { + CollisionInfo* collision = myCollisions[i]; + glm::vec3 cubeCenter = collision->_vecData; + float cubeSide = collision->_floatData; + float verticalDepth = glm::dot(collision->_penetration, _worldUpDirection); + float horizontalDepth = glm::length(collision->_penetration - verticalDepth * _worldUpDirection); + const float MAX_TRAP_PERIOD = 0.125f; + if (horizontalDepth > capsuleRadius || fabsf(verticalDepth) > MAX_STEP_HEIGHT) { + isTrapped = true; + if (_trapDuration > MAX_TRAP_PERIOD) { + float distance = glm::dot(boundingShape.getTranslation() - cubeCenter, _worldUpDirection); + if (distance < 0.0f) { + distance = fabsf(distance) + 0.5f * cubeSide; + } + distance += capsuleRadius + capsuleHalfHeight; + totalPenetration = addPenetrations(totalPenetration, - distance * _worldUpDirection); + continue; + } + } else if (_trapDuration > MAX_TRAP_PERIOD) { + // we're trapped, ignore this collision + continue; + } + totalPenetration = addPenetrations(totalPenetration, collision->_penetration); + if (glm::dot(collision->_penetration, _velocity) >= 0.0f) { + glm::vec3 cubeTop = cubeCenter + (0.5f * cubeSide) * _worldUpDirection; + float stepHeight = glm::dot(_worldUpDirection, cubeTop - footBase); + if (stepHeight > highestStep) { + highestStep = stepHeight; + stepPenetration = collision->_penetration; + } + if (stepHeight < lowestStep) { + lowestStep = stepHeight; + floorPoint = collision->_contactPoint - collision->_penetration; + } + } + } + if (lowestStep < MAX_STEP_HEIGHT) { + _lastFloorContactPoint = floorPoint; + } + + float penetrationLength = glm::length(totalPenetration); + if (penetrationLength < EPSILON) { + _trapDuration = 0.0f; + return; + } + float verticalPenetration = glm::dot(totalPenetration, _worldUpDirection); + if (highestStep > MIN_STEP_HEIGHT && highestStep < MAX_STEP_HEIGHT && verticalPenetration <= 0.0f) { + // we're colliding against an edge + glm::vec3 targetVelocity = _motorVelocity; + if (_motionBehaviors & AVATAR_MOTION_MOTOR_USE_LOCAL_FRAME) { + // rotate _motorVelocity into world frame + glm::quat rotation = getHead()->getCameraOrientation(); + targetVelocity = rotation * _motorVelocity; + } + if (_wasPushing && glm::dot(targetVelocity, totalPenetration) > EPSILON) { + // we're puhing into the edge, so we want to lift + + // remove unhelpful horizontal component of the step's penetration + totalPenetration -= stepPenetration - (glm::dot(stepPenetration, _worldUpDirection) * _worldUpDirection); + + // further adjust penetration to help lift + float liftSpeed = glm::max(MAX_WALKING_SPEED, speed); + float thisStep = glm::min(liftSpeed * deltaTime, highestStep); + float extraStep = glm::dot(totalPenetration, _worldUpDirection) + thisStep; + if (extraStep > 0.0f) { + totalPenetration -= extraStep * _worldUpDirection; + } + + _position -= totalPenetration; + } else { + // we're not pushing into the edge, so let the avatar fall + applyHardCollision(totalPenetration, VOXEL_ELASTICITY, VOXEL_DAMPING); } - - _position -= totalPenetration; } else { - // we're not pushing into the edge, so let the avatar fall applyHardCollision(totalPenetration, VOXEL_ELASTICITY, VOXEL_DAMPING); } - } else { - applyHardCollision(totalPenetration, VOXEL_ELASTICITY, VOXEL_DAMPING); - } - - // Don't make a collision sound against voxlels by default -- too annoying when walking - //const float VOXEL_COLLISION_FREQUENCY = 0.5f; - //updateCollisionSound(myCollisions[0]->_penetration, deltaTime, VOXEL_COLLISION_FREQUENCY); - } - _trapDuration = isTrapped ? _trapDuration + deltaTime : 0.0f; + + // Don't make a collision sound against voxlels by default -- too annoying when walking + //const float VOXEL_COLLISION_FREQUENCY = 0.5f; + //updateCollisionSound(myCollisions[0]->_penetration, deltaTime, VOXEL_COLLISION_FREQUENCY); + } + _trapDuration = isTrapped ? _trapDuration + deltaTime : 0.0f; + } } void MyAvatar::applyHardCollision(const glm::vec3& penetration, float elasticity, float damping) { @@ -1837,6 +1851,9 @@ void MyAvatar::updateMotionBehaviorsFromMenu() { } else { _motionBehaviors &= ~AVATAR_MOTION_STAND_ON_NEARBY_FLOORS; } + if (!(_collisionGroups | COLLISION_GROUP_VOXELS)) { + _voxelShapeManager.clearShapes(); + } } void MyAvatar::renderAttachments(RenderMode renderMode) { diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 5d45dad25d..d7b955ce06 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -17,6 +17,7 @@ #include #include "Avatar.h" +#include "VoxelShapeManager.h" class ModelItemID; @@ -213,6 +214,7 @@ private: QList _animationHandles; PhysicsSimulation _physicsSimulation; + VoxelShapeManager _voxelShapeManager; RecorderPointer _recorder; diff --git a/interface/src/avatar/VoxelShapeManager.cpp b/interface/src/avatar/VoxelShapeManager.cpp new file mode 100644 index 0000000000..a73679a2c4 --- /dev/null +++ b/interface/src/avatar/VoxelShapeManager.cpp @@ -0,0 +1,99 @@ +// +// VoxelShapeManager.cpp +// interface/src/avatar +// +// Created by Andrew Meadows on 2014.09.02 +// Copyright 2012 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include + +#include +#include +#include + +#include "VoxelShapeManager.h" + +VoxelShapeManager::VoxelShapeManager() : PhysicsEntity(), _lastSimulationTranslation(0.0f) { +} + +VoxelShapeManager::~VoxelShapeManager() { + clearShapes(); +} + +void VoxelShapeManager::stepForward(float deltaTime) { + PhysicsSimulation* simulation = getSimulation(); + if (simulation) { + glm::vec3 simulationOrigin = simulation->getTranslation(); + if (glm::distance2(_lastSimulationTranslation, simulationOrigin) > EPSILON) { + VoxelPool::const_iterator voxelItr = _voxels.constBegin(); + while (voxelItr != _voxels.constEnd()) { + // the shape's position is stored in the simulation-frame + const VoxelInfo& voxel = voxelItr.value(); + voxel._shape->setTranslation(voxel._cube.calcCenter() - simulationOrigin); + ++voxelItr; + } + _lastSimulationTranslation = simulationOrigin; + } + } +} + +void VoxelShapeManager::buildShapes() { + // the shapes are owned by the elements of _voxels, + // so _shapes is constructed by harvesting them from _voxels + _shapes.clear(); + VoxelPool::const_iterator voxelItr = _voxels.constBegin(); + while (voxelItr != _voxels.constEnd()) { + _shapes.push_back(voxelItr.value()._shape); + ++voxelItr; + } +} + +void VoxelShapeManager::clearShapes() { + PhysicsEntity::clearShapes(); + _voxels.clear(); +} + +void VoxelShapeManager::updateVoxels(CubeList& cubes) { + PhysicsSimulation* simulation = getSimulation(); + if (!simulation) { + return; + } + + int numChanges = 0; + VoxelPool::iterator voxelItr = _voxels.begin(); + while (voxelItr != _voxels.end()) { + // look for this voxel in cubes + CubeList::iterator cubeItr = cubes.find(voxelItr.key()); + if (cubeItr == cubes.end()) { + // did not find it --> remove the voxel + simulation->removeShape(voxelItr.value()._shape); + voxelItr = _voxels.erase(voxelItr); + ++numChanges; + } else { + // found it --> remove the cube + cubes.erase(cubeItr); + voxelItr++; + } + } + + // add remaining cubes to _voxels + glm::vec3 simulationOrigin = simulation->getTranslation(); + CubeList::const_iterator cubeItr = cubes.constBegin(); + while (cubeItr != cubes.constEnd()) { + AACube cube = cubeItr.value(); + AACubeShape* shape = new AACubeShape(cube.getScale(), cube.calcCenter() - simulationOrigin); + shape->setEntity(this); + VoxelInfo voxel = {cube, shape }; + _voxels.insert(cubeItr.key(), voxel); + ++numChanges; + ++cubeItr; + } + + if (numChanges > 0) { + buildShapes(); + } +} diff --git a/interface/src/avatar/VoxelShapeManager.h b/interface/src/avatar/VoxelShapeManager.h new file mode 100644 index 0000000000..1b7179788d --- /dev/null +++ b/interface/src/avatar/VoxelShapeManager.h @@ -0,0 +1,51 @@ +// +// VoxelShapeManager.h +// interface/src/avatar +// +// Created by Andrew Meadows on 2014.09.02 +// Copyright 2012 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_VoxelShapeManager_h +#define hifi_VoxelShapeManager_h + +#include + +#include +#include +#include + +#include "VoxelShapeManager.h" + +class AACubeShape; + +class VoxelInfo{ +public: + AACube _cube; + AACubeShape* _shape; +}; + +typedef QHash VoxelPool; + +class VoxelShapeManager : public PhysicsEntity { +public: + VoxelShapeManager(); + ~VoxelShapeManager(); + + void stepForward(float deltaTime); + void buildShapes(); + void clearShapes(); + + /// \param cubes list of AACubes representing all of the voxels that should be in this VoxelShapeManager + void updateVoxels(CubeList& cubes); + + +private: + glm::vec3 _lastSimulationTranslation; + VoxelPool _voxels; +}; + +#endif // hifi_VoxelShapeManager_h diff --git a/interface/src/renderer/TextureCache.cpp b/interface/src/renderer/TextureCache.cpp index d960525817..bb07e83980 100644 --- a/interface/src/renderer/TextureCache.cpp +++ b/interface/src/renderer/TextureCache.cpp @@ -29,6 +29,7 @@ TextureCache::TextureCache() : _whiteTextureID(0), _blueTextureID(0), _primaryDepthTextureID(0), + _primaryNormalTextureID(0), _primaryFramebufferObject(NULL), _secondaryFramebufferObject(NULL), _tertiaryFramebufferObject(NULL), @@ -46,6 +47,7 @@ TextureCache::~TextureCache() { } if (_primaryFramebufferObject) { glDeleteTextures(1, &_primaryDepthTextureID); + glDeleteTextures(1, &_primaryNormalTextureID); } if (_primaryFramebufferObject) { @@ -71,6 +73,8 @@ void TextureCache::setFrameBufferSize(QSize frameBufferSize) { _primaryFramebufferObject = NULL; glDeleteTextures(1, &_primaryDepthTextureID); _primaryDepthTextureID = 0; + glDeleteTextures(1, &_primaryNormalTextureID); + _primaryNormalTextureID = 0; } if (_secondaryFramebufferObject) { @@ -205,15 +209,22 @@ QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { glGenTextures(1, &_primaryDepthTextureID); glBindTexture(GL_TEXTURE_2D, _primaryDepthTextureID); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, _frameBufferSize.width(), _frameBufferSize.height(), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + glGenTextures(1, &_primaryNormalTextureID); + glBindTexture(GL_TEXTURE_2D, _primaryNormalTextureID); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _frameBufferSize.width(), _frameBufferSize.height(), + 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); _primaryFramebufferObject->bind(); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _primaryDepthTextureID, 0); + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _primaryNormalTextureID, 0); _primaryFramebufferObject->release(); } return _primaryFramebufferObject; @@ -225,6 +236,12 @@ GLuint TextureCache::getPrimaryDepthTextureID() { return _primaryDepthTextureID; } +GLuint TextureCache::getPrimaryNormalTextureID() { + // ensure that the primary framebuffer object is initialized before returning the normal texture id + getPrimaryFramebufferObject(); + return _primaryNormalTextureID; +} + QOpenGLFramebufferObject* TextureCache::getSecondaryFramebufferObject() { if (!_secondaryFramebufferObject) { _secondaryFramebufferObject = createFramebufferObject(); @@ -278,6 +295,7 @@ bool TextureCache::eventFilter(QObject* watched, QEvent* event) { delete _primaryFramebufferObject; _primaryFramebufferObject = NULL; glDeleteTextures(1, &_primaryDepthTextureID); + glDeleteTextures(1, &_primaryNormalTextureID); } if (_secondaryFramebufferObject && _secondaryFramebufferObject->size() != size) { delete _secondaryFramebufferObject; diff --git a/interface/src/renderer/TextureCache.h b/interface/src/renderer/TextureCache.h index e1d69677f6..ce0e7661af 100644 --- a/interface/src/renderer/TextureCache.h +++ b/interface/src/renderer/TextureCache.h @@ -61,6 +61,9 @@ public: /// Returns the ID of the primary framebuffer object's depth texture. This contains the Z buffer used in rendering. GLuint getPrimaryDepthTextureID(); + /// Returns the ID of the primary framebuffer object's normal texture. + GLuint getPrimaryNormalTextureID(); + /// Returns a pointer to the secondary framebuffer object, used as an additional render target when performing full /// screen effects. QOpenGLFramebufferObject* getSecondaryFramebufferObject(); @@ -95,6 +98,7 @@ private: QHash > _dilatableNetworkTextures; GLuint _primaryDepthTextureID; + GLuint _primaryNormalTextureID; QOpenGLFramebufferObject* _primaryFramebufferObject; QOpenGLFramebufferObject* _secondaryFramebufferObject; QOpenGLFramebufferObject* _tertiaryFramebufferObject; diff --git a/interface/src/ui/MetavoxelEditor.cpp b/interface/src/ui/MetavoxelEditor.cpp index ce09e3657d..dc669365e8 100644 --- a/interface/src/ui/MetavoxelEditor.cpp +++ b/interface/src/ui/MetavoxelEditor.cpp @@ -66,13 +66,16 @@ MetavoxelEditor::MetavoxelEditor() : attributeLayout->addLayout(attributeButtonLayout); QPushButton* newAttribute = new QPushButton("New..."); - attributeButtonLayout->addWidget(newAttribute); + attributeButtonLayout->addWidget(newAttribute, 1); connect(newAttribute, SIGNAL(clicked()), SLOT(createNewAttribute())); - attributeButtonLayout->addWidget(_deleteAttribute = new QPushButton("Delete")); + attributeButtonLayout->addWidget(_deleteAttribute = new QPushButton("Delete"), 1); _deleteAttribute->setEnabled(false); connect(_deleteAttribute, SIGNAL(clicked()), SLOT(deleteSelectedAttribute())); + attributeButtonLayout->addWidget(_showAll = new QCheckBox("Show All")); + connect(_showAll, SIGNAL(clicked()), SLOT(updateAttributes())); + QFormLayout* formLayout = new QFormLayout(); topLayout->addLayout(formLayout); @@ -116,11 +119,15 @@ MetavoxelEditor::MetavoxelEditor() : addTool(new RemoveSpannerTool(this)); addTool(new ClearSpannersTool(this)); addTool(new SetSpannerTool(this)); - addTool(new ImportHeightfieldTool(this)); - addTool(new EraseHeightfieldTool(this)); addTool(new HeightfieldHeightBrushTool(this)); addTool(new HeightfieldColorBrushTool(this)); - addTool(new HeightfieldTextureBrushTool(this)); + addTool(new HeightfieldMaterialBrushTool(this)); + addTool(new ImportHeightfieldTool(this)); + addTool(new EraseHeightfieldTool(this)); + addTool(new VoxelColorBoxTool(this)); + addTool(new VoxelMaterialBoxTool(this)); + addTool(new VoxelColorSphereTool(this)); + addTool(new VoxelMaterialSphereTool(this)); updateAttributes(); @@ -200,7 +207,7 @@ void MetavoxelEditor::selectedAttributeChanged() { AttributePointer attribute = AttributeRegistry::getInstance()->getAttribute(selected); foreach (MetavoxelTool* tool, _tools) { - if (tool->appliesTo(attribute)) { + if (tool->appliesTo(attribute) && (tool->isUserFacing() || _showAll->isChecked())) { _toolBox->addItem(tool->objectName(), QVariant::fromValue(tool)); } } @@ -214,6 +221,7 @@ void MetavoxelEditor::selectedAttributeChanged() { editor->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); _valueArea->setWidget(editor); } + updateTool(); } void MetavoxelEditor::createNewAttribute() { @@ -271,6 +279,35 @@ void MetavoxelEditor::alignGridPosition() { _gridPosition->setValue(step * floor(_gridPosition->value() / step)); } +void MetavoxelEditor::updateAttributes(const QString& select) { + // remember the selection in order to preserve it + QString selected = select.isNull() ? getSelectedAttribute() : select; + _attributes->clear(); + + // sort the names for consistent ordering + QList names; + if (_showAll->isChecked()) { + names = AttributeRegistry::getInstance()->getAttributes().keys(); + + } else { + foreach (const AttributePointer& attribute, AttributeRegistry::getInstance()->getAttributes()) { + if (attribute->isUserFacing()) { + names.append(attribute->getName()); + } + } + } + qSort(names); + + foreach (const QString& name, names) { + QListWidgetItem* item = new QListWidgetItem(name); + _attributes->addItem(item); + if (name == selected || selected.isNull()) { + item->setSelected(true); + selected = name; + } + } +} + void MetavoxelEditor::updateTool() { MetavoxelTool* active = getActiveTool(); foreach (MetavoxelTool* tool, _tools) { @@ -335,25 +372,6 @@ void MetavoxelEditor::addTool(MetavoxelTool* tool) { layout()->addWidget(tool); } -void MetavoxelEditor::updateAttributes(const QString& select) { - // remember the selection in order to preserve it - QString selected = select.isNull() ? getSelectedAttribute() : select; - _attributes->clear(); - - // sort the names for consistent ordering - QList names = AttributeRegistry::getInstance()->getAttributes().keys(); - qSort(names); - - foreach (const QString& name, names) { - QListWidgetItem* item = new QListWidgetItem(name); - _attributes->addItem(item); - if (name == selected || selected.isNull()) { - item->setSelected(true); - selected = name; - } - } -} - MetavoxelTool* MetavoxelEditor::getActiveTool() const { int index = _toolBox->currentIndex(); return (index == -1) ? NULL : static_cast(_toolBox->itemData(index).value()); @@ -361,9 +379,10 @@ MetavoxelTool* MetavoxelEditor::getActiveTool() const { ProgramObject MetavoxelEditor::_gridProgram; -MetavoxelTool::MetavoxelTool(MetavoxelEditor* editor, const QString& name, bool usesValue) : +MetavoxelTool::MetavoxelTool(MetavoxelEditor* editor, const QString& name, bool usesValue, bool userFacing) : _editor(editor), - _usesValue(usesValue) { + _usesValue(usesValue), + _userFacing(userFacing) { QVBoxLayout* layout = new QVBoxLayout(); setLayout(layout); @@ -385,13 +404,13 @@ void MetavoxelTool::render() { // nothing by default } -BoxSetTool::BoxSetTool(MetavoxelEditor* editor) : - MetavoxelTool(editor, "Set Value (Box)") { +BoxTool::BoxTool(MetavoxelEditor* editor, const QString& name, bool usesValue, bool userFacing) : + MetavoxelTool(editor, name, usesValue, userFacing) { resetState(); } -void BoxSetTool::render() { +void BoxTool::render() { if (Application::getInstance()->isMouseHidden()) { resetState(); return; @@ -457,7 +476,7 @@ void BoxSetTool::render() { glTranslatef(0.5f, 0.5f, 0.5f); if (_state != HOVERING_STATE) { const float BOX_ALPHA = 0.25f; - QColor color = _editor->getValue().value(); + QColor color = getColor(); if (color.isValid()) { glColor4f(color.redF(), color.greenF(), color.blueF(), BOX_ALPHA); } else { @@ -476,7 +495,7 @@ void BoxSetTool::render() { glPopMatrix(); } -bool BoxSetTool::eventFilter(QObject* watched, QEvent* event) { +bool BoxTool::eventFilter(QObject* watched, QEvent* event) { switch (_state) { case HOVERING_STATE: if (event->type() == QEvent::MouseButtonPress && _startPosition != INVALID_VECTOR) { @@ -515,12 +534,20 @@ bool BoxSetTool::eventFilter(QObject* watched, QEvent* event) { return false; } -void BoxSetTool::resetState() { +void BoxTool::resetState() { _state = HOVERING_STATE; _startPosition = INVALID_VECTOR; _height = 0.0f; } +BoxSetTool::BoxSetTool(MetavoxelEditor* editor) : + BoxTool(editor, "Set Value (Box)", true, false) { +} + +QColor BoxSetTool::getColor() { + return _editor->getValue().value(); +} + void BoxSetTool::applyValue(const glm::vec3& minimum, const glm::vec3& maximum) { AttributePointer attribute = AttributeRegistry::getInstance()->getAttribute(_editor->getSelectedAttribute()); if (!attribute) { @@ -533,7 +560,7 @@ void BoxSetTool::applyValue(const glm::vec3& minimum, const glm::vec3& maximum) } GlobalSetTool::GlobalSetTool(MetavoxelEditor* editor) : - MetavoxelTool(editor, "Set Value (Global)") { + MetavoxelTool(editor, "Set Value (Global)", true, false) { QPushButton* button = new QPushButton("Apply"); layout()->addWidget(button); @@ -944,11 +971,9 @@ ImportHeightfieldTool::ImportHeightfieldTool(MetavoxelEditor* editor) : connect(_height, &QAbstractButton::clicked, this, &ImportHeightfieldTool::selectHeightFile); _form->addRow("Color:", _color = new QPushButton()); connect(_color, &QAbstractButton::clicked, this, &ImportHeightfieldTool::selectColorFile); -} - -void ImportHeightfieldTool::render() { - HeightfieldTool::render(); - _preview.render(_translation->getValue(), _translation->getSingleStep()); + + connect(Application::getInstance()->getMetavoxels(), &MetavoxelSystem::rendering, + this, &ImportHeightfieldTool::renderPreview); } void ImportHeightfieldTool::apply() { @@ -966,7 +991,7 @@ void ImportHeightfieldTool::apply() { QByteArray color; if (buffer->getColor().isEmpty()) { const int WHITE_VALUE = 0xFF; - color = QByteArray(height.size() * HeightfieldData::COLOR_BYTES, WHITE_VALUE); + color = QByteArray(height.size() * DataBlock::COLOR_BYTES, WHITE_VALUE); } else { color = buffer->getUnextendedColor(); } @@ -975,10 +1000,10 @@ void ImportHeightfieldTool::apply() { AttributeRegistry::getInstance()->getHeightfieldColorAttribute(), encodeInline(colorPointer)))); int size = glm::sqrt(height.size()) + HeightfieldBuffer::SHARED_EDGE; - QByteArray texture(size * size, 0); - HeightfieldTextureDataPointer texturePointer(new HeightfieldTextureData(texture)); - data.setRoot(AttributeRegistry::getInstance()->getHeightfieldTextureAttribute(), new MetavoxelNode(AttributeValue( - AttributeRegistry::getInstance()->getHeightfieldTextureAttribute(), encodeInline(texturePointer)))); + QByteArray material(size * size, 0); + HeightfieldMaterialDataPointer materialPointer(new HeightfieldMaterialData(material)); + data.setRoot(AttributeRegistry::getInstance()->getHeightfieldMaterialAttribute(), new MetavoxelNode(AttributeValue( + AttributeRegistry::getInstance()->getHeightfieldMaterialAttribute(), encodeInline(materialPointer)))); MetavoxelEditMessage message = { QVariant::fromValue(SetDataEdit( _translation->getValue() + buffer->getTranslation() * scale, data)) }; @@ -1032,22 +1057,22 @@ void ImportHeightfieldTool::updatePreview() { int rows = qMin(heightSize - offsetY, _heightImage.height() - extendedI); int columns = qMin(heightSize - offsetX, _heightImage.width() - extendedJ); for (int y = 0; y < rows; y++) { - uchar* src = _heightImage.scanLine(extendedI + y) + extendedJ * HeightfieldData::COLOR_BYTES; + uchar* src = _heightImage.scanLine(extendedI + y) + extendedJ * DataBlock::COLOR_BYTES; char* dest = height.data() + (y + offsetY) * heightSize + offsetX; for (int x = 0; x < columns; x++) { *dest++ = *src; - src += HeightfieldData::COLOR_BYTES; + src += DataBlock::COLOR_BYTES; } } QByteArray color; if (!_colorImage.isNull()) { - color = QByteArray(colorSize * colorSize * HeightfieldData::COLOR_BYTES, 0); + color = QByteArray(colorSize * colorSize * DataBlock::COLOR_BYTES, 0); rows = qMax(0, qMin(colorSize, _colorImage.height() - i)); columns = qMax(0, qMin(colorSize, _colorImage.width() - j)); for (int y = 0; y < rows; y++) { - memcpy(color.data() + y * colorSize * HeightfieldData::COLOR_BYTES, - _colorImage.scanLine(i + y) + j * HeightfieldData::COLOR_BYTES, - columns * HeightfieldData::COLOR_BYTES); + memcpy(color.data() + y * colorSize * DataBlock::COLOR_BYTES, + _colorImage.scanLine(i + y) + j * DataBlock::COLOR_BYTES, + columns * DataBlock::COLOR_BYTES); } } buffers.append(BufferDataPointer(new HeightfieldBuffer(glm::vec3(x, 0.0f, z), 1.0f, height, color))); @@ -1057,6 +1082,12 @@ void ImportHeightfieldTool::updatePreview() { _preview.setBuffers(buffers); } +void ImportHeightfieldTool::renderPreview() { + if (isVisible()) { + _preview.render(_translation->getValue(), _translation->getSingleStep()); + } +} + EraseHeightfieldTool::EraseHeightfieldTool(MetavoxelEditor* editor) : HeightfieldTool(editor, "Erase Heightfield") { @@ -1177,25 +1208,190 @@ QVariant HeightfieldColorBrushTool::createEdit(bool alternate) { alternate ? QColor() : _color->getColor())); } -HeightfieldTextureBrushTool::HeightfieldTextureBrushTool(MetavoxelEditor* editor) : - HeightfieldBrushTool(editor, "Texture Brush") { +HeightfieldMaterialBrushTool::HeightfieldMaterialBrushTool(MetavoxelEditor* editor) : + HeightfieldBrushTool(editor, "Material Brush") { - _form->addRow(_textureEditor = new SharedObjectEditor(&HeightfieldTexture::staticMetaObject, false)); - connect(_textureEditor, &SharedObjectEditor::objectChanged, this, &HeightfieldTextureBrushTool::updateTexture); + _form->addRow(_materialEditor = new SharedObjectEditor(&MaterialObject::staticMetaObject, false)); + connect(_materialEditor, &SharedObjectEditor::objectChanged, this, &HeightfieldMaterialBrushTool::updateTexture); } -QVariant HeightfieldTextureBrushTool::createEdit(bool alternate) { +QVariant HeightfieldMaterialBrushTool::createEdit(bool alternate) { if (alternate) { - return QVariant::fromValue(PaintHeightfieldTextureEdit(_position, _radius->value(), SharedObjectPointer(), QColor())); + return QVariant::fromValue(PaintHeightfieldMaterialEdit(_position, _radius->value(), SharedObjectPointer(), QColor())); } else { - SharedObjectPointer texture = _textureEditor->getObject(); - _textureEditor->detachObject(); - return QVariant::fromValue(PaintHeightfieldTextureEdit(_position, _radius->value(), texture, + SharedObjectPointer material = _materialEditor->getObject(); + _materialEditor->detachObject(); + return QVariant::fromValue(PaintHeightfieldMaterialEdit(_position, _radius->value(), material, _texture ? _texture->getAverageColor() : QColor())); } } -void HeightfieldTextureBrushTool::updateTexture() { - HeightfieldTexture* texture = static_cast(_textureEditor->getObject().data()); - _texture = Application::getInstance()->getTextureCache()->getTexture(texture->getURL()); +void HeightfieldMaterialBrushTool::updateTexture() { + MaterialObject* material = static_cast(_materialEditor->getObject().data()); + _texture = Application::getInstance()->getTextureCache()->getTexture(material->getDiffuse(), SPLAT_TEXTURE); +} + +VoxelColorBoxTool::VoxelColorBoxTool(MetavoxelEditor* editor) : + BoxTool(editor, "Set Voxel Color (Box)", false) { + + QWidget* widget = new QWidget(); + QFormLayout* form = new QFormLayout(); + widget->setLayout(form); + layout()->addWidget(widget); + + form->addRow("Color:", _color = new QColorEditor(this)); +} + +bool VoxelColorBoxTool::appliesTo(const AttributePointer& attribute) const { + return attribute->inherits("VoxelColorAttribute"); +} + +QColor VoxelColorBoxTool::getColor() { + return _color->getColor(); +} + +void VoxelColorBoxTool::applyValue(const glm::vec3& minimum, const glm::vec3& maximum) { + MetavoxelEditMessage message = { QVariant::fromValue(VoxelColorBoxEdit(Box(minimum, maximum), + _editor->getGridSpacing(), _color->getColor())) }; + Application::getInstance()->getMetavoxels()->applyEdit(message, true); +} + +VoxelMaterialBoxTool::VoxelMaterialBoxTool(MetavoxelEditor* editor) : + BoxTool(editor, "Set Voxel Material (Box)", false) { + + QWidget* widget = new QWidget(); + QFormLayout* form = new QFormLayout(); + widget->setLayout(form); + layout()->addWidget(widget); + + form->addRow(_materialEditor = new SharedObjectEditor(&MaterialObject::staticMetaObject, false)); + connect(_materialEditor, &SharedObjectEditor::objectChanged, this, &VoxelMaterialBoxTool::updateTexture); +} + +bool VoxelMaterialBoxTool::appliesTo(const AttributePointer& attribute) const { + return attribute->inherits("VoxelColorAttribute"); +} + +QColor VoxelMaterialBoxTool::getColor() { + return _texture ? _texture->getAverageColor() : QColor(); +} + +void VoxelMaterialBoxTool::applyValue(const glm::vec3& minimum, const glm::vec3& maximum) { + SharedObjectPointer material = _materialEditor->getObject(); + _materialEditor->detachObject(); + MetavoxelEditMessage message = { QVariant::fromValue(VoxelMaterialBoxEdit(Box(minimum, maximum), + _editor->getGridSpacing(), material, _texture ? _texture->getAverageColor() : QColor())) }; + Application::getInstance()->getMetavoxels()->applyEdit(message, true); +} + +void VoxelMaterialBoxTool::updateTexture() { + MaterialObject* material = static_cast(_materialEditor->getObject().data()); + _texture = Application::getInstance()->getTextureCache()->getTexture(material->getDiffuse(), SPLAT_TEXTURE); +} + +SphereTool::SphereTool(MetavoxelEditor* editor, const QString& name) : + MetavoxelTool(editor, name, false, true) { + + QWidget* widget = new QWidget(); + widget->setLayout(_form = new QFormLayout()); + layout()->addWidget(widget); + + _form->addRow("Radius:", _radius = new QDoubleSpinBox()); + _radius->setSingleStep(0.01); + _radius->setMaximum(FLT_MAX); + _radius->setValue(1.0); +} + +void SphereTool::render() { + if (Application::getInstance()->isMouseHidden()) { + return; + } + glm::quat rotation = _editor->getGridRotation(); + glm::quat inverseRotation = glm::inverse(rotation); + glm::vec3 rayOrigin = inverseRotation * Application::getInstance()->getMouseRayOrigin(); + glm::vec3 rayDirection = inverseRotation * Application::getInstance()->getMouseRayDirection(); + float position = _editor->getGridPosition(); + if (glm::abs(rayDirection.z) < EPSILON) { + return; + } + float distance = (position - rayOrigin.z) / rayDirection.z; + _position = Application::getInstance()->getMouseRayOrigin() + + Application::getInstance()->getMouseRayDirection() * distance; + + glPushMatrix(); + glTranslatef(_position.x, _position.y, _position.z); + + const float CURSOR_ALPHA = 0.5f; + QColor color = getColor(); + glColor4f(color.redF(), color.greenF(), color.blueF(), CURSOR_ALPHA); + + glEnable(GL_CULL_FACE); + + glutSolidSphere(_radius->value(), 10, 10); + + glDisable(GL_CULL_FACE); + + glPopMatrix(); +} + +bool SphereTool::eventFilter(QObject* watched, QEvent* event) { + if (event->type() == QEvent::Wheel) { + float angle = static_cast(event)->angleDelta().y(); + const float ANGLE_SCALE = 1.0f / 1000.0f; + _radius->setValue(_radius->value() * glm::pow(2.0f, angle * ANGLE_SCALE)); + return true; + + } else if (event->type() == QEvent::MouseButtonPress) { + applyValue(_position, _radius->value()); + return true; + } + return false; +} + +VoxelColorSphereTool::VoxelColorSphereTool(MetavoxelEditor* editor) : + SphereTool(editor, "Set Voxel Color (Sphere)") { + + _form->addRow("Color:", _color = new QColorEditor(this)); +} + +bool VoxelColorSphereTool::appliesTo(const AttributePointer& attribute) const { + return attribute->inherits("VoxelColorAttribute"); +} + +QColor VoxelColorSphereTool::getColor() { + return _color->getColor(); +} + +void VoxelColorSphereTool::applyValue(const glm::vec3& position, float radius) { + MetavoxelEditMessage message = { QVariant::fromValue(VoxelColorSphereEdit(position, radius, + _editor->getGridSpacing(), _color->getColor())) }; + Application::getInstance()->getMetavoxels()->applyEdit(message, true); +} + +VoxelMaterialSphereTool::VoxelMaterialSphereTool(MetavoxelEditor* editor) : + SphereTool(editor, "Set Voxel Material (Sphere)") { + + _form->addRow(_materialEditor = new SharedObjectEditor(&MaterialObject::staticMetaObject, false)); + connect(_materialEditor, &SharedObjectEditor::objectChanged, this, &VoxelMaterialSphereTool::updateTexture); +} + +bool VoxelMaterialSphereTool::appliesTo(const AttributePointer& attribute) const { + return attribute->inherits("VoxelColorAttribute"); +} + +QColor VoxelMaterialSphereTool::getColor() { + return _texture ? _texture->getAverageColor() : QColor(); +} + +void VoxelMaterialSphereTool::applyValue(const glm::vec3& position, float radius) { + SharedObjectPointer material = _materialEditor->getObject(); + _materialEditor->detachObject(); + MetavoxelEditMessage message = { QVariant::fromValue(VoxelMaterialSphereEdit(position, radius, + _editor->getGridSpacing(), material, _texture ? _texture->getAverageColor() : QColor())) }; + Application::getInstance()->getMetavoxels()->applyEdit(message, true); +} + +void VoxelMaterialSphereTool::updateTexture() { + MaterialObject* material = static_cast(_materialEditor->getObject().data()); + _texture = Application::getInstance()->getTextureCache()->getTexture(material->getDiffuse(), SPLAT_TEXTURE); } diff --git a/interface/src/ui/MetavoxelEditor.h b/interface/src/ui/MetavoxelEditor.h index 7e37b819d7..447d2197cd 100644 --- a/interface/src/ui/MetavoxelEditor.h +++ b/interface/src/ui/MetavoxelEditor.h @@ -57,6 +57,7 @@ private slots: void deleteSelectedAttribute(); void centerGridPosition(); void alignGridPosition(); + void updateAttributes(const QString& select = QString()); void updateTool(); void simulate(float deltaTime); @@ -65,11 +66,11 @@ private slots: private: void addTool(MetavoxelTool* tool); - void updateAttributes(const QString& select = QString()); MetavoxelTool* getActiveTool() const; QListWidget* _attributes; QPushButton* _deleteAttribute; + QCheckBox* _showAll; QComboBox* _gridPlane; QDoubleSpinBox* _gridSpacing; @@ -90,10 +91,12 @@ class MetavoxelTool : public QWidget { public: - MetavoxelTool(MetavoxelEditor* editor, const QString& name, bool usesValue = true); + MetavoxelTool(MetavoxelEditor* editor, const QString& name, bool usesValue = true, bool userFacing = true); bool getUsesValue() const { return _usesValue; } + bool isUserFacing() const { return _userFacing; } + virtual bool appliesTo(const AttributePointer& attribute) const; virtual void simulate(float deltaTime); @@ -105,24 +108,30 @@ protected: MetavoxelEditor* _editor; bool _usesValue; + bool _userFacing; }; -/// Allows setting the value of a region by dragging out a box. -class BoxSetTool : public MetavoxelTool { +/// Base class for tools that allow dragging out a 3D box. +class BoxTool : public MetavoxelTool { Q_OBJECT public: - BoxSetTool(MetavoxelEditor* editor); - + BoxTool(MetavoxelEditor* editor, const QString& name, bool usesValue = true, bool userFacing = true); + virtual void render(); virtual bool eventFilter(QObject* watched, QEvent* event); +protected: + + virtual QColor getColor() = 0; + + virtual void applyValue(const glm::vec3& minimum, const glm::vec3& maximum) = 0; + private: void resetState(); - void applyValue(const glm::vec3& minimum, const glm::vec3& maximum); enum State { HOVERING_STATE, DRAGGING_STATE, RAISING_STATE }; @@ -134,6 +143,21 @@ private: float _height; ///< the selection height }; +/// Allows setting the value of a region by dragging out a box. +class BoxSetTool : public BoxTool { + Q_OBJECT + +public: + + BoxSetTool(MetavoxelEditor* editor); + +protected: + + virtual QColor getColor(); + + virtual void applyValue(const glm::vec3& minimum, const glm::vec3& maximum); +}; + /// Allows setting the value across the entire space. class GlobalSetTool : public MetavoxelTool { Q_OBJECT @@ -259,8 +283,6 @@ public: ImportHeightfieldTool(MetavoxelEditor* editor); - virtual void render(); - protected: virtual void apply(); @@ -270,6 +292,7 @@ private slots: void selectHeightFile(); void selectColorFile(); void updatePreview(); + void renderPreview(); private: @@ -363,12 +386,12 @@ private: }; /// Allows texturing parts of the heightfield. -class HeightfieldTextureBrushTool : public HeightfieldBrushTool { +class HeightfieldMaterialBrushTool : public HeightfieldBrushTool { Q_OBJECT public: - HeightfieldTextureBrushTool(MetavoxelEditor* editor); + HeightfieldMaterialBrushTool(MetavoxelEditor* editor); protected: @@ -380,7 +403,125 @@ private slots: private: - SharedObjectEditor* _textureEditor; + SharedObjectEditor* _materialEditor; + QSharedPointer _texture; +}; + +/// Allows setting voxel colors by dragging out a box. +class VoxelColorBoxTool : public BoxTool { + Q_OBJECT + +public: + + VoxelColorBoxTool(MetavoxelEditor* editor); + + virtual bool appliesTo(const AttributePointer& attribute) const; + +protected: + + virtual QColor getColor(); + + virtual void applyValue(const glm::vec3& minimum, const glm::vec3& maximum); + +private: + + QColorEditor* _color; +}; + +/// Allows setting voxel materials by dragging out a box. +class VoxelMaterialBoxTool : public BoxTool { + Q_OBJECT + +public: + + VoxelMaterialBoxTool(MetavoxelEditor* editor); + + virtual bool appliesTo(const AttributePointer& attribute) const; + +protected: + + virtual QColor getColor(); + + virtual void applyValue(const glm::vec3& minimum, const glm::vec3& maximum); + +private slots: + + void updateTexture(); + +private: + + SharedObjectEditor* _materialEditor; + QSharedPointer _texture; +}; + +/// Base class for tools based on a sphere brush. +class SphereTool : public MetavoxelTool { + Q_OBJECT + +public: + + SphereTool(MetavoxelEditor* editor, const QString& name); + + virtual void render(); + + virtual bool eventFilter(QObject* watched, QEvent* event); + +protected: + + virtual QColor getColor() = 0; + + virtual void applyValue(const glm::vec3& position, float radius) = 0; + + QFormLayout* _form; + QDoubleSpinBox* _radius; + + glm::vec3 _position; +}; + +/// Allows setting voxel colors by moving a sphere around. +class VoxelColorSphereTool : public SphereTool { + Q_OBJECT + +public: + + VoxelColorSphereTool(MetavoxelEditor* editor); + + virtual bool appliesTo(const AttributePointer& attribute) const; + +protected: + + virtual QColor getColor(); + + virtual void applyValue(const glm::vec3& position, float radius); + +private: + + QColorEditor* _color; +}; + +/// Allows setting voxel materials by moving a sphere around. +class VoxelMaterialSphereTool : public SphereTool { + Q_OBJECT + +public: + + VoxelMaterialSphereTool(MetavoxelEditor* editor); + + virtual bool appliesTo(const AttributePointer& attribute) const; + +protected: + + virtual QColor getColor(); + + virtual void applyValue(const glm::vec3& position, float radius); + +private slots: + + void updateTexture(); + +private: + + SharedObjectEditor* _materialEditor; QSharedPointer _texture; }; diff --git a/libraries/metavoxels/src/AttributeRegistry.cpp b/libraries/metavoxels/src/AttributeRegistry.cpp index 425bf8ff4a..aec9a069be 100644 --- a/libraries/metavoxels/src/AttributeRegistry.cpp +++ b/libraries/metavoxels/src/AttributeRegistry.cpp @@ -23,13 +23,16 @@ REGISTER_META_OBJECT(QRgbAttribute) REGISTER_META_OBJECT(PackedNormalAttribute) REGISTER_META_OBJECT(SpannerQRgbAttribute) REGISTER_META_OBJECT(SpannerPackedNormalAttribute) -REGISTER_META_OBJECT(HeightfieldTexture) +REGISTER_META_OBJECT(MaterialObject) REGISTER_META_OBJECT(HeightfieldAttribute) REGISTER_META_OBJECT(HeightfieldColorAttribute) -REGISTER_META_OBJECT(HeightfieldTextureAttribute) +REGISTER_META_OBJECT(HeightfieldMaterialAttribute) REGISTER_META_OBJECT(SharedObjectAttribute) REGISTER_META_OBJECT(SharedObjectSetAttribute) REGISTER_META_OBJECT(SpannerSetAttribute) +REGISTER_META_OBJECT(VoxelColorAttribute) +REGISTER_META_OBJECT(VoxelHermiteAttribute) +REGISTER_META_OBJECT(VoxelMaterialAttribute) static int attributePointerMetaTypeId = qRegisterMetaType(); static int ownedAttributeValueMetaTypeId = qRegisterMetaType(); @@ -52,7 +55,10 @@ AttributeRegistry::AttributeRegistry() : _spannerMaskAttribute(registerAttribute(new FloatAttribute("spannerMask"))), _heightfieldAttribute(registerAttribute(new HeightfieldAttribute("heightfield"))), _heightfieldColorAttribute(registerAttribute(new HeightfieldColorAttribute("heightfieldColor"))), - _heightfieldTextureAttribute(registerAttribute(new HeightfieldTextureAttribute("heightfieldTexture"))) { + _heightfieldMaterialAttribute(registerAttribute(new HeightfieldMaterialAttribute("heightfieldMaterial"))), + _voxelColorAttribute(registerAttribute(new VoxelColorAttribute("voxelColor"))), + _voxelMaterialAttribute(registerAttribute(new VoxelMaterialAttribute("voxelMaterial"))), + _voxelHermiteAttribute(registerAttribute(new VoxelHermiteAttribute("voxelHermite"))) { // our baseline LOD threshold is for voxels; spanners and heightfields are a different story const float SPANNER_LOD_THRESHOLD_MULTIPLIER = 8.0f; @@ -60,8 +66,15 @@ AttributeRegistry::AttributeRegistry() : const float HEIGHTFIELD_LOD_THRESHOLD_MULTIPLIER = 32.0f; _heightfieldAttribute->setLODThresholdMultiplier(HEIGHTFIELD_LOD_THRESHOLD_MULTIPLIER); + _heightfieldAttribute->setUserFacing(true); _heightfieldColorAttribute->setLODThresholdMultiplier(HEIGHTFIELD_LOD_THRESHOLD_MULTIPLIER); - _heightfieldTextureAttribute->setLODThresholdMultiplier(HEIGHTFIELD_LOD_THRESHOLD_MULTIPLIER); + _heightfieldMaterialAttribute->setLODThresholdMultiplier(HEIGHTFIELD_LOD_THRESHOLD_MULTIPLIER); + + const float VOXEL_LOD_THRESHOLD_MULTIPLIER = 32.0f; + _voxelColorAttribute->setLODThresholdMultiplier(VOXEL_LOD_THRESHOLD_MULTIPLIER); + _voxelColorAttribute->setUserFacing(true); + _voxelMaterialAttribute->setLODThresholdMultiplier(VOXEL_LOD_THRESHOLD_MULTIPLIER); + _voxelHermiteAttribute->setLODThresholdMultiplier(VOXEL_LOD_THRESHOLD_MULTIPLIER); } static QScriptValue qDebugFunction(QScriptContext* context, QScriptEngine* engine) { @@ -201,7 +214,8 @@ OwnedAttributeValue& OwnedAttributeValue::operator=(const OwnedAttributeValue& o } Attribute::Attribute(const QString& name) : - _lodThresholdMultiplier(1.0f) { + _lodThresholdMultiplier(1.0f), + _userFacing(false) { setObjectName(name); } @@ -392,6 +406,10 @@ QRgb packNormal(const glm::vec3& normal) { return qRgb((char)(normal.x * CHAR_SCALE), (char)(normal.y * CHAR_SCALE), (char)(normal.z * CHAR_SCALE)); } +QRgb packNormal(const glm::vec3& normal, int alpha) { + return qRgba((char)(normal.x * CHAR_SCALE), (char)(normal.y * CHAR_SCALE), (char)(normal.z * CHAR_SCALE), alpha); +} + glm::vec3 unpackNormal(QRgb value) { return glm::vec3((char)qRed(value) * INVERSE_CHAR_SCALE, (char)qGreen(value) * INVERSE_CHAR_SCALE, (char)qBlue(value) * INVERSE_CHAR_SCALE); @@ -497,11 +515,7 @@ AttributeValue SpannerPackedNormalAttribute::inherit(const AttributeValue& paren return AttributeValue(parentValue.getAttribute()); } -HeightfieldData::HeightfieldData(const QByteArray& contents) : - _contents(contents) { -} - -HeightfieldData::~HeightfieldData() { +DataBlock::~DataBlock() { } enum HeightfieldImage { NULL_HEIGHTFIELD_IMAGE, NORMAL_HEIGHTFIELD_IMAGE, DEFLATED_HEIGHTFIELD_IMAGE }; @@ -548,7 +562,7 @@ const QImage decodeHeightfieldImage(const QByteArray& data) { } HeightfieldHeightData::HeightfieldHeightData(const QByteArray& contents) : - HeightfieldData(contents) { + _contents(contents) { } HeightfieldHeightData::HeightfieldHeightData(Bitstream& in, int bytes) { @@ -562,7 +576,7 @@ HeightfieldHeightData::HeightfieldHeightData(Bitstream& in, int bytes, const Hei } QMutexLocker locker(&reference->getEncodedDeltaMutex()); reference->setEncodedDelta(in.readAligned(bytes)); - reference->setDeltaData(HeightfieldDataPointer(this)); + reference->setDeltaData(DataBlockPointer(this)); _contents = reference->getContents(); QImage image = decodeHeightfieldImage(reference->getEncodedDelta()); if (image.isNull()) { @@ -685,7 +699,7 @@ void HeightfieldHeightData::writeDelta(Bitstream& out, const HeightfieldHeightDa } image.setOffset(QPoint(minX + 1, minY + 1)); reference->setEncodedDelta(encodeHeightfieldImage(image)); - reference->setDeltaData(HeightfieldDataPointer(this)); + reference->setDeltaData(DataBlockPointer(this)); } out << reference->getEncodedDelta().size(); out.writeAligned(reference->getEncodedDelta()); @@ -744,7 +758,7 @@ void HeightfieldHeightData::set(const QImage& image) { } HeightfieldColorData::HeightfieldColorData(const QByteArray& contents) : - HeightfieldData(contents) { + _contents(contents) { } HeightfieldColorData::HeightfieldColorData(Bitstream& in, int bytes) { @@ -758,7 +772,7 @@ HeightfieldColorData::HeightfieldColorData(Bitstream& in, int bytes, const Heigh } QMutexLocker locker(&reference->getEncodedDeltaMutex()); reference->setEncodedDelta(in.readAligned(bytes)); - reference->setDeltaData(HeightfieldDataPointer(this)); + reference->setDeltaData(DataBlockPointer(this)); _contents = reference->getContents(); QImage image = decodeHeightfieldImage(reference->getEncodedDelta()); if (image.isNull()) { @@ -875,7 +889,7 @@ void HeightfieldColorData::writeDelta(Bitstream& out, const HeightfieldColorData } image.setOffset(QPoint(minX + 1, minY + 1)); reference->setEncodedDelta(encodeHeightfieldImage(image)); - reference->setDeltaData(HeightfieldDataPointer(this)); + reference->setDeltaData(DataBlockPointer(this)); } out << reference->getEncodedDelta().size(); out.writeAligned(reference->getEncodedDelta()); @@ -930,10 +944,10 @@ void HeightfieldColorData::set(const QImage& image) { memcpy(_contents.data(), image.constBits(), _contents.size()); } -const int TEXTURE_HEADER_SIZE = sizeof(qint32) * 4; +const int HEIGHTFIELD_MATERIAL_HEADER_SIZE = sizeof(qint32) * 4; -static QByteArray encodeTexture(int offsetX, int offsetY, int width, int height, const QByteArray& contents) { - QByteArray inflated(TEXTURE_HEADER_SIZE, 0); +static QByteArray encodeHeightfieldMaterial(int offsetX, int offsetY, int width, int height, const QByteArray& contents) { + QByteArray inflated(HEIGHTFIELD_MATERIAL_HEADER_SIZE, 0); qint32* header = (qint32*)inflated.data(); *header++ = offsetX; *header++ = offsetY; @@ -943,38 +957,38 @@ static QByteArray encodeTexture(int offsetX, int offsetY, int width, int height, return qCompress(inflated); } -static QByteArray decodeTexture(const QByteArray& encoded, int& offsetX, int& offsetY, int& width, int& height) { +static QByteArray decodeHeightfieldMaterial(const QByteArray& encoded, int& offsetX, int& offsetY, int& width, int& height) { QByteArray inflated = qUncompress(encoded); const qint32* header = (const qint32*)inflated.constData(); offsetX = *header++; offsetY = *header++; width = *header++; height = *header++; - return inflated.mid(TEXTURE_HEADER_SIZE); + return inflated.mid(HEIGHTFIELD_MATERIAL_HEADER_SIZE); } -HeightfieldTextureData::HeightfieldTextureData(const QByteArray& contents, const QVector& textures) : - HeightfieldData(contents), - _textures(textures) { +HeightfieldMaterialData::HeightfieldMaterialData(const QByteArray& contents, const QVector& materials) : + _contents(contents), + _materials(materials) { } -HeightfieldTextureData::HeightfieldTextureData(Bitstream& in, int bytes) { +HeightfieldMaterialData::HeightfieldMaterialData(Bitstream& in, int bytes) { read(in, bytes); } -HeightfieldTextureData::HeightfieldTextureData(Bitstream& in, int bytes, const HeightfieldTextureDataPointer& reference) { +HeightfieldMaterialData::HeightfieldMaterialData(Bitstream& in, int bytes, const HeightfieldMaterialDataPointer& reference) { if (!reference) { read(in, bytes); return; } QMutexLocker locker(&reference->getEncodedDeltaMutex()); reference->setEncodedDelta(in.readAligned(bytes)); - in.readDelta(_textures, reference->getTextures()); - reference->setDeltaData(HeightfieldDataPointer(this)); + in.readDelta(_materials, reference->getMaterials()); + reference->setDeltaData(DataBlockPointer(this)); _contents = reference->getContents(); int offsetX, offsetY, width, height; - QByteArray delta = decodeTexture(reference->getEncodedDelta(), offsetX, offsetY, width, height); + QByteArray delta = decodeHeightfieldMaterial(reference->getEncodedDelta(), offsetX, offsetY, width, height); if (delta.isEmpty()) { return; } @@ -992,18 +1006,18 @@ HeightfieldTextureData::HeightfieldTextureData(Bitstream& in, int bytes, const H } } -void HeightfieldTextureData::write(Bitstream& out) { +void HeightfieldMaterialData::write(Bitstream& out) { QMutexLocker locker(&_encodedMutex); if (_encoded.isEmpty()) { int size = glm::sqrt((float)_contents.size()); - _encoded = encodeTexture(0, 0, size, size, _contents); + _encoded = encodeHeightfieldMaterial(0, 0, size, size, _contents); } out << _encoded.size(); out.writeAligned(_encoded); - out << _textures; + out << _materials; } -void HeightfieldTextureData::writeDelta(Bitstream& out, const HeightfieldTextureDataPointer& reference) { +void HeightfieldMaterialData::writeDelta(Bitstream& out, const HeightfieldMaterialDataPointer& reference) { if (!reference || reference->getContents().size() != _contents.size()) { write(out); return; @@ -1041,21 +1055,21 @@ void HeightfieldTextureData::writeDelta(Bitstream& out, const HeightfieldTexture memcpy(dest, src, width); } } - reference->setEncodedDelta(encodeTexture(minX + 1, minY + 1, width, height, delta)); - reference->setDeltaData(HeightfieldDataPointer(this)); + reference->setEncodedDelta(encodeHeightfieldMaterial(minX + 1, minY + 1, width, height, delta)); + reference->setDeltaData(DataBlockPointer(this)); } out << reference->getEncodedDelta().size(); out.writeAligned(reference->getEncodedDelta()); - out.writeDelta(_textures, reference->getTextures()); + out.writeDelta(_materials, reference->getMaterials()); } -void HeightfieldTextureData::read(Bitstream& in, int bytes) { +void HeightfieldMaterialData::read(Bitstream& in, int bytes) { int offsetX, offsetY, width, height; - _contents = decodeTexture(_encoded = in.readAligned(bytes), offsetX, offsetY, width, height); - in >> _textures; + _contents = decodeHeightfieldMaterial(_encoded = in.readAligned(bytes), offsetX, offsetY, width, height); + in >> _materials; } -HeightfieldTexture::HeightfieldTexture() : +MaterialObject::MaterialObject() : _scaleS(1.0f), _scaleT(1.0f) { } @@ -1251,8 +1265,8 @@ bool HeightfieldColorAttribute::merge(void*& parent, void* children[], bool post *(HeightfieldColorDataPointer*)&parent = HeightfieldColorDataPointer(); return true; } - int size = glm::sqrt(maxSize / (float)HeightfieldData::COLOR_BYTES); - QByteArray contents(size * size * HeightfieldData::COLOR_BYTES, 0); + int size = glm::sqrt(maxSize / (float)DataBlock::COLOR_BYTES); + QByteArray contents(size * size * DataBlock::COLOR_BYTES, 0); int halfSize = size / 2; for (int i = 0; i < MERGE_COUNT; i++) { HeightfieldColorDataPointer child = decodeInline(children[i]); @@ -1260,7 +1274,7 @@ bool HeightfieldColorAttribute::merge(void*& parent, void* children[], bool post continue; } const QByteArray& childContents = child->getContents(); - int childSize = glm::sqrt(childContents.size() / (float)HeightfieldData::COLOR_BYTES); + int childSize = glm::sqrt(childContents.size() / (float)DataBlock::COLOR_BYTES); const int INDEX_MASK = 1; int xIndex = i & INDEX_MASK; const int Y_SHIFT = 1; @@ -1270,24 +1284,24 @@ bool HeightfieldColorAttribute::merge(void*& parent, void* children[], bool post } int Z_SHIFT = 2; int zIndex = (i >> Z_SHIFT) & INDEX_MASK; - char* dest = contents.data() + ((zIndex * halfSize * size) + (xIndex * halfSize)) * HeightfieldData::COLOR_BYTES; + char* dest = contents.data() + ((zIndex * halfSize * size) + (xIndex * halfSize)) * DataBlock::COLOR_BYTES; uchar* src = (uchar*)childContents.data(); - int childStride = childSize * HeightfieldData::COLOR_BYTES; - int stride = size * HeightfieldData::COLOR_BYTES; + int childStride = childSize * DataBlock::COLOR_BYTES; + int stride = size * DataBlock::COLOR_BYTES; int halfStride = stride / 2; - int childStep = 2 * HeightfieldData::COLOR_BYTES; - int redOffset3 = childStride + HeightfieldData::COLOR_BYTES; - int greenOffset1 = HeightfieldData::COLOR_BYTES + 1; + int childStep = 2 * DataBlock::COLOR_BYTES; + int redOffset3 = childStride + DataBlock::COLOR_BYTES; + int greenOffset1 = DataBlock::COLOR_BYTES + 1; int greenOffset2 = childStride + 1; - int greenOffset3 = childStride + HeightfieldData::COLOR_BYTES + 1; - int blueOffset1 = HeightfieldData::COLOR_BYTES + 2; + int greenOffset3 = childStride + DataBlock::COLOR_BYTES + 1; + int blueOffset1 = DataBlock::COLOR_BYTES + 2; int blueOffset2 = childStride + 2; - int blueOffset3 = childStride + HeightfieldData::COLOR_BYTES + 2; + int blueOffset3 = childStride + DataBlock::COLOR_BYTES + 2; if (childSize == size) { // simple case: one destination value for four child values for (int z = 0; z < halfSize; z++) { - for (char* end = dest + halfSize * HeightfieldData::COLOR_BYTES; dest != end; src += childStep) { - *dest++ = ((int)src[0] + (int)src[HeightfieldData::COLOR_BYTES] + + for (char* end = dest + halfSize * DataBlock::COLOR_BYTES; dest != end; src += childStep) { + *dest++ = ((int)src[0] + (int)src[DataBlock::COLOR_BYTES] + (int)src[childStride] + (int)src[redOffset3]) >> 2; *dest++ = ((int)src[1] + (int)src[greenOffset1] + (int)src[greenOffset2] + (int)src[greenOffset3]) >> 2; *dest++ = ((int)src[2] + (int)src[blueOffset1] + (int)src[blueOffset2] + (int)src[blueOffset3]) >> 2; @@ -1300,14 +1314,14 @@ bool HeightfieldColorAttribute::merge(void*& parent, void* children[], bool post int halfChildSize = childSize / 2; int destPerSrc = size / childSize; for (int z = 0; z < halfChildSize; z++) { - for (uchar* end = src + childSize * HeightfieldData::COLOR_BYTES; src != end; src += childStep) { - *dest++ = ((int)src[0] + (int)src[HeightfieldData::COLOR_BYTES] + + for (uchar* end = src + childSize * DataBlock::COLOR_BYTES; src != end; src += childStep) { + *dest++ = ((int)src[0] + (int)src[DataBlock::COLOR_BYTES] + (int)src[childStride] + (int)src[redOffset3]) >> 2; *dest++ = ((int)src[1] + (int)src[greenOffset1] + (int)src[greenOffset2] + (int)src[greenOffset3]) >> 2; *dest++ = ((int)src[2] + (int)src[blueOffset1] + (int)src[blueOffset2] + (int)src[blueOffset3]) >> 2; for (int j = 1; j < destPerSrc; j++) { - memcpy(dest, dest - HeightfieldData::COLOR_BYTES, HeightfieldData::COLOR_BYTES); - dest += HeightfieldData::COLOR_BYTES; + memcpy(dest, dest - DataBlock::COLOR_BYTES, DataBlock::COLOR_BYTES); + dest += DataBlock::COLOR_BYTES; } } dest += halfStride; @@ -1323,28 +1337,28 @@ bool HeightfieldColorAttribute::merge(void*& parent, void* children[], bool post return false; } -HeightfieldTextureAttribute::HeightfieldTextureAttribute(const QString& name) : - InlineAttribute(name) { +HeightfieldMaterialAttribute::HeightfieldMaterialAttribute(const QString& name) : + InlineAttribute(name) { } -void HeightfieldTextureAttribute::read(Bitstream& in, void*& value, bool isLeaf) const { +void HeightfieldMaterialAttribute::read(Bitstream& in, void*& value, bool isLeaf) const { if (!isLeaf) { return; } int size; in >> size; if (size == 0) { - *(HeightfieldTextureDataPointer*)&value = HeightfieldTextureDataPointer(); + *(HeightfieldMaterialDataPointer*)&value = HeightfieldMaterialDataPointer(); } else { - *(HeightfieldTextureDataPointer*)&value = HeightfieldTextureDataPointer(new HeightfieldTextureData(in, size)); + *(HeightfieldMaterialDataPointer*)&value = HeightfieldMaterialDataPointer(new HeightfieldMaterialData(in, size)); } } -void HeightfieldTextureAttribute::write(Bitstream& out, void* value, bool isLeaf) const { +void HeightfieldMaterialAttribute::write(Bitstream& out, void* value, bool isLeaf) const { if (!isLeaf) { return; } - HeightfieldTextureDataPointer data = decodeInline(value); + HeightfieldMaterialDataPointer data = decodeInline(value); if (data) { data->write(out); } else { @@ -1352,41 +1366,661 @@ void HeightfieldTextureAttribute::write(Bitstream& out, void* value, bool isLeaf } } -void HeightfieldTextureAttribute::readDelta(Bitstream& in, void*& value, void* reference, bool isLeaf) const { +void HeightfieldMaterialAttribute::readDelta(Bitstream& in, void*& value, void* reference, bool isLeaf) const { if (!isLeaf) { return; } int size; in >> size; if (size == 0) { - *(HeightfieldTextureDataPointer*)&value = HeightfieldTextureDataPointer(); + *(HeightfieldMaterialDataPointer*)&value = HeightfieldMaterialDataPointer(); } else { - *(HeightfieldTextureDataPointer*)&value = HeightfieldTextureDataPointer(new HeightfieldTextureData( - in, size, decodeInline(reference))); + *(HeightfieldMaterialDataPointer*)&value = HeightfieldMaterialDataPointer(new HeightfieldMaterialData( + in, size, decodeInline(reference))); } } -void HeightfieldTextureAttribute::writeDelta(Bitstream& out, void* value, void* reference, bool isLeaf) const { +void HeightfieldMaterialAttribute::writeDelta(Bitstream& out, void* value, void* reference, bool isLeaf) const { if (!isLeaf) { return; } - HeightfieldTextureDataPointer data = decodeInline(value); + HeightfieldMaterialDataPointer data = decodeInline(value); if (data) { - data->writeDelta(out, decodeInline(reference)); + data->writeDelta(out, decodeInline(reference)); } else { out << 0; } } -bool HeightfieldTextureAttribute::merge(void*& parent, void* children[], bool postRead) const { +bool HeightfieldMaterialAttribute::merge(void*& parent, void* children[], bool postRead) const { int maxSize = 0; for (int i = 0; i < MERGE_COUNT; i++) { - HeightfieldTextureDataPointer pointer = decodeInline(children[i]); + HeightfieldMaterialDataPointer pointer = decodeInline(children[i]); if (pointer) { maxSize = qMax(maxSize, pointer->getContents().size()); } } - *(HeightfieldTextureDataPointer*)&parent = HeightfieldTextureDataPointer(); + *(HeightfieldMaterialDataPointer*)&parent = HeightfieldMaterialDataPointer(); + return maxSize == 0; +} + +const int VOXEL_COLOR_HEADER_SIZE = sizeof(qint32) * 6; + +static QByteArray encodeVoxelColor(int offsetX, int offsetY, int offsetZ, + int sizeX, int sizeY, int sizeZ, const QVector& contents) { + QByteArray inflated(VOXEL_COLOR_HEADER_SIZE, 0); + qint32* header = (qint32*)inflated.data(); + *header++ = offsetX; + *header++ = offsetY; + *header++ = offsetZ; + *header++ = sizeX; + *header++ = sizeY; + *header++ = sizeZ; + inflated.append((const char*)contents.constData(), contents.size() * sizeof(QRgb)); + return qCompress(inflated); +} + +static QVector decodeVoxelColor(const QByteArray& encoded, int& offsetX, int& offsetY, int& offsetZ, + int& sizeX, int& sizeY, int& sizeZ) { + QByteArray inflated = qUncompress(encoded); + const qint32* header = (const qint32*)inflated.constData(); + offsetX = *header++; + offsetY = *header++; + offsetZ = *header++; + sizeX = *header++; + sizeY = *header++; + sizeZ = *header++; + int payloadSize = inflated.size() - VOXEL_COLOR_HEADER_SIZE; + QVector contents(payloadSize / sizeof(QRgb)); + memcpy(contents.data(), inflated.constData() + VOXEL_COLOR_HEADER_SIZE, payloadSize); + return contents; +} + +VoxelColorData::VoxelColorData(const QVector& contents, int size) : + _contents(contents), + _size(size) { +} + +VoxelColorData::VoxelColorData(Bitstream& in, int bytes) { + read(in, bytes); +} + +VoxelColorData::VoxelColorData(Bitstream& in, int bytes, const VoxelColorDataPointer& reference) { + if (!reference) { + read(in, bytes); + return; + } + QMutexLocker locker(&reference->getEncodedDeltaMutex()); + reference->setEncodedDelta(in.readAligned(bytes)); + reference->setDeltaData(DataBlockPointer(this)); + _contents = reference->getContents(); + _size = reference->getSize(); + + int offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ; + QVector delta = decodeVoxelColor(reference->getEncodedDelta(), offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ); + if (delta.isEmpty()) { + return; + } + if (offsetX == 0) { + _contents = delta; + _size = sizeX; + return; + } + int minX = offsetX - 1; + int minY = offsetY - 1; + int minZ = offsetZ - 1; + const QRgb* src = delta.constData(); + int size2 = _size * _size; + QRgb* planeDest = _contents.data() + minZ * size2 + minY * _size + minX; + int length = sizeX * sizeof(QRgb); + for (int z = 0; z < sizeZ; z++, planeDest += size2) { + QRgb* dest = planeDest; + for (int y = 0; y < sizeY; y++, src += sizeX, dest += _size) { + memcpy(dest, src, length); + } + } +} + +void VoxelColorData::write(Bitstream& out) { + QMutexLocker locker(&_encodedMutex); + if (_encoded.isEmpty()) { + _encoded = encodeVoxelColor(0, 0, 0, _size, _size, _size, _contents); + } + out << _encoded.size(); + out.writeAligned(_encoded); +} + +void VoxelColorData::writeDelta(Bitstream& out, const VoxelColorDataPointer& reference) { + if (!reference || reference->getSize() != _size) { + write(out); + return; + } + QMutexLocker locker(&reference->getEncodedDeltaMutex()); + if (reference->getEncodedDelta().isEmpty() || reference->getDeltaData() != this) { + int minX = _size, minY = _size, minZ = _size; + int maxX = -1, maxY = -1, maxZ = -1; + const QRgb* src = _contents.constData(); + const QRgb* ref = reference->getContents().constData(); + for (int z = 0; z < _size; z++) { + bool differenceZ = false; + for (int y = 0; y < _size; y++) { + bool differenceY = false; + for (int x = 0; x < _size; x++) { + if (*src++ != *ref++) { + minX = qMin(minX, x); + maxX = qMax(maxX, x); + differenceY = differenceZ = true; + } + } + if (differenceY) { + minY = qMin(minY, y); + maxY = qMax(maxY, y); + } + } + if (differenceZ) { + minZ = qMin(minZ, z); + maxZ = qMax(maxZ, z); + } + } + QVector delta; + int sizeX = 0, sizeY = 0, sizeZ = 0; + if (maxX >= minX) { + sizeX = maxX - minX + 1; + sizeY = maxY - minY + 1; + sizeZ = maxZ - minZ + 1; + delta = QVector(sizeX * sizeY * sizeZ, 0); + QRgb* dest = delta.data(); + int size2 = _size * _size; + const QRgb* planeSrc = _contents.constData() + minZ * size2 + minY * _size + minX; + int length = sizeX * sizeof(QRgb); + for (int z = 0; z < sizeZ; z++, planeSrc += size2) { + src = planeSrc; + for (int y = 0; y < sizeY; y++, src += _size, dest += sizeX) { + memcpy(dest, src, length); + } + } + } + reference->setEncodedDelta(encodeVoxelColor(minX + 1, minY + 1, minZ + 1, sizeX, sizeY, sizeZ, delta)); + reference->setDeltaData(DataBlockPointer(this)); + } + out << reference->getEncodedDelta().size(); + out.writeAligned(reference->getEncodedDelta()); +} + +void VoxelColorData::read(Bitstream& in, int bytes) { + int offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ; + _contents = decodeVoxelColor(_encoded = in.readAligned(bytes), offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ); + _size = sizeX; +} + +VoxelColorAttribute::VoxelColorAttribute(const QString& name) : + InlineAttribute(name) { +} + +void VoxelColorAttribute::read(Bitstream& in, void*& value, bool isLeaf) const { + if (!isLeaf) { + return; + } + int size; + in >> size; + if (size == 0) { + *(VoxelColorDataPointer*)&value = VoxelColorDataPointer(); + } else { + *(VoxelColorDataPointer*)&value = VoxelColorDataPointer(new VoxelColorData(in, size)); + } +} + +void VoxelColorAttribute::write(Bitstream& out, void* value, bool isLeaf) const { + if (!isLeaf) { + return; + } + VoxelColorDataPointer data = decodeInline(value); + if (data) { + data->write(out); + } else { + out << 0; + } +} + +void VoxelColorAttribute::readDelta(Bitstream& in, void*& value, void* reference, bool isLeaf) const { + if (!isLeaf) { + return; + } + int size; + in >> size; + if (size == 0) { + *(VoxelColorDataPointer*)&value = VoxelColorDataPointer(); + } else { + *(VoxelColorDataPointer*)&value = VoxelColorDataPointer(new VoxelColorData( + in, size, decodeInline(reference))); + } +} + +void VoxelColorAttribute::writeDelta(Bitstream& out, void* value, void* reference, bool isLeaf) const { + if (!isLeaf) { + return; + } + VoxelColorDataPointer data = decodeInline(value); + if (data) { + data->writeDelta(out, decodeInline(reference)); + } else { + out << 0; + } +} + +bool VoxelColorAttribute::merge(void*& parent, void* children[], bool postRead) const { + int maxSize = 0; + for (int i = 0; i < MERGE_COUNT; i++) { + VoxelColorDataPointer pointer = decodeInline(children[i]); + if (pointer) { + maxSize = qMax(maxSize, pointer->getSize()); + } + } + *(VoxelColorDataPointer*)&parent = VoxelColorDataPointer(); + return maxSize == 0; +} + +const int VOXEL_MATERIAL_HEADER_SIZE = sizeof(qint32) * 6; + +static QByteArray encodeVoxelMaterial(int offsetX, int offsetY, int offsetZ, + int sizeX, int sizeY, int sizeZ, const QByteArray& contents) { + QByteArray inflated(VOXEL_MATERIAL_HEADER_SIZE, 0); + qint32* header = (qint32*)inflated.data(); + *header++ = offsetX; + *header++ = offsetY; + *header++ = offsetZ; + *header++ = sizeX; + *header++ = sizeY; + *header++ = sizeZ; + inflated.append(contents); + return qCompress(inflated); +} + +static QByteArray decodeVoxelMaterial(const QByteArray& encoded, int& offsetX, int& offsetY, int& offsetZ, + int& sizeX, int& sizeY, int& sizeZ) { + QByteArray inflated = qUncompress(encoded); + const qint32* header = (const qint32*)inflated.constData(); + offsetX = *header++; + offsetY = *header++; + offsetZ = *header++; + sizeX = *header++; + sizeY = *header++; + sizeZ = *header++; + return inflated.mid(VOXEL_MATERIAL_HEADER_SIZE); +} + +VoxelMaterialData::VoxelMaterialData(const QByteArray& contents, int size, const QVector& materials) : + _contents(contents), + _size(size), + _materials(materials) { +} + +VoxelMaterialData::VoxelMaterialData(Bitstream& in, int bytes) { + read(in, bytes); +} + +VoxelMaterialData::VoxelMaterialData(Bitstream& in, int bytes, const VoxelMaterialDataPointer& reference) { + if (!reference) { + read(in, bytes); + return; + } + QMutexLocker locker(&reference->getEncodedDeltaMutex()); + reference->setEncodedDelta(in.readAligned(bytes)); + in.readDelta(_materials, reference->getMaterials()); + reference->setDeltaData(DataBlockPointer(this)); + _contents = reference->getContents(); + _size = reference->getSize(); + + int offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ; + QByteArray delta = decodeVoxelMaterial(reference->getEncodedDelta(), offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ); + if (delta.isEmpty()) { + return; + } + if (offsetX == 0) { + _contents = delta; + _size = sizeX; + return; + } + int minX = offsetX - 1; + int minY = offsetY - 1; + int minZ = offsetZ - 1; + const char* src = delta.constData(); + int size2 = _size * _size; + char* planeDest = _contents.data() + minZ * size2 + minY * _size + minX; + for (int z = 0; z < sizeZ; z++, planeDest += size2) { + char* dest = planeDest; + for (int y = 0; y < sizeY; y++, src += sizeX, dest += _size) { + memcpy(dest, src, sizeX); + } + } +} + +void VoxelMaterialData::write(Bitstream& out) { + QMutexLocker locker(&_encodedMutex); + if (_encoded.isEmpty()) { + _encoded = encodeVoxelMaterial(0, 0, 0, _size, _size, _size, _contents); + } + out << _encoded.size(); + out.writeAligned(_encoded); + out << _materials; +} + +void VoxelMaterialData::writeDelta(Bitstream& out, const VoxelMaterialDataPointer& reference) { + if (!reference || reference->getSize() != _size) { + write(out); + return; + } + QMutexLocker locker(&reference->getEncodedDeltaMutex()); + if (reference->getEncodedDelta().isEmpty() || reference->getDeltaData() != this) { + int minX = _size, minY = _size, minZ = _size; + int maxX = -1, maxY = -1, maxZ = -1; + const char* src = _contents.constData(); + const char* ref = reference->getContents().constData(); + for (int z = 0; z < _size; z++) { + bool differenceZ = false; + for (int y = 0; y < _size; y++) { + bool differenceY = false; + for (int x = 0; x < _size; x++) { + if (*src++ != *ref++) { + minX = qMin(minX, x); + maxX = qMax(maxX, x); + differenceY = differenceZ = true; + } + } + if (differenceY) { + minY = qMin(minY, y); + maxY = qMax(maxY, y); + } + } + if (differenceZ) { + minZ = qMin(minZ, z); + maxZ = qMax(maxZ, z); + } + } + QByteArray delta; + int sizeX = 0, sizeY = 0, sizeZ = 0; + if (maxX >= minX) { + sizeX = maxX - minX + 1; + sizeY = maxY - minY + 1; + sizeZ = maxZ - minZ + 1; + delta = QByteArray(sizeX * sizeY * sizeZ, 0); + char* dest = delta.data(); + int size2 = _size * _size; + const char* planeSrc = _contents.constData() + minZ * size2 + minY * _size + minX; + for (int z = 0; z < sizeZ; z++, planeSrc += size2) { + src = planeSrc; + for (int y = 0; y < sizeY; y++, src += _size, dest += sizeX) { + memcpy(dest, src, sizeX); + } + } + } + reference->setEncodedDelta(encodeVoxelMaterial(minX + 1, minY + 1, minZ + 1, sizeX, sizeY, sizeZ, delta)); + reference->setDeltaData(DataBlockPointer(this)); + } + out << reference->getEncodedDelta().size(); + out.writeAligned(reference->getEncodedDelta()); + out.writeDelta(_materials, reference->getMaterials()); +} + +void VoxelMaterialData::read(Bitstream& in, int bytes) { + int offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ; + _contents = decodeVoxelMaterial(_encoded = in.readAligned(bytes), offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ); + _size = sizeX; + in >> _materials; +} + +VoxelMaterialAttribute::VoxelMaterialAttribute(const QString& name) : + InlineAttribute(name) { +} + +void VoxelMaterialAttribute::read(Bitstream& in, void*& value, bool isLeaf) const { + if (!isLeaf) { + return; + } + int size; + in >> size; + if (size == 0) { + *(VoxelMaterialDataPointer*)&value = VoxelMaterialDataPointer(); + } else { + *(VoxelMaterialDataPointer*)&value = VoxelMaterialDataPointer(new VoxelMaterialData(in, size)); + } +} + +void VoxelMaterialAttribute::write(Bitstream& out, void* value, bool isLeaf) const { + if (!isLeaf) { + return; + } + VoxelMaterialDataPointer data = decodeInline(value); + if (data) { + data->write(out); + } else { + out << 0; + } +} + +void VoxelMaterialAttribute::readDelta(Bitstream& in, void*& value, void* reference, bool isLeaf) const { + if (!isLeaf) { + return; + } + int size; + in >> size; + if (size == 0) { + *(VoxelMaterialDataPointer*)&value = VoxelMaterialDataPointer(); + } else { + *(VoxelMaterialDataPointer*)&value = VoxelMaterialDataPointer(new VoxelMaterialData( + in, size, decodeInline(reference))); + } +} + +void VoxelMaterialAttribute::writeDelta(Bitstream& out, void* value, void* reference, bool isLeaf) const { + if (!isLeaf) { + return; + } + VoxelMaterialDataPointer data = decodeInline(value); + if (data) { + data->writeDelta(out, decodeInline(reference)); + } else { + out << 0; + } +} + +bool VoxelMaterialAttribute::merge(void*& parent, void* children[], bool postRead) const { + int maxSize = 0; + for (int i = 0; i < MERGE_COUNT; i++) { + VoxelMaterialDataPointer pointer = decodeInline(children[i]); + if (pointer) { + maxSize = qMax(maxSize, pointer->getSize()); + } + } + *(VoxelMaterialDataPointer*)&parent = VoxelMaterialDataPointer(); + return maxSize == 0; +} + +VoxelHermiteData::VoxelHermiteData(const QVector& contents, int size) : + _contents(contents), + _size(size) { +} + +VoxelHermiteData::VoxelHermiteData(Bitstream& in, int bytes) { + read(in, bytes); +} + +VoxelHermiteData::VoxelHermiteData(Bitstream& in, int bytes, const VoxelHermiteDataPointer& reference) { + if (!reference) { + read(in, bytes); + return; + } + QMutexLocker locker(&reference->getEncodedDeltaMutex()); + reference->setEncodedDelta(in.readAligned(bytes)); + reference->setDeltaData(DataBlockPointer(this)); + _contents = reference->getContents(); + _size = reference->getSize(); + + int offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ; + QVector delta = decodeVoxelColor(reference->getEncodedDelta(), offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ); + if (delta.isEmpty()) { + return; + } + if (offsetX == 0) { + _contents = delta; + _size = sizeX; + return; + } + int minX = offsetX - 1; + int minY = offsetY - 1; + int minZ = offsetZ - 1; + const QRgb* src = delta.constData(); + int destStride = _size * EDGE_COUNT; + int destStride2 = _size * destStride; + QRgb* planeDest = _contents.data() + minZ * destStride2 + minY * destStride + minX * EDGE_COUNT; + int srcStride = sizeX * EDGE_COUNT; + int length = srcStride * sizeof(QRgb); + for (int z = 0; z < sizeZ; z++, planeDest += destStride2) { + QRgb* dest = planeDest; + for (int y = 0; y < sizeY; y++, src += srcStride, dest += destStride) { + memcpy(dest, src, length); + } + } +} + +void VoxelHermiteData::write(Bitstream& out) { + QMutexLocker locker(&_encodedMutex); + if (_encoded.isEmpty()) { + _encoded = encodeVoxelColor(0, 0, 0, _size, _size, _size, _contents); + } + out << _encoded.size(); + out.writeAligned(_encoded); +} + +void VoxelHermiteData::writeDelta(Bitstream& out, const VoxelHermiteDataPointer& reference) { + if (!reference || reference->getSize() != _size) { + write(out); + return; + } + QMutexLocker locker(&reference->getEncodedDeltaMutex()); + if (reference->getEncodedDelta().isEmpty() || reference->getDeltaData() != this) { + int minX = _size, minY = _size, minZ = _size; + int maxX = -1, maxY = -1, maxZ = -1; + const QRgb* src = _contents.constData(); + const QRgb* ref = reference->getContents().constData(); + for (int z = 0; z < _size; z++) { + bool differenceZ = false; + for (int y = 0; y < _size; y++) { + bool differenceY = false; + for (int x = 0; x < _size; x++, src += EDGE_COUNT, ref += EDGE_COUNT) { + if (src[0] != ref[0] || src[1] != ref[1] || src[2] != ref[2]) { + minX = qMin(minX, x); + maxX = qMax(maxX, x); + differenceY = differenceZ = true; + } + } + if (differenceY) { + minY = qMin(minY, y); + maxY = qMax(maxY, y); + } + } + if (differenceZ) { + minZ = qMin(minZ, z); + maxZ = qMax(maxZ, z); + } + } + QVector delta; + int sizeX = 0, sizeY = 0, sizeZ = 0; + if (maxX >= minX) { + sizeX = maxX - minX + 1; + sizeY = maxY - minY + 1; + sizeZ = maxZ - minZ + 1; + delta = QVector(sizeX * sizeY * sizeZ * EDGE_COUNT, 0); + QRgb* dest = delta.data(); + int srcStride = _size * EDGE_COUNT; + int srcStride2 = _size * srcStride; + const QRgb* planeSrc = _contents.constData() + minZ * srcStride2 + minY * srcStride + minX * EDGE_COUNT; + int destStride = sizeX * EDGE_COUNT; + int length = destStride * sizeof(QRgb); + for (int z = 0; z < sizeZ; z++, planeSrc += srcStride2) { + src = planeSrc; + for (int y = 0; y < sizeY; y++, src += srcStride, dest += destStride) { + memcpy(dest, src, length); + } + } + } + reference->setEncodedDelta(encodeVoxelColor(minX + 1, minY + 1, minZ + 1, sizeX, sizeY, sizeZ, delta)); + reference->setDeltaData(DataBlockPointer(this)); + } + out << reference->getEncodedDelta().size(); + out.writeAligned(reference->getEncodedDelta()); +} + +void VoxelHermiteData::read(Bitstream& in, int bytes) { + int offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ; + _contents = decodeVoxelColor(_encoded = in.readAligned(bytes), offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ); + _size = sizeX; +} + +VoxelHermiteAttribute::VoxelHermiteAttribute(const QString& name) : + InlineAttribute(name) { +} + +void VoxelHermiteAttribute::read(Bitstream& in, void*& value, bool isLeaf) const { + if (!isLeaf) { + return; + } + int size; + in >> size; + if (size == 0) { + *(VoxelHermiteDataPointer*)&value = VoxelHermiteDataPointer(); + } else { + *(VoxelHermiteDataPointer*)&value = VoxelHermiteDataPointer(new VoxelHermiteData(in, size)); + } +} + +void VoxelHermiteAttribute::write(Bitstream& out, void* value, bool isLeaf) const { + if (!isLeaf) { + return; + } + VoxelHermiteDataPointer data = decodeInline(value); + if (data) { + data->write(out); + } else { + out << 0; + } +} + +void VoxelHermiteAttribute::readDelta(Bitstream& in, void*& value, void* reference, bool isLeaf) const { + if (!isLeaf) { + return; + } + int size; + in >> size; + if (size == 0) { + *(VoxelHermiteDataPointer*)&value = VoxelHermiteDataPointer(); + } else { + *(VoxelHermiteDataPointer*)&value = VoxelHermiteDataPointer(new VoxelHermiteData( + in, size, decodeInline(reference))); + } +} + +void VoxelHermiteAttribute::writeDelta(Bitstream& out, void* value, void* reference, bool isLeaf) const { + if (!isLeaf) { + return; + } + VoxelHermiteDataPointer data = decodeInline(value); + if (data) { + data->writeDelta(out, decodeInline(reference)); + } else { + out << 0; + } +} + +bool VoxelHermiteAttribute::merge(void*& parent, void* children[], bool postRead) const { + int maxSize = 0; + for (int i = 0; i < MERGE_COUNT; i++) { + VoxelHermiteDataPointer pointer = decodeInline(children[i]); + if (pointer) { + maxSize = qMax(maxSize, pointer->getSize()); + } + } + *(VoxelHermiteDataPointer*)&parent = VoxelHermiteDataPointer(); return maxSize == 0; } diff --git a/libraries/metavoxels/src/AttributeRegistry.h b/libraries/metavoxels/src/AttributeRegistry.h index 66da7a9b6f..cee01cdbef 100644 --- a/libraries/metavoxels/src/AttributeRegistry.h +++ b/libraries/metavoxels/src/AttributeRegistry.h @@ -29,14 +29,17 @@ class QScriptEngine; class QScriptValue; class Attribute; +class DataBlock; class HeightfieldColorData; -class HeightfieldData; class HeightfieldHeightData; -class HeightfieldTextureData; +class HeightfieldMaterialData; class MetavoxelData; class MetavoxelLOD; class MetavoxelNode; class MetavoxelStreamState; +class VoxelColorData; +class VoxelHermiteData; +class VoxelMaterialData; typedef SharedObjectPointerTemplate AttributePointer; @@ -100,14 +103,23 @@ public: /// Returns a reference to the standard "spannerMask" attribute. const AttributePointer& getSpannerMaskAttribute() const { return _spannerMaskAttribute; } - /// Returns a reference to the standard HeightfieldDataPointer "heightfield" attribute. + /// Returns a reference to the standard HeightfieldHeightDataPointer "heightfield" attribute. const AttributePointer& getHeightfieldAttribute() const { return _heightfieldAttribute; } - /// Returns a reference to the standard HeightfieldDataPointer "heightfieldColor" attribute. + /// Returns a reference to the standard HeightfieldColorDataPointer "heightfieldColor" attribute. const AttributePointer& getHeightfieldColorAttribute() const { return _heightfieldColorAttribute; } - /// Returns a reference to the standard HeightfieldDataPointer "heightfieldTexture" attribute. - const AttributePointer& getHeightfieldTextureAttribute() const { return _heightfieldTextureAttribute; } + /// Returns a reference to the standard HeightfieldMaterialDataPointer "heightfieldMaterial" attribute. + const AttributePointer& getHeightfieldMaterialAttribute() const { return _heightfieldMaterialAttribute; } + + /// Returns a reference to the standard VoxelColorDataPointer "voxelColor" attribute. + const AttributePointer& getVoxelColorAttribute() const { return _voxelColorAttribute; } + + /// Returns a reference to the standard VoxelMaterialDataPointer "voxelMaterial" attribute. + const AttributePointer& getVoxelMaterialAttribute() const { return _voxelMaterialAttribute; } + + /// Returns a reference to the standard VoxelHermiteDataPointer "voxelHermite" attribute. + const AttributePointer& getVoxelHermiteAttribute() const { return _voxelHermiteAttribute; } private: @@ -126,7 +138,10 @@ private: AttributePointer _spannerMaskAttribute; AttributePointer _heightfieldAttribute; AttributePointer _heightfieldColorAttribute; - AttributePointer _heightfieldTextureAttribute; + AttributePointer _heightfieldMaterialAttribute; + AttributePointer _voxelColorAttribute; + AttributePointer _voxelMaterialAttribute; + AttributePointer _voxelHermiteAttribute; }; /// Converts a value to a void pointer. @@ -206,6 +221,7 @@ Q_DECLARE_METATYPE(OwnedAttributeValue) class Attribute : public SharedObject { Q_OBJECT Q_PROPERTY(float lodThresholdMultiplier MEMBER _lodThresholdMultiplier) + Q_PROPERTY(bool userFacing MEMBER _userFacing) public: @@ -219,6 +235,9 @@ public: float getLODThresholdMultiplier() const { return _lodThresholdMultiplier; } void setLODThresholdMultiplier(float multiplier) { _lodThresholdMultiplier = multiplier; } + bool isUserFacing() const { return _userFacing; } + void setUserFacing(bool userFacing) { _userFacing = userFacing; } + void* create() const { return create(getDefaultValue()); } virtual void* create(void* copy) const = 0; virtual void destroy(void* value) const = 0; @@ -283,6 +302,7 @@ public: private: float _lodThresholdMultiplier; + bool _userFacing; }; /// A simple attribute class that stores its values inline. @@ -396,6 +416,9 @@ public: /// Packs a normal into an RGB value. QRgb packNormal(const glm::vec3& normal); +/// Packs a normal (plus extra alpha value) into an RGBA value. +QRgb packNormal(const glm::vec3& normal, int alpha); + /// Unpacks a normal from an RGB value. glm::vec3 unpackNormal(QRgb value); @@ -435,21 +458,18 @@ public: virtual AttributeValue inherit(const AttributeValue& parentValue) const; }; -typedef QExplicitlySharedDataPointer HeightfieldDataPointer; +typedef QExplicitlySharedDataPointer DataBlockPointer; -/// Contains a block of heightfield data. -class HeightfieldData : public QSharedData { +/// Base class for blocks of data. +class DataBlock : public QSharedData { public: static const int COLOR_BYTES = 3; - HeightfieldData(const QByteArray& contents = QByteArray()); - virtual ~HeightfieldData(); + virtual ~DataBlock(); - const QByteArray& getContents() const { return _contents; } - - void setDeltaData(const HeightfieldDataPointer& deltaData) { _deltaData = deltaData; } - const HeightfieldDataPointer& getDeltaData() const { return _deltaData; } + void setDeltaData(const DataBlockPointer& deltaData) { _deltaData = deltaData; } + const DataBlockPointer& getDeltaData() const { return _deltaData; } void setEncodedDelta(const QByteArray& encodedDelta) { _encodedDelta = encodedDelta; } const QByteArray& getEncodedDelta() const { return _encodedDelta; } @@ -458,17 +478,16 @@ public: protected: - QByteArray _contents; QByteArray _encoded; QMutex _encodedMutex; - HeightfieldDataPointer _deltaData; + DataBlockPointer _deltaData; QByteArray _encodedDelta; QMutex _encodedDeltaMutex; class EncodedSubdivision { public: - HeightfieldDataPointer ancestor; + DataBlockPointer ancestor; QByteArray data; }; QVector _encodedSubdivisions; @@ -478,7 +497,7 @@ protected: typedef QExplicitlySharedDataPointer HeightfieldHeightDataPointer; /// Contains a block of heightfield height data. -class HeightfieldHeightData : public HeightfieldData { +class HeightfieldHeightData : public DataBlock { public: HeightfieldHeightData(const QByteArray& contents); @@ -487,6 +506,8 @@ public: HeightfieldHeightData(Bitstream& in, int bytes, const HeightfieldHeightDataPointer& ancestor, const glm::vec3& minimum, float size); + const QByteArray& getContents() const { return _contents; } + void write(Bitstream& out); void writeDelta(Bitstream& out, const HeightfieldHeightDataPointer& reference); void writeSubdivided(Bitstream& out, const HeightfieldHeightDataPointer& ancestor, @@ -496,75 +517,8 @@ private: void read(Bitstream& in, int bytes); void set(const QImage& image); -}; - -typedef QExplicitlySharedDataPointer HeightfieldColorDataPointer; - -/// Contains a block of heightfield color data. -class HeightfieldColorData : public HeightfieldData { -public: - HeightfieldColorData(const QByteArray& contents); - HeightfieldColorData(Bitstream& in, int bytes); - HeightfieldColorData(Bitstream& in, int bytes, const HeightfieldColorDataPointer& reference); - HeightfieldColorData(Bitstream& in, int bytes, const HeightfieldColorDataPointer& ancestor, - const glm::vec3& minimum, float size); - - void write(Bitstream& out); - void writeDelta(Bitstream& out, const HeightfieldColorDataPointer& reference); - void writeSubdivided(Bitstream& out, const HeightfieldColorDataPointer& ancestor, - const glm::vec3& minimum, float size); - -private: - - void read(Bitstream& in, int bytes); - void set(const QImage& image); -}; - -typedef QExplicitlySharedDataPointer HeightfieldTextureDataPointer; - -/// Contains a block of heightfield texture data. -class HeightfieldTextureData : public HeightfieldData { -public: - - HeightfieldTextureData(const QByteArray& contents, - const QVector& textures = QVector()); - HeightfieldTextureData(Bitstream& in, int bytes); - HeightfieldTextureData(Bitstream& in, int bytes, const HeightfieldTextureDataPointer& reference); - - const QVector& getTextures() const { return _textures; } - - void write(Bitstream& out); - void writeDelta(Bitstream& out, const HeightfieldTextureDataPointer& reference); - -private: - - void read(Bitstream& in, int bytes); - - QVector _textures; -}; - -/// Contains the description of a heightfield texture. -class HeightfieldTexture : public SharedObject { - Q_OBJECT - Q_PROPERTY(QUrl url MEMBER _url) - Q_PROPERTY(float scaleS MEMBER _scaleS) - Q_PROPERTY(float scaleT MEMBER _scaleT) - -public: - - Q_INVOKABLE HeightfieldTexture(); - - const QUrl& getURL() const { return _url; } - - float getScaleS() const { return _scaleS; } - float getScaleT() const { return _scaleT; } - -private: - - QUrl _url; - float _scaleS; - float _scaleT; + QByteArray _contents; }; /// An attribute that stores heightfield data. @@ -584,6 +538,33 @@ public: virtual bool merge(void*& parent, void* children[], bool postRead = false) const; }; +typedef QExplicitlySharedDataPointer HeightfieldColorDataPointer; + +/// Contains a block of heightfield color data. +class HeightfieldColorData : public DataBlock { +public: + + HeightfieldColorData(const QByteArray& contents); + HeightfieldColorData(Bitstream& in, int bytes); + HeightfieldColorData(Bitstream& in, int bytes, const HeightfieldColorDataPointer& reference); + HeightfieldColorData(Bitstream& in, int bytes, const HeightfieldColorDataPointer& ancestor, + const glm::vec3& minimum, float size); + + const QByteArray& getContents() const { return _contents; } + + void write(Bitstream& out); + void writeDelta(Bitstream& out, const HeightfieldColorDataPointer& reference); + void writeSubdivided(Bitstream& out, const HeightfieldColorDataPointer& ancestor, + const glm::vec3& minimum, float size); + +private: + + void read(Bitstream& in, int bytes); + void set(const QImage& image); + + QByteArray _contents; +}; + /// An attribute that stores heightfield colors. class HeightfieldColorAttribute : public InlineAttribute { Q_OBJECT @@ -601,13 +582,194 @@ public: virtual bool merge(void*& parent, void* children[], bool postRead = false) const; }; -/// An attribute that stores heightfield textures. -class HeightfieldTextureAttribute : public InlineAttribute { +typedef QExplicitlySharedDataPointer HeightfieldMaterialDataPointer; + +/// Contains a block of heightfield material data. +class HeightfieldMaterialData : public DataBlock { +public: + + HeightfieldMaterialData(const QByteArray& contents, + const QVector& materials = QVector()); + HeightfieldMaterialData(Bitstream& in, int bytes); + HeightfieldMaterialData(Bitstream& in, int bytes, const HeightfieldMaterialDataPointer& reference); + + const QByteArray& getContents() const { return _contents; } + + const QVector& getMaterials() const { return _materials; } + + void write(Bitstream& out); + void writeDelta(Bitstream& out, const HeightfieldMaterialDataPointer& reference); + +private: + + void read(Bitstream& in, int bytes); + + QByteArray _contents; + QVector _materials; +}; + +/// Contains the description of a material. +class MaterialObject : public SharedObject { + Q_OBJECT + Q_PROPERTY(QUrl diffuse MEMBER _diffuse) + Q_PROPERTY(float scaleS MEMBER _scaleS) + Q_PROPERTY(float scaleT MEMBER _scaleT) + +public: + + Q_INVOKABLE MaterialObject(); + + const QUrl& getDiffuse() const { return _diffuse; } + + float getScaleS() const { return _scaleS; } + float getScaleT() const { return _scaleT; } + +private: + + QUrl _diffuse; + float _scaleS; + float _scaleT; +}; + +/// An attribute that stores heightfield materials. +class HeightfieldMaterialAttribute : public InlineAttribute { Q_OBJECT public: - Q_INVOKABLE HeightfieldTextureAttribute(const QString& name = QString()); + Q_INVOKABLE HeightfieldMaterialAttribute(const QString& name = QString()); + + virtual void read(Bitstream& in, void*& value, bool isLeaf) const; + virtual void write(Bitstream& out, void* value, bool isLeaf) const; + + virtual void readDelta(Bitstream& in, void*& value, void* reference, bool isLeaf) const; + virtual void writeDelta(Bitstream& out, void* value, void* reference, bool isLeaf) const; + + virtual bool merge(void*& parent, void* children[], bool postRead = false) const; +}; + +typedef QExplicitlySharedDataPointer VoxelColorDataPointer; + +/// Contains a block of voxel color data. +class VoxelColorData : public DataBlock { +public: + + VoxelColorData(const QVector& contents, int size); + VoxelColorData(Bitstream& in, int bytes); + VoxelColorData(Bitstream& in, int bytes, const VoxelColorDataPointer& reference); + + const QVector& getContents() const { return _contents; } + + int getSize() const { return _size; } + + void write(Bitstream& out); + void writeDelta(Bitstream& out, const VoxelColorDataPointer& reference); + +private: + + void read(Bitstream& in, int bytes); + + QVector _contents; + int _size; +}; + +/// An attribute that stores voxel colors. +class VoxelColorAttribute : public InlineAttribute { + Q_OBJECT + +public: + + Q_INVOKABLE VoxelColorAttribute(const QString& name = QString()); + + virtual void read(Bitstream& in, void*& value, bool isLeaf) const; + virtual void write(Bitstream& out, void* value, bool isLeaf) const; + + virtual void readDelta(Bitstream& in, void*& value, void* reference, bool isLeaf) const; + virtual void writeDelta(Bitstream& out, void* value, void* reference, bool isLeaf) const; + + virtual bool merge(void*& parent, void* children[], bool postRead = false) const; +}; + +typedef QExplicitlySharedDataPointer VoxelMaterialDataPointer; + +/// Contains a block of voxel material data. +class VoxelMaterialData : public DataBlock { +public: + + VoxelMaterialData(const QByteArray& contents, int size, + const QVector& materials = QVector()); + VoxelMaterialData(Bitstream& in, int bytes); + VoxelMaterialData(Bitstream& in, int bytes, const VoxelMaterialDataPointer& reference); + + const QByteArray& getContents() const { return _contents; } + + int getSize() const { return _size; } + + const QVector& getMaterials() const { return _materials; } + + void write(Bitstream& out); + void writeDelta(Bitstream& out, const VoxelMaterialDataPointer& reference); + +private: + + void read(Bitstream& in, int bytes); + + QByteArray _contents; + int _size; + QVector _materials; +}; + +/// An attribute that stores voxel materials. +class VoxelMaterialAttribute : public InlineAttribute { + Q_OBJECT + +public: + + Q_INVOKABLE VoxelMaterialAttribute(const QString& name = QString()); + + virtual void read(Bitstream& in, void*& value, bool isLeaf) const; + virtual void write(Bitstream& out, void* value, bool isLeaf) const; + + virtual void readDelta(Bitstream& in, void*& value, void* reference, bool isLeaf) const; + virtual void writeDelta(Bitstream& out, void* value, void* reference, bool isLeaf) const; + + virtual bool merge(void*& parent, void* children[], bool postRead = false) const; +}; + +typedef QExplicitlySharedDataPointer VoxelHermiteDataPointer; + +/// Contains a block of voxel Hermite data (positions and normals at edge crossings). +class VoxelHermiteData : public DataBlock { +public: + + static const int EDGE_COUNT = 3; + + VoxelHermiteData(const QVector& contents, int size); + VoxelHermiteData(Bitstream& in, int bytes); + VoxelHermiteData(Bitstream& in, int bytes, const VoxelHermiteDataPointer& reference); + + const QVector& getContents() const { return _contents; } + + int getSize() const { return _size; } + + void write(Bitstream& out); + void writeDelta(Bitstream& out, const VoxelHermiteDataPointer& reference); + +private: + + void read(Bitstream& in, int bytes); + + QVector _contents; + int _size; +}; + +/// An attribute that stores voxel Hermite data. +class VoxelHermiteAttribute : public InlineAttribute { + Q_OBJECT + +public: + + Q_INVOKABLE VoxelHermiteAttribute(const QString& name = QString()); virtual void read(Bitstream& in, void*& value, bool isLeaf) const; virtual void write(Bitstream& out, void* value, bool isLeaf) const; diff --git a/libraries/metavoxels/src/MetavoxelMessages.cpp b/libraries/metavoxels/src/MetavoxelMessages.cpp index 1a8f64d935..79ac74a1f6 100644 --- a/libraries/metavoxels/src/MetavoxelMessages.cpp +++ b/libraries/metavoxels/src/MetavoxelMessages.cpp @@ -414,119 +414,36 @@ PaintHeightfieldColorEdit::PaintHeightfieldColorEdit(const glm::vec3& position, color(color) { } -class PaintHeightfieldColorEditVisitor : public MetavoxelVisitor { +class PaintHeightfieldMaterialEditVisitor : public MetavoxelVisitor { public: - PaintHeightfieldColorEditVisitor(const PaintHeightfieldColorEdit& edit); + PaintHeightfieldMaterialEditVisitor(const glm::vec3& position, float radius, + const SharedObjectPointer& material, const QColor& color); virtual int visit(MetavoxelInfo& info); private: - PaintHeightfieldColorEdit _edit; + glm::vec3 _position; + float _radius; + SharedObjectPointer _material; + QColor _color; Box _bounds; }; -PaintHeightfieldColorEditVisitor::PaintHeightfieldColorEditVisitor(const PaintHeightfieldColorEdit& edit) : - MetavoxelVisitor(QVector() << AttributeRegistry::getInstance()->getHeightfieldColorAttribute(), - QVector() << AttributeRegistry::getInstance()->getHeightfieldColorAttribute()), - _edit(edit) { +PaintHeightfieldMaterialEditVisitor::PaintHeightfieldMaterialEditVisitor(const glm::vec3& position, float radius, + const SharedObjectPointer& material, const QColor& color) : + MetavoxelVisitor(QVector() << AttributeRegistry::getInstance()->getHeightfieldColorAttribute() << + AttributeRegistry::getInstance()->getHeightfieldMaterialAttribute(), QVector() << + AttributeRegistry::getInstance()->getHeightfieldColorAttribute() << + AttributeRegistry::getInstance()->getHeightfieldMaterialAttribute()), + _position(position), + _radius(radius), + _material(material), + _color(color) { - glm::vec3 extents(_edit.radius, _edit.radius, _edit.radius); - _bounds = Box(_edit.position - extents, _edit.position + extents); -} - -static void paintColor(MetavoxelInfo& info, int index, const glm::vec3& position, float radius, const QColor& color) { - HeightfieldColorDataPointer pointer = info.inputValues.at(index).getInlineValue(); - if (!pointer) { - return; - } - QByteArray contents(pointer->getContents()); - int size = glm::sqrt((float)contents.size() / HeightfieldData::COLOR_BYTES); - int highest = size - 1; - float heightScale = size / info.size; - - glm::vec3 center = (position - info.minimum) * heightScale; - float scaledRadius = radius * heightScale; - glm::vec3 extents(scaledRadius, scaledRadius, scaledRadius); - - glm::vec3 start = glm::floor(center - extents); - glm::vec3 end = glm::ceil(center + extents); - - // paint all points within the radius - float z = qMax(start.z, 0.0f); - float startX = qMax(start.x, 0.0f), endX = qMin(end.x, (float)highest); - int stride = size * HeightfieldData::COLOR_BYTES; - char* lineDest = contents.data() + (int)z * stride + (int)startX * HeightfieldData::COLOR_BYTES; - float squaredRadius = scaledRadius * scaledRadius; - char red = color.red(), green = color.green(), blue = color.blue(); - bool changed = false; - for (float endZ = qMin(end.z, (float)highest); z <= endZ; z += 1.0f) { - char* dest = lineDest; - for (float x = startX; x <= endX; x += 1.0f, dest += HeightfieldData::COLOR_BYTES) { - float dx = x - center.x, dz = z - center.z; - if (dx * dx + dz * dz <= squaredRadius) { - dest[0] = red; - dest[1] = green; - dest[2] = blue; - changed = true; - } - } - lineDest += stride; - } - if (changed) { - HeightfieldColorDataPointer newPointer(new HeightfieldColorData(contents)); - info.outputValues[index] = AttributeValue(info.inputValues.at(index).getAttribute(), - encodeInline(newPointer)); - } -} - -int PaintHeightfieldColorEditVisitor::visit(MetavoxelInfo& info) { - if (!info.getBounds().intersects(_bounds)) { - return STOP_RECURSION; - } - if (!info.isLeaf) { - return DEFAULT_ORDER; - } - paintColor(info, 0, _edit.position, _edit.radius, _edit.color); - return STOP_RECURSION; -} - -void PaintHeightfieldColorEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { - PaintHeightfieldColorEditVisitor visitor(*this); - data.guide(visitor); -} - -PaintHeightfieldTextureEdit::PaintHeightfieldTextureEdit(const glm::vec3& position, float radius, - const SharedObjectPointer& texture, const QColor& averageColor) : - position(position), - radius(radius), - texture(texture), - averageColor(averageColor) { -} - -class PaintHeightfieldTextureEditVisitor : public MetavoxelVisitor { -public: - - PaintHeightfieldTextureEditVisitor(const PaintHeightfieldTextureEdit& edit); - - virtual int visit(MetavoxelInfo& info); - -private: - - PaintHeightfieldTextureEdit _edit; - Box _bounds; -}; - -PaintHeightfieldTextureEditVisitor::PaintHeightfieldTextureEditVisitor(const PaintHeightfieldTextureEdit& edit) : - MetavoxelVisitor(QVector() << AttributeRegistry::getInstance()->getHeightfieldTextureAttribute() << - AttributeRegistry::getInstance()->getHeightfieldColorAttribute(), QVector() << - AttributeRegistry::getInstance()->getHeightfieldTextureAttribute() << - AttributeRegistry::getInstance()->getHeightfieldColorAttribute()), - _edit(edit) { - - glm::vec3 extents(_edit.radius, _edit.radius, _edit.radius); - _bounds = Box(_edit.position - extents, _edit.position + extents); + glm::vec3 extents(_radius, _radius, _radius); + _bounds = Box(_position - extents, _position + extents); } static QHash countIndices(const QByteArray& contents) { @@ -539,106 +456,624 @@ static QHash countIndices(const QByteArray& contents) { return counts; } -int PaintHeightfieldTextureEditVisitor::visit(MetavoxelInfo& info) { +uchar getMaterialIndex(const SharedObjectPointer& material, QVector& materials, QByteArray& contents) { + if (!(material && static_cast(material.data())->getDiffuse().isValid())) { + return 0; + } + // first look for a matching existing material, noting the first reusable slot + int firstEmptyIndex = -1; + for (int i = 0; i < materials.size(); i++) { + const SharedObjectPointer& existingMaterial = materials.at(i); + if (existingMaterial) { + if (existingMaterial->equals(material.data())) { + return i + 1; + } + } else if (firstEmptyIndex == -1) { + firstEmptyIndex = i; + } + } + // if nothing found, use the first empty slot or append + if (firstEmptyIndex != -1) { + materials[firstEmptyIndex] = material; + return firstEmptyIndex + 1; + } + if (materials.size() < EIGHT_BIT_MAXIMUM) { + materials.append(material); + return materials.size(); + } + // last resort: find the least-used material and remove it + QHash counts = countIndices(contents); + uchar materialIndex = 0; + int lowestCount = INT_MAX; + for (QHash::const_iterator it = counts.constBegin(); it != counts.constEnd(); it++) { + if (it.value() < lowestCount) { + materialIndex = it.key(); + lowestCount = it.value(); + } + } + contents.replace((char)materialIndex, (char)0); + return materialIndex; +} + +void clearUnusedMaterials(QVector& materials, QByteArray& contents) { + QHash counts = countIndices(contents); + for (int i = 0; i < materials.size(); i++) { + if (counts.value(i + 1) == 0) { + materials[i] = SharedObjectPointer(); + } + } + while (!(materials.isEmpty() || materials.last())) { + materials.removeLast(); + } +} + +int PaintHeightfieldMaterialEditVisitor::visit(MetavoxelInfo& info) { if (!info.getBounds().intersects(_bounds)) { return STOP_RECURSION; } if (!info.isLeaf) { return DEFAULT_ORDER; } - HeightfieldTextureDataPointer pointer = info.inputValues.at(0).getInlineValue(); - if (!pointer) { - return STOP_RECURSION; - } - QVector textures = pointer->getTextures(); - QByteArray contents(pointer->getContents()); - uchar textureIndex = 0; - if (_edit.texture && static_cast(_edit.texture.data())->getURL().isValid()) { - // first look for a matching existing texture, noting the first reusable slot - int firstEmptyIndex = -1; - for (int i = 0; i < textures.size(); i++) { - const SharedObjectPointer& texture = textures.at(i); - if (texture) { - if (texture->equals(_edit.texture.data())) { - textureIndex = i + 1; - break; + HeightfieldColorDataPointer colorPointer = info.inputValues.at(0).getInlineValue(); + if (colorPointer) { + QByteArray contents(colorPointer->getContents()); + int size = glm::sqrt((float)contents.size() / DataBlock::COLOR_BYTES); + int highest = size - 1; + float heightScale = size / info.size; + + glm::vec3 center = (_position - info.minimum) * heightScale; + float scaledRadius = _radius * heightScale; + glm::vec3 extents(scaledRadius, scaledRadius, scaledRadius); + + glm::vec3 start = glm::floor(center - extents); + glm::vec3 end = glm::ceil(center + extents); + + // paint all points within the radius + float z = qMax(start.z, 0.0f); + float startX = qMax(start.x, 0.0f), endX = qMin(end.x, (float)highest); + int stride = size * DataBlock::COLOR_BYTES; + char* lineDest = contents.data() + (int)z * stride + (int)startX * DataBlock::COLOR_BYTES; + float squaredRadius = scaledRadius * scaledRadius; + char red = _color.red(), green = _color.green(), blue = _color.blue(); + bool changed = false; + for (float endZ = qMin(end.z, (float)highest); z <= endZ; z += 1.0f) { + char* dest = lineDest; + for (float x = startX; x <= endX; x += 1.0f, dest += DataBlock::COLOR_BYTES) { + float dx = x - center.x, dz = z - center.z; + if (dx * dx + dz * dz <= squaredRadius) { + dest[0] = red; + dest[1] = green; + dest[2] = blue; + changed = true; } - } else if (firstEmptyIndex == -1) { - firstEmptyIndex = i; } + lineDest += stride; } - // if nothing found, use the first empty slot or append - if (textureIndex == 0) { - if (firstEmptyIndex != -1) { - textures[firstEmptyIndex] = _edit.texture; - textureIndex = firstEmptyIndex + 1; - - } else if (textures.size() < EIGHT_BIT_MAXIMUM) { - textures.append(_edit.texture); - textureIndex = textures.size(); - - } else { - // last resort: find the least-used texture and remove it - QHash counts = countIndices(contents); - int lowestCount = INT_MAX; - for (QHash::const_iterator it = counts.constBegin(); it != counts.constEnd(); it++) { - if (it.value() < lowestCount) { - textureIndex = it.key(); - lowestCount = it.value(); - } + if (changed) { + HeightfieldColorDataPointer newPointer(new HeightfieldColorData(contents)); + info.outputValues[0] = AttributeValue(info.inputValues.at(0).getAttribute(), + encodeInline(newPointer)); + } + } + + HeightfieldMaterialDataPointer materialPointer = info.inputValues.at(1).getInlineValue(); + if (materialPointer) { + QVector materials = materialPointer->getMaterials(); + QByteArray contents(materialPointer->getContents()); + uchar materialIndex = getMaterialIndex(_material, materials, contents); + int size = glm::sqrt((float)contents.size()); + int highest = size - 1; + float heightScale = highest / info.size; + + glm::vec3 center = (_position - info.minimum) * heightScale; + float scaledRadius = _radius * heightScale; + glm::vec3 extents(scaledRadius, scaledRadius, scaledRadius); + + glm::vec3 start = glm::floor(center - extents); + glm::vec3 end = glm::ceil(center + extents); + + // paint all points within the radius + float z = qMax(start.z, 0.0f); + float startX = qMax(start.x, 0.0f), endX = qMin(end.x, (float)highest); + uchar* lineDest = (uchar*)contents.data() + (int)z * size + (int)startX; + float squaredRadius = scaledRadius * scaledRadius; + bool changed = false; + QHash counts; + for (float endZ = qMin(end.z, (float)highest); z <= endZ; z += 1.0f) { + uchar* dest = lineDest; + for (float x = startX; x <= endX; x += 1.0f, dest++) { + float dx = x - center.x, dz = z - center.z; + if (dx * dx + dz * dz <= squaredRadius) { + *dest = materialIndex; + changed = true; } - contents.replace((char)textureIndex, (char)0); } + lineDest += size; + } + if (changed) { + clearUnusedMaterials(materials, contents); + HeightfieldMaterialDataPointer newPointer(new HeightfieldMaterialData(contents, materials)); + info.outputValues[1] = AttributeValue(_outputs.at(1), encodeInline(newPointer)); } } - int size = glm::sqrt((float)contents.size()); - int highest = size - 1; - float heightScale = highest / info.size; - - glm::vec3 center = (_edit.position - info.minimum) * heightScale; - float scaledRadius = _edit.radius * heightScale; - glm::vec3 extents(scaledRadius, scaledRadius, scaledRadius); - - glm::vec3 start = glm::floor(center - extents); - glm::vec3 end = glm::ceil(center + extents); - - // paint all points within the radius - float z = qMax(start.z, 0.0f); - float startX = qMax(start.x, 0.0f), endX = qMin(end.x, (float)highest); - uchar* lineDest = (uchar*)contents.data() + (int)z * size + (int)startX; - float squaredRadius = scaledRadius * scaledRadius; - bool changed = false; - QHash counts; - for (float endZ = qMin(end.z, (float)highest); z <= endZ; z += 1.0f) { - uchar* dest = lineDest; - for (float x = startX; x <= endX; x += 1.0f, dest++) { - float dx = x - center.x, dz = z - center.z; - if (dx * dx + dz * dz <= squaredRadius) { - *dest = textureIndex; - changed = true; - } - } - lineDest += size; - } - if (changed) { - // clear any unused textures - QHash counts = countIndices(contents); - for (int i = 0; i < textures.size(); i++) { - if (counts.value(i + 1) == 0) { - textures[i] = SharedObjectPointer(); - } - } - while (!(textures.isEmpty() || textures.last())) { - textures.removeLast(); - } - HeightfieldTextureDataPointer newPointer(new HeightfieldTextureData(contents, textures)); - info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(newPointer)); - } - paintColor(info, 1, _edit.position, _edit.radius, _edit.averageColor); return STOP_RECURSION; } -void PaintHeightfieldTextureEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { - PaintHeightfieldTextureEditVisitor visitor(*this); +void PaintHeightfieldColorEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { + PaintHeightfieldMaterialEditVisitor visitor(position, radius, SharedObjectPointer(), color); + data.guide(visitor); +} + +PaintHeightfieldMaterialEdit::PaintHeightfieldMaterialEdit(const glm::vec3& position, float radius, + const SharedObjectPointer& material, const QColor& averageColor) : + position(position), + radius(radius), + material(material), + averageColor(averageColor) { +} + +void PaintHeightfieldMaterialEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { + PaintHeightfieldMaterialEditVisitor visitor(position, radius, material, averageColor); + data.guide(visitor); +} + +VoxelColorBoxEdit::VoxelColorBoxEdit(const Box& region, float granularity, const QColor& color) : + region(region), + granularity(granularity), + color(color) { +} + +const int VOXEL_BLOCK_SIZE = 16; +const int VOXEL_BLOCK_SAMPLES = VOXEL_BLOCK_SIZE + 1; +const int VOXEL_BLOCK_AREA = VOXEL_BLOCK_SAMPLES * VOXEL_BLOCK_SAMPLES; +const int VOXEL_BLOCK_VOLUME = VOXEL_BLOCK_AREA * VOXEL_BLOCK_SAMPLES; + +class VoxelMaterialBoxEditVisitor : public MetavoxelVisitor { +public: + + VoxelMaterialBoxEditVisitor(const Box& region, float granularity, + const SharedObjectPointer& material, const QColor& color); + + virtual int visit(MetavoxelInfo& info); + +private: + + Box _region; + SharedObjectPointer _material; + QColor _color; + float _blockSize; +}; + +VoxelMaterialBoxEditVisitor::VoxelMaterialBoxEditVisitor(const Box& region, float granularity, + const SharedObjectPointer& material, const QColor& color) : + MetavoxelVisitor(QVector() << AttributeRegistry::getInstance()->getVoxelColorAttribute() << + AttributeRegistry::getInstance()->getVoxelHermiteAttribute() << + AttributeRegistry::getInstance()->getVoxelMaterialAttribute(), QVector() << + AttributeRegistry::getInstance()->getVoxelColorAttribute() << + AttributeRegistry::getInstance()->getVoxelHermiteAttribute() << + AttributeRegistry::getInstance()->getVoxelMaterialAttribute()), + _region(region), + _material(material), + _color(color), + _blockSize(granularity * VOXEL_BLOCK_SIZE) { +} + +int VoxelMaterialBoxEditVisitor::visit(MetavoxelInfo& info) { + Box bounds = info.getBounds(); + if (!bounds.intersects(_region)) { + return STOP_RECURSION; + } + if (info.size > _blockSize) { + return DEFAULT_ORDER; + } + VoxelColorDataPointer colorPointer = info.inputValues.at(0).getInlineValue(); + QVector colorContents = (colorPointer && colorPointer->getSize() == VOXEL_BLOCK_SAMPLES) ? + colorPointer->getContents() : QVector(VOXEL_BLOCK_VOLUME); + + Box overlap = info.getBounds().getIntersection(_region); + float scale = VOXEL_BLOCK_SIZE / info.size; + overlap.minimum = (overlap.minimum - info.minimum) * scale; + overlap.maximum = (overlap.maximum - info.minimum) * scale; + int minX = glm::ceil(overlap.minimum.x); + int minY = glm::ceil(overlap.minimum.y); + int minZ = glm::ceil(overlap.minimum.z); + int sizeX = (int)overlap.maximum.x - minX + 1; + int sizeY = (int)overlap.maximum.y - minY + 1; + int sizeZ = (int)overlap.maximum.z - minZ + 1; + + QRgb rgb = _color.rgba(); + for (QRgb* destZ = colorContents.data() + minZ * VOXEL_BLOCK_AREA + minY * VOXEL_BLOCK_SAMPLES + minX, + *endZ = destZ + sizeZ * VOXEL_BLOCK_AREA; destZ != endZ; destZ += VOXEL_BLOCK_AREA) { + for (QRgb* destY = destZ, *endY = destY + sizeY * VOXEL_BLOCK_SAMPLES; destY != endY; destY += VOXEL_BLOCK_SAMPLES) { + for (QRgb* destX = destY, *endX = destX + sizeX; destX != endX; destX++) { + *destX = rgb; + } + } + } + + VoxelColorDataPointer newColorPointer(new VoxelColorData(colorContents, VOXEL_BLOCK_SAMPLES)); + info.outputValues[0] = AttributeValue(info.inputValues.at(0).getAttribute(), + encodeInline(newColorPointer)); + + VoxelHermiteDataPointer hermitePointer = info.inputValues.at(1).getInlineValue(); + QVector hermiteContents = (hermitePointer && hermitePointer->getSize() == VOXEL_BLOCK_SAMPLES) ? + hermitePointer->getContents() : QVector(VOXEL_BLOCK_VOLUME * VoxelHermiteData::EDGE_COUNT); + int hermiteArea = VOXEL_BLOCK_AREA * VoxelHermiteData::EDGE_COUNT; + int hermiteSamples = VOXEL_BLOCK_SAMPLES * VoxelHermiteData::EDGE_COUNT; + + int hermiteMinX = minX, hermiteMinY = minY, hermiteMinZ = minZ; + int hermiteSizeX = sizeX, hermiteSizeY = sizeY, hermiteSizeZ = sizeZ; + if (minX > 0) { + hermiteMinX--; + hermiteSizeX++; + } + if (minY > 0) { + hermiteMinY--; + hermiteSizeY++; + } + if (minZ > 0) { + hermiteMinZ--; + hermiteSizeZ++; + } + const int NORMAL_MAX = 127; + QRgb* hermiteDestZ = hermiteContents.data() + hermiteMinZ * hermiteArea + hermiteMinY * hermiteSamples + + hermiteMinX * VoxelHermiteData::EDGE_COUNT; + for (int z = hermiteMinZ, hermiteMaxZ = z + hermiteSizeZ - 1; z <= hermiteMaxZ; z++, hermiteDestZ += hermiteArea) { + QRgb* hermiteDestY = hermiteDestZ; + for (int y = hermiteMinY, hermiteMaxY = y + hermiteSizeY - 1; y <= hermiteMaxY; y++, hermiteDestY += hermiteSamples) { + QRgb* hermiteDestX = hermiteDestY; + for (int x = hermiteMinX, hermiteMaxX = x + hermiteSizeX - 1; x <= hermiteMaxX; x++, + hermiteDestX += VoxelHermiteData::EDGE_COUNT) { + // internal edges are set to zero; border edges (when non-terminal) are set to the intersection values + hermiteDestX[0] = 0x0; + if ((x == hermiteMinX || x == hermiteMaxX) && x != VOXEL_BLOCK_SIZE) { + const QRgb* color = colorContents.constData() + z * VOXEL_BLOCK_AREA + y * VOXEL_BLOCK_SAMPLES + x; + int alpha0 = qAlpha(color[0]); + if (alpha0 != qAlpha(color[1])) { + hermiteDestX[0] = qRgba(alpha0 == 0 ? -NORMAL_MAX : NORMAL_MAX, 0, 0, + ((x == hermiteMinX ? overlap.minimum.x : overlap.maximum.x) - x) * EIGHT_BIT_MAXIMUM); + } + } + hermiteDestX[1] = 0x0; + if ((y == hermiteMinY || y == hermiteMaxY) && y != VOXEL_BLOCK_SIZE) { + const QRgb* color = colorContents.constData() + z * VOXEL_BLOCK_AREA + y * VOXEL_BLOCK_SAMPLES + x; + int alpha0 = qAlpha(color[0]); + if (alpha0 != qAlpha(color[VOXEL_BLOCK_SAMPLES])) { + hermiteDestX[1] = qRgba(0, alpha0 == 0 ? -NORMAL_MAX : NORMAL_MAX, 0, + ((y == hermiteMinY ? overlap.minimum.y : overlap.maximum.y) - y) * EIGHT_BIT_MAXIMUM); + } + } + hermiteDestX[2] = 0x0; + if ((z == hermiteMinZ || z == hermiteMaxZ) && z != VOXEL_BLOCK_SIZE) { + const QRgb* color = colorContents.constData() + z * VOXEL_BLOCK_AREA + y * VOXEL_BLOCK_SAMPLES + x; + int alpha0 = qAlpha(color[0]); + if (alpha0 != qAlpha(color[VOXEL_BLOCK_AREA])) { + hermiteDestX[2] = qRgba(0, 0, alpha0 == 0 ? -NORMAL_MAX : NORMAL_MAX, + ((z == hermiteMinZ ? overlap.minimum.z : overlap.maximum.z) - z) * EIGHT_BIT_MAXIMUM); + } + } + } + } + } + + VoxelHermiteDataPointer newHermitePointer(new VoxelHermiteData(hermiteContents, VOXEL_BLOCK_SAMPLES)); + info.outputValues[1] = AttributeValue(info.inputValues.at(1).getAttribute(), + encodeInline(newHermitePointer)); + + VoxelMaterialDataPointer materialPointer = info.inputValues.at(2).getInlineValue(); + QByteArray materialContents; + QVector materials; + if (materialPointer && materialPointer->getSize() == VOXEL_BLOCK_SAMPLES) { + materialContents = materialPointer->getContents(); + materials = materialPointer->getMaterials(); + + } else { + materialContents = QByteArray(VOXEL_BLOCK_VOLUME, 0); + } + + uchar materialIndex = getMaterialIndex(_material, materials, materialContents); + for (uchar* destZ = (uchar*)materialContents.data() + minZ * VOXEL_BLOCK_AREA + minY * VOXEL_BLOCK_SAMPLES + minX, + *endZ = destZ + sizeZ * VOXEL_BLOCK_AREA; destZ != endZ; destZ += VOXEL_BLOCK_AREA) { + for (uchar* destY = destZ, *endY = destY + sizeY * VOXEL_BLOCK_SAMPLES; destY != endY; destY += VOXEL_BLOCK_SAMPLES) { + for (uchar* destX = destY, *endX = destX + sizeX; destX != endX; destX++) { + *destX = materialIndex; + } + } + } + + clearUnusedMaterials(materials, materialContents); + VoxelMaterialDataPointer newMaterialPointer(new VoxelMaterialData(materialContents, VOXEL_BLOCK_SAMPLES, materials)); + info.outputValues[2] = AttributeValue(_inputs.at(2), encodeInline(newMaterialPointer)); + + return STOP_RECURSION; +} + +void VoxelColorBoxEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { + // expand to fit the entire edit + while (!data.getBounds().contains(region)) { + data.expand(); + } + VoxelMaterialBoxEditVisitor visitor(region, granularity, SharedObjectPointer(), color); + data.guide(visitor); +} + +VoxelMaterialBoxEdit::VoxelMaterialBoxEdit(const Box& region, float granularity, + const SharedObjectPointer& material, const QColor& averageColor) : + region(region), + granularity(granularity), + material(material), + averageColor(averageColor) { +} + +void VoxelMaterialBoxEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { + // expand to fit the entire edit + while (!data.getBounds().contains(region)) { + data.expand(); + } + VoxelMaterialBoxEditVisitor visitor(region, granularity, material, averageColor); + data.guide(visitor); +} + +VoxelColorSphereEdit::VoxelColorSphereEdit(const glm::vec3& center, float radius, float granularity, const QColor& color) : + center(center), + radius(radius), + granularity(granularity), + color(color) { +} + +class VoxelMaterialSphereEditVisitor : public MetavoxelVisitor { +public: + + VoxelMaterialSphereEditVisitor(const glm::vec3& center, float radius, const Box& bounds, float granularity, + const SharedObjectPointer& material, const QColor& color); + + virtual int visit(MetavoxelInfo& info); + +private: + + glm::vec3 _center; + float _radius; + Box _bounds; + SharedObjectPointer _material; + QColor _color; + float _blockSize; +}; + +VoxelMaterialSphereEditVisitor::VoxelMaterialSphereEditVisitor(const glm::vec3& center, float radius, const Box& bounds, + float granularity, const SharedObjectPointer& material, const QColor& color) : + MetavoxelVisitor(QVector() << AttributeRegistry::getInstance()->getVoxelColorAttribute() << + AttributeRegistry::getInstance()->getVoxelHermiteAttribute() << + AttributeRegistry::getInstance()->getVoxelMaterialAttribute(), QVector() << + AttributeRegistry::getInstance()->getVoxelColorAttribute() << + AttributeRegistry::getInstance()->getVoxelHermiteAttribute() << + AttributeRegistry::getInstance()->getVoxelMaterialAttribute()), + _center(center), + _radius(radius), + _bounds(bounds), + _material(material), + _color(color), + _blockSize(granularity * VOXEL_BLOCK_SIZE) { +} + +int VoxelMaterialSphereEditVisitor::visit(MetavoxelInfo& info) { + Box bounds = info.getBounds(); + if (!bounds.intersects(_bounds)) { + return STOP_RECURSION; + } + if (info.size > _blockSize) { + return DEFAULT_ORDER; + } + VoxelColorDataPointer colorPointer = info.inputValues.at(0).getInlineValue(); + QVector colorContents = (colorPointer && colorPointer->getSize() == VOXEL_BLOCK_SAMPLES) ? + colorPointer->getContents() : QVector(VOXEL_BLOCK_VOLUME); + + Box overlap = info.getBounds().getIntersection(_bounds); + float scale = VOXEL_BLOCK_SIZE / info.size; + overlap.minimum = (overlap.minimum - info.minimum) * scale; + overlap.maximum = (overlap.maximum - info.minimum) * scale; + int minX = glm::ceil(overlap.minimum.x); + int minY = glm::ceil(overlap.minimum.y); + int minZ = glm::ceil(overlap.minimum.z); + int sizeX = (int)overlap.maximum.x - minX + 1; + int sizeY = (int)overlap.maximum.y - minY + 1; + int sizeZ = (int)overlap.maximum.z - minZ + 1; + + glm::vec3 relativeCenter = (_center - info.minimum) * scale; + float relativeRadius = _radius * scale; + float relativeRadiusSquared = relativeRadius * relativeRadius; + + QRgb rgb = _color.rgba(); + glm::vec3 position(0.0f, 0.0f, minZ); + for (QRgb* destZ = colorContents.data() + minZ * VOXEL_BLOCK_AREA + minY * VOXEL_BLOCK_SAMPLES + minX, + *endZ = destZ + sizeZ * VOXEL_BLOCK_AREA; destZ != endZ; destZ += VOXEL_BLOCK_AREA, position.z++) { + position.y = minY; + for (QRgb* destY = destZ, *endY = destY + sizeY * VOXEL_BLOCK_SAMPLES; destY != endY; + destY += VOXEL_BLOCK_SAMPLES, position.y++) { + position.x = minX; + for (QRgb* destX = destY, *endX = destX + sizeX; destX != endX; destX++, position.x++) { + if (glm::distance(relativeCenter, position) <= relativeRadius) { + *destX = rgb; + } + } + } + } + + VoxelColorDataPointer newColorPointer(new VoxelColorData(colorContents, VOXEL_BLOCK_SAMPLES)); + info.outputValues[0] = AttributeValue(info.inputValues.at(0).getAttribute(), + encodeInline(newColorPointer)); + + VoxelHermiteDataPointer hermitePointer = info.inputValues.at(1).getInlineValue(); + QVector hermiteContents = (hermitePointer && hermitePointer->getSize() == VOXEL_BLOCK_SAMPLES) ? + hermitePointer->getContents() : QVector(VOXEL_BLOCK_VOLUME * VoxelHermiteData::EDGE_COUNT); + int hermiteArea = VOXEL_BLOCK_AREA * VoxelHermiteData::EDGE_COUNT; + int hermiteSamples = VOXEL_BLOCK_SAMPLES * VoxelHermiteData::EDGE_COUNT; + + int hermiteMinX = minX, hermiteMinY = minY, hermiteMinZ = minZ; + int hermiteSizeX = sizeX, hermiteSizeY = sizeY, hermiteSizeZ = sizeZ; + if (minX > 0) { + hermiteMinX--; + hermiteSizeX++; + } + if (minY > 0) { + hermiteMinY--; + hermiteSizeY++; + } + if (minZ > 0) { + hermiteMinZ--; + hermiteSizeZ++; + } + QRgb* hermiteDestZ = hermiteContents.data() + hermiteMinZ * hermiteArea + hermiteMinY * hermiteSamples + + hermiteMinX * VoxelHermiteData::EDGE_COUNT; + for (int z = hermiteMinZ, hermiteMaxZ = z + hermiteSizeZ - 1; z <= hermiteMaxZ; z++, hermiteDestZ += hermiteArea) { + QRgb* hermiteDestY = hermiteDestZ; + for (int y = hermiteMinY, hermiteMaxY = y + hermiteSizeY - 1; y <= hermiteMaxY; y++, hermiteDestY += hermiteSamples) { + QRgb* hermiteDestX = hermiteDestY; + for (int x = hermiteMinX, hermiteMaxX = x + hermiteSizeX - 1; x <= hermiteMaxX; x++, + hermiteDestX += VoxelHermiteData::EDGE_COUNT) { + // at each intersected non-terminal edge, we check for a transition and, if one is detected, we assign the + // crossing and normal values based on intersection with the sphere + hermiteDestX[0] = 0x0; + glm::vec3 offset(x - relativeCenter.x, y - relativeCenter.y, z - relativeCenter.z); + if (x != VOXEL_BLOCK_SIZE) { + const QRgb* color = colorContents.constData() + z * VOXEL_BLOCK_AREA + y * VOXEL_BLOCK_SAMPLES + x; + int alpha0 = qAlpha(color[0]); + if (alpha0 != qAlpha(color[1])) { + float radicand = relativeRadiusSquared - offset.y * offset.y - offset.z * offset.z; + float parameter = 0.5f; + if (radicand >= 0.0f) { + float root = glm::sqrt(radicand); + parameter = -offset.x - root; + if (parameter < 0.0f || parameter > 1.0f) { + parameter = glm::clamp(-offset.x + root, 0.0f, 1.0f); + } + } + glm::vec3 normal = offset + glm::vec3(parameter, 0.0f, 0.0f); + float length = glm::length(normal); + if (length > EPSILON) { + normal /= length; + } else { + normal = glm::vec3(0.0f, 1.0f, 0.0f); + } + hermiteDestX[0] = packNormal(normal, parameter * EIGHT_BIT_MAXIMUM); + } + } + hermiteDestX[1] = 0x0; + if (y != VOXEL_BLOCK_SIZE) { + const QRgb* color = colorContents.constData() + z * VOXEL_BLOCK_AREA + y * VOXEL_BLOCK_SAMPLES + x; + int alpha0 = qAlpha(color[0]); + if (alpha0 != qAlpha(color[VOXEL_BLOCK_SAMPLES])) { + float radicand = relativeRadiusSquared - offset.x * offset.x - offset.z * offset.z; + float parameter = 0.5f; + if (radicand >= 0.0f) { + float root = glm::sqrt(radicand); + parameter = -offset.y - root; + if (parameter < 0.0f || parameter > 1.0f) { + parameter = glm::clamp(-offset.y + root, 0.0f, 1.0f); + } + } + glm::vec3 normal = offset + glm::vec3(parameter, 0.0f, 0.0f); + float length = glm::length(normal); + if (length > EPSILON) { + normal /= length; + } else { + normal = glm::vec3(1.0f, 0.0f, 0.0f); + } + hermiteDestX[1] = packNormal(normal, parameter * EIGHT_BIT_MAXIMUM); + } + } + hermiteDestX[2] = 0x0; + if (z != VOXEL_BLOCK_SIZE) { + const QRgb* color = colorContents.constData() + z * VOXEL_BLOCK_AREA + y * VOXEL_BLOCK_SAMPLES + x; + int alpha0 = qAlpha(color[0]); + if (alpha0 != qAlpha(color[VOXEL_BLOCK_AREA])) { + float radicand = relativeRadiusSquared - offset.x * offset.x - offset.y * offset.y; + float parameter = 0.5f; + if (radicand >= 0.0f) { + float root = glm::sqrt(radicand); + parameter = -offset.z - root; + if (parameter < 0.0f || parameter > 1.0f) { + parameter = glm::clamp(-offset.z + root, 0.0f, 1.0f); + } + } + glm::vec3 normal = offset + glm::vec3(parameter, 0.0f, 0.0f); + float length = glm::length(normal); + if (length > EPSILON) { + normal /= length; + } else { + normal = glm::vec3(1.0f, 0.0f, 0.0f); + } + hermiteDestX[2] = packNormal(normal, parameter * EIGHT_BIT_MAXIMUM); + } + } + } + } + } + + VoxelHermiteDataPointer newHermitePointer(new VoxelHermiteData(hermiteContents, VOXEL_BLOCK_SAMPLES)); + info.outputValues[1] = AttributeValue(info.inputValues.at(1).getAttribute(), + encodeInline(newHermitePointer)); + + VoxelMaterialDataPointer materialPointer = info.inputValues.at(2).getInlineValue(); + QByteArray materialContents; + QVector materials; + if (materialPointer && materialPointer->getSize() == VOXEL_BLOCK_SAMPLES) { + materialContents = materialPointer->getContents(); + materials = materialPointer->getMaterials(); + + } else { + materialContents = QByteArray(VOXEL_BLOCK_VOLUME, 0); + } + + uchar materialIndex = getMaterialIndex(_material, materials, materialContents); + position.z = minZ; + for (uchar* destZ = (uchar*)materialContents.data() + minZ * VOXEL_BLOCK_AREA + minY * VOXEL_BLOCK_SAMPLES + minX, + *endZ = destZ + sizeZ * VOXEL_BLOCK_AREA; destZ != endZ; destZ += VOXEL_BLOCK_AREA, position.z++) { + position.y = minY; + for (uchar* destY = destZ, *endY = destY + sizeY * VOXEL_BLOCK_SAMPLES; destY != endY; + destY += VOXEL_BLOCK_SAMPLES, position.y++) { + position.x = minX; + for (uchar* destX = destY, *endX = destX + sizeX; destX != endX; destX++, position.x++) { + if (glm::distance(relativeCenter, position) <= relativeRadius) { + *destX = materialIndex; + } + } + } + } + + clearUnusedMaterials(materials, materialContents); + VoxelMaterialDataPointer newMaterialPointer(new VoxelMaterialData(materialContents, VOXEL_BLOCK_SAMPLES, materials)); + info.outputValues[2] = AttributeValue(_inputs.at(2), encodeInline(newMaterialPointer)); + + return STOP_RECURSION; +} + +void VoxelColorSphereEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { + // expand to fit the entire edit + glm::vec3 extents(radius, radius, radius); + Box bounds(center - extents, center + extents); + while (!data.getBounds().contains(bounds)) { + data.expand(); + } + VoxelMaterialSphereEditVisitor visitor(center, radius, bounds, granularity, SharedObjectPointer(), color); + data.guide(visitor); +} + +VoxelMaterialSphereEdit::VoxelMaterialSphereEdit(const glm::vec3& center, float radius, float granularity, + const SharedObjectPointer& material, const QColor& averageColor) : + center(center), + radius(radius), + granularity(granularity), + material(material), + averageColor(averageColor) { +} + +void VoxelMaterialSphereEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { + // expand to fit the entire edit + glm::vec3 extents(radius, radius, radius); + Box bounds(center - extents, center + extents); + while (!data.getBounds().contains(bounds)) { + data.expand(); + } + VoxelMaterialSphereEditVisitor visitor(center, radius, bounds, granularity, material, averageColor); data.guide(visitor); } diff --git a/libraries/metavoxels/src/MetavoxelMessages.h b/libraries/metavoxels/src/MetavoxelMessages.h index 3d610b10df..2eb5684d77 100644 --- a/libraries/metavoxels/src/MetavoxelMessages.h +++ b/libraries/metavoxels/src/MetavoxelMessages.h @@ -241,23 +241,98 @@ public: DECLARE_STREAMABLE_METATYPE(PaintHeightfieldColorEdit) -/// An edit that sets a region of a heightfield texture. -class PaintHeightfieldTextureEdit : public MetavoxelEdit { +/// An edit that sets a region of a heightfield material. +class PaintHeightfieldMaterialEdit : public MetavoxelEdit { STREAMABLE public: STREAM glm::vec3 position; STREAM float radius; - STREAM SharedObjectPointer texture; + STREAM SharedObjectPointer material; STREAM QColor averageColor; - PaintHeightfieldTextureEdit(const glm::vec3& position = glm::vec3(), float radius = 0.0f, - const SharedObjectPointer& texture = SharedObjectPointer(), const QColor& averageColor = QColor()); + PaintHeightfieldMaterialEdit(const glm::vec3& position = glm::vec3(), float radius = 0.0f, + const SharedObjectPointer& material = SharedObjectPointer(), const QColor& averageColor = QColor()); virtual void apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const; }; -DECLARE_STREAMABLE_METATYPE(PaintHeightfieldTextureEdit) +DECLARE_STREAMABLE_METATYPE(PaintHeightfieldMaterialEdit) + +/// An edit that sets the color of voxels within a box to a value. +class VoxelColorBoxEdit : public MetavoxelEdit { + STREAMABLE + +public: + + STREAM Box region; + STREAM float granularity; + STREAM QColor color; + + VoxelColorBoxEdit(const Box& region = Box(), float granularity = 0.0f, const QColor& color = QColor()); + + virtual void apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const; +}; + +DECLARE_STREAMABLE_METATYPE(VoxelColorBoxEdit) + +/// An edit that sets the materials of voxels within a box to a value. +class VoxelMaterialBoxEdit : public MetavoxelEdit { + STREAMABLE + +public: + + STREAM Box region; + STREAM float granularity; + STREAM SharedObjectPointer material; + STREAM QColor averageColor; + + VoxelMaterialBoxEdit(const Box& region = Box(), float granularity = 0.0f, + const SharedObjectPointer& material = SharedObjectPointer(), const QColor& averageColor = QColor()); + + virtual void apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const; +}; + +DECLARE_STREAMABLE_METATYPE(VoxelMaterialBoxEdit) + +/// An edit that sets the color of voxels within a sphere to a value. +class VoxelColorSphereEdit : public MetavoxelEdit { + STREAMABLE + +public: + + STREAM glm::vec3 center; + STREAM float radius; + STREAM float granularity; + STREAM QColor color; + + VoxelColorSphereEdit(const glm::vec3& center = glm::vec3(), float radius = 0.0f, + float granularity = 0.0f, const QColor& color = QColor()); + + virtual void apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const; +}; + +DECLARE_STREAMABLE_METATYPE(VoxelColorSphereEdit) + +/// An edit that sets the materials of voxels within a sphere to a value. +class VoxelMaterialSphereEdit : public MetavoxelEdit { + STREAMABLE + +public: + + STREAM glm::vec3 center; + STREAM float radius; + STREAM float granularity; + STREAM SharedObjectPointer material; + STREAM QColor averageColor; + + VoxelMaterialSphereEdit(const glm::vec3& center = glm::vec3(), float radius = 0.0f, float granularity = 0.0f, + const SharedObjectPointer& material = SharedObjectPointer(), const QColor& averageColor = QColor()); + + virtual void apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const; +}; + +DECLARE_STREAMABLE_METATYPE(VoxelMaterialSphereEdit) #endif // hifi_MetavoxelMessages_h diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index cbd057ecda..3611605515 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -19,6 +19,7 @@ #include // to load voxels from file #include +#include #include #include @@ -744,6 +745,12 @@ public: bool found; }; +class ContentArgs { +public: + AACube cube; + CubeList* cubes; +}; + bool findCapsulePenetrationOp(OctreeElement* element, void* extraData) { CapsuleArgs* args = static_cast(extraData); @@ -786,6 +793,39 @@ bool findShapeCollisionsOp(OctreeElement* element, void* extraData) { return false; } +quint64 cubeListHashKey(const glm::vec3& point) { + // NOTE: TREE_SCALE = 16384 (15 bits) and multiplier is 1024 (11 bits), + // so each component (26 bits) uses more than its alloted 21 bits. + // however we don't expect to span huge cubes so it is ok if we wrap + // (every 2^21 / 2^10 = 2048 meters). + const uint BITS_PER_COMPONENT = 21; + const quint64 MAX_SCALED_COMPONENT = 2097152; // 2^21 + const float RESOLUTION_PER_METER = 1024.0f; // 2^10 + return (quint64)(point.x * RESOLUTION_PER_METER) % MAX_SCALED_COMPONENT + + (((quint64)(point.y * RESOLUTION_PER_METER)) % MAX_SCALED_COMPONENT << BITS_PER_COMPONENT) + + (((quint64)(point.z * RESOLUTION_PER_METER)) % MAX_SCALED_COMPONENT << 2 * BITS_PER_COMPONENT); +} + +bool findContentInCubeOp(OctreeElement* element, void* extraData) { + ContentArgs* args = static_cast(extraData); + + // coarse check against bounds + AACube cube = element->getAACube(); + cube.scale(TREE_SCALE); + if (!cube.touches(args->cube)) { + return false; + } + if (!element->isLeaf()) { + return true; // recurse on children + } + if (element->hasContent()) { + // NOTE: the voxel's center is unique so we use it as the input for the key + args->cubes->insert(cubeListHashKey(cube.calcCenter()), cube); + return true; + } + return false; +} + bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration, Octree::lockType lockType, bool* accurateResult) { @@ -854,6 +894,16 @@ bool Octree::findShapeCollisions(const Shape* shape, CollisionList& collisions, return args.found; } +bool Octree::findContentInCube(const AACube& cube, CubeList& cubes) { + if (!tryLockForRead()) { + return false; + } + ContentArgs args = { cube, &cubes }; + recurseTreeWithOperation(findContentInCubeOp, &args); + unlock(); + return true; +} + class GetElementEnclosingArgs { public: OctreeElement* element; diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index c7e35c0d2a..f58b5f0cbd 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -33,6 +33,7 @@ class Shape; #include +#include #include #include @@ -47,6 +48,7 @@ public: // Callback function, for recuseTreeWithOperation typedef bool (*RecurseOctreeOperation)(OctreeElement* element, void* extraData); typedef enum {GRADIENT, RANDOM, NATURAL} creationMode; +typedef QHash CubeList; const bool NO_EXISTS_BITS = false; const bool WANT_EXISTS_BITS = true; @@ -308,6 +310,8 @@ public: bool findShapeCollisions(const Shape* shape, CollisionList& collisions, Octree::lockType = Octree::TryLock, bool* accurateResult = NULL); + bool findContentInCube(const AACube& cube, CubeList& cubes); + OctreeElement* getElementEnclosingPoint(const glm::vec3& point, Octree::lockType lockType = Octree::TryLock, bool* accurateResult = NULL); diff --git a/libraries/shared/src/PhysicsEntity.h b/libraries/shared/src/PhysicsEntity.h index 3407ac8421..9f98cc96ca 100644 --- a/libraries/shared/src/PhysicsEntity.h +++ b/libraries/shared/src/PhysicsEntity.h @@ -33,6 +33,8 @@ public: PhysicsEntity(); virtual ~PhysicsEntity(); + virtual void stepForward(float deltaTime) { } + void setTranslation(const glm::vec3& translation); void setRotation(const glm::quat& rotation); diff --git a/libraries/shared/src/PhysicsSimulation.cpp b/libraries/shared/src/PhysicsSimulation.cpp index b58f62dfd4..93f0797c94 100644 --- a/libraries/shared/src/PhysicsSimulation.cpp +++ b/libraries/shared/src/PhysicsSimulation.cpp @@ -30,6 +30,10 @@ PhysicsSimulation::PhysicsSimulation() : _translation(0.0f), _frameCount(0), _en } PhysicsSimulation::~PhysicsSimulation() { + clear(); +} + +void PhysicsSimulation::clear() { // entities have a backpointer to this simulator that must be cleaned up int numEntities = _otherEntities.size(); for (int i = 0; i < numEntities; ++i) { @@ -43,6 +47,9 @@ PhysicsSimulation::~PhysicsSimulation() { // but Ragdolls do not _ragdoll = NULL; _otherRagdolls.clear(); + + // contacts have backpointers to shapes so we clear them + _contacts.clear(); } void PhysicsSimulation::setRagdoll(Ragdoll* ragdoll) { @@ -134,6 +141,18 @@ void PhysicsSimulation::removeShapes(const PhysicsEntity* entity) { } } +void PhysicsSimulation::removeShape(const Shape* shape) { + // remove data structures with pointers to shape + QMap::iterator itr = _contacts.begin(); + while (itr != _contacts.end()) { + if (shape == itr.value().getShapeA() || shape == itr.value().getShapeB()) { + itr = _contacts.erase(itr); + } else { + ++itr; + } + } +} + const float OTHER_RAGDOLL_MASS_SCALE = 10.0f; bool PhysicsSimulation::addRagdoll(Ragdoll* doll) { @@ -195,7 +214,7 @@ void PhysicsSimulation::stepForward(float deltaTime, float minError, int maxIter quint64 startTime = now; quint64 expiry = startTime + maxUsec; - moveRagdolls(deltaTime); + integrate(deltaTime); enforceContacts(); int numDolls = _otherRagdolls.size(); { @@ -238,8 +257,12 @@ void PhysicsSimulation::stepForward(float deltaTime, float minError, int maxIter pruneContacts(); } -void PhysicsSimulation::moveRagdolls(float deltaTime) { +void PhysicsSimulation::integrate(float deltaTime) { PerformanceTimer perfTimer("integrate"); + int numEntities = _otherEntities.size(); + for (int i = 0; i < numEntities; ++i) { + _otherEntities[i]->stepForward(deltaTime); + } _ragdoll->stepForward(deltaTime); int numDolls = _otherRagdolls.size(); for (int i = 0; i < numDolls; ++i) { diff --git a/libraries/shared/src/PhysicsSimulation.h b/libraries/shared/src/PhysicsSimulation.h index 1db56a46e2..955029c174 100644 --- a/libraries/shared/src/PhysicsSimulation.h +++ b/libraries/shared/src/PhysicsSimulation.h @@ -27,6 +27,8 @@ public: PhysicsSimulation(); ~PhysicsSimulation(); + + void clear(); void setTranslation(const glm::vec3& translation) { _translation = translation; } const glm::vec3& getTranslation() const { return _translation; } @@ -39,6 +41,7 @@ public: void removeEntity(PhysicsEntity* entity); void removeShapes(const PhysicsEntity* entity); + void removeShape(const Shape* shape); /// \return true if doll was added to or is already in the list bool addRagdoll(Ragdoll* doll); @@ -52,7 +55,7 @@ public: void stepForward(float deltaTime, float minError, int maxIterations, quint64 maxUsec); protected: - void moveRagdolls(float deltaTime); + void integrate(float deltaTime); /// \return true if main ragdoll collides with other avatar bool computeCollisions(); diff --git a/libraries/shared/src/Shape.h b/libraries/shared/src/Shape.h index 52369139c0..345d69d8e4 100644 --- a/libraries/shared/src/Shape.h +++ b/libraries/shared/src/Shape.h @@ -81,17 +81,22 @@ public: protected: // these ctors are protected (used by derived classes only) - Shape(Type type) : _type(type), _owningEntity(NULL), _boundingRadius(0.f), _translation(0.f), _rotation() { + Shape(Type type) : _type(type), _owningEntity(NULL), + _boundingRadius(0.f), _translation(0.f), + _rotation(), _mass(MAX_SHAPE_MASS) { _id = getNextID(); } - Shape(Type type, const glm::vec3& position) - : _type(type), _owningEntity(NULL), _boundingRadius(0.f), _translation(position), _rotation() { + Shape(Type type, const glm::vec3& position) : + _type(type), _owningEntity(NULL), + _boundingRadius(0.f), _translation(position), + _rotation(), _mass(MAX_SHAPE_MASS) { _id = getNextID(); } - Shape(Type type, const glm::vec3& position, const glm::quat& rotation) - : _type(type), _owningEntity(NULL), _boundingRadius(0.f), _translation(position), _rotation(rotation) { + Shape(Type type, const glm::vec3& position, const glm::quat& rotation) : _type(type), _owningEntity(NULL), + _boundingRadius(0.f), _translation(position), + _rotation(rotation), _mass(MAX_SHAPE_MASS) { _id = getNextID(); } diff --git a/libraries/shared/src/ShapeCollider.cpp b/libraries/shared/src/ShapeCollider.cpp index 399d6e3d42..259b7c9118 100644 --- a/libraries/shared/src/ShapeCollider.cpp +++ b/libraries/shared/src/ShapeCollider.cpp @@ -676,8 +676,7 @@ CollisionInfo* sphereVsAACubeHelper(const glm::vec3& sphereCenter, float sphereR // sphereCenter is touching cube surface, so we can't use the difference between those two // points to compute the penetration direction. Instead we use the unitary components of // cubeContact. - direction = cubeContact / halfCubeSide; - glm::modf(BA, direction); + glm::modf(cubeContact / halfCubeSide, direction); lengthDirection = glm::length(direction); } else if (lengthDirection > sphereRadius) { collisions.deleteLastCollision(); diff --git a/libraries/shared/src/ShutdownEventListener.cpp b/libraries/shared/src/ShutdownEventListener.cpp index 961d18e793..6cd7592b69 100644 --- a/libraries/shared/src/ShutdownEventListener.cpp +++ b/libraries/shared/src/ShutdownEventListener.cpp @@ -27,5 +27,5 @@ bool ShutdownEventListener::nativeEventFilter(const QByteArray &eventType, void* } } #endif - return true; + return false; }