Archived

This topic is now archived and is closed to further replies.

Metal Typhoon

i can't understand gluPerspective...

Recommended Posts

i have a simple program that only is showing a BOX ... it runs fine if i dont use "ResizeGLScene" but if i use it the box wont show i dont get it... here's the code
        
#pragma comment (lib,"opengl32.lib")
#pragma comment (lib,"glu32.lib")


#include <windows.h>

#include <gl\gl.h>

#include <gl\glu.h>

#define WIN_WID 600

#define WIN_HEI 600

HDC		hdc;
HGLRC	hrc;

char CN[] = "X";
void RenderScene();
void Enable (HWND hwnd,HDC * hdc,HGLRC * hrc);
void Disable (HWND hwnd,HDC hdc,HGLRC hrc);
void ResizeOpenGLScene(int width, int height);

LRESULT CALLBACK WndProc (HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);

int WINAPI WinMain (HINSTANCE hi,HINSTANCE hp,LPSTR Cl,int Sc)
{

	WNDCLASS wc;
	MSG		 msg;
	HWND	 hwnd;

	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject (4);
    wc.hCursor = LoadCursor (NULL,IDC_ARROW);
	wc.hIcon = LoadIcon (NULL,IDI_APPLICATION);
	wc.hInstance = hi;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = CN;
	wc.lpszMenuName = NULL;
	wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;

	if (!RegisterClass (&wc))
	{
		MessageBox (hwnd,"Error registering class","Error # 1",MB_OK|MB_ICONEXCLAMATION);
		return 0;
	}

	hwnd = CreateWindow (CN,"Texture Test",WS_CAPTION,50,50,WIN_WID,WIN_HEI,NULL,NULL,hi,NULL);

	ShowWindow (hwnd,SW_NORMAL);
	UpdateWindow (hwnd);
	Enable (hwnd,&hdc,&hrc);
	ResizeOpenGLScene (WIN_WID,WIN_HEI);
			
	while (1)
	{
		if (PeekMessage (&msg,hwnd,0,0,PM_REMOVE))
		{
			if (msg.wParam == VK_ESCAPE)
				break;

			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}
		else
		{
			RenderScene();
		}
	}

	Disable (hwnd,hdc,hrc);
	return 0;

}

