Jump to content
  • Advertisement
Sign in to follow this  
Rarge

Projecting a Texture Under Mouse

This topic is 2619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!