projet-rendu/TextureShader.java

56 lines
1.6 KiB
Java
Raw Normal View History

2022-04-19 09:02:07 +00:00
import java.awt.Color;
2022-04-12 10:08:58 +00:00
/**
* Simple shader that just copy the interpolated color to the screen,
* taking the depth of the fragment into acount.
2022-04-14 20:42:57 +00:00
*
2022-04-12 10:08:58 +00:00
* @author: cdehais
*/
public class TextureShader extends Shader {
2022-04-14 20:42:57 +00:00
2022-04-12 10:08:58 +00:00
DepthBuffer depth;
Texture texture;
boolean combineWithBaseColor;
2022-04-14 20:42:57 +00:00
public TextureShader(GraphicsWrapper screen) {
super(screen);
depth = new DepthBuffer(screen.getWidth(), screen.getHeight());
2022-04-12 10:08:58 +00:00
texture = null;
}
2022-04-14 20:42:57 +00:00
public void setTexture(String path) {
2022-04-12 10:08:58 +00:00
try {
2022-04-14 20:42:57 +00:00
texture = new Texture(path);
2022-04-12 10:08:58 +00:00
} catch (Exception e) {
2022-04-14 20:42:57 +00:00
System.out.println("Could not load texture " + path);
e.printStackTrace();
2022-04-12 10:08:58 +00:00
texture = null;
}
}
2022-04-14 20:42:57 +00:00
public void setCombineWithBaseColor(boolean combineWithBaseColor) {
2022-04-12 10:08:58 +00:00
this.combineWithBaseColor = combineWithBaseColor;
}
2022-04-14 20:42:57 +00:00
public void shade(Fragment fragment) {
if (depth.testFragment(fragment)) {
2022-04-12 10:08:58 +00:00
/* The Fragment may not have texture coordinates */
try {
2022-04-19 09:02:07 +00:00
double u = fragment.getAttribute(7) % 1;
double v = fragment.getAttribute(8) % 1;
2022-04-12 10:08:58 +00:00
2022-04-19 09:02:07 +00:00
Color couleur = texture.sample(u, v);
screen.setPixel(fragment.getX(), fragment.getY(), couleur);
2022-04-12 10:08:58 +00:00
} catch (ArrayIndexOutOfBoundsException e) {
2022-04-14 20:42:57 +00:00
screen.setPixel(fragment.getX(), fragment.getY(), fragment.getColor());
2022-04-12 10:08:58 +00:00
}
2022-04-14 20:42:57 +00:00
depth.writeFragment(fragment);
2022-04-12 10:08:58 +00:00
}
}
2022-04-14 20:42:57 +00:00
public void reset() {
depth.clear();
2022-04-12 10:08:58 +00:00
}
}