Water Reflection

Marjock
Unregistered
 
Post: #16
But an array packed with three values for Texture Coordinates has worked for all of my other objects.

-Mark
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #17
Are you using vertex arrays or just immediate mode (with glVertex3f, glVertex2f/3f)? If it's the former, what I said is the problem. If it's the latter, it doesn't really make as much of a difference, since it's going to only look at the first 2 anyway, but you really should only use the 2 rather than 3. Can you be a little more specific as to what the problem is and how the water looks at this point?
Quote this message in a reply
Marjock
Unregistered
 
Post: #18
I'm using vertex arrays. However, when I draw other objects using Vertex arrays, I still supply three texture coordinates, and it still works. glTexCoordPointer() seems to take a parameter which would alter this. However, it seems stupid to ignore you when you're kindly helping me, so I'll try with just two coordinates shortly. :-)

Here are some pictures of the water, as it is now.

From far away:

http://i76.photobucket.com/albums/j37/At...arAway.png


From a middle distance:

http://i76.photobucket.com/albums/j37/At...MidWay.png


From close up:

http://i76.photobucket.com/albums/j37/At...loseUp.png


The quad floating in the middle is textured from what is being drawn to the Pbuffer:

http://i76.photobucket.com/albums/j37/At...exture.png

It looks suspiciously like I'm getting a huge number of tiny versions of my texture over the surface of the water.

-Mark
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #19
So are you sure that you changed every instance of GL_TEXTURE_2D to GL_TEXTURE_RECTANGLE_ARB that has to do with that texture, and you've kept the viewport the same when rendering the water as with the scene? Having many tiny parts of it repeated would certainly make sense, though, if every other texture coordinate had a 0 in it from having the coordinates be set up incorrectly. When you set up your texture coordinate array to have only 2 coordinates, be sure that you glTexCoordPointer call has 2 as it's first parameter. If it's 3 right now, it won't make much of a difference (except for the array being 50% larger than it needs to be), since it will at least look at the right intervals. I would say the most likely cause is the use of GL_TEXTURE_2D in some critical areas, though, but the viewport suggestion, if it isn't already implemented, will cause you some problems after you fix that part.
Quote this message in a reply
Marjock
Unregistered
 
Post: #20
Okay, well, it turns out my problem with GL_ARB_TEXTURE_RECTANGLE was that I didn't realise that the texture coordianmtes went [0...width],[0....height]. But I do now, and so have made GL_TEXTURE_RECTANGLE work (My floating textured quad proves this for me.)
However, the water surface is still very resistant to working. Below are the latest pictures. You can see in some places that there are things that almost start to look like reflections, but in other places there's nothing at all.
The bendy coloured lines are axis lines drawn to the PBuffer (they're straight when they're drawn, obviously, so something in the texture placement is bending them).

So, here are the pictures from four different angles.

http://i76.photobucket.com/albums/j37/At...gleOne.png

http://i76.photobucket.com/albums/j37/At...gleTwo.png

http://i76.photobucket.com/albums/j37/At...eThree.png

http://i76.photobucket.com/albums/j37/At...leFour.png

Once again, thanks heap for the help,
Mark
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #21
Edit: :O WOW!

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #22
Something tells me you're doing something to the texture coordinates you don't have to. If you simply use the results of gluProject, it already does the work of going 0 - width and 0 - height for you. This is assuming you draw the reflection right: essentially, from the camera's perspective, but below the water with everything upside-down, redrawing each screen.
Quote this message in a reply
Marjock
Unregistered
 
Post: #23
Yeah, the texture is exactly the same as the real world, except it's upside down.

I really can't see anything else that I'm doing to the Texture Coordiantes.

Let's see, I draw the water with the following code:

Code:
-(void)drawPondWater
{
    int i;
    int a;
    float matrixArray[16];
    
    for(i=0;i<[pondScene numWater];i++)
    {
        
        
        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, pBufferTexture);
        glEnable(GL_TEXTURE_RECTANGLE_ARB);
        glDisable(GL_TEXTURE_2D);
        
        
        
        glPushMatrix();
        glRotatef(270.0f,1.0f,0.0f,0.0f);
        glTranslatef([pondScene waterProperties][i*6]*SCENE_SCALE,[pondScene waterProperties][i*6+1]*SCENE_SCALE,[pondScene waterProperties][i*6+2]*SCENE_SCALE);
        
        
        
        for(a=0;a<16;a++)
        {
            matrixArray[a] = [pondScene waterMatrix][(i*16)+a];
        }
        glMultMatrixf(matrixArray);
        
        [self calculateWaterTexCoords];
        [waterModel drawModel];
        
        glPopMatrix();
    }
    
}

-calculateWaterTexCoords looks like this:

