PDA

View Full Version : Only 2D works.... my 2nd attempt at opengl



beakie
08-26-2012, 01:24 AM
Ummmm... I drew a triangle in 3D

859

I then changed a z coordinate to be on a different z plane... but it doesnt work?

858

Any ideas please?



#include "frmDeviceRender.h"

void initGL() {
glClearColor (0.0, 0.0, 0.0, 0.0);
glColor3f (1.0, 1.0, 1.0);
glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 10.0);
};

void drawIt() {
glClear (GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f (2.5, 2.5, 8);
glVertex3f (7.5, 2.5, 0.0);
glVertex3f (7.5, 7.5, 0.0);
glEnd();
glFlush();
};

void FrmDeviceRender::populateForm() {
hdc = GetDC (me) ;

GLsizei width = 200;
GLsizei height = 200;
int bits = 32;
GLuint PixelFormat;
static PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

if (!(PixelFormat=ChoosePixelFormat(hdc,&pfd))) {
MessageBox(NULL,L"Can't Find A Suitable PixelFormat.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
}

if(!SetPixelFormat(hdc,PixelFormat,&pfd)) {
MessageBox(NULL,L"Can't Set The PixelFormat.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
}

if (!(hRC=wglCreateContext(hdc))) {
MessageBox(NULL,L"Can't Create A GL Rendering Context.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
}

if(!wglMakeCurrent(hdc,hRC)) {
MessageBox(NULL,L"Can't Activate The GL Rendering Context.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
}
};

void FrmDeviceRender::onPaint() {
initGL();

drawIt();
SwapBuffers(hdc);
}

void FrmDeviceRender::onUnload() {
ReleaseDC (me, hdc) ;

PostQuitMessage (0);
};

Rosario Leonardi
08-26-2012, 05:29 AM
Let's see how you trasform your eye space to clip space.
With that parameter you have the following projection matrix


left = 0
right = 10
top = 0
bottom - 10
near = -1
far = 10
projection = 1/5 0 0 -1
0 1/5 0 -1
0 0 -2/11 -9/11
0 0 0 1

Now you don't have any model view transformation active, so your vertexes are only transformed by the projection matrix and viewport.

cause you send only three coordinates the w is assumed to be 1.0
glVertex3f (2.5, 2.5, 8); -> -0.5 -0.5 -2.27222 1
OOOPS!! The Z is outside the clip space, your triangle is clipped. And today you have learned that the camera look on the -Z axis.
you points will not be clipped if the Z coord is from 1 to -8

beakie
08-26-2012, 05:58 AM
Now you don't have any model view transformation active, so your vertexes are only transformed by the projection matrix and viewport.

cause you send only three coordinates the w is assumed to be 1.0
glVertex3f (2.5, 2.5, 8); -> -0.5 -0.5 -2.27222 1
OOOPS!! The Z is outside the clip space, your triangle is clipped. And today you have learned that the camera look on the -Z axis.
you points will not be clipped if the Z coord is from 1 to -8

Thanks for this.

In my own/non-OpenGL engine, I use the projection matrix from wikipedias 3D projection page (http://en.wikipedia.org/wiki/3D_projection#Perspective_projection). How can I achieve a similar style view? I know I need to move over to glFrustum... but I am not sure of what values I need to specify.

I would like to get to the point where I have a frustum 1000 wide, 500 high and 1000 deep. I would then like to be able to specify a coordinate in that range (10, 23, 200... say) and have it mapped automatically.

How is this best done.


I have read tutorials online but they are all overwhelmed with gaming-tutorials/translation/animation/other-irrelevant-stuff

Any help is massively appreciated.

Rosario Leonardi
08-26-2012, 06:32 AM
I guess your non-openGL engine used DirectX or did you wrote a custom API? I never wrote a single line in DirectX but projection matrix are projection matrix.
You can write your own function that compute the projection matrix (is usually better) or use glFrustum (http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml).


I would like to get to the point where I have a frustum 1000 wide, 500 high and 1000 deep. I would then like to be able to specify a coordinate in that range (10, 23, 200... say) and have it mapped automatically.
glFrustum specify a perspective projection, so 1000 wide in witch point? At the end? Usually the matrix is specified like that

p/a 0 0 0
0 p 0 0
0 0 -(f+n)/(f-n) -(2fn)/(f-n)
0 0 -1 0

where a is the aspect ratio = width / height
and p is atan(yfov angle / 2)
f = farPlane
n = nearPlane

If you want to use glFrustum the left right.. etc are the size of the near plane.

beakie
08-26-2012, 06:47 AM
I guess your non-openGL engine used DirectX or did you wrote a custom API? I never wrote a single line in DirectX but projection matrix are projection matrix.


Actually, I wrote it using winAPI (MoveToEx, LineTo etc). It's not very fast (software only) but I included coordinate projection orthographic/perspective, triangle splitting, backface culling, painters algorithm, a zbuffer and a couple of other bits. it only deals with basic shapes, but thats all i needed until now. its NOT for gaming. the speed issues meant i am forced to look into opengl or directx... so i chose opengl :)

I build a projection matrix in that (based on wiki page) but i thought (?) opengl would create that for me... if i specified the frustum????

beakie
08-26-2012, 06:51 AM
glFrustum specify a perspective projection, so 1000 wide in witch point? At the end?

the thinnest end