This site requires JavaScript, please enable it in your browser!
Greenfoot back
SPower
SPower wrote ...

2012/7/18

Problem speeding up texture mapping

SPower SPower

2012/7/18

#
Hi all, I'm programming my version of the old game Wolfenstein 3D, using this scenario's View class: http://www.greenfoot.org/scenarios/5619 For optimisation, I replaced this for loop:
GreenfootImage texture = box.getTexture();
for (int wallY = 0; wallY <= 2 * height; wallY++)
{
   double v = (double)wallY / (double)(2 * height);
   double u = r.getImageOffset();
   
   Color c = texture.getColorAt((int)(u *(texture.getWidth() - 1)), (int)(v*texture.getHeight() - 1)));
   
   int y = img.getHeight()/2 - height + wallY;
   if (y >= 0 && y < img.getHeight()) {
        img.setColorAt((int)column, y, c);
   }
}
by this:
GreenfootImage texture = box.getTexture();
GreenfootImage stroke = new GreenfootImage(1,texture.getHeight());
stroke.drawImage(texture, x,0);
stroke.scale(1, 2*height);
int y = getImage().getHeight() /2 -stroke.getHeight() /2;
getImage().drawImage(stroke, (int)column, y);
There is only one big problem, look at this: with for-loop: my solution: Can somebody help me? I don't know what I did wrong...
mjrb4 mjrb4

2012/7/18

#
You seem to be creating your stroke with a width of 1, and I don't see anywhere where you scale that up - I'm not overly familiar with 3D in Greenfoot, but judging by the fact your walls appear of the correct height but thin, that'd be my best guess as to where the problem lies.
nccb nccb

2012/7/18

#
On this line:
stroke.drawImage(texture, x,0); 
Where is x coming from? The correct X coordinate in the original code to source the original image from is:
(int)(u *(texture.getWidth() - 1))
So one problem could be that your "x" is not the correctly scaled version of "u"? It looks like your code is only working when u=0, and is not drawing correctly at the other times.
SPower SPower

2012/7/18

#
sorry, forgot to post. This is x:
int x = (int)(u * (texture.getWidth() - 1));
the same as what you gave me.
nccb nccb

2012/7/18

#
Ah, I think I see. The x and y coordinates for drawImage are the destination x and y. So any time that x is not zero, it's attempting to draw outside the image. You'll need to use some different methods to copy the slice of the image as you intend.
SPower SPower

2012/7/18

#
Thank you so much for finding the error! But, can you help me with getting a slice of the image? I don't really can come up with an idea, sorry.
SPower SPower

2012/7/18

#
You don't have to help me: I just had to change this:
stroke.drawImage(texture, x,0);
to this:
stroke.drawImage(texture, -x,0);
I usually don't see those things very fast: usually after speaking out load what's written down :), which is what I did this time.
You need to login to post a reply.