Code:
-(void)calculateWaterTexCoords
{
    double screenX, screenY, screenZ;
    unsigned int i;
    GLdouble modelMatrix[16];
    GLdouble projMatrix[16];
    GLint viewport[4];


    
    for(i=0;i<[waterModel numFaces];i+=3)
    {
        
        glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
        glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
        glGetIntegerv(GL_VIEWPORT, viewport);
        
        if(gluProject((GLdouble)[waterModel vertexArray][i],
                       (GLdouble)[waterModel vertexArray][i+1],
                       (GLdouble)[waterModel vertexArray][i+2],
                       modelMatrix,
                       projMatrix,
                       viewport,
                       &screenX,
                       &screenY,
                       &screenZ))
        {
            [waterModel pixelCoordArray][i] = (int)screenX;
            [waterModel pixelCoordArray][i+1] = (int)screenY;
            [waterModel pixelCoordArray][i+2] = (int)0.0f;
            
            NSLog(@"%d: %d, %d, %d",i, [waterModel pixelCoordArray][i], [waterModel pixelCoordArray][i+1], [waterModel pixelCoordArray][i+2]);
            
        }
        else
        {
            NSLog(@"Error at gluProject for index: %d",i);
        }

        
    }
                      
}

Everything really looks fine. The only thing that is that when I shit my calculateTexCoords call to before the glTranslate, the texture is drawn in a different place, but still suffers the same odd zig-zaggy distortion.

-Mark
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #24
Just out of curiosity, what is waterMatrix supposed to represent? Multiplying the modelview matrix by another matrix would certainly mess things up in this way.
Quote this message in a reply
Marjock
Unregistered
 
Post: #25
It's a matrix that I pull from Blender which defines the size and rotation of the model currently being drawn.

-Mark
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #26
I see. I think it may be a problem with where you draw the reflection: if you mess up your camera calculations (such as by inverting an axis, which then causes it to move in an opposite way) it won't show what you meant to, possibly even nothing if it's in the right place.
Quote this message in a reply
Marjock
Unregistered
 
Post: #27
Hmm, well, I know I'm drawing everything correctly to the PBuffer, as I can texture a little 300x150pixel quad, and it looks fine.

Are there any obvious cures to this problem that you can think of, such as multiplying something else by my waterMatrix?
It took me a very long time to get things drawing right straight from setting up a scene in Blender, and the only way seemed to be by using matrices, so I'm very reluctant to remove matrix multiplication from the drawing code.

-Mark
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #28
I'm going to have to look into it, since I can't find any obvious reasons as to why it would fail. However, I can make a suggestion right now: you should move the glGetDoublev and glGetIntegerv calls in calculateWaterTexCoords outside the loop: there's no reason to get those matrices more than once. Also, for your texture coordinate array, it may be better if you were to use floats. It shouldn't matter too much, due to the scale, but it couldn't hurt to have a little more precision in there, especially when it keeps them as floats internally anyway. Also, it might be easier to diagnose if you provide the project for me to look at, assuming this isn't some super-secret code you're working on. Rasp
Quote this message in a reply
Marjock
Unregistered
 
Post: #29
Hmm, okay, thanks heaps for all of your help, it really is very good of you. :-)

I was told that when using GL_ARB_TEXTURE_RECTANGLE my tex coords should be ints, but if you're using it successfully with floats, that's actually easier for me. :-)

I'll happily provide the project for you, shortly (When I have access to the other computer, and have fixed up the couple of suggestions you just made).
But, er, I can't promise that the code is at all pretty. :-/ The "SceneLoader" class is especially hideous :-p

So, yeah, thanks heaps, and I'll upload the project shortly.

-Mark
Quote this message in a reply
Marjock
Unregistered
 
Post: #30
Wow, it's been a while, and sadly I haven't been able to work on this project at all over the time, let alone fix it.

Now the code and project are probably largely unreadable, I find it real hard to guage how tough it will be for an outside person looking in. Please, just ask anything you want.

The methods that I can see as being of concern are -caculateWaterTexCoords in MyOpenGLView.m and perhaps -drawPondWater in the same class.
-drawModel in ModelLoader.m is another one.

Once again I apologise for the illegibility of the code, and thank you very much for taking the time to do this.

The link is here:

http://www.yousendit.com/transfer.php?ac...101A05F8B0

-Mark
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Rendering a reflection about an arbitrary plane TomorrowPlusX 3 4,466 Jan 14, 2008 07:51 AM
Last Post: TomorrowPlusX
  Water gfx in a terrain engine Mars_999 3 3,017 Sep 4, 2003 04:32 PM
Last Post: David
  Good-looking Water? Namoreh 7 3,879 Mar 21, 2003 01:40 PM
Last Post: Namoreh