• Advertisement

eflatmajor

Member
  • Content count

    15
  • Joined

  • Last visited

Community Reputation

122 Neutral

About eflatmajor

  • Rank
    Member
  1. Straight Projection

    Thank you D3DXMatrixOrthoLH was exactly what i needed
  2. Is there a way to have directx not project? as in a create a world with a flat square and it will project a flat square of the same size onto my screen no matter how far away from it I am. IN short i want to make a directdraw program using direct3d because i have no idea whats happening in directdraw. Its pretty nitpicky and ive tried turning projection to 90 degrees calculating positions but for some reason my vertical lines are very slightly slanted
  3. Ok thank you ive figured out how to use resources now, I dont like the idea of my application being too large so i might look into the crude zip file method some time. If anyone has any more ideas i'd be happy to hear them
  4. For some reason when i go to add resource from the project folder resource is greyed out and i cant click it and if i try to add a new item there is no Resource File (.rc) file to choose
  5. Im trying to find a way to load textures into a Directx program that doesnt require me to have png or bmp etc, files sitting in the program folder where they can be easily accessed or modified by anyone i give the application to with mspaint. How can I do this?
  6. Filling vertex buffer without memcpy

    I sorta get it now but I guess I'll just stick to using memcpy until I can get my head around it completely. I just liked how this method didnt require me to define the size of the array before it started.
  7. Filling vertex buffer without memcpy

    I think I understand, the third parrameter in Lock() is the array I want the buffer to store. I understand how to use it but im still abit unclear on how it works and what exactly the third parammeter does. Also is there any specific reason DWORD was used for i and not int or float? edit: what happened to eppo's post? also I understand how they stored the vertex points in the array but not how it got to the buffer
  8. Ive been looking at the tutorial that comes with the samples in Directx SDK in the Direct3d folder and the following section of code came up in the lighting part of the tutorial. Normally I assumed that the memcpy function would be needed to fill the vertex buffer but im not sure how the buffer is filled in this code. Can someone please explain the code to me? (I get all the stuff about drawing the cylinder) HRESULT InitGeometry() { // Create the vertex buffer. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50 * 2 * sizeof( CUSTOMVERTEX ), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) { return E_FAIL; } // Fill the vertex buffer. We are algorithmically generating a cylinder // here, including the normals, which are used for lighting. CUSTOMVERTEX* pVertices; if( FAILED( g_pVB->Lock( 0, 0, ( void** )&pVertices, 0 ) ) ) return E_FAIL; for( DWORD i = 0; i < 50; i++ ) { FLOAT theta = ( 2 * D3DX_PI * i ) / ( 50 - 1 ); pVertices[2 * i + 0].position = D3DXVECTOR3( sinf( theta ), -1.0f, cosf( theta ) ); pVertices[2 * i + 0].normal = D3DXVECTOR3( sinf( theta ), 0.0f, cosf( theta ) ); pVertices[2 * i + 1].position = D3DXVECTOR3( sinf( theta ), 1.0f, cosf( theta ) ); pVertices[2 * i + 1].normal = D3DXVECTOR3( sinf( theta ), 0.0f, cosf( theta ) ); } g_pVB->Unlock(); return S_OK; }
  9. Memory Leaks

    I'll try out that safe_release thing but right now im using ptexture->Release(); Would there be any problem with that? Also my release functions all occur at the end of my main program so if my program has a runtime error while debuging and i break the program does that mean that the everything wont be released?
  10. Memory Leaks

    I'm abit worried about the things im hearing about having memory leaks. Its said that if I dont properly release my textures, devices etc. then the memory becomes inaccesible and takes up memory. Does this mean that the memory will not clear itself even if I restart my computer and it will never go away? I've tried to look it up but i cant seem to find any info. Im getting abit worried now because my memory usage seems pretty high even when it's idle with no programs running. Im not sure what the memory usage readings were before i started programing directx and im not sure if it's just a normal reading for this computer so I was just wondering if by inaccesible they REALLY mean inaccesible.
  11. Ok I think i get the perpindicular thing now but i want to be able to control the up vector of the camera using shift + left or shift + right and i cant quite figure out the math behind it. Here's an attempt. static float distance = 10.0f; static float horiangle = 0.0f; static float vertangle = 90.0f; static float camrightangle = 0.0f; static float xpos = 0.0f; static float zpos = 0.0f; static float ypos = 0.0f; static float rotationx = 0.0f; static float rotationy = 0.0f; static float rotationz = 0.0f; static float xright = 1.0f; static float yright = 0.0f; static float zright = 0.0f; if(KEY_DOWN(VK_UP) && distance >= 7.0f) { distance -= 0.5f; } if(KEY_DOWN(VK_DOWN)) { distance += 0.5f; } if(KEY_DOWN(VK_RIGHT) && KEY_UP(VK_SHIFT)) { horiangle += 4.0f; } if(KEY_DOWN(VK_LEFT) && KEY_UP(VK_SHIFT)) { horiangle -= 4.0f; } if(KEY_DOWN(VK_PRIOR) && vertangle > 0) { vertangle -=3.0f; } if(KEY_DOWN(VK_NEXT) && vertangle < 180) { vertangle +=3.0f; } if(KEY_DOWN(VK_RIGHT) && KEY_DOWN(VK_SHIFT)) { camrightangle += 4.0f; } if(KEY_DOWN(VK_LEFT) && KEY_DOWN(VK_SHIFT)) { camrightangle -= 4.0f; } xpos = distance * sin(D3DXToRadian(horiangle)) * sin(D3DXToRadian(vertangle)); ypos = distance * cos(D3DXToRadian(vertangle)) ; zpos = distance * cos(D3DXToRadian(horiangle)) * sin(D3DXToRadian(vertangle)); xright = xpos + distance * sin(D3DXToRadian(camrightangle)) * sin(D3DXToRadian(horiangle)); yright = ypos + distance * cos(D3DXToRadian(vertangle)) ; zright = zpos + distance * cos(D3DXToRadian(camrightangle)) * sin(D3DXToRadian(horiangle)); D3DXMATRIX matView; // the view transform matrix D3DXVECTOR3 CamPos(xpos, ypos, zpos); D3DXVECTOR3 LookAt(0.0f, 0.0f, 0.0f); D3DXVECTOR3 Right(xright, yright, zright); D3DXVECTOR3 ViewVec = LookAt - CamPos; D3DXVECTOR3 Up; D3DXVec3Cross(&Up, &Right, &ViewVec); D3DXVec3Normalize(&Up, &Up); D3DXMatrixLookAtLH(&matView, &CamPos, // the camera position &LookAt, // the look-at position &Up); // the up direction Im thinking it would be something along the lines of a slanted circle perpindicular to th camera lookat vector with the 'right' placed somewhere on this circle but i cant get my head around the parametrics.
  12. Thanks for the help, i thought I might have to alter the up axis but found the math of changing the way axis face abit confusing. I couldnt really think of a good way to phrase it when i wrote the title (which was also part of the reason i found trouble searching for the problem online) And I added the part that clears textures during every render because when I didnt the program would run and eventually get slower. I checked my memory usage in task manager while i ran it and noticed that as the program ran it continually accumulated more and more memory until it slowed eveything down. Please tell me if you know why that is.
  13. I'm abit new to direct3d and im having this problem with my program where if i rotate the camera to point straight down or straight up then my textures all vanish. (when im just using diffuse colours then the colours switch places) I've tried searching around for a solution and checking all my math but i cant seem to find the problem. left and right moves the camera right and left up and down zooms in and out pgup and pgdn moves the camera up and down In my program when you move the camera all the way up or down everything vanishes. I have the .cpp code below, just make any png file called wood.png in the program directory Thanks in advance edit: oh if anyone is wondering i am using this tutorial http://www.directxtutorial.com/Tutorial9/B-Direct3DBasics/dx9B.aspx // include the basic windows header file #include <windows.h> #include <windowsx.h> #include <d3d9.h> #include <d3dx9.h> // include the Direct3D Library file #pragma comment (lib, "d3d9.lib") #pragma comment (lib, "d3dx9.lib") #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) // define the screen resolution #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 // global declarations LPDIRECT3D9 d3d; // the pointer to our Direct3D interface LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer // texture declarations LPDIRECT3DTEXTURE9 texture_1; // function prototypes void initD3D(HWND hWnd); // sets up and initializes Direct3D void render_frame(void); // renders a single frame void cleanD3D(void); // closes Direct3D and releases memory void init_graphics(void); // 3D declarations struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR; FLOAT U, V;}; #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1) // the WindowProc function prototype LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // the entry point for any Windows program int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // the handle for the window, filled by a function HWND hWnd; // this struct holds information for the window class WNDCLASSEX wc; // clear out the window class for use ZeroMemory(&wc, sizeof(WNDCLASSEX)); // fill in the struct with the needed information wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.lpszClassName = "WindowClass1"; // register the window class RegisterClassEx(&wc); // create the window and use the result as the handle hWnd = CreateWindowEx(NULL, "WindowClass1", // name of the window class "Our First Windowed Program", // title of the window WS_EX_TOPMOST | WS_POPUP, // fullscreen values // window style 0,0, // x-position of the window // y-position of the window SCREEN_WIDTH, SCREEN_HEIGHT, // set window to new resolution // width of the window NULL, // we have no parent window, NULL NULL, // we aren't using menus, NULL hInstance, // application handle NULL); // used with multiple windows, NULL // display the window on the screen ShowWindow(hWnd, nCmdShow); // set up and initialize Direct3D initD3D(hWnd); // enter the main loop: // this struct holds Windows event messages MSG msg; // Enter the infinite message loop while(TRUE) { // Check to see if any messages are waiting in the queue while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // Translate the message and dispatch it to WindowProc() TranslateMessage(&msg); DispatchMessage(&msg); } // If the message is WM_QUIT, exit the while loop if(msg.message == WM_QUIT) break; init_graphics(); render_frame(); // check the 'escape' key if(KEY_DOWN(VK_ESCAPE)) { PostMessage(hWnd, WM_DESTROY, 0, 0); } // Run game code here // ... // ... } cleanD3D(); // return this part of the WM_QUIT message to Windows return msg.wParam; } // this is the main message handler for the program LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { // sort through and find what code to run for the message given switch(message) { // this message is read when the window is closed case WM_DESTROY: { // close the application entirely PostQuitMessage(0); return 0; } break; } // Handle any messages the switch statement didn't return DefWindowProc (hWnd, message, wParam, lParam); } // this function initializes and prepares Direct3D for use void initD3D(HWND hWnd) { d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use d3dpp.Windowed = true; // program windowed, not fullscreen d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer d3dpp.EnableAutoDepthStencil = TRUE; // automatically run the z-buffer for us d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 16-bit pixel format for the z-buffer // create a device class using this information and information from the d3dpp stuct d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); init_graphics(); // call the function to initialize the triangle d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); // turn off the 3D lighting d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW );//draw one side //D3DCULL_CCW draws none d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer } // this is the function used to render a single frame void render_frame(void) { // clear the window to a deep blue d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0); d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); d3ddev->BeginScene(); // begins the 3D scene // do 3D rendering on the back buffer here // select which vertex format we are using d3ddev->SetFVF(CUSTOMFVF); // SET UP THE PIPELINE static float distance = 10.0f; static float horiangle = 0.0f; static float vertangle = 90.0f; static float xpos = 0.0f; static float zpos = 0.0f; static float ypos = 0.0f; static float rotationx = 0.0f; static float rotationy = 0.0f; static float rotationz = 0.0f; if(KEY_DOWN(VK_UP) && distance >= 7.0f) { distance -= 0.5f; } if(KEY_DOWN(VK_DOWN)) { distance += 0.5f; } if(KEY_DOWN(VK_RIGHT)) { horiangle += 4.0f; } if(KEY_DOWN(VK_LEFT)) { horiangle -= 4.0f; } if(KEY_DOWN(VK_PRIOR) && vertangle > 0) { vertangle -=3.0f; } if(KEY_DOWN(VK_NEXT) && vertangle < 180) { vertangle +=3.0f; } xpos = distance * sin(D3DXToRadian(horiangle)) * sin(D3DXToRadian(vertangle)); ypos = distance * cos(D3DXToRadian(vertangle)) ; zpos = distance * cos(D3DXToRadian(horiangle)) * sin(D3DXToRadian(vertangle)); // tell Direct3D about our matrix D3DXMATRIX matView; // the view transform matrix D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3 (xpos, ypos, zpos), // the camera position &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView D3DXMATRIX matProjection; // the projection transform matrix D3DXMatrixPerspectiveFovLH(&matProjection, D3DXToRadian(45), // the horizontal field of view (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio 1.0f, // the near view-plane 1000.0f); // the far view-plane d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection // select the vertex buffer to display // select the vertex buffer to display d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX)); D3DXMATRIX matRotateX;// a matrix to store the translation D3DXMatrixRotationX(&matRotateX, D3DXToRadian(rotationx)); d3ddev->SetTransform(D3DTS_WORLD, &matRotateX); // build MULTIPLE matrices to translate the model and one to rotate // the front side // tell Direct3D about each world transform, and then draw another triangle d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX)); // set the texture d3ddev->SetTexture(0, texture_1); d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2); d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2); d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2); d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2); d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2); texture_1->Release(); v_buffer->Release(); // close and release the vertex buffer // copy the vertex buffer to the back buffer d3ddev->EndScene(); // ends the 3D scene d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame } // this is the function that cleans up Direct3D and COM void cleanD3D(void) { v_buffer->Release(); // close and release the vertex buffer texture_1->Release(); // close and release the texture d3ddev->Release(); // close and release the 3D device d3d->Release();// close and release Direct3D } // this is the function that puts the 3D models into video RAM void init_graphics(void) { // load the texture we will use D3DXCreateTextureFromFile(d3ddev, "wood.png", &texture_1); // create the vertices using the CUSTOMVERTEX struct CUSTOMVERTEX vertices[] = { // side 1 { -3.0f, 3.0f, -3.0f, 0xffffffff,1 ,0 ,}, { 3.0f, 3.0f, -3.0f, 0xffffffff,0 ,0 ,}, { -3.0f, -3.0f, -3.0f, 0xffffffff,1 ,1 ,}, { 3.0f, -3.0f, -3.0f, 0xffffffff,0 ,1 ,}, // side 2 { -3.0f, 3.0f, 3.0f, 0xffffffff ,1 ,0 ,}, { -3.0f, -3.0f, 3.0f, 0xffffffff ,0 ,0 ,}, { 3.0f, 3.0f, 3.0f, 0xffffffff ,1 ,1 ,}, { 3.0f, -3.0f, 3.0f, 0xffffffff ,0 ,1 ,}, // side 3 { -3.0f, 3.0f, 3.0f, 0xffffffff ,1 ,0 ,}, { 3.0f, 3.0f, 3.0f, 0xffffffff ,0 ,0 ,}, { -3.0f, 3.0f, -3.0f, 0xffffffff ,1 ,1 ,}, { 3.0f, 3.0f, -3.0f, 0xffffffff ,0 ,1 ,}, // side 4 { -3.0f, -3.0f, 3.0f, 0xffffffff ,1 ,0 ,}, { -3.0f, -3.0f, -3.0f, 0xffffffff ,0 ,0 ,}, { 3.0f, -3.0f, 3.0f, 0xffffffff ,1 ,1 ,}, { 3.0f, -3.0f, -3.0f, 0xffffffff ,0 ,1 ,}, // side 5 { 3.0f, 3.0f, -3.0f, 0xffffffff ,1 ,0 ,}, { 3.0f, 3.0f, 3.0f, 0xffffffff ,0 ,0 ,}, { 3.0f, -3.0f, -3.0f, 0xffffffff ,1 , 1 ,}, { 3.0f, -3.0f, 3.0f, 0xffffffff ,0 , 1 ,}, // side 6 { -3.0f, 3.0f, -3.0f, 0xffffffff ,1 ,0 ,}, { -3.0f, -3.0f, -3.0f, 0xffffffff ,0 ,0 ,}, { -3.0f, 3.0f, 3.0f, 0xffffffff ,1 ,1 ,}, { -3.0f, -3.0f, 3.0f, 0xffffffff ,0 , 1 ,}, }; // create a vertex buffer interface called v_buffer d3ddev->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL); VOID* pVoid; // a void pointer // lock v_buffer and load the vertices into it v_buffer->Lock(0, 0, (void**)&pVoid, 0); memcpy(pVoid, vertices, sizeof(vertices)); v_buffer->Unlock(); } [Edited by - jollyjeffers on May 16, 2009 9:51:27 AM]
  • Advertisement