D3D problem

Discussions related to graphics (2D and 3D), animation and games programming
RichardRussell

Re: D3D problem

Post by RichardRussell »

Ric wrote: Thu 02 Apr 2020, 19:43 Could you supply the code for that please
Of course, it was a straightforward merging of the existing BBCOWL and LIGHTING demos. I assume it's the BB4W (Direct3D) code you are more interested in rather than the BBCSDL (OpenGL) version:

Code: Select all

      MODE 8 : OFF
      INSTALL @lib$+"D3D9LIB"

      DIM Light%(1), Object%(2), nVert%(2), Format%(2), Size%(2)
      DIM Material%(2), Texture%(2), Yaw(2), Pitch(2), Roll(2)
      DIM Xpos(2), Ypos(2), Zpos(2), Eye(2), LookAt(2), r{pitch%,pbits%}

      ON CLOSE PROCcleanup:QUIT
      ON ERROR PROCcleanup:PRINT REPORT$:END

      pDevice% = FN_initd3d(@hwnd%, 2, 1)
      IF pDevice% = 0 ERROR 100, "Can't initialise Direct3D"
      SYS !(!pDevice%+228), pDevice%, 27, 1 : REM ALPHABLENDENABLE
      SYS !(!pDevice%+228), pDevice%, 19, 5 : REM SRCBLEND
      SYS !(!pDevice%+228), pDevice%, 20, 6 : REM DSTBLEND
      SYS !(!pDevice%+228), pDevice%, 29, 1 : REM D3DRS_SPECULARENABLE
      SYS !(!pDevice%+228), pDevice%, 147, 1 : REM D3DRS_AMBIENTMATERIALSOURCE

      Object%(0) = FN_load3d(pDevice%, @dir$+"OBJECT_texture_opaque_matt.FVF", \
      \                      nVert%(0), Format%(0), Size%(0))
      IF Object%(0) = 0 ERROR 100, "Couldn't load OBJECT_texture_opaque_matt.FVF"

      Object%(1) = FN_load3d(pDevice%, @dir$+"OBJECT_colour_opaque_matt.FVF", \
      \                      nVert%(1), Format%(1), Size%(1))
      IF Object%(1) = 0 ERROR 100, "Couldn't load OBJECT_colour_opaque_matt.FVF"

      Object%(2) = FN_load3d(pDevice%, @dir$+"OBJECT_colour_transp_matt.FVF", \
      \                      nVert%(2), Format%(2), Size%(2))
      IF Object%(2) = 0 ERROR 100, "Couldn't load OBJECT_colour_transp_matt.FVF"

      Texture%(0) = FN_loadtexture(pDevice%, @dir$+"is.bmp")
      IF Texture%(0) = 0 ERROR 100, "Couldn't load is.bmp"

      REM Make texture opaque:
      SYS !(!Texture%(0)+76), Texture%(0), 0, r{}, 0, 0 : REM LockRect
      FOR Y% = 0 TO 340
        FOR X% = 0 TO 501
          P% = r.pbits% + r.pitch%*Y% + 4*X%
          P%?3 = &FF
        NEXT
      NEXT Y%
      SYS!(!Texture%(0)+80), Texture%(0), 0 : REM UnlockRect

      DIM Material{Diffuse{r%,g%,b%,a%}, Ambient{r%,g%,b%,a%}, \
      \   Specular{r%,g%,b%,a%}, Emissive{r%,g%,b%,a%}, Power%}

      Material.Specular.r% = FN_f4(120/255)
      Material.Specular.g% = FN_f4(120/255)
      Material.Specular.b% = FN_f4(120/255)
      Material.Power% = FN_f4(40)
      Material%(0) = Material{}

      DIM D3Dlight9{(1) Type%, Diffuse{r%,g%,b%,a%}, Specular{r%,g%,b%,a%}, \
      \ Ambient{r%,g%,b%,a%}, Position{x%,y%,z%}, Direction{x%,y%,z%}, \
      \ Range%, Falloff%, Attenuation0%, Attenuation1%, Attenuation2%, \
      \ Theta%, Phi% }

      D3Dlight9{(0)}.Type% = 3 : REM directional
      D3Dlight9{(0)}.Diffuse.r% = FN_f4(170/255)
      D3Dlight9{(0)}.Diffuse.g% = FN_f4(170/255)
      D3Dlight9{(0)}.Diffuse.b% = FN_f4(170/255)
      D3Dlight9{(0)}.Ambient.r% = FN_f4(85/255)
      D3Dlight9{(0)}.Ambient.g% = FN_f4(85/255)
      D3Dlight9{(0)}.Ambient.b% = FN_f4(85/255)
      D3Dlight9{(0)}.Direction.x% = FN_f4(-1)
      D3Dlight9{(0)}.Direction.y% = FN_f4(-1)
      D3Dlight9{(0)}.Direction.z% = FN_f4(-1)
      Light%(0) = D3Dlight9{(0)}

      D3Dlight9{(1)}.Type% = 1 : REM point source
      D3Dlight9{(1)}.Specular.r% = FN_f4(1.0)
      D3Dlight9{(1)}.Specular.g% = FN_f4(1.0)
      D3Dlight9{(1)}.Specular.b% = FN_f4(1.0)
      D3Dlight9{(1)}.Position.x% = FN_f4(-6)
      D3Dlight9{(1)}.Position.y% = FN_f4(0)
      D3Dlight9{(1)}.Position.z% = FN_f4(6)
      D3Dlight9{(1)}.Range% = FN_f4(100)
      D3Dlight9{(1)}.Attenuation0% = FN_f4(1)
      Light%(1) = D3Dlight9{(1)}

      Eye() = 0, 0, 6
      LookAt() = 0, 0, 0
      REPEAT
        Yaw() = -TIME/200
        Pitch() = PI/6
        PROC_render(pDevice%, &00202020, 2, Light%(), \
        \           3, Material%(), Texture%(), Object%(), nVert%(), Format%(), Size%(), \
        \           Yaw(), Pitch(), Roll(), Xpos(), Ypos(), Zpos(), \
        \           Eye(), LookAt(), 0.4, 5/4, 1, 1000)
      UNTIL INKEY(1)=0
      END

      DEF PROCcleanup
      Texture%(0) += 0:IF Texture%(0) PROC_release(Texture%(0))
      Object%(0) += 0 :IF Object%(0)  PROC_release(Object%(0))
      Object%(1) += 0 :IF Object%(1)  PROC_release(Object%(1))
      Object%(2) += 0 :IF Object%(2)  PROC_release(Object%(2))
      pDevice% += 0   :IF pDevice%    PROC_release(pDevice%)
      ENDPROC
