Simple 3D demo source program

zhaozj2021-02-11  161

//uaiiadx.h

#include "stdafx.h" #include #include // Todo: Reference Any Additional Headers you need in stdafx.h // and not in this file # pragma message ("Linking with Microsoft's DirectX Library ... "# Pragma Comment (lib," ddraw.lib ") # Pragma Comment (lib," dxguid.lib ")

Class uaiiadx {public: lpdirect3d lpd3d; lpdirectdraw4 lpdd; lpdirectdrawsurface4 lpddsprimary; lpdirectdrawsurface4 lpddsback; lptstr lpbuffer;

int nlPitch; int dwWidth; int dwHeight; int dwScreenWidth; int dwScreenHeight; int dwScreenColor; public: UAIIADX (); ~ UAIIADX (); BOOL Init (HWND hWnd = GetActiveWindow ()); BOOL Init (int, int, HWND hWnd = GetActiveWindow (); BOOL INITDDRAW (hwnd); Void FreedDraw (Void); Bool Text (int, int, colorref, colorref, lptstr, ...); BOOL LOCK (ColorRef); Bool Lock (Void); BOOL Unlock void); BOOL Backup (void); BOOL Restore (void); BOOL Fill (COLORREF); inline COLORREF GetPixel (int, int); inline COLORREF GetPixel (POINT); inline void SetPixel (int, int, COLORREF); inline void SetPixel (Point, ColorRef); Void Drawline (int, int, int, int, colorref); Void Drawline (Point, Point, ColorRef);

//uaiia3d.h

#include "stdafx.h" #include "uaiiadx.h"

#define m_pai 0.01745329251994329577

class UAIIA3D {typedef struct _tag_POINT3D {int x; int y; int z; int tx; int ty; int tz; POINT pt;} POINT3D; typedef struct _tag_POINT2X {DWORD start; DWORD end;} POINT2X; public: UAIIADX * lpDX; POINT3D * lpPoint; POINT2X * lpLine; DWORD dwPoint, dwLine; DWORD dwMaxPoint, dwMaxLine; public: UAIIA3D (); ~ UAIIA3D (); void Bind (UAIIADX *); DWORD SetPoint (int, int, int); DWORD SetPoint (POINT3D Bool SetLine (int, int, int, int, int, int); BOOL setLine (Point3D, Point3D); Bool SetBox (int, int, int, int, int, int); BOOL Fillbox (int, int, int, int, int , INT, INT, INT; Bool Setare (int, int, int, int, int N = 17); Void Draw (colorRef RGB = RGB (0, 0, 0)); Void Run3D (int x = 0, int Y = 0, int z = 0, double a = 0, double b = 0, double c = 0); void Run2D (int x = 0, int y = 0, int z = 0, double a = 0, Double B = 0, Double C = 0);}; // uaiiadx.cpp

#include "stdafx.h" #include "uaiiadx.h"

UAiiadx :: uaiiadx () {dwscreenwidth = 800; dwscreenheight = 600;}

UAiiadx :: ~ uaiiadx () {freeddraw ();

Bool uaiiadx :: init (hwnd) {if (! "," Error "," error "," ",", ",",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, }

BOOL UAIIADX :: Init (int x, int y, HWND hWnd) {dwScreenWidth = x; dwScreenHeight = y; return Init ();} BOOL UAIIADX :: InitDDraw (HWND hWnd) {DDSURFACEDESC2 ddsd; LPDIRECTDRAW pDD; DDSCAPS2 ddsCaps; if (DirectDrawCreate (NULL, & pDD, NULL) = DD_OK!) return FALSE; if (! pDD-> SetCooperativeLevel (hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) = DD_OK) return FALSE; if (pDD-> SetDisplayMode (dwScreenWidth, dwScreenHeight, 32)! = DD_OK) return FALSE;! if (pDD-> QueryInterface (IID_IDirectDraw4, (LPVOID *) & lpDD) = DD_OK) return false;! if (pDD-> QueryInterface (IID_IDirect3D, (LPVOID *) & lpD3D) = DD_OK) return FALSE ; UpdateWindow (hWnd); ZeroMemory (& ddsd, sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_BACKBUFFERCOUNT | DDSD_CAPS; ddsd.dwBackBufferCount = 1; ddsd.ddsCaps.dwCaps = DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE ; If (LPDD-> CreateSurface (& DDSD, & LPDDSPRIM) ! Ary, NULL) = DD_OK) return FALSE; ZeroMemory (& ddsCaps, sizeof (ddsCaps)); ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;! If (lpDDSPrimary-> GetAttachedSurface (& ddsCaps, & lpDDSBack) = DD_OK) return FALSE; / * ZeroMemory (& ddsd , sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_VIDEOMEMORY; if (lpDD-> CreateSurface (& ddsd, & lpDDSPrimary, NULL)! = DD_OK) return False;

ZeroMemory (& ddsd, sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY; ddsd.dwHeight = dwScreenHeight; ddsd.dwWidth = dwScreenWidth ; If (LPDD, & LPDDSBACK, NULL)! = DD_OK) Return False; / ** / return true;} void uaiiadx :: freeddraw (void) {if (lpdd! = Null) {if (LPDDSPRIMARY! = NULL) {LPDDSPRIMARY-> Release (); lpddsprimary = null;} LPDD-> Release (); lpdd = null;}} Bool Uaiiadx :: Text (int X, int y, colorref col, colorref bkcol, lptstr Str, ...) {Char Buffer [300]; HDC HDC; VA_LIST _List; VA_Start (_List, Str); wvsprintf (buffer, str, _list); if (LPDDSPRIMARY-> Getdc (& HDC)! = DD_OK) Return False; setTextColor (HDC, col); :: setBkcolor (HDC, BKCOL); Textout (HDC, X, Y, Buffer, Lstrlen); LPDDSPRIMARY-> ReleaseDC (HDC); Return TRUE;} BOOL UAIIADX :: Lock (void) {DDSURFACEDESC2 ddsd; // while (lpDDSBack-> GetFlipStatus (DDGFS_ISFLIPDONE) == DDERR_WASSTILLDRAWING); ZeroMemory (& ddsd, sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); switch (lpDDSBack-> Lock (NULL, & ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL)) {case DD_OK: lpBuffer = (LPTSTR) ddsd.lpSurface; nlPitch = ddsd.lPitch; dwWidth = ddsd.dwWidth; dwHeight = ddsd.dwHeight; return True; Case Dderr_INValidObject: Case Dderr_INValidParams: Case Dderr_SurfaceBusy: Case Dderr_surfacebusy: Case Dderr_surfacebusy: LPDDSBACK-> Restore (); Break;

case DDERR_WASSTILLDRAWING: break; default: break;} if (lpDDSBack-> IsLost ()) lpDDSBack-> Restore (); return FALSE;} BOOL UAIIADX :: Lock (COLORREF rgb) {DDBLTFX ddbltfx; ddbltfx.dwSize = sizeof (ddbltfx ); ddbltfx.dwFillColor = rgb;! if (lpDDSBack-> Blt (NULL, NULL, NULL, DDBLT_COLORFILL, & ddbltfx) = DD_OK) return FALSE; return Lock ();} BOOL UAIIADX :: UnLock (void) {lpDDSBack-> UNLOCK (NULL); LPDDSPRIMARY-> FLIP (NULL, DDFLIP_WAIT); / * Rect = 0; rt.top = 0; rt.right = dwscreenwidth; rt.bottom = dwscreenheight; LPDDSBACK-> Unlock (null ); If (LPDDSPRIMARY-> BLTFAST (0, 0, LPDDSBACK, & RT, FALSE)! = DD_OK) {//iff (LPDDSPRIMARY-> BLT (& RT, LPDDSBACK, & RT, FALSE, FALSE)! = DD_OK) {IF LPDDSBACK-> ISLOST ()) LPDDSBACK-> Restore (); if (LPDDSPRIMARY-> ISLOST ()) LPDDSPRIMARY-> RESTORE (); return false;} * / return true;} BOOL UAIIADX :: Backup (void) {RECT Rt; rt.left = 0; rt.top = 0; rt.right = dwscreenwidth; rt.bottom = dwscreenheight; if (lpddsback-> Bltfast (0, 0, LPDDSPRIMARY, & RT, FALSE)! = DD_OK) {if (LPDDSBACK-> ISLOST ()) LPDDSBACK-> RESTORE (); if (lpddsprimary-> islost ()) LPDDSPRIMARY-> RESTORE (); return false;} return True;} bool uaiiadx :: restore (void) {rect {RT.LT = 0; rt.top = 0; rt.right = dwscreenwidth; rt.bottom = dwscreenheight; if (LPDDSPRIMARY-> BLTFast (0, 0, LPDDSBACK, & RT, FALSE)! = dd_ok) {if (LPDDSBACK-> islost ()) LPDDSBACK-> Restore (); if (lpddsprimary-> islost ()) LPDDSPRIMARY-> RESTORE (); return false;} return true; }

BOOL UAIIADX :: Fill (COLORREF rgb) {DDBLTFX ddbltfx; ddbltfx.dwSize = sizeof (ddbltfx); ddbltfx.dwFillColor = rgb; if (! LpDDSBack-> Blt (NULL, NULL, NULL, DDBLT_COLORFILL, & ddbltfx) = DD_OK) return False; return true;} colorref uaiiadx :: getpixel (int x, int y) {return * (lpbuffer y * nlpitch x * 4);} colorref uaiiadx :: getpixel (Point Pt) {Return * (Colorref *) (lpbuffer pt.y * nlpitch pt.x * 4);} void uaiiadx :: setpixel (int x, int y, colorref rg) {ix (x <0 || y <0 || x > = dwidth || y> = dwheight) Return; * (colorref *) (lpbuffer y * nlpitch x * 4) = RGB;} void uaiiadx :: setPixel (Pt, ColorRef RGB) {ix (pt.x <0 || pt.y <0 || pt.x> = dwidth || pt.y> = dwheight) return; * (colorref *) (lpbuffer pt.y * nlpitch pt.x * 4) = RGB } void uaiiadx :: Drawline (int X, int y, int X0, int y0, colorref rg) {register int L, sum; int DX, DY, DM, DN, M, N, U, V, K; DX = X0-x; DY = Y0-Y; if (dx == 0) {IF (DY <0) {u = Y0; v = y;} else {u = y; v = Y0 ;} For (l = u; l <= v; l ) setpixel (x, l, rgb); return;} if (dy == 0) {IF (DX <0) {u = X0; v = x; } Else {u = x; v = x0;} for (l = u; l <= v; l ) setpixel (l, y, rgb); Return;} DM = 1; DN = 1; IF (DX <0 ) {dx = -dx; DM = -1;} if (DY <0) {DY = -Dy; DN = -1;} m = DX; n = Dy; k = 1; u = x; v = Y Y ; If (DX = m) {v = V DN; l = LM;

} IF (k == 1) SetPixel (U, V, RGB); Else Setpixel (V, U, RGB);} return;} void uaiiadx :: Drawline (Point Pt, Point PT0, ColorRef RGB) {Drawline (PT .x, pt.y, pt0.x, pt0.y, rgb);} // uaiia3d.cpp

#include "stdafx.h" #include "uaiia3d.h" #include #include

#define MAXMEMPOINT 50 # define __FILL_LINE__ 20UAIIA3D :: UAIIA3D () {lpDX = NULL; dwPoint = 0; dwLine = 0; dwMaxPoint = MAXMEMPOINT; dwMaxLine = MAXMEMPOINT; lpPoint = new POINT3D [dwMaxPoint]; lpLine = new POINT2X [dwMaxLine]; } Uaiia3d :: ~ uaiia3d () {delete [] lppoint; delete [] lpline; lppoint = null; lpline = null;} void uaiia3d :: bind (uaiiadx * udx) {lpdx = udx; run3d (); Run2d () DWORD UAIA3D :: SETPOINT (POINT3D PT) {for (DWORD I = 0; I = dwmaxpoint-1) {Point3D * lptemppoint = LPPOINT; DWMAXPOINT = MaxMemPoint; LPPOINT = New Point3D [dwMaxpoint]; for (dword i = 0; i = dwmaxline - 1) {Point2x * lptemppoint = lpline; dwmaxline = maxmempoint; lpline = new point2x [dwmaxline]; for (i = 0;

I

LZ); setLine (x, y i, z lz, x width, y i, z lz); setLine (x width, y i, z, x width, y i, z LZ);} if (i! = height) {i = height; setLine (x, y i, z, x width, y i, z); setLine (x, y i, z, x, y i, z lz); setLine (x, y i, z lz, x width, y i, z lz); setline (x width, y i, z, x width, y i, z lz);} for (i = 0; i <= lz; i = __ fill_line __) {setLine (x, y, z i, x width, y, z i); setLine (x, Y, Z i, x, y height, z i); setLine (x, y height, z i, x width, y height, z i); setLine (x width, y, Z i, x width, y height, z i);} IF (i! = lz) {i = lz; setline (x, y, z i, x width, y, z i) SetLine (x, y, z i, x, y height, z i); setLine (x, y height, z i, x width, y height, z i); setLine (X width, y, z i, x width, y height, z i);} reburn true;} BOOL UAIA3D :: Setare (int X, int y, int z, int r, int n) {Point3D * XDX, * XD, X0; Double DA; INT I, J; XD = New Point3D [N]; XDX = New Point3D [N * n]; DA = M_PAI * (Double) 360 / (Double) N; X0. X = r; x0.y = 0; x0.z = 0; for (i = 0; i

} For (i = 0; i Drawline (LPPOINT [LPLINE [i] .start] .pt, lppoint [lpline [i] .end] .pt, rgb);}} void uaiia3d :: run3d (int X, int y, int z, double a, double b, double c) {Int DX, DY, DZ; Double DA, DB, DC; Double Sina, SINB, SINC, COSA, COSB, COSC; DOUBLE MX1, MX2, MX3, MY1, MY2, MY3, MZ1, MZ2, MZ3; DA = M_PAI * (Double) A; DB = M_PAI * (Double) B; DC = M_Pai * (double) C;

Sina = sin (da); sinb = sin (db); sinc = sin (dc); cosa = cos (da); cosb = cos (db); cosc ​​= COS (DC);

MX1 = COSA * COSB; MX2 = -COSA * SINB * SINC - SINA * COSC; MX3 = SINA * SINC - COSA * SINB * COSC; MY1 = SINA * COSB; My2 = COSA * COSC - SINA * SINB * SINC; MY3 = SINA * SINB * COSC COSA * SINC; MZ1 = SINB; MZ2 = COSB * SINC; MZ3 = COSB * COSC;

For (dword i = 0; i

MX1 = COSA * COSB; MX2 = -COSA * SINB * SINC - SINA * COSC; MX3 = SINA * SINC - COSA * SINB * COSC; MY1 = SINA * COSB; My2 = COSA * COSC - SINA * SINB * SINC; MY3 = SINA * SINB * COSC COSA * SINC; MZ1 = SINB; MZ2 = COSB * SINC; MZ3 = COSB * COSC;

For (DWORD I = 0; i

IF (tz == 0) Tz = 1; if (tz> 0) {LPPOINT [i] .pt.x = (tx * 500) / tz lpdx-> dwwidth / 2; lppoint [i] .pt.y = (TY * 500) / TZ LPDX-> dwheight / 2;} else {lppoint [i] .pt.x = - ((TX * 500) / TZ-LPDX-> dwwidth / 2); lppoint [i] .pt.y = - ((Ty * 500) / tz-lpdx-> dwheight / 2);}}}

// Screen.cpp: defines the entry point for the application.//include "stdafx.h" #include "uaiia3d.h"

Uaiiadx uaiiadx; uaiia3d uaiia3d1, uaiia3d2, uaiia3d3;

Lresult Callback WinProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM); BOOL INITWINDOW (Hinstance Hinstance, Int Ncmdshow);

STATIC DOUBLE DA = 0; static double dc = 0; static int TDX = 0; Double TDA [15] = {0.011, 1.876, 2.456, 3.555, 4.987, 5.234, 5.123, 4.222, 3.111, 2.198 1.124, 0.121, 0.297, 1.27, 2.214}; Double TDB [15] = {0.11, 2.876, 3.456, 4.555, 5.987, 4.234, 3.123, 2.222, 1.611, 2.397, 1.27, 0.214}; double tdc [15] = {5.311,4.876,3.456,2.555,2.987,1.234,0.123,1.222,2.111,2.198,1.124,2.121,1.097,2.127,3.214}; int PASCAL WinMain (hINSTANCE hInstance, hINSTANCE hPrevInstance, LPSTR lpCmdLine , int nCmdShow) {MSG msg; if (InitWindow (hInstance, nCmdShow!)) return FALSE; if (uaiiadx.Init (!)) {DestroyWindow (GetActiveWindow ()); return FALSE;} uaiia3d1.Bind (& uaiiadx); uaiia3d2 .Bind (& uaiiadx); uaiia3d3.bind; uaiia3d1.fillbox (100, 300, 300, 500, 100, 300); uaiia3d2.setare (350, 150, 450, 128, 17); uaiia3d3.setare (350, 450, 350, 128, 19);

/ * Uaiia3d1.run3d (); uaiia3d1.run2d (); uaiia3d2.run3d (); uaiia3d2.run2d (); uaiia3d3.run3d (); uaiia3d3.run2d (); * /

WHILE (GetMASG, NULL, 0, 0)) {TranslateMersage (& MSG); Dispatch Msg.wParam;} // ****************** ********************************************************* / / Function: INITWINDOW () // Function: Create a main window.

// ******************************************************** ****************** Static Bool INITWINDOW (Hinstance Hinstance, INT NCMDSHOW) {hwnd hwnd; // window handle WNDCLASS WC; // window structure // fill window class structure wc.style = 0; wc.lpfnWndProc = WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon (hInstance, IDI_APPLICATION); wc.hCursor = LoadCursor (NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "dxuaiia"; // Register the window class RegisterClass (& wc); // create the main window hwnd = CreateWindowEx (0, wc .lpszClassName, "", WS_POPUP, 0, 0, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN), // 0,0, NULL, NULL, hInstance, NULL);! if (hwnd) return FALSE; ShowWindow (hwnd, nCmdShow ); UpdateWindow (hwnd); return true;} // ************************************************ ***************************** / / Function: WinProc () // Function: Handling Main Window Message // **** *********************************************************** ************ LRESULT CALLBACK WINPROC (HWND HWND, UINT MESSAGE, WPARAM W PARAM, LPARAM LPARAM) {HDC HDC; Switch (Message) {Case WM_Keydown: {// PostMessage (hwnd, wm_close, 0, 0); switch (wparam) {copy vk_escape: PostMessage (hwnd, wm_close, 0, 0); Break; Case '1': uaiiadx.lpddsprimary-> getdc (& HDC); Textout (HDC, 2, 2, "UAIIA", 5); uaiiadx.lpddsprimary-> releasedc (hdc); break; case '2': uaiiadx .lpddsback-> getdc (& HDC); Textout (HDC, 2, 20, "XHX", 3); uaiiadx.lpddsback-> releasedc (hdc); break; case '3': uaiiadx.lpddsprimary-> flip (null, DDFLIP_WAIT; Break;}} Break;

转载请注明原文地址:https://www.9cbs.com/read-5376.html

New Post(0)