Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / UV Mapping In-Program

Author
Message
PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 5th Jul 2015 05:43
I have searched for a simple tutorial on how to do UV Mapping within the code but I haven't come up with anything.

I'm working on a game that will display the scores on the screen. At first I had a separate image for each digit but that was cumbersome, so I created one .png image that has all of the numbers on it. Now all I need to do is move the UV coordinates on the textured image so that it only shows the correct number, but I haven't been able to figure out how to do UV mapping.

Is there a tutorial somewhere or can someone help me? I've searched everywhere and haven't found anything that works.

Basically, I am making a small plain and loading the .png image as a texture. I want to set the UV coordinates of that plain so that the texture only shows the number that I want it to, but I don't know how to change the UV coordinates of the texture from within the code.
PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 5th Jul 2015 05:44
This is what the .png file looks like. The score is displayed on small plains that will each hold one digit of the score.

Attachments

Login to view attachments
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 5th Jul 2015 18:26
You can combine the functions Scale Object Texture and Scroll Object Texture or use the Vertex Data functions to achieve what you need. In this snippet, I use the latter. To simplify the calculation in code, I created my own image. The magic number 0.0625 is the width of a single character divided by the width of the image. Image attached.

Attachments

Login to view attachments
PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 5th Jul 2015 20:24 Edited at: 5th Jul 2015 20:26
Thank you! I'll give that a try. If I'm interpreting your code correctly, it will select a random number then create a UV mapping to the number it selects, right?

I had been using SCROLL OBJECT TEXTURE, which works for initializing the score but hasn't worked when changing the score from one number to another.

I'll try incorporating your code and see how it looks.
PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 5th Jul 2015 20:47
Yay, it works! I owe you a beer or something

I had to do just a small bit of tweaking to the coordinates of the UV data in the function, but once I did, it now works perfectly.
PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 5th Jul 2015 20:56
Here's a screenshot of the game now. The scores at the bottom are where I am now using the function you wrote.

Attachments

Login to view attachments
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 5th Jul 2015 21:55
Your game looks pretty cool. Makes me happy I could have a small part in its creation. Good Luck.
PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 6th Jul 2015 00:21
Hmm, very strange problem occurring now... I copied the function again for a different purpose and it worked fine. Then I tried again and it isn't working.

Here is the code for the function that isn't working:



But when I tried this:


...where I replaced the variable "QObject" with a number, it worked for that particular object.

Obviously, I'm thinking that I passed the wrong variable to the function, but when I created the small test routine:

DO
TEXT 100, 100, STR$(QObject)
SYNC
LOOP

The variable QObject was the correct number. So for some reason it seems that it won't allow me to use a variable in the "lock vertexdata for limb" command.
PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 6th Jul 2015 00:41
I ran an additional test. When I print out the results of GET VERTEXDATA U and GET VERTEXDATA V, it comes out as it should, even when I use a variable as the object number. But for some reason, the UV coordinates are not where they should be when I use a variable for the object number...
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jul 2015 02:35 Edited at: 6th Jul 2015 02:52
I modified my function to except a variable for the object. Lock Vertexdata For Limb seems to work either way. The problem must be somewhere else in your code. With the code you provided, I can not see anything wrong. Sorry.

Edit: Maybe the uv coords are not correct. What is the objects vertex count and unaltered uv data.
PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 6th Jul 2015 03:11 Edited at: 6th Jul 2015 03:17
The vertex count is 6, like it's supposed to be, and the vertexdata is the default -- top left corner to bottom right corner.

And when I use the line:

lock vertexdata for limb 131, 0

it works just fine, but when I use the line:

lock vertexdata for limb QObject, 0

it doesn't work at all, even though I tested and QObject does equal 131. And when I test the vertexdata at the end of the function, it is what it is supposed to be, but it is still displaying the entire image instead of the chunk I am telling it to.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jul 2015 03:21
That is odd. Lock Vertexdata For Limb works fine for me, whether I use a variable or number. Is the variable QObject used elsewhere and global? Since it works using a number the uv data must be correct. idk.

PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 6th Jul 2015 03:23
Ahhh, found the error!

I have been applying a shader to all of my plains to brighten them up (they look really dull when the shader isn't applied). If I apply the shader after calling the function, it undoes all of the UV data.

I changed the code so that the shader is applied *before* calling the function, and everything is fixed now!

*breathes easy*

Thanks again for all your help!
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 6th Jul 2015 03:48
Glad you got that sorted. You could also make the textured object brighter by disabling its reaction to light and ambient light.

PirateJohn
13
Years of Service
User Offline
Joined: 4th Aug 2011
Location: California
Posted: 6th Jul 2015 06:37
Ah, never tried that before. I just put it in my code.

It doesn't quite "pop" as much as the shader did, but I'm sure the savings on processing time more than offset that.

Login to post a reply

Server time is: 2024-11-21 13:31:12
Your offset time is: 2024-11-21 13:31:12