Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/org.eclipse.swt.examples/src/examples_graphics.properties
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,15 @@ TwisterDescription=This is a miscellaneous demonstration of an animated twister

Wave=Wave
WaveDescription=This is a miscellaneous demonstration of an animated wave effect with multiple motion patterns cycling through four different wave algorithms.

Dancing=Dancing
DancingDescription=This is a miscellaneous demonstration of an animated isometric dancing shape with sine-wave deformations across a grid of polygons.

BumpMapping=Bump Mapping
BumpMappingDescription=This is a miscellaneous demonstration of an animated bump mapping effect with a moving light source illuminating a textured surface.

FlatText=Flat Text
FlatTextDescription=This is a miscellaneous demonstration of an animated flat text perspective distortion effect using sine/cosine rotation on a tiled texture.

Lens=Lens
LensDescription=This is a miscellaneous demonstration of an animated spherical lens distortion effect that bounces across an image, magnifying the area beneath the lens.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*******************************************************************************
* Copyright (c) 2018 Laurent Caron and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Laurent Lepinay - Original Version
* Laurent Caron (laurent.caron at gmail dot com) - Conversion to SWT
* IBM Corporation - adaptation to GraphicsExample
*******************************************************************************/

package org.eclipse.swt.examples.graphics;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;

/**
* This tab displays an animated bump mapping effect with a moving light source
* illuminating a textured surface.
*/
public class BumpMappingTab extends AnimatedGraphicsTab {

private ImageData bumpImage;
private int[] envMap;
private float fTime;
private int lightX, lightY;
private ImageData imageData;
private Image outputImage;
private int imgWidth, imgHeight;

public BumpMappingTab(GraphicsExample example) {
super(example);
}

@Override
public String getCategory() {
return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$
}

@Override
public String getText() {
return GraphicsExample.getResourceString("BumpMapping"); //$NON-NLS-1$
}

@Override
public String getDescription() {
return GraphicsExample.getResourceString("BumpMappingDescription"); //$NON-NLS-1$
}

@Override
public int getInitialAnimationTime() {
return 10;
}

@Override
public void dispose() {
if (outputImage != null) {
outputImage.dispose();
outputImage = null;
}
}

@Override
public void next(int width, int height) {
if (bumpImage == null) {
return;
}

for (int y = 1; y < imgHeight - 1; y++) {
for (int x = 1; x < imgWidth - 1; x++) {
int dX = (bumpImage.getPixel(x + 1, y) >> 16) - (bumpImage.getPixel(x - 1, y) >> 16);
int dY = (bumpImage.getPixel(x, y + 1) >> 16) - (bumpImage.getPixel(x, y - 1) >> 16);

dX = dX - (lightX - x);
dY = dY - (lightY - y);
if (dX <= -128 || dX >= 128) {
dX = dY = -128;
}
if (dY <= -128 || dY >= 128) {
dX = dY = -128;
}
dX += 128;
dY += 128;

imageData.setPixel(x, y, envMap[dX + 256 * dY]);
}
}

lightX = (int) (imgWidth / 2 + 80 * Math.cos(fTime += .1));
lightY = (int) (imgHeight / 2 + 80 * Math.sin(fTime));
}

@Override
public void paint(GC gc, int width, int height) {
if (!example.checkAdvancedGraphics()) return;

if (bumpImage == null) {
Image sourceImage = example.loadImage(gc.getDevice(), "bump.png"); //$NON-NLS-1$
if (sourceImage == null) return;
bumpImage = sourceImage.getImageData();
imgWidth = bumpImage.width;
imgHeight = bumpImage.height;

envMap = new int[256 * 256];
for (int y = 0; y < 256; y++) {
for (int x = 0; x < 256; x++) {
envMap[x + 256 * y] = (int) (255 - 255 * Math.sqrt((x - 128) * (x - 128) + (y - 128) * (y - 128)) / Math.sqrt(128 * 128 + 128 * 128));
}
}

RGB[] colors = new RGB[256];
for (int i = 0; i < 64; i++) {
colors[i] = new RGB(0, 0, 0);
}
for (int i = 64; i < 128; i++) {
colors[i] = new RGB(0, 0, (i - 64) * 4);
}
for (int i = 128; i < 256; i++) {
colors[i] = new RGB((i - 128) * 2, (i - 128) * 2, 255);
}

fTime = 0.0f;
lightX = (int) (imgWidth / 2 + 80 * Math.cos(fTime));
lightY = (int) (imgHeight / 2 + 80 * Math.sin(fTime));

imageData = new ImageData(imgWidth, imgHeight, 8, new PaletteData(colors));
}

if (imageData == null) {
return;
}

if (outputImage != null) {
outputImage.dispose();
}
outputImage = new Image(gc.getDevice(), imageData);

int x = (width - imgWidth) / 2;
int y = (height - imgHeight) / 2;
gc.drawImage(outputImage, x, y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*******************************************************************************
* Copyright (c) 2018 Laurent Caron and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Guillaume Bouchon (bouchon_guillaume@yahoo.fr) - Original Version
* Laurent Caron (laurent.caron at gmail dot com) - Conversion to SWT
* IBM Corporation - adaptation to GraphicsExample
*******************************************************************************/

package org.eclipse.swt.examples.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;

/**
* This tab displays an animated isometric dancing shape with sine-wave
* deformations across a grid of polygons.
*/
public class DancingTab extends AnimatedGraphicsTab {

private int pw, ph;
private float[][] pointsX;
private float[][] pointsY;
private float[][] pointsZ;
private double dec;
private int cw, ch, dx;
private int waveCx, waveCy;
private Image offscreenImage;
private int lastWidth, lastHeight;

public DancingTab(GraphicsExample example) {
super(example);
}

@Override
public String getCategory() {
return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$
}

@Override
public String getText() {
return GraphicsExample.getResourceString("Dancing"); //$NON-NLS-1$
}

@Override
public String getDescription() {
return GraphicsExample.getResourceString("DancingDescription"); //$NON-NLS-1$
}

@Override
public int getInitialAnimationTime() {
return 10;
}

@Override
public void dispose() {
if (offscreenImage != null) {
offscreenImage.dispose();
offscreenImage = null;
}
}

@Override
public void next(int width, int height) {
if (pointsX == null || width != lastWidth || height != lastHeight) {
init(width, height);
}
dec += 0.25;
for (int x = 0; x < pw; x++) {
for (int y = 0; y < ph; y++) {
pointsX[x][y] = x * cw + y * dx;
pointsZ[x][y] = (float) (-Math.sin(dec + 1.5 * Math.sqrt((x - waveCx) * (x - waveCx) + (y - waveCy) * (y - waveCy)) + ch) * dx);
pointsY[x][y] = y * ch - pointsZ[x][y];
}
}
}

@Override
public void paint(GC gc, int width, int height) {
if (pointsX == null) {
init(width, height);
}

gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
gc.fillRectangle(0, 0, width, height);

if (offscreenImage != null) {
offscreenImage.dispose();
}
offscreenImage = new Image(gc.getDevice(), width, height);
GC imageGC = new GC(offscreenImage);
imageGC.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
imageGC.fillRectangle(0, 0, width, height);
imageGC.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK));

int xc = ph / 2 * dx;
int[] p = new int[8];

for (int x = 0; x < pw - 1; x++) {
for (int y = 0; y < ph - 1; y++) {
int gray = 255 / ph * y;
Color color = new Color(gray, gray, gray);
imageGC.setBackground(color);

p[0] = (int) (pointsX[x][y] - xc);
p[2] = (int) (pointsX[x + 1][y] - xc);
p[4] = (int) (pointsX[x + 1][y + 1] - xc);
p[6] = (int) (pointsX[x][y + 1] - xc);

p[1] = (int) pointsY[x][y];
p[3] = (int) pointsY[x + 1][y];
p[5] = (int) pointsY[x + 1][y + 1];
p[7] = (int) pointsY[x][y + 1];

imageGC.fillPolygon(p);
}
}
imageGC.dispose();

gc.drawImage(offscreenImage, 0, 0);
}

private void init(int width, int height) {
lastWidth = width;
lastHeight = height;
cw = 15;
ch = 15;
dx = 10;

pw = width / cw;
ph = height / ch;
if (pw < 2) pw = 2;
if (ph < 2) ph = 2;

waveCx = pw / 2;
waveCy = ph / 2;

pointsX = new float[pw][ph];
pointsY = new float[pw][ph];
pointsZ = new float[pw][ph];
dec = 0;
}
}
Loading
Loading