// callback function. 視窗大小改變時會被呼叫, 并傳入新的視窗大小. void ResizeWindowOpenGL(int width, int height) { // 使用新的視窗大小做為新的繪圖解析度 glViewport(0, 0, width, height); // 投影矩陣, 重設水平跟垂直方向的視角. float aspect = (float) height / (float) width; g_fOrthoWidth = g_fOrthoSize; g_fOrthoHeight = g_fOrthoSize; if ( aspect > 1.0f ) g_fOrthoHeight *= aspect; else g_fOrthoWidth /= aspect; g_proj_matrix = GutMatrixOrthoRH_OpenGL(g_fOrthoWidth, g_fOrthoHeight, 0.1f, 100.0f); }
void CGutFontOpenGL::Render(void) { if ( m_iNumCharacters==0 ) return; glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); // 套用字型貼圖 sModelMaterial_OpenGL mtl; mtl.m_bCullFace = false; mtl.m_Textures[0] = m_FontTexture; mtl.Submit(NULL); // 開啟alpha test glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5f); // 使用平行視角 Matrix4x4 proj_matrix = GutMatrixOrthoRH_OpenGL(m_fWidth, m_fHeight, 0.0f, 1.0f); Matrix4x4 view_matrix; view_matrix.Identity(); view_matrix[3].Set(-m_fWidth/2.0f, -m_fHeight/2.0f, 0.0f, 1.0f); // 套用轉換矩陣 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadMatrixf( (float *) &proj_matrix); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadMatrixf( (float *) &view_matrix); // 設定vertex array來源 glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(_FontVertex), &m_pVertexArray[0].m_Position); glTexCoordPointer(2, GL_FLOAT, sizeof(_FontVertex), &m_pVertexArray[0].m_Texcoord); // 畫出所有文字 glDrawElements(GL_TRIANGLES, m_iNumCharacters * 6, GL_UNSIGNED_SHORT, m_pIndexArray); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glPopClientAttrib(); }
bool InitResourceOpenGL(void) { // 投影矩陣 g_proj_matrix = GutMatrixOrthoRH_OpenGL(g_fOrthoWidth, g_fOrthoHeight, 0.1f, 100.0f); //g_proj_matrix = GutMatrixPerspectiveRH_OpenGL(60.0f, 1.0f, 0.1f, 100.0f); glMatrixMode(GL_PROJECTION); glLoadMatrixf( (float *) &g_proj_matrix); Vector4 a(0.0f, 0.0f, -0.1f, 1.0f); Vector4 b(0.0f, 0.0f, -100.0f, 1.0f); Vector4 ta = a * g_proj_matrix; Vector4 tb = b * g_proj_matrix; // 載入貼圖 g_Texture0_ID = GutLoadTexture_OpenGL("../../textures/brickwall.tga"); // 套用Trilinear Filter glBindTexture(GL_TEXTURE_2D, g_Texture0_ID); glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); // 載入貼圖 g_Texture1_ID = GutLoadTexture_OpenGL("../../textures/spotlight_effect.tga"); // 套用Trilinear Filter glBindTexture(GL_TEXTURE_2D, g_Texture1_ID); glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); glBindTexture(GL_TEXTURE_2D, g_Texture1_ID); // 把正向跟反向的面都畫出來 glDisable(GL_CULL_FACE); // 啟動2D貼圖功能 glEnable(GL_TEXTURE_2D); // 啟動zbuffer隱藏面測試 glEnable(GL_DEPTH_TEST); return true; }
void __InitParticleDrawing() { srand(0); m_pParticleVertices = new Vector4[PARTICLE_LINE_NUMBER * 3]; m_pVelocities = new Vector4[PARTICLE_LINE_NUMBER * 3]; for (int i = 0; i < PARTICLE_LINE_NUMBER; ++i) { m_pParticleVertices[i].SetX(0); m_pParticleVertices[i].SetY(0); m_pParticleVertices[i].SetZ(-1.83); m_pParticleVertices[i].SetW(1); m_pVelocities[i].SetX((RANDOM * 2 - 1) * 0.05); m_pVelocities[i].SetY((RANDOM * 2 - 1) * 0.05); m_pVelocities[i].SetZ(0.93 + RANDOM * 0.02); m_pVelocities[i].SetW(1); } glClearColor(0.0, 0.0, 0.0, 1.0); glClearDepth(1.0); //glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glEnable(GL_ALPHA_TEST); glEnable(GL_LINE_SMOOTH); glColor4f(0.24, 0.51, 0.18, 0.04); Matrix4x4 view_matrix = Matrix4x4::IdentityMatrix(); //Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up); //Matrix4x4 perspective_matrix = GutMatrixPerspectiveRH_OpenGL(30.0f, g_ratio, 1.0f, 10000.0f); //Matrix4x4 perspective_matrix = Matrix4x4::IdentityMatrix(); //float fieldOfView = 30.0; //float aspectRatio = g_ratio; //float nearPlane = 1.0; //float farPlane = 10000.0; //float top = nearPlane * tan(fieldOfView * PI / 360.0); //float bottom = -1 * top; //float right = top * aspectRatio; //float left = -1 * right; //float a = (right + left) / (right - left); //float b = (top + bottom) / (top - bottom); //float c = (farPlane + nearPlane) / (farPlane - nearPlane); //float d = (2 * farPlane * nearPlane) / (farPlane - nearPlane); //float x = (2 * nearPlane) / (right - left); //float y = (2 * nearPlane) / (top - bottom); //perspective_matrix[0][0] = x; //perspective_matrix[1][1] = y; //perspective_matrix[2][2] = c; //perspective_matrix[3][3] = 0; //perspective_matrix[2][0] = a; //perspective_matrix[2][1] = b; //perspective_matrix[3][2] = d; //perspective_matrix[2][3] = -1; Matrix4x4 perspective_matrix = GutMatrixOrthoRH_OpenGL(2, 2, 1, 10000); Matrix4x4 view_perspective_matrix = view_matrix * perspective_matrix; glLoadMatrixf((float *) &view_perspective_matrix); m_bIsTouch = false; m_fTouchX = 0; m_fTouchY = 0; }
// 使用OpenGL來繪圖 void RenderFrameOpenGL(void) { // `取得視窗大小` int w, h; GutGetWindowSize(w, h); // `清除畫面` glClearColor(0.4f, 0.4f, 0.4f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Vector4 camera_lookat(0.0f, 0.0f, 0.0f); Matrix4x4 ortho_proj = GutMatrixOrthoRH_OpenGL(20.0f, 20.0f, 0.1f, 100.0f); { // `前視圖` glViewport(0, h/2, w/2, h/2); // view matrix Vector4 camera_pos(0.0f, -20.0f, 0.0f); Vector4 camera_up(0.0f, 0.0f, 1.0f); g_view_matrix = GutMatrixLookAtRH(camera_pos, camera_lookat, camera_up); // projection matrix glMatrixMode(GL_PROJECTION); glLoadMatrixf(&ortho_proj[0][0]); // render objects RenderSolarSystemOpenGL(); } { // `上視圖` glViewport(w/2, h/2, w/2, h/2); // view matrix Vector4 camera_pos(0.0f, 0.0f, 20.0f); Vector4 camera_up(0.0f, 1.0f, 0.0f); g_view_matrix = GutMatrixLookAtRH(camera_pos, camera_lookat, camera_up); // projection matrix glMatrixMode(GL_PROJECTION); glLoadMatrixf(&ortho_proj[0][0]); // render objects RenderSolarSystemOpenGL(); } { // `右視圖` glViewport(0, 0, w/2, h/2); // view matrix Vector4 camera_pos(20.0f, 0.0f, 0.0f); Vector4 camera_up(0.0f, 0.0f, 1.0f); g_view_matrix = GutMatrixLookAtRH(camera_pos, camera_lookat, camera_up); // projection matrix glMatrixMode(GL_PROJECTION); glLoadMatrixf(&ortho_proj[0][0]); // render objects RenderSolarSystemOpenGL(); } { // `使用者視角` glViewport(w/2, 0, w/2, h/2); // object * view matrix Matrix4x4 object_matrix = g_Control.GetObjectMatrix(); Matrix4x4 view_matrix = g_Control.GetViewMatrix(); g_view_matrix = object_matrix * view_matrix; // projection matrix glMatrixMode(GL_PROJECTION); glLoadMatrixf(&g_projection_matrix[0][0]); // render objects RenderSolarSystemOpenGL(); } { // `畫出viewport的邊界` glViewport(0, 0, w, h); glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); glEnableClientState(GL_VERTEX_ARRAY); // projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); // worldview matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // `畫邊界` glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glVertexPointer(3, GL_FLOAT, sizeof(Vertex_VC), g_Border); glDrawArrays(GL_LINES, 0, 4); glPopClientAttrib(); } // `把背景backbuffer的畫面呈現出來` GutSwapBuffersOpenGL(); }