Scroll through all the pixels and get / set an individual pixel color in OpenGL?

I wrote something with the processing that I now want to do Mac OS X Screen Saver. However, diving into OpenGL was not as easy as I thought it would be.

Basically, I want to scroll through all the pixels on the screen and set a different pixel color based on this pixel color.

The processing code is as follows:

void setup(){
  size(500,500, P2D);
  frameRate(30);
  background(255);
}

void draw(){
 for(int x = 0; x<width; x++){
  for(int y = 0; y<height; y++){
     float xRand2 = x+random(2);
     float yRand2 = y+random(2);

     int xRand = int(xRand2);
     int yRand = int(yRand2);

     if(get(x,y) == -16777216){
     set(x+xRand, y+yRand, #FFFFFF);
     }
     else if(get(x,y) == -1){
     set(x+xRand, y+yRand, #000000);
     }
   }
 }
}

It is not very beautiful and not very effective. However, I would like to learn how to do something similar to OpenGL. I don’t even know where to start.

+3
source share
3 answers

, , , . , , , draw():

void draw() {
  loadPixels();
  for(int x = 0; x<width/2; x++) {
    for(int y = 0; y<height/2; y++) {
      int x_new = 2*x+int(random(2));
      int y_new = 2*y+int(random(2));

      if (x_new < width && y_new < height) {
        int dest_pixel = (y_new*width + x_new);

        color c = pixels[y*width+x];

        if(c == #FFFFFF){
          pixels[dest_pixel] = #000000;
        }
        else {
          pixels[dest_pixel] = #FFFFFF;
        }
      }
    }
  }
  updatePixels();
}

, . , 3/4 set() , . if - .

. , , / . , , , .

glDrawPixels GL_LUMINANCE GL_UNSIGNED_BYTE, , 32- RGBA.

0

OpenGL , , . , , ..

, OpenGL, , . , . , GPU CPU, CPU. , , , OpenGL, .

, SDL (, , glfw), . , OpenGL. glDrawPixels. . , , .

, SDL, .

: , ( ) , , . . :

  • : A B
  • ,
  • A B,
  • Render again
+2

, CPU , glDrawPixels , . , . , - glTexImage2D, , . , . - , .

+2

Source: https://habr.com/ru/post/1787063/


All Articles