永发信息网

编程题目:利用Opengl绘制一个三角形。

答案:3  悬赏:60  手机版
解决时间 2021-07-25 19:57

编程题目:利用Opengl绘制一个三角形。

请给出详细的编程内容!答的好追加!

最佳答案

void CTes1View::DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕及深度缓存



SwapBuffers(wglGetCurrentDC());



glLoadIdentity(); // 重置



glTranslatef(-1.5f,0.0f,-6.0f); // 左移 1.5 单位,并移入屏幕 6.0

glBegin(GL_TRIANGLES); // 绘制三角形

glVertex3f(0.0f,0.0f,0.0f); // 上顶点

glVertex3f(0.0f, 0.0f,-1.0f); // 左下顶点

glVertex3f(0.0f,1.0f,0.0f); // 右下顶点



glEnd(); // 三角形绘制结束

glFinish();
}

全部回答
glBegin(GL_TRIANGLES); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f, 0.0f,-1.0f); glVertex3f(0.0f,1.0f,0.0f); glEnd();
//编译环境VS2005 选WIN32应用程序, 我从来不用MFC #include <GL/GL.h> #include <gl/GLU.h> #include <gl/GLAux.h> #include <math.h> #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glu32.lib") #pragma comment(lib, "glaux.lib") #define MAX_LOADSTRING 100 //定义递归调用阀值,即其中最小三角形边长 #define SIDELENGHT (0.05) // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name //自定义全局变量 HGLRC g_hRC = NULL; //OpenGL 绘图上下文 HDC g_hDC = NULL; //设备上下文 HWND g_hWnd = NULL; //保存当前窗口句柄 //定义三角形顶点指针 typedef struct tagTRIANGLEPOINT { GLfloat tpPointX; GLfloat tpPointY; }TRIANGLEPOINT,*LTRIANGLEPOINT; //定义最外面大三角形三个点 TRIANGLEPOINT g_TrianglePoint[3] = {{0.0, 0.0}, {5.0, 0.0}, {2.5,2.5}}; // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); //自定义函数 void EnableOpenGL(HWND hWnd); void SceneInit(); void SceneResizeViewport(); void DisableOpenGL(); void Display(); void DrawTriangle(LTRIANGLEPOINT pTrianglePoint, int iPointNum = 3); void DrawLargeTriangle(LTRIANGLEPOINT pTrianglePoint, int iPointNum); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_OPENGLTRIANGLE, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } EnableOpenGL(g_hWnd); SceneInit(); SceneResizeViewport(); hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_OPENGLTRIANGLE)); // Main message loop: ZeroMemory(&msg, sizeof(msg)); while ( msg.message != WM_QUIT ) { if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) { if(!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } else { Display(); SwapBuffers(g_hDC); } } DisableOpenGL(); return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage are only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OPENGLTRIANGLE)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_OPENGLTRIANGLE); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } g_hWnd = hWnd; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORd(wParam); wmEvent = HIWORd(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORd(wParam) == IDOK || LOWORd(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORd(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } //初始化opengl void EnableOpenGL(HWND hWnd) { PIXELFORMATDESCRIPTOR pfd; int iFormat; g_hDC = GetDC(hWnd); ZeroMemory(&pfd, sizeof(pfd)); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW| PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat( g_hDC, &pfd ); SetPixelFormat( g_hDC, iFormat, &pfd ); g_hRC = wglCreateContext(g_hDC); wglMakeCurrent( g_hDC, g_hRC); } //设置着色模式 void SceneInit() { glShadeModel(GL_SMOOTH); glClearColor(1.0, 1.0, 1.0, 0.5); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); } //设置视口 void SceneResizeViewport() { RECT rect; int w,h; GLfloat aspect; GetClientRect( g_hWnd, &rect ); w = rect.right - rect.left; h =rect.bottom - rect.top; aspect = (GLfloat)w/ (GLfloat)h; glViewport(0,0,w,h);//设置视口 glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 5.0, 0.0, 5.0/aspect); } void DisableOpenGL(){ wglMakeCurrent(NULL, NULL); wglDeleteContext(g_hRC); ReleaseDC(g_hWnd, g_hDC); } void Display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); DrawLargeTriangle(g_TrianglePoint, 3); DrawTriangle(g_TrianglePoint, 3); glFlush(); } //画大三角形 void DrawLargeTriangle(LTRIANGLEPOINT pTrianglePoint, int iPointNum) { glBegin(GL_LINE_LOOP); for ( int i = 0; i < 3; ++i) { glColor3f(1.0,0.0,0.0); glVertex2f(pTrianglePoint[i].tpPointX,pTrianglePoint[i].tpPointY); } glEnd(); } //递归函数 void DrawTriangle(LTRIANGLEPOINT pTrianglePoint, int iPointNum) { //得到中间三角形的三个点 TRIANGLEPOINT trianglepoint[3]; for (int i = 0; i < 3; ++i) { if ( i == 2) { trianglepoint[i].tpPointX = (pTrianglePoint[i].tpPointX + pTrianglePoint[0].tpPointX) / 2; trianglepoint[i].tpPointY = (pTrianglePoint[i].tpPointY + pTrianglePoint[0].tpPointY) / 2; } else { trianglepoint[i].tpPointX = (pTrianglePoint[i].tpPointX + pTrianglePoint[i+1].tpPointX) / 2; trianglepoint[i].tpPointY = (pTrianglePoint[i].tpPointY + pTrianglePoint[i+1].tpPointY) / 2; } } glBegin(GL_LINE_LOOP); for ( int i = 0; i < 3; ++i) { glColor3f(1.0,0.0,0.0); glVertex2f(trianglepoint[i].tpPointX,trianglepoint[i].tpPointY); } glEnd(); //构建其它三角 TRIANGLEPOINT trianglepoint1[3]; TRIANGLEPOINT trianglepoint2[3]; TRIANGLEPOINT trianglepoint3[3]; ZeroMemory(&trianglepoint1, sizeof(trianglepoint1)); ZeroMemory(&trianglepoint2, sizeof(trianglepoint2)); ZeroMemory(&trianglepoint2, sizeof(trianglepoint2)); trianglepoint1[0] = pTrianglePoint[0]; trianglepoint1[1] = trianglepoint[0]; trianglepoint1[2] = trianglepoint[2]; trianglepoint2[0] = pTrianglePoint[1]; trianglepoint2[1] = trianglepoint[1]; trianglepoint2[2] = trianglepoint[0]; trianglepoint3[0] = pTrianglePoint[2]; trianglepoint3[1] = trianglepoint[2]; trianglepoint3[2] = trianglepoint[1]; //设定边界 if(pow(abs(trianglepoint1[0].tpPointX - trianglepoint1[1].tpPointX), 2) +pow(abs(trianglepoint1[0].tpPointY - trianglepoint1[1].tpPointY), 2) > SIDELENGHT) DrawTriangle(trianglepoint1, iPointNum); if(pow(trianglepoint2[0].tpPointX - trianglepoint2[1].tpPointX, 2) +pow(trianglepoint2[0].tpPointY - trianglepoint2[1].tpPointY, 2) > SIDELENGHT) DrawTriangle(trianglepoint2, iPointNum); if(pow(trianglepoint2[0].tpPointX - trianglepoint2[1].tpPointX, 2) +pow(trianglepoint2[0].tpPointY - trianglepoint2[1].tpPointY, 2) > SIDELENGHT) DrawTriangle(trianglepoint3, iPointNum);
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
hp 4311功能及配置如何,大伙有用的没?
以六羊为题 的对联
白胡子会被黑胡子杀了吗
描写父爱或母爱的作文,关于母爱的作文252字
黑芝麻有什各用
看见脚下的路,看不见头上的天那云彩。今天快
分手之后又和好了,坏处不是好处?
QQ农场怎么样得到注册码?
cf这么用Q币换经验
中国石油开采史
求番禺百万葵园的门票票价和路线图
谁能教我现在CF如何鬼跳啊?我总是在不知不觉
男朋友的妈妈对我印象一直很好.昨天晚上我居
在这个世界上,是否有后悔药呢?
鲍照写过的诗
推荐资讯
大家觉得杰伦拍的电影怎么样?
高分跪求班徽设计图及其内涵!急!
作文 《我的梦 中国梦》
帮我找几对情侣名。
水瓶男送礼物给摩羯女要送什么呀?
足球手抄报简单又漂亮,端午节的手抄报简单字
找个人加我帮我开下游戏人生。。。
哈尔滨平房区二手房能卖多少钱
遥控车坏了,自己能修吗?该怎么修?
急死拉,帮帮忙啊
帐号的问题!!!!
诗经对朋友思念的句子,诗经里怀念的句子
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?