Skip to main content
gamedev.net
🔒 Locked

first 3D program problem (flat image)

Started by ThreeDParadox 4 replies 850+ views
Original Post
ThreeDParadox
ThreeDParadox
I am entering into 3D graphics and i am having some problems. For some reason my image is flat, the bottom of the cube in my app is only like .6 of a unit from the top. Im not sure what to look for, since ive been trying just to get something on the screen for like a week, and i just now got that accomplished. Anyway if anyone can help, go to this link and download the cpp file and the header file. I had to change the header file to a cpp because freeservers doesnt support .h for some reason. So when you download it change Eng3D.cpp to Eng3D.h. Thanks a million, i seriously need the help. Also if you notice anything else that isnt right please let me know, and if you could explain the right way, im kinda at a loss right now... Thanks. http://digitalmindset.8m.com/links.html
ThreeDParadox
ThreeDParadox
#define WIN_32_LEAN_AND_MEAN
#define INITGUID
#define WINDOW_CLASS_NAME "WindowClassName"
#define screenWidth 600
#define screenHeight 600


#include <windows.h>
#include <windowsx.h>
#include <math.h>
#include <stdio.h>
#include "Engine3D.h"



int gameMain();



HWND mainHandle;
object cube1;
camera cam;

point4D camPos = {0,50,0,1};
point4D camDir = {0,0,0,1};

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;

switch(msg)
{
case WM_CREATE:
{
return(0);
}break;
case WM_PAINT:
{
BeginPaint(hwnd,&ps);

EndPaint(hwnd,&ps);
return(0);
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;
default:break;
}
return(DefWindowProc(hwnd,msg,wparam,lparam));
}


int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;

winclass.cbClsExtra = 0;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.cbWndExtra = 0;
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
winclass.hInstance = hinstance;
winclass.lpfnWndProc = WinProc;
winclass.lpszClassName = "WindowClassName";
winclass.lpszMenuName = NULL;
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

RegisterClassEx(&winclass);

if(!(mainHandle = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"Lab 06 3D Demo",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100,100,
screenWidth,screenHeight,
NULL,
NULL,
hinstance,
NULL)))
return(0);


initCam(&cam,1,&camPos,&camDir,NULL,50,500,90,screenWidth,screenHeight);

cube1.worldPostion.x = 0;
cube1.worldPostion.y = 10000;
cube1.worldPostion.z = 34;
cube1.angX = 0;
cube1.angY = 0;
cube1.angZ = 1;
cube1.numVerts = 8;

point3D tempVerts[8] =
{{10,10,10}, //p0
{10,10,-10}, //p2
{-10,10,-10},
{-10,10,10},
{10,-10,10},
{-10,-10,10},
{-10,-10,-10},
{10,-10,-10}}; //p7

for(int vertex = 0; vertex < 8; vertex++)
{
cube1.localList[vertex].x = tempVerts[vertex].x;
cube1.localList[vertex].y = tempVerts[vertex].y; //transfer vertices from temp to object
cube1.localList[vertex].z = tempVerts[vertex].z;
}



//create polygon(triangle) indices

int tempPolyIndices[12*3] = {
0,1,2, 0,2,3,
0,7,1, 0,4,7,
1,7,6, 1,6,2,
2,6,5, 2,3,5,
0,5,4, 0,3,5,
5,6,7, 4,5,7 };

// now initialize each polygon (triangle)

for(int tri = 0; tri < 12; tri++)
{
cube1.polyList[tri].state = 0;
cube1.polyList[tri].attr = 0;
cube1.polyList[tri].color = 0;

//point the vertex list to the local coordinates of the polygon
cube1.polyList[tri].vertexList = cube1.localList;

//assign vertex indices
cube1.polyList[tri].vert[0] = tempPolyIndices[tri*3+0];
cube1.polyList[tri].vert[1] = tempPolyIndices[tri*3+1];
cube1.polyList[tri].vert[2] = tempPolyIndices[tri*3+2];

}





while(TRUE)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;

TranslateMessage(&msg);

DispatchMessage(&msg);
}

gameMain();
}


return(msg.wParam);
}

