3D Creating background at runtime to be used as 3D texture

kamiyasi

Member
Hi, everyone. I've got randomly generated planets in my 3D game. To give them unique textures, I am using surfaces. However, the method I am using does not seem to be working; the model does not draw at all when I use the texture referenced from a background made from a surface. I am guessing it is because creating a new background during runtime does not include the option to check the texture as 'use for 3D' but I am not sure the actual reason.
Code:
var surfbg;
   
    /////create the draw surface
   
    if !surface_exists(psurf)
    {
        psurf = surface_create(512, 512);
        surface_set_target(psurf);
        draw_clear_alpha(c_black, 0);
        surface_reset_target();
    }
   
        ////draw to surface
         surface_set_target(psurf);
         draw_clear_alpha(c_black, 0);
         
         draw_background_ext(bck_craters,0,0,1,1,0,c_white,1);
         
       
    /////make texture from surface
   
    surfbg = background_create_from_surface(psurf,0,0,512,512,0,0);
   
    tex = background_get_texture(surfbg);
   
    //tex = background_get_texture(tex_spacewall);
   
   
   
    //background_delete(surfbg);
   
    ////done with surface
        surface_reset_target();
    ////clear surface
    surface_free(psurf);

    //////done
    maketex = 1;
 

icuurd12b42

TMC Founder
GMC Elder
Nice guess but no...

A surface is it's own personal space so it's like it has the use for 3d flag checked.

first thing first. does your model draw period?. pass -1 for texture ID you should see something on the screen.

second what are you deleting the surface?

////clear surface
surface_free(psurf);

!!!
You can make a sprite from the surface if you want then get the texture from the sprite. with a sprite it'll be a little slower initialising but at least you wont risk the chance of losing the surface and it's texture
 

kamiyasi

Member
Nice guess but no...

A surface is it's own personal space so it's like it has the use for 3d flag checked.

first thing first. does your model draw period?. pass -1 for texture ID you should see something on the screen.

second what are you deleting the surface?

////clear surface
surface_free(psurf);

!!!
You can make a sprite from the surface if you want then get the texture from the sprite. with a sprite it'll be a little slower initialising but at least you wont risk the chance of losing the surface and it's texture
Yes; the model does draw properly using a normal background texture
I am deleting the surface afterwards because once the background (or sprite as you're suggesting) is created I shouldn't need it anymore. I'll try using a sprite instead although I'm not sure right now what difference that should make
 

kamiyasi

Member
Yeah, it's not working.
I've revised my code, but still can't get it to work.
Code:
var surf;
surf = surface_create(512, 512);
surface_set_target(surf);
draw_clear_alpha(c_black, 0);
draw_background(bck_craters, 0, 0);
tmex = background_create_from_surface(surf, 0, 0, 512, 512, 0,0);
tex = background_get_texture(tmex);
surface_reset_target();
surface_free(surf);
 
Last edited:

icuurd12b42

TMC Founder
GMC Elder
debug:
dont delete the surface and draw it in the draw, does it show?
check the result of create background... is it -1?
move the surface_reset_target up to the line after the draw.
Are you doing this in the create event of an object in the very first room? NONONO... you cant do it there... You MUST have a boot room to bypass this little quirks
 

kamiyasi

Member
debug:
dont delete the surface and draw it in the draw, does it show?
check the result of create background... is it -1?
move the surface_reset_target up to the line after the draw.
Are you doing this in the create event of an object in the very first room? NONONO... you cant do it there... You MUST have a boot room to bypass this little quirks
I do have a buffer room that moves to the main room in the creation code.
The surface does draw and the background created using background_create_from_surface also draws using draw_surface.
Moving surface_reset_target didn't work either.
It's only when I try to use the newly created background on a 3D object that it doesn't work.
I've tried using the background itself and also background_get_texture but both just aren't drawing at all on a 3D object.
 

Mert

Member
Why do you create a background(texture) from surface when you can use the surface as texture. I believe you need.
Code:
surface_get_texture(surface_id);
 

kamiyasi

Member
Why do you create a background(texture) from surface when you can use the surface as texture. I believe you need.
Code:
surface_get_texture(surface_id);
I created a background texture because it doesn't require a surface always being drawn to reference, which I may be wrong about but I think surface_get_texture needs the surface to exist as long as it's being used, correct me if I'm wrong. At any rate, I have also tried using surface_get_texture but I get the same result (or lack thereof) as with sprite_get_texture and background_get_texture. Am I just going crazy here? If anyone has time, can someone confirm whether the system I'm trying to do even works on d3d models? There's always the possibility that it's a bug I should add a ticket for.
 

icuurd12b42

TMC Founder
GMC Elder
Yeah it should all work!

OK so your model must have bad uvs is my conclusion. make a background in the project, set the use for 3d flag and use that background to get the texture from to pass to you model...
 

kamiyasi

Member
Yeah it should all work!

OK so your model must have bad uvs is my conclusion. make a background in the project, set the use for 3d flag and use that background to get the texture from to pass to you model...
Yeah, I've done that and it works just fine when I do that. When I try to draw my surface made background textured to a default d3D primitive that doesn't work either. so it's not the uvs or normals
 

kamiyasi

Member
I have figured it out!!!!

Ok, so I don't know why I had to do this since I recall making this sort of thing a few years ago and not needing to, but here's how I fixed it.

All I had to do was surround my code with d3d_start() and d3d_end()
Code:
d3d_end();


//var surf;
surf = surface_create(512, 512);
surface_set_target(surf);
draw_clear_alpha(c_black, 0);
draw_background(bck_craters, 0, 0);
surface_reset_target();
tmex = background_create_from_surface(surf, 0, 0, 512, 512, false, false);
tex = background_get_texture(tmex);


d3d_start();

surface_free(surf);
Hopefully this can help someone else too. This thread can be closed now, thanks for the suggestions everyone
 

icuurd12b42

TMC Founder
GMC Elder
^ Oh yeah, but why is that code in the draw in the first place? BTW next time you post code tell us what event it's in!
 

icuurd12b42

TMC Founder
GMC Elder
Yeah, are you calling d3d_start in the draw and d3d_end in the draw gui or in an instance that draws last? it's the safest way
 
Top