opengl 中透視投影到螢幕的對應關係

2021-06-17 18:46:24 字數 3292 閱讀 1587

#include #include #include #include #define pi 3.14159265358979323846264338327950288   // pi

lresult callback wndproc(hwnd,uint,wparam,lparam);

int winapi winmain(hinstance hinstance,hinstance hprevinstance,pstr szcmdline,int icmdshow)

text("the opengl program"), // window caption

cw_usedefault, // initial x position

cw_usedefault, // initial y position

400, // initial x size

400, // initial y size

null, // parent window handle

null, // window menu handle

hinstance, // program instance handle

null); // creation parameters

showwindow(hwnd,icmdshow);

updatewindow(hwnd);

while(getmessage(&msg,null,0,0))

return msg.wparam;

}lresult callback wndproc(hwnd hwnd,uint message,wparam wparam,lparam lparam)

glviewport(0,0,rc.right,rc.bottom);

glmatrixmode(gl_projection);

glloadidentity();

x2y=(glfloat)rc.right/(glfloat)rc.bottom;

// 設定視口的大小

gluperspective(90.0,x2y,1.0,10.0);

glmatrixmode(gl_modelview);

glloadidentity();

return 0;

case wm_timer:

glclear(gl_color_buffer_bit | gl_depth_buffer_bit);

//glloadidentity();

//gltranslatef(0.0,0.0,-1.0);

glbegin(gl_points);

//近平面,注意近平面不能完全與near相等,要加個小量?

glcolor3f(0.0f,0.0f,1.0f); //藍色

glvertex3f( 0.0, 0.0, -1.0000001); //螢幕正中心

glvertex3f( 0.0, tan(pi*90.0/360.0), -1.0000001); //螢幕正中上方

glvertex3f( 0.0, -tan(pi*90.0/360.0), -1.0000001); //螢幕正中下方

glvertex3f( -x2y*tan(pi*90.0/360.0), 0.0, -1.0000001); //螢幕正中左方

glvertex3f( x2y*tan(pi*90.0/360.0), 0.0, -1.0000001); //螢幕正中右方

glvertex3f( 0.0, 0.5*tan(pi*90.0/360.0), -1.0000001); //螢幕正中上方1/2

glvertex3f( 0.0, -0.5*tan(pi*90.0/360.0), -1.0000001); //螢幕正中下方1/2

glvertex3f( -0.5*x2y*tan(pi*90.0/360.0), 0.0, -1.0000001); //螢幕正中左方1/2

glvertex3f( 0.5*x2y*tan(pi*90.0/360.0), 0.0, -1.0000001); //螢幕正中右方1/2

//遠平面

glcolor3f(1.0f,0.0f,0.0f); //紅色

glvertex3f(-x2y*10.0*tan(pi*90.0/360.0), 10.0*tan(pi*90.0/360.0), -10.0); //螢幕左上角

glvertex3f( x2y*10.0*tan(pi*90.0/360.0), 10.0*tan(pi*90.0/360.0), -10.0); //螢幕右上角

glvertex3f(-x2y*10.0*tan(pi*90.0/360.0), -10.0*tan(pi*90.0/360.0), -10.0); //螢幕左下角

glvertex3f( x2y*10.0*tan(pi*90.0/360.0), -10.0*tan(pi*90.0/360.0), -10.0); //螢幕右下角

//在遠近平面中心的一點投射在近平面上

glcolor3f(0.0f,1.0f,0.0f); //綠色

glvertex3f( 0.0, 0.0, -1.0000001); //螢幕正中心

glend();

swapbuffers(hdc);

return 0;

case wm_destroy:

killtimer(hwnd, 1);

// make the rendering context not current

wglmakecurrent(null,null) ;

// delete the rendering context

wgldeletecontext(hrc);

postquitmessage(0);

return 0;

}return defwindowproc(hwnd,message,wparam,lparam);

}

效果圖:

視窗拉抻後

openGL學習筆記十四 透視投影

三維空間物體顯示近大遠小。透視投影區域是個稜錐體 透視投影顯示效果 透視投影 define glew static include include include include include pragma comment linker,subsystem windows entry maincr...

OpenGL學習十 視口,正投影,透視投影

正投影 將物體直接投影到螢幕上,不改變其大小 glortho gldouble left,gldouble right,gldouble bottom,gldouble top,gldouble znear,gldouble zfar 透視投影 遠處的東西看起來要小一些,近處的東西看起來會大一些,這...

openGL 平行投影和透視投影函式分析

glviewport glint x,glint y,glsizei width,glsizei height 在視窗中定義乙個畫素矩形,最終的影象會對映到這個矩形中。x,y 指定了的視口的左下角,width和height指定了這個視口矩形的寬度和高度。在預設情況下,視口的初始值為 0,0,winw...