int gameMain()
{

cube1.localList[0].x = 10;
cube1.localList[0].y = 10;
cube1.localList[0].z = 10;
cube1.localList[0].w = 1;
cube1.localList[1].x = 10;
cube1.localList[1].y = 10;
cube1.localList[1].z = -10;
cube1.localList[1].w = 1;

cube1.numPolys = 12;
cube1.numVerts = 8;

modelToWorld(&cube1,1);
worldToCamera(&cam,&cube1);
cameraToPerspective(&cube1, &cam);
perspectiveToScreen(&cube1,&cam);


HDC hdc = GetDC(mainHandle);
HPEN newPen = CreatePen(BS_SOLID,1,RGB(255,0,0));
HPEN oldPen = (HPEN)SelectObject(hdc,newPen);
SetTextColor(hdc,RGB(255,255,0));
SetBkMode(hdc,TRANSPARENT);


MoveToEx(hdc,cube1.transformedList[0].x,cube1.transformedList[0].y,NULL);

for(int tri = 0; tri < 12; tri++)
{
LineTo(hdc,cube1.transformedList[cube1.polyList[tri].vert[0]].x,cube1.transformedList[cube1.polyList[tri].vert[0]].y);
LineTo(hdc,cube1.transformedList[cube1.polyList[tri].vert[1]].x,cube1.transformedList[cube1.polyList[tri].vert[1]].y);
LineTo(hdc,cube1.transformedList[cube1.polyList[tri].vert[2]].x,cube1.transformedList[cube1.polyList[tri].vert[2]].y);
}

char buffer[80];
sprintf(buffer,"x1 = %f, y1 = %f, x2 = %f, y2 = %f",cube1.transformedList[0].x,cube1.transformedList[0].y,cube1.transformedList[1].x,cube1.transformedList[1].y);
TextOut(hdc,0,0,buffer,sizeof(buffer));
sprintf(buffer,"x3 = %f, y3 = %f, x4 = %f, y4 = %f",cube1.transformedList[2].x,cube1.transformedList[2].y,cube1.transformedList[3].x,cube1.transformedList[3].y);
TextOut(hdc,0,20,buffer,sizeof(buffer));
sprintf(buffer,"x5 = %f, y5 = %f, x6 = %f, y6 = %f",cube1.transformedList[4].x,cube1.transformedList[4].y,cube1.transformedList[5].x,cube1.transformedList[5].y);
TextOut(hdc,0,40,buffer,sizeof(buffer));
sprintf(buffer,"x7 = %f, y7 = %f, x8 = %f, y8 = %f",cube1.transformedList[6].x,cube1.transformedList[6].y,cube1.transformedList[7].x,cube1.transformedList[7].y);
TextOut(hdc,0,60,buffer,sizeof(buffer));

/*

MoveToEx(hdc,cube1.transformedList[0].x,cube1.transformedList[0].y,NULL);
LineTo(hdc,cube1.transformedList[1].x,cube1.transformedList[1].y);
LineTo(hdc,cube1.transformedList[2].x,cube1.transformedList[2].y);
LineTo(hdc,cube1.transformedList[3].x,cube1.transformedList[3].y);
LineTo(hdc,cube1.transformedList[4].x,cube1.transformedList[4].y);
LineTo(hdc,cube1.transformedList[5].x,cube1.transformedList[5].y+ 50);
LineTo(hdc,cube1.transformedList[6].x,cube1.transformedList[6].y + 50);
LineTo(hdc,cube1.transformedList[7].x,cube1.transformedList[7].y + 50);
LineTo(hdc,cube1.transformedList[0].x,cube1.transformedList[0].y + 50);
// MoveToEx(hdc,10,10,NULL);
// LineTo(hdc,100,100);
*/
DeleteObject(newPen);
ReleaseDC(mainHandle,hdc);

return 0;
}
ThreeDParadox
ThreeDParadox
#define TRANSFORM_LOCAL_TO_TRANS 1

#define DEG_TO_RAD(ang) ((ang)*PI/180.0)
#define PI 3.1415926538

typedef struct _matrix4x4
{
union
{
float m[4][4];

struct
{
float m00, m01, m02, m03;
float m10, m11, m12, m13;
float m20, m21, m22, m23;
float m30, m31, m32, m33;
};
};
}matrix4x4, *ptrMatrix4x4;


typedef struct _point3D
{
union{
float points[3];
struct
{
float x, y, z;
};
};
}point3D, *ptrPoint3D;

typedef struct _point4D
{
union{
float points[4];
struct
{
float x, y, z, w;
};
};
}point4D, *ptrPoint4D;

typedef struct _poly
{

int state;
int attr;
int color;

ptrPoint4D vertexList;
int vert[3];

}poly, *ptrPoly;

typedef struct _polySC
{
int state;
int attr;
int color;

point4D vertexLis[3];
point4D transformVertexList[3];

_polySC *next;
_polySC *prev;

} polySC, *ptrPolySC;