LRESULT CALLBACK WndProc (HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{

	switch (msg)
	{
	case WM_CLOSE:
		DestroyWindow (hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage (0);
		break;
	default:
		break;
	}
	
	return DefWindowProc (hwnd,msg,wparam,lparam);
}


void RenderScene ()
{
	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	 glLoadIdentity();

	  glBegin (GL_QUADS);
		glVertex2f (0.5,0.5);
		glVertex2f (0.5,-0.5);
		glVertex2f (-0.5,-0.5);
		glVertex2f (-0.5,0.5);
	glEnd();

	SwapBuffers (hdc);
}

void ResizeOpenGLScene(int width, int height)
{  
	if (height == 0) 
	{    height = 1;   
	}  
	
	glViewport(0,0, width, height);		

	glMatrixMode(GL_PROJECTION);					
	glLoadIdentity();					

	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f, 100.0f);  

	glMatrixMode(GL_MODELVIEW);					
	glLoadIdentity();						
}

void Enable (HWND hwnd,HDC * hdc,HGLRC * hrc)
{
	int FORMAT;					 
	PIXELFORMATDESCRIPTOR PFD;   

	* hdc = GetDC (hwnd);        

	PFD.cColorBits = 24;
	PFD.cDepthBits = 16;
	PFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	PFD.iPixelType = PFD_TYPE_RGBA;
	PFD.nSize = sizeof (PFD);
	PFD.nVersion = 1;

	FORMAT = ChoosePixelFormat (* hdc,&PFD);
	SetPixelFormat (* hdc,FORMAT,&PFD);

	* hrc = wglCreateContext (* hdc);
	wglMakeCurrent (* hdc,* hrc); 

}
 
void Disable (HWND hwnd,HDC hdc,HGLRC hrc)
{
	wglMakeCurrent (NULL,NULL);
	wglDeleteContext (hrc);
	ReleaseDC (hwnd,hdc);
}
        
Metal Typhoon
[edited by - Metal Typhoon on July 17, 2002 3:16:54 AM]

Share this post


Link to post
Share on other sites
With glVertex2f() *only* passes the X and Y coordinates to OpenGL and it than assumes the Z values are 0 (not completely sure though).....

Try using glVertex3f()
glBegin (GL_QUADS);
glVertex3f( 0.5, 0.5, 2 );
glVertex3f( 0.5,-0.5, 2 );
glVertex3f( -0.5,-0.5, 2 );
glVertex3f( -0.5, 0.5, 2 );
glEnd();

also look into gluLookAt() to set your "camera" position....

-Crawl

[edited by - Crawl on July 17, 2002 3:38:02 AM]

Share this post


Link to post
Share on other sites
No no, the problem is the order. You can''t call gluPerspective where you did. Move it so that its called when the matrix mode is GL_MODELVIEW. Before glViewport or after the second glLoadIdentity will fix your problem.

--Buzzy
(formerly buzzy_b)

Share this post


Link to post
Share on other sites
quote:
Original post by Buzzy
No no, the problem is the order. You can''t call gluPerspective where you did. Move it so that its called when the matrix mode is GL_MODELVIEW. Before glViewport or after the second glLoadIdentity will fix your problem.

--Buzzy
(formerly buzzy_b)


lol that worked... but why in nehe they put the perspective in the middle ??? between the two matrixmodes ??

Share this post


Link to post
Share on other sites
That's a very good question...

[edit] When I move the gluPerspective in NeHe's basecode, it doesn't work, but it does for you. When it's left where it is, it works in the basecode, but not for you... You must be doing something in a different order to be cause this anomoly. I can't see it though. hmm.... [/edit]

--Buzzy
(formerly buzzy_b)

[edited by - Buzzy on July 17, 2002 4:26:48 AM]

Share this post


Link to post
Share on other sites
He checks if the window''s been resize each time through the loop, and if it has get the new width and height and do another ReshapeGL (your ResizeOpenGLScene). Line 327 of the original basecode, or line 270 in NeHeGL.cpp of the newer basecode.

--Buzzy
(formerly buzzy_b)

Share this post


Link to post
Share on other sites
quote:
Original post by Buzzy
He checks if the window''s been resize each time through the loop, and if it has get the new width and height and do another ReshapeGL (your ResizeOpenGLScene). Line 327 of the original basecode, or line 270 in NeHeGL.cpp of the newer basecode.

--Buzzy
(formerly buzzy_b)


the lines was 437 but i did that it didn''t work i guess u idnd''t understand..i mean like this.. even though the screen resolution is 600x480 the 1.0f for the vertices keep a ration so that the square wont show as a rectangle.. if i do like 500x500 or 600x600 a 1 ratio than it''s works fine.. but else it draws a rectangle

Metal Typhoon

Share this post


Link to post
Share on other sites
First, Windows programming is not my forte. That said, I think the way he''s doing that is with a call to AdjustWindowRectEx when he makes the window. It''s pretty much the only thing he''s doing that you aren''t (that I can see, at 5:00 in the morning ).

btw, what do you mean line 437? I just unzipped all the basecodes from NeHe''s site, and in none of them is that where the ReshapeGL is when the window''s resized... are you sure you''re not looking at a modified version? In fact the only basecode with a line 437 is the NeHeGL 2, and in there, that line is a check for WM_QUIT message. Then again, now that I think of it, I could very well be wrong. I am very tired.

Hope that helps!

--Buzzy
(formerly buzzy_b)

Share this post


Link to post
Share on other sites
OMG! I just noticed something! You're using glVertex2f! TWO! Okay, yeah, Crawl was basically right. Move that gluPerspective back in between the two matrix modes, and in RenderScene, after glLoadIdentity do a glTranslatef(0.0f, 0.0f, -3.0f). I feel like such an idiot. I didn't even notice. The reason the square didn't show up was because it was being drawn on the Z plane (like Crawl said, z=0), which was behind the near clip plane, ie the camera. So you just have to move it out in front of the camera. Or you could screw around with gluLookAt, but that'll basically just do the glTranslatef for you. In any case, sorry if this seemed like a waste of time.

[edit] That actually fixes the whole rectangle thing too... [/edit]

--Buzzy
(formerly buzzy_b)

[edited by - Buzzy on July 17, 2002 5:32:20 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Buzzy
OMG! I just noticed something! You''re using glVertex2f! TWO! Okay, yeah, Crawl was basically right. Move that gluPerspective back in between the two matrix modes, and in RenderScene, after glLoadIdentity do a glTranslatef(0.0f, 0.0f, -3.0f). I feel like such an idiot. I didn''t even notice. The reason the square didn''t show up was because it was being drawn on the Z plane (like Crawl said, z=0), which was behind the near clip plane, ie the camera. So you just have to move it out in front of the camera. Or you could screw around with gluLookAt, but that''ll basically just do the glTranslatef for you. In any case, sorry if this seemed like a waste of time.

[edit] That actually fixes the whole rectangle thing too... [/edit]

--Buzzy
(formerly buzzy_b)

[edited by - Buzzy on July 17, 2002 5:32:20 AM]


dont worry my friend i''ll just take that "i fell like an idiot" away... i ust work on gamedev using a plane swuare but on my prog i''m using a cub hehehh 3f !!! yay !!! but but ya i was tired ti was 5 in the morning hehehe.

Metal Typhoon

Share this post


Link to post
Share on other sites
Buzzy is right about the window resize...

You check in your windows window proc for the WM_SIZE message, when you catch that message...



  
class Renderer
{
public:
Some functions here...

void setViewPort(int pWidth, int pHeight)
{
width = pWidth;
height = pHeight;

glViewport (0, 0, (GLsizei)(width), (GLsizei)(height));
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)(width)/(GLfloat)(height), 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
private:

Some private member here....
};

// My window''s Windows procedure...

LRESULT MyWindow::windowProc(UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_SIZE:
// LOWORD contains the width, HIWORD contains the height.

render->setViewPort(LOWORD(lParam), HIWORD(lParam));
break;
default:
return DefWindowProc(getHandle(), Msg, wParam, lParam);
}

return 0;
}

Share this post


Link to post
Share on other sites