projet-rendu/TextureShader.java

51 lines
1.4 KiB
Java
Raw Normal View History

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-14 20:42:57 +00:00
/* à compléter */
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
}
}