typedef struct _object
{
int numVerts;
point4D worldPostion;
point4D localList[64];
point4D transformedList[64];
point4D worldCoords[64];
point4D cameraCoords[64];
point4D screenCoords[64];

float angX, angY, angZ;

int numPolys;
poly polyList[128];
}object, *ptrObject;

typedef struct _renderList
{
int state;
int attr;

ptrPolySC polyPtrs[128];
polySC polyData[128];
int numPolys;

}renderList, *ptrRenderList;

typedef struct _camera
{
int state;
int attr;

point4D position;
point4D direction;
float viewDistanceHorizontal;
float viewDistanceVerticle;
float FieldOfView;

float nearClipZ;
float farClipZ;

float viewPlaneWidth;
float viewPlaneHeight;

float viewPortWidth;
float viewPortHeight;
float viewPortCenterX;
float ViewPortCenterY;

float aspectRatio;

matrix4x4 worldToCamMatrix;
matrix4x4 camToPers;
matrix4x4 persToScreen;

}camera, *ptrCamera;


void matrixMultiply4x4(ptrMatrix4x4 ma,
ptrMatrix4x4 mb,
ptrMatrix4x4 mprod);

void matrixMultiply4x4Point3D(ptrPoint4D va,
ptrMatrix4x4 mb,
ptrPoint4D vprod);

void point4DCopy(ptrPoint4D vdst, ptrPoint4D vsrc);


void resetRenderList(ptrRenderList rendList)
{
rendList->numPolys = 0;
}

void modelToWorld(ptrObject obj, int coordSelect = TRANSFORM_LOCAL_TO_TRANS)
{
for(int vertex = 0; vertex < obj->numVerts; vertex++)
{
obj->transformedList[vertex].x = obj->localList[vertex].x + obj->worldPostion.x;
obj->transformedList[vertex].y = obj->localList[vertex].y + obj->worldPostion.y;
obj->transformedList[vertex].z = obj->localList[vertex].z + obj->worldPostion.z;
}
}

void initCam (ptrCamera cam, int camAttr, ptrPoint4D camPosition, ptrPoint4D camDirection,
ptrPoint4D camTarget, float NearClipZ, float FarClipZ, float FOV, float ViewPortWidth, float ViewPortHeight)
{
cam->attr = camAttr;

cam->position.x = camPosition->x;
cam->position.y = camPosition->y;
cam->position.z = camPosition->z;
cam->position.w = 1;

cam->viewPortWidth = ViewPortWidth;
cam->viewPlaneHeight = ViewPortHeight;
cam->viewPortCenterX = (ViewPortWidth-1)/2;
cam->ViewPortCenterY = (ViewPortHeight-1)/2;

cam->aspectRatio = (float)ViewPortWidth/(float)ViewPortHeight;

cam->viewPlaneWidth = 2.0;
cam->viewPlaneHeight = 2.0/cam->aspectRatio;

float tanFOVdiv2 = tan(DEG_TO_RAD((FOV/2)));
cam->viewDistanceHorizontal = (0.5)*(cam->viewPlaneWidth)*tanFOVdiv2;
cam->viewDistanceVerticle = (0.5)*(cam->viewPlaneHeight)*tanFOVdiv2;

cam->worldToCamMatrix.m00 = 1;
cam->worldToCamMatrix.m01 = 0;
cam->worldToCamMatrix.m02 = 0;
cam->worldToCamMatrix.m03 = 0;
cam->worldToCamMatrix.m10 = 0;
cam->worldToCamMatrix.m11 = 1;
cam->worldToCamMatrix.m12 = 0;
cam->worldToCamMatrix.m13 = 0;
cam->worldToCamMatrix.m20 = 0;
cam->worldToCamMatrix.m21 = 0;
cam->worldToCamMatrix.m22 = 1;
cam->worldToCamMatrix.m23 = 0;
cam->worldToCamMatrix.m30 = 0;
cam->worldToCamMatrix.m31 = 0;
cam->worldToCamMatrix.m32 = 0;
cam->worldToCamMatrix.m33 = 1;
}


void buildCamEuler(ptrCamera cam, int cam_rot_seq)
{
matrix4x4 invTrans, invX, invY, invZ, rotM, tempM;

for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 0; j++)
{
invTrans.m[j] = 0;
invX.m[j] = 0;
invY.m[j] = 0;
invZ.m[j] = 0;

}
}//end for loop

invTrans.m00 = 1;
invTrans.m11 = 1;
invTrans.m22 = 1;
invTrans.m30 = -cam->position.x;
invTrans.m31 = -cam->position.y;
invTrans.m32 = -cam->position.z;
invTrans.m33 = 1;

float thetaX = cam->direction.x;
float thetaY = cam->direction.y;
float thetaZ = cam->direction.z;

