- Joined
- Jun 21, 2017
- Messages
- 87
I have been looking into creating a Direct X 9 target. First I had to create a new transcc to get a DirectX 9 target in there. Second, I tried compiling a simple rectangle example to make sure target directory is working and also transcc is working. Once all those are working I tried to get Mojo 1 going. I thought I had to rewrite Mojo 1, but it turn out I can used the variables in Mojo 1 and just plug it into Direct X 9 with minor modification of course.
In mojo.glfw.cpp at line 221, we can see that the Mojo 1 Open GL custom vertex is float x, float y, float textureU, float textureV, and 4 bytes color (alpha, red, green, blue). In DirectX 9 the custom vertex is float x, float y, float z, float rhw, then the 4 bytes color, and then float textureU, and float textureV.
So the difference between Open GL and DirectX 9 is the addition of z and rhw. and moving the texture co-ordinate to the back. Code like drawing rectangle needs to be updated to conform to this. So in Open GL drawing rectangle is like this, mojo.glfw.cpp, line 420:
In DirectX 9 it is like this:
Now for the Flush code (drawing code), in Open GL it is:
And for the DirectX 9 is:
So what this does is whatever we were drawing in Open GL we just plug it into DirectX 9 with that modification of custom vertex from 5 items in Open GL to 7 items in DirectX 9. Hopefully this should create as little problem.
A wierd bug I encounter is Z needs to be 0.5 to always draw. It was drawing at z = 1.0. But then I think I tried playing with 3D like lighting, z buffer etc etc and when I tried DX9 Mojo 1 code again, it doesn't draw. I haven't figure out the root of the problem for this.
The other problem I am having is that I have managed to get DrawText (which uses mojo font) going. But not very good, and it is a hack. Because when DirectX load an image it loads it to power of 2 texture (ie 32, 64, 128 etc etc).
The code currently is quite messy, lots of example code that is redudant. Even that flush code, BeginScene and EndScene should only be called once per draw frame. A screenshot where it is up to:
Anyway things I still need to look at:
* loading an image.
* direct sound.
* direct input but I read using window messages is better, so I may just rip GLFW code.
In mojo.glfw.cpp at line 221, we can see that the Mojo 1 Open GL custom vertex is float x, float y, float textureU, float textureV, and 4 bytes color (alpha, red, green, blue). In DirectX 9 the custom vertex is float x, float y, float z, float rhw, then the 4 bytes color, and then float textureU, and float textureV.
So the difference between Open GL and DirectX 9 is the addition of z and rhw. and moving the texture co-ordinate to the back. Code like drawing rectangle needs to be updated to conform to this. So in Open GL drawing rectangle is like this, mojo.glfw.cpp, line 420:
C++:
vp[0 ]=x0;vp[1 ]=y0;(int&)vp[4 ]=colorARGB;
vp[5 ]=x1;vp[6 ]=y1;(int&)vp[9 ]=colorARGB;
vp[10]=x2;vp[11]=y2;(int&)vp[14]=colorARGB;
vp[15]=x3;vp[16]=y3;(int&)vp[19]=colorARGB;
In DirectX 9 it is like this:
C++:
vp[ 0]=x0;vp[ 1]=y0;vp[ 2]=0.5f;vp[ 3]=1.0f;(int&)vp[ 4]=colorARGB;
vp[ 7]=x1;vp[ 8]=y1;vp[ 9]=0.5f;vp[10]=1.0f;(int&)vp[11]=colorARGB;
vp[14]=x2;vp[15]=y2;vp[16]=0.5f;vp[17]=1.0f;(int&)vp[18]=colorARGB;
vp[21]=x3;vp[22]=y3;vp[23]=0.5f;vp[24]=1.0f;(int&)vp[25]=colorARGB;
Now for the Flush code (drawing code), in Open GL it is:
C++:
void gxtkGraphics::Flush(){
if( !vertCount ) return;
if( primSurf ){
glEnable( GL_TEXTURE_2D );
primSurf->Bind();
}
switch( primType ){
case 1:
glDrawArrays( GL_POINTS,0,vertCount );
break;
case 2:
glDrawArrays( GL_LINES,0,vertCount );
break;
case 3:
glDrawArrays( GL_TRIANGLES,0,vertCount );
break;
case 4:
glDrawElements( GL_TRIANGLES,vertCount/4*6,GL_UNSIGNED_SHORT,quadIndices );
break;
default:
for( int j=0;j<vertCount;j+=primType ){
glDrawArrays( GL_TRIANGLE_FAN,j,primType );
}
break;
}
if( primSurf ){
glDisable( GL_TEXTURE_2D );
}
vertCount=0;
}
And for the DirectX 9 is:
C++:
void gxtkGraphics::Flush(){
if( !vertCount ) return;
if( primSurf )
{
primSurf->Bind();
}
else
{
d3dDev->SetTexture(0, NULL);
}
d3dDev->BeginScene(); // begins the 3D scene
// do 3D rendering on the back buffer here
// select which vertex format we are using
// d3dDev->SetFVF(CUSTOMFVF);
d3dDev->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
switch( primType ){
case 1:
d3dDev->DrawPrimitiveUP(D3DPT_POINTLIST, vertCount, vertices, VERTEX_SIZE * sizeof(float));
break;
case 2:
d3dDev->DrawPrimitiveUP(D3DPT_LINELIST, vertCount / 2, vertices, VERTEX_SIZE * sizeof(float));
break;
case 3:
d3dDev->DrawPrimitiveUP(D3DPT_TRIANGLELIST, vertCount / 3, vertices, VERTEX_SIZE * sizeof(float));
break;
case 4:
d3dDev->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, vertCount, vertCount / 4 * 2, quadIndices, D3DFMT_INDEX16, vertices, VERTEX_SIZE * sizeof(float));
break;
default:
for( int j=0;j<vertCount;j+=primType ){
d3dDev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, primType - 2, &vertices[j], VERTEX_SIZE * sizeof(float));
}
break;
}
d3dDev->EndScene(); // ends the 3D scene
vertCount=0;
}
So what this does is whatever we were drawing in Open GL we just plug it into DirectX 9 with that modification of custom vertex from 5 items in Open GL to 7 items in DirectX 9. Hopefully this should create as little problem.
A wierd bug I encounter is Z needs to be 0.5 to always draw. It was drawing at z = 1.0. But then I think I tried playing with 3D like lighting, z buffer etc etc and when I tried DX9 Mojo 1 code again, it doesn't draw. I haven't figure out the root of the problem for this.
The other problem I am having is that I have managed to get DrawText (which uses mojo font) going. But not very good, and it is a hack. Because when DirectX load an image it loads it to power of 2 texture (ie 32, 64, 128 etc etc).
The code currently is quite messy, lots of example code that is redudant. Even that flush code, BeginScene and EndScene should only be called once per draw frame. A screenshot where it is up to:
Anyway things I still need to look at:
* loading an image.
* direct sound.
* direct input but I read using window messages is better, so I may just rip GLFW code.
Last edited by a moderator: