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

编程题目:利用Opengl绘制一个三角形。请给出详细的编程内容!答的好追加!... 编程题目:利用Opengl绘制一个三角形。请给出详细的编程内容!答的好追加! 展开
 我来答
匿名用户
2013-11-18
展开全部
//编译环境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);
匿名用户
推荐于2017-11-26
展开全部
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();
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-11-18
展开全部
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();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式