float cosTheta = cos(thetaX);
float sinTheta = -sin(thetaX);

invX.m00 = 1;
invX.m11 = cosTheta;
invX.m12 = sinTheta;
invX.m21 = -sinTheta;
invX.m22 = cosTheta;
invX.m33 = 1;

cosTheta = cos(thetaY);
sinTheta = -sin(thetaY);

invY.m00 = cosTheta;
invY.m02 = -sinTheta;
invY.m11 = 1;
invY.m20 = sinTheta;
invY.m22 = cosTheta;
invY.m33 = 1;

cosTheta = cos(thetaZ);
sinTheta = -sin(thetaZ);

invZ.m00 = cosTheta;
invZ.m01 = sinTheta;
invZ.m10 = -sinTheta;
invZ.m11 = cosTheta;
invZ.m22 = 1;
invZ.m33 = 1;


matrixMultiply4x4(&invZ, &invY, &tempM);
matrixMultiply4x4(&tempM, &invX, &rotM);

}


void worldToCamera(ptrCamera cam, ptrObject obj)
{

for(int vertex = 0; vertex < obj->numVerts; vertex++)
{
point4D pResult;

matrixMultiply4x4Point3D(&obj->transformedList[vertex],&cam->worldToCamMatrix, &pResult);

point4DCopy(&obj->transformedList[vertex], &pResult);

}

}

void cameraToPerspective(ptrObject obj, ptrCamera cam)
{

for(int vertex = 0; vertex < obj->numVerts; vertex++)
{
float z = obj->transformedList[vertex].z;

obj->transformedList[vertex].x = cam->viewDistanceHorizontal * obj->transformedList[vertex].x/z;
obj->transformedList[vertex].y = cam->viewDistanceVerticle * obj->transformedList[vertex].y * cam->aspectRatio/z;
}
}

void perspectiveToScreen(ptrObject obj, ptrCamera cam)
{

float alpha = (0.5 * cam->viewPortWidth - 0.5);
float beta = (0.5 * cam->viewPortHeight - 0.5);

for(int vertex = 0; vertex < obj->numVerts; vertex++)
{
obj->transformedList[vertex].x = alpha + alpha*obj->transformedList[vertex].x;
obj->transformedList[vertex].y = beta - beta*obj->transformedList[vertex].y;
}

}


void matrixMultiply4x4(ptrMatrix4x4 ma,
ptrMatrix4x4 mb,
ptrMatrix4x4 mprod)
{
// this function multiplies two 4x4 matrices together and
// and stores the result in mprod
// note later we will take advantage of the fact that we know
// that w=1 always, and that the last column of a 4x4 is
// always 0

for (int row=0; row<4; row++)
{
for (int col=0; col<4; col++)
{
// compute dot product from row of ma
// and column of mb

float sum = 0; // used to hold result

for (int index=0; index<4; index++)
{
// add in next product pair
sum+=(ma->m[row][index]*mb->m[index][col]);
} // end for index

// insert resulting row,col element
mprod->m[row][col] = sum;

} // end for col

} // end for row

} // end Mat_Mul_4X4

void matrixMultiply4x4Point3D(ptrPoint4D va,
ptrMatrix4x4 mb,
ptrPoint4D vprod)
{
// this function multiplies a VECTOR4D against a
// 4x4 matrix - ma*mb and stores the result in mprod
// the function makes no assumptions

for (int col=0; col < 4; col++)
{
// compute dot product from row of ma
// and column of mb
float sum = 0; // used to hold result

for (int row=0; row<4; row++)
{
// add in next product pair
sum+=(va->points[row]*mb->m[row][col]);
} // end for index

// insert resulting col element
vprod->points[col] = sum;

} // end for col

} // end Mat_Mul_VECTOR4D_4X4

inline void point4DCopy(ptrPoint4D vdst, ptrPoint4D vsrc)
{(vdst)->x = (vsrc)->x; (vdst)->y = (vsrc)->y;
(vdst)->z = (vsrc)->z; (vdst)->w = (vsrc)->w; }






ThreeDParadox
ThreeDParadox
a flattened cube, just a square. I added a link to a copy of a picture of the app and what its doing. Also, something else to know, to get the object to show up where it is on the screen, my x axis for the world coords of the object is 0, y is 20,000, no thats not a missprint, and z axis is 50; The camera coords that are loaded in initCam are 0,0,100, but it doesnt seem to matter if i change these variables, it doesnt do anything to the program... If anyone can help i would be very grateful. Thanks


http://digitalmindset.8m.com/demo.JPG

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.