i can't understand gluPerspective...

Started by
11 comments, last by Metal Typhoon 21 years, 9 months ago
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]
Metal Typhoon
Advertisement
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]
--------<a href="http://www.icarusindie.com/rpc>Reverse Pop Culture
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)
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 ??
Metal Typhoon
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]
quote:Original post by Buzzy
That''s a very good question...

--Buzzy
(formerly buzzy_b)


hehehei have one little question.. how does he do it so that nomatter the size of the window the cube will remain wiht the same ratio ??
Metal Typhoon
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)
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
Metal Typhoon
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)
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]

This topic is closed to new replies.

Advertisement