OpenGL - Nothing Happens When Running Tweaked Version of Moving Linear Gradient
in
Core Library Questions
•
1 year ago
I have changed the example sketch of Linear Gradient, so the gradient moves.
Unfortunately, there are horizontal scan lines when I run it. P2D improves it a little, yet I was hoping OPENGL would improve the appearance, but it does not work. Just a blank grey screen. I am guessing OPENGL does not handle the set() function?
OPENGL works fine on othe Processing sketches on my mac.
Here's the code:
- import processing.opengl.*;
- int w, h;
- int Y_AXIS = 1;
- int X_AXIS = 2;
- int b1dir = 1;
- int b2dir = 1;
- float x1;
- float y1;
- int x1dir;
- int y1dir;
- color b1;
- color b2;
- color b3;
- void setup(){
- w = screen.width;
- h = screen.height;
- size(w, h, OPENGL);
- //smooth();
- x1 = 0;
- y1 = 600;
- x1dir = 1;
- y1dir = -1;
- b1 = color(69, 139, 116);
- b2 = color(205, 38, 38);
- b3 = color(0, 0, 0);
- }
- void draw() {
- background(0);
- setGradient(0, int(y1), width, 100, b3, b1, Y_AXIS);
- setGradient(0, int(y1) + 100, width, 100, b1, b2, Y_AXIS);
- setGradient(0, int(y1) + 200, width, 100, b2, b3, Y_AXIS);
- y1 += y1dir * 1;
- if(y1 <= -200) {
- y1 = 600;
- }
- }
- void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ){
- // calculate differences between color components
- float deltaR = red(c2)-red(c1);
- float deltaG = green(c2)-green(c1);
- float deltaB = blue(c2)-blue(c1);
- // choose axis
- if(axis == Y_AXIS){
- /*nested for loops set pixels
- in a basic table structure */
- // column
- for (int i=x; i<=(x+w); i++){
- // row
- for (int j = y; j<=(y+h); j++){
- color c = color(
- (red(c1)+(j-y)*(deltaR/h)),
- (green(c1)+(j-y)*(deltaG/h)),
- (blue(c1)+(j-y)*(deltaB/h))
- );
- set(i, j, c);
- }
- }
- }
- else if(axis == X_AXIS){
- // column
- for (int i=y; i<=(y+h); i++){
- // row
- for (int j = x; j<=(x+w); j++){
- color c = color(
- (red(c1)+(j-x)*(deltaR/h)),
- (green(c1)+(j-x)*(deltaG/h)),
- (blue(c1)+(j-x)*(deltaB/h))
- );
- set(j, i, c);
- }
- }
- }
- }
1