Projecting a Texture Under Mouse

Started by
4 comments, last by Rarge 12 years, 7 months ago
I am trying to project a texture onto some terrain from under the mouse. I have done the hard part of projecting a ray from the mouse to the terrain and finding where they collide however I'm unsure how to get the texture to project onto the terrain.

I did some research and fiddling and ended up with this:
//mouse_projection is the vector with the coords of where the mouse ray hits the plane
//width is the width of the terrain
//length is the length of the terrain

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(10.0f, 10.0f, 1.0f);
glTranslatef(-(mouse_projection.x + w/2.0f - 1.0f) / w, -(mouse_projection.z + l/2.0f - 1.0f) / l, 0.0f);
glMatrixMode(GL_MODELVIEW);


but this gives me this: (the white dot being where I clicked) The top left has texture coords (0, 0) and the bottom right has texture coords (1, 1).
wBA0Xl.jpg


As you can see the texture is being stretched and darkened, I'm not sure how to prevent this. I think it's something to do with scaling the texture and because of the texture being set to clamp. I'm not sure what to do from here to get it just so there's that square in the middle without any stretching.
Advertisement
Well you cant just draw a quad over some smoothed terrain because it wont warp. So you either have to use a shadow map method which is pretty stupid or if you are using a heightmap. Have a grid mesh for the mouse quad. And as the position changes, sample the terrain heightmap so that you can have the mouse quad vertices map exactly like the terrains vertices because they are using the same heightmap.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal


Well you cant just draw a quad over some smoothed terrain because it wont warp. So you either have to use a shadow map method which is pretty stupid or if you are using a heightmap. Have a grid mesh for the mouse quad. And as the position changes, sample the terrain heightmap so that you can have the mouse quad vertices map exactly like the terrains vertices because they are using the same heightmap.


I don't think he is drawing a quad over a terrain.
I think he says he is projecting a texture onto a terrain and getting some clamping mode behavior. Which clamp mode are you using? GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER?

You can try GL_CLAMP_TO_BORDER and use a border color of {0, 0, 0, 0} so it comes out black.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

[quote name='dpadam450' timestamp='1316018705' post='4861625']
Well you cant just draw a quad over some smoothed terrain because it wont warp. So you either have to use a shadow map method which is pretty stupid or if you are using a heightmap. Have a grid mesh for the mouse quad. And as the position changes, sample the terrain heightmap so that you can have the mouse quad vertices map exactly like the terrains vertices because they are using the same heightmap.


I don't think he is drawing a quad over a terrain.
I think he says he is projecting a texture onto a terrain and getting some clamping mode behavior. Which clamp mode are you using? GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER?

You can try GL_CLAMP_TO_BORDER and use a border color of {0, 0, 0, 0} so it comes out black.
[/quote]

Yes, sorry if I was unclear. I'm trying to get the texture to project without all the stretching being caused by the clamping but I don't know how, I am using GL_CLAMP atm. I tried GL_CLAMP_TO_BORDER; it stopped the stretching and instead the terrain became the colour of the border but ignored lighting and any other textures.
Well that is what GL_CLAMP is going to do. You need to use GL_CLAMP_TO_BORDER and then there is a glTexParameter (GL_BORDER_COLOR, array[]); something like that where you pass an array of 4 GL_FLOATS. Did you also do that part because it sounds like no.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal


Well that is what GL_CLAMP is going to do. You need to use GL_CLAMP_TO_BORDER and then there is a glTexParameter (GL_BORDER_COLOR, array[]); something like that where you pass an array of 4 GL_FLOATS. Did you also do that part because it sounds like no.


Yes I did that part.

I have got it working now though. I set the border colour to white and fixed the lighting problem.

Thank you.

This topic is closed to new replies.

Advertisement