# Mipmaps in OpenGL

**URL:** https://discourse.processing.org/t/mipmaps-in-opengl/42315
**Category:** Coding Questions
**Created:** [June 30, 2023, 12:23pm UTC](https://discourse.processing.org/t/mipmaps-in-opengl/42315 "2023-06-30T12:23:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [June 30, 2023, 12:23pm UTC](https://discourse.processing.org/t/mipmaps-in-opengl/42315/1 "2023-06-30T12:23:50Z")

</div>

I have the following image:

 ![big](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/b/3b5adb20f76c92c947adf4b2e2039250c74c79a5.png)

And the following sketch:

```java
PImage img;
PGraphics big;
PGraphics small;

boolean from_big_pg = true;

void setup() {
  size(320, 180, P2D);
  img = loadImage("big.png");
  big = createGraphics(1280, 720, P2D);
  small = createGraphics(32, 18, P2D);
}

void draw() {
  
  big.beginDraw();
  big.image(img, 0, 0, big.width, big.height);
  big.endDraw();
  
  small.beginDraw();
  small.image(from_big_pg ? big : img, 0, 0, small.width, small.height);
  small.endDraw();
  
  
  gl_nearest_for_texture(small);
  image(small, 0, 0, 320, 180);
  gl_linear_for_texture(small);
}

public void gl_nearest_for_texture(PImage image) {
  PGL pgl = beginPGL();
  Texture image_tex = ((PGraphicsOpenGL)g).getTexture(image);
  pgl.bindTexture(PGL.TEXTURE_2D, image_tex.glName);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MIN_FILTER, PGL.NEAREST);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MAG_FILTER, PGL.NEAREST);
  pgl.bindTexture(PGL.TEXTURE_2D, 0);
  endPGL();
}

public void gl_linear_for_texture(PImage image) {
  PGL pgl = beginPGL();
  Texture image_tex = ((PGraphicsOpenGL)g).getTexture(image);
  pgl.bindTexture(PGL.TEXTURE_2D, image_tex.glName);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MIN_FILTER, PGL.LINEAR);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MAG_FILTER, PGL.LINEAR);
  pgl.bindTexture(PGL.TEXTURE_2D, 0);
  endPGL();
}

```

This is the result with from\_big\_pg set to false:

 ![Screenshot 2023-06-30 at 14.06.56](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/e/ce8b980bbe6c154102b755ed83cd4815ad518f25.png)

and this when it is set to true:

 ![Screenshot 2023-06-30 at 14.07.03](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/d/fd6e78c43c525f143df6d2e884e4d6ae3776a38c.png)

I would like the second result to look like the first. I suspect I have to set something with the mipmaps but I’m stuck here. Does someone know why the downscale from the PGraphics looks so bad?

This is one of the attempts:

```java
public void gl_texParameter_min_mag(PImage image, int min, int mag) {
  PGL pgl = beginPGL();
  Texture image_tex = ((PGraphicsOpenGL)g).getTexture(image);
  pgl.bindTexture(PGL.TEXTURE_2D, image_tex.glName);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MIN_FILTER, min);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MAG_FILTER, mag);
  pgl.bindTexture(PGL.TEXTURE_2D, 0);
  endPGL();
}

```

Then before `small.beginDraw()` I added `gl_texParameter_min_mag(big, PGL.LINEAR_MIPMAP_LINEAR, PGL.LINEAR);`

Now everything is black, so maybe this would work, but the mipmaps are not updated?

I don’t know, hope someone can help since this seems a bit advanced.

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [July 4, 2023, 10:31am UTC](https://discourse.processing.org/t/mipmaps-in-opengl/42315/2 "2023-07-04T10:31:59Z")

</div>

Ok I’m a bit further, I can use `gl_texture(in).usingMipmaps(true, 3);` and this fixes it.  
But it does not work when the input comes from a video file…

```auto
public Texture gl_texture(PImage img) {
  return ((PGraphicsOpenGL)g).getTexture(img);
}

```

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [February 13, 2024, 8:18pm UTC](https://discourse.processing.org/t/mipmaps-in-opengl/42315/3 "2024-02-13T20:18:52Z")

</div>

> <https://github.com/processing/processing-video/issues/218>
>
> I tried to isolate the problem, but if I get rid of the PGraphics for example th…e bug seems to disappear.
> 
> If I run the below script I get:
> 
> \<img width="432" alt="Screenshot 2023-07-05 at 13 57 00" src="https://github.com/processing/processing-video/assets/738650/3743ba39-4624-4289-9fa7-06e01d7824df"\>
> 
> \`\`\`java
> import processing.video.\*;
> 
> PGraphics big;
> PGraphics small;
> 
> boolean from\_big\_pg = true;
> 
> Movie movie;
> 
> void setup() {
> size(320, 180, P2D);
> movie = new Movie(this, "big.mp4");
> movie.loop();
> 
> big = createGraphics(1280, 720, P2D);
> small = createGraphics(32, 18, P2D);
> }
> 
> //void movieEvent(Movie m) {
> // m.read();
> //}
> 
> void draw() {
> 
> if (movie.available()) {
> movie.read();
> }
>   
> // only works with \`void movieEvent(Movie m)\`
> // and not with \`if (movie.available())\` (it stays grey)
>   
> gl\_texture(movie).usingMipmaps(true, 3);
> gl\_texture(big).usingMipmaps(true, 3);
> gl\_texture(small).usingMipmaps(true, 3);
> 
> big.beginDraw();
> big.image(movie, 0, 0, big.width, big.height);
> big.endDraw();
> 
> 
> small.beginDraw();
> small.image(from\_big\_pg ? big : movie, 0, 0, small.width, small.height);
> small.endDraw();
> 
> image(small, 0, 0, 320, 180);
> }
> 
> public Texture gl\_texture(PImage img) {
> return ((PGraphicsOpenGL)g).getTexture(img);
> }
> \`\`\`
> 
> If I comment out the following: (which is not what I want):
> 
> \`\`\`java
> gl\_texture(movie).usingMipmaps(true, 3);
> gl\_texture(big).usingMipmaps(true, 3);
> gl\_texture(small).usingMipmaps(true, 3);
> \`\`\`
> 
> Then it works again:
> 
> \<img width="432" alt="Screenshot 2023-07-05 at 14 02 43" src="https://github.com/processing/processing-video/assets/738650/d0f7838f-9905-498b-be12-6fb2975fb500"\>
> 
> Now the interesting part:
> 
> If I use:
> 
> \`\`\`java
> void movieEvent(Movie m) {
> m.read();
> }
> \`\`\`
> 
> Instead of:
> 
> \`\`\`java
> if (movie.available()) {
> movie.read();
> }
> \`\`\`
> 
> Then the \`usingMipmaps\` part works...
> 
> (Also if the usingMipmaps is before \`if (movie.available()) {\` then there is a nullpointerException).
> 
> The video file:
> https://github.com/processing/processing-video/assets/738650/f7a6ba31-b697-49fb-aeac-110823d0453d