Ric
Posts: 208
Joined: Tue 17 Apr 2018, 21:03

Re: D3D problem

Post by Ric »

Once again, thank you.
I will not have chance to look at it until Monday because of work. But hopefully this will help me produce a viable 3d application.
Kind regards Ric
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Ric
Posts: 208
Joined: Tue 17 Apr 2018, 21:03

Re: D3D problem

Post by Ric »

Thanks Richard,
I have now had chance to study the code and have realised (using a bit of d3d research) that if you settexture this is the used by all buffers regardless, so the settexture needs to be set to zero if you want to raster a non-textured primative after the statement is used. A simple insert of

SYS d3ddev.SetTexture%, d3ddev%,0,texture%(0)

Where texture%(0) is a NULL texture, in each buffer set up solves the problem.

Regards Ric
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
RichardRussell

Re: D3D problem

Post by RichardRussell »

Ric wrote: Mon 06 Apr 2020, 21:59 settexture needs to be set to zero if you want to raster a non-textured primative.
I've checked the code of D3D9LIB and SetTexture is indeed called with a NULL parameter for any object without a texture (otherwise all three of your objects would be textured, not just the first!). So I don't believe there is a "problem" that I need to fix.

If you get to a point where you find that you are no longer able to use the library (and from your earlier comments you evidently think you will) please report here what feature(s) it is lacking for your application. Before you abandon the library completely I would obviously want to investigate whether it would be possible to extend it to support your needs (and hopefully to update the OpenGL library in a compatible fashion).

As you will appreciate I'm keen to promote cross-platform programming, at a time when the dominance of Windows is waning (especially as Microsoft have all but abandoned Windows on mobile devices, and Intel have also pulled out of the low-power CPU market). Sometimes it won't be practical (for example for apps which need the full Windows UI look-and-feel) but in the case of 3D graphics OpenGL and Direct3D are neck-and-neck in their capabilities (not forgetting Metal and Vulkan too, but they muddy the waters)