Skip to content

Commit

Permalink
use pixelWidth/Height throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Jun 5, 2015
1 parent 09a5a67 commit 824e8f2
Showing 1 changed file with 45 additions and 28 deletions.
73 changes: 45 additions & 28 deletions core/src/processing/opengl/PGraphicsOpenGL.java
Expand Up @@ -588,9 +588,7 @@ public void setPrimary(boolean primary) {
public void setSize(int iwidth, int iheight) {
width = iwidth;
height = iheight;
float f = getPixelScale();
pixelWidth = (int)(width * f);
pixelHeight = (int)(height * f);
updatePixelSize();

// init perspective projection based on new dimensions
cameraFOV = 60 * DEG_TO_RAD; // at least for now
Expand Down Expand Up @@ -677,6 +675,13 @@ public PSurface createSurface() { // ignore
}


protected void updatePixelSize() {
float f = getPixelScale();
pixelWidth = (int)(width * f);
pixelHeight = (int)(height * f);
}


//////////////////////////////////////////////////////////////

// IMAGE METADATA FOR THIS RENDERER
Expand Down Expand Up @@ -5586,8 +5591,9 @@ public void loadPixels() {


protected void allocatePixels() {
if ((pixels == null) || (pixels.length != width * height)) {
pixels = new int[width * height];
updatePixelSize();
if ((pixels == null) || (pixels.length != pixelWidth * pixelHeight)) {
pixels = new int[pixelWidth * pixelHeight];
pixelBuffer = PGL.allocateIntBuffer(pixels);
}
}
Expand All @@ -5605,14 +5611,15 @@ protected void restoreSurfaceFromPixels() {


protected void readPixels() {
updatePixelSize();
beginPixelsOp(OP_READ);
try {
// The readPixelsImpl() call in inside a try/catch block because it appears
// that (only sometimes) JOGL will run beginDraw/endDraw on the EDT
// thread instead of the Animation thread right after a resize. Because
// of this the width and height might have a different size than the
// one of the pixels arrays.
pgl.readPixelsImpl(0, 0, width, height, PGL.RGBA, PGL.UNSIGNED_BYTE,
pgl.readPixelsImpl(0, 0, pixelWidth, pixelHeight, PGL.RGBA, PGL.UNSIGNED_BYTE,
pixelBuffer);
} catch (IndexOutOfBoundsException e) {
// Silently catch the exception.
Expand All @@ -5621,14 +5628,15 @@ protected void readPixels() {
try {
// Idem...
PGL.getIntArray(pixelBuffer, pixels);
PGL.nativeToJavaARGB(pixels, width, height);
PGL.nativeToJavaARGB(pixels, pixelWidth, pixelHeight);
} catch (ArrayIndexOutOfBoundsException e) {
}
}


protected void drawPixels(int x, int y, int w, int h) {
int len = w * h;
int f = (int)getPixelScale();
int len = f * w * h;
if (nativePixels == null || nativePixels.length < len) {
nativePixels = new int[len];
nativePixelBuffer = PGL.allocateIntBuffer(nativePixels);
Expand All @@ -5639,18 +5647,18 @@ protected void drawPixels(int x, int y, int w, int h) {
// The pixels to be copied to the texture need to be consecutive, and
// they are not in the pixels array, so putting each row one after
// another in nativePixels.
int offset0 = y * width + x;
int offset0 = f * (y * width + x);
int offset1 = 0;

for (int yc = y; yc < y + h; yc++) {
System.arraycopy(pixels, offset0, nativePixels, offset1, w);
offset0 += width;
offset1 += w;
for (int yc = f * y; yc < f * (y + h); yc++) {
System.arraycopy(pixels, offset0, nativePixels, offset1, f * w);
offset0 += f * width;
offset1 += f * w;
}
} else {
PApplet.arrayCopy(pixels, 0, nativePixels, 0, len);
}
PGL.javaToNativeARGB(nativePixels, w, h);
PGL.javaToNativeARGB(nativePixels, f * w, f * h);
} catch (ArrayIndexOutOfBoundsException e) {
}
PGL.putIntArray(nativePixelBuffer, nativePixels);
Expand All @@ -5673,10 +5681,10 @@ protected void drawPixels(int x, int y, int w, int h) {
// (off)screen buffer.
// First, copy the pixels to the texture. We don't need to invert the
// pixel copy because the texture will be drawn inverted.
int tw = PApplet.min(texture.glWidth - x, w);
int th = PApplet.min(texture.glHeight - y, h);
int tw = PApplet.min(texture.glWidth - f * x, f * w);
int th = PApplet.min(texture.glHeight - f * y, f * h);
pgl.copyToTexture(texture.glTarget, texture.glFormat, texture.glName,
x, y, tw, th, nativePixelBuffer);
f * x, f * y, tw, th, nativePixelBuffer);
beginPixelsOp(OP_WRITE);
drawTexture(x, y, w, h);
endPixelsOp();
Expand All @@ -5685,7 +5693,7 @@ protected void drawPixels(int x, int y, int w, int h) {
// currently drawing to. Because the texture is invertex along Y, we
// need to reflect that in the vertical arguments.
pgl.copyToTexture(texture.glTarget, texture.glFormat, texture.glName,
x, height - (y + h), w, h, nativePixelBuffer);
f * x, f * (height - (y + h)), f * w, f * h, nativePixelBuffer);
}
}

Expand Down Expand Up @@ -5724,6 +5732,8 @@ protected void setImpl(PImage sourceImage,
int sourceX, int sourceY,
int sourceWidth, int sourceHeight,
int targetX, int targetY) {
updatePixelSize();

// Copies the pixels
loadPixels();
int sourceOffset = sourceY * sourceImage.pixelWidth + sourceX;
Expand Down Expand Up @@ -5780,6 +5790,8 @@ public void loadTexture() {
flush(); // To make sure the color buffer is updated.

if (primarySurface) {
updatePixelSize();

if (pgl.isFBOBacked()) {
// In the case of MSAA, this is needed so the back buffer is in sync
// with the rendering.
Expand All @@ -5790,21 +5802,21 @@ public void loadTexture() {
// Here we go the slow route: we first copy the contents of the color
// buffer into a pixels array (but we keep it in native format) and
// then copy this array into the texture.
if (nativePixels == null || nativePixels.length < width * height) {
nativePixels = new int[width * height];
if (nativePixels == null || nativePixels.length < pixelWidth * pixelHeight) {
nativePixels = new int[pixelWidth * pixelHeight];
nativePixelBuffer = PGL.allocateIntBuffer(nativePixels);
}

beginPixelsOp(OP_READ);
try {
// See comments in readPixels() for the reason for this try/catch.
pgl.readPixelsImpl(0, 0, width, height, PGL.RGBA, PGL.UNSIGNED_BYTE,
pgl.readPixelsImpl(0, 0, pixelWidth, pixelHeight, PGL.RGBA, PGL.UNSIGNED_BYTE,
nativePixelBuffer);
} catch (IndexOutOfBoundsException e) {
}
endPixelsOp();

texture.setNative(nativePixelBuffer, 0, 0, width, height);
texture.setNative(nativePixelBuffer, 0, 0, pixelWidth, pixelHeight);
}
} else if (offscreenMultisample) {
// We need to copy the contents of the multisampled buffer to the color
Expand Down Expand Up @@ -5841,11 +5853,12 @@ public void updateDisplay() {


protected void loadTextureImpl(int sampling, boolean mipmap) {
if (width == 0 || height == 0) return;
updatePixelSize();
if (pixelWidth == 0 || pixelHeight == 0) return;
if (texture == null || texture.contextIsOutdated()) {
Texture.Parameters params = new Texture.Parameters(ARGB,
sampling, mipmap);
texture = new Texture(this, width, height, params);
texture = new Texture(this, pixelWidth, pixelHeight, params);
texture.invertedY(true);
texture.colorBuffer(true);
setCache(this, texture);
Expand All @@ -5854,7 +5867,8 @@ protected void loadTextureImpl(int sampling, boolean mipmap) {


protected void createPTexture() {
ptexture = new Texture(this, width, height, texture.getParameters());
updatePixelSize();
ptexture = new Texture(this, pixelWidth, pixelHeight, texture.getParameters());
ptexture.invertedY(true);
ptexture.colorBuffer(true);
}
Expand Down Expand Up @@ -5922,7 +5936,8 @@ protected void drawPTexture() {

@Override
public void mask(PImage alpha) {
if (alpha.width != width || alpha.height != height) {
updatePixelSize();
if (alpha.width != pixelWidth || alpha.height != pixelHeight) {
throw new RuntimeException("The PImage used with mask() must be " +
"the same size as the applet.");
}
Expand Down Expand Up @@ -6485,14 +6500,16 @@ protected void initPrimary() {


protected void beginOnscreenDraw() {
updatePixelSize();

pgl.beginDraw(clearColorBuffer);

if (drawFramebuffer == null) {
drawFramebuffer = new FrameBuffer(this, width, height, true);
drawFramebuffer = new FrameBuffer(this, pixelWidth, pixelHeight, true);
}
drawFramebuffer.setFBO(pgl.getDrawFramebuffer());
if (readFramebuffer == null) {
readFramebuffer = new FrameBuffer(this, width, height, true);
readFramebuffer = new FrameBuffer(this, pixelWidth, pixelHeight, true);
}
readFramebuffer.setFBO(pgl.getReadFramebuffer());
if (currentFramebuffer == null) {
Expand Down

0 comments on commit 824e8f2

Please sign in to comment.