Перейти к содержимому



OpenGL Rotating Cube

OpenGL Rotating Cube куб вращающийся

  • Авторизуйтесь для ответа в теме
В этой теме нет ответов

#1 admin

admin

    Администратор

  • Администраторы
  • 76 сообщений

Отправлено 25 Август 2013 - 13:05

Вращающийся куб с простой моделью освещения.
Создаем WIN32 проект с названием samplegl2, вставляем туда приведенный ниже текст
Замените IDM_about на IDM_ABOUT (здесь заменяется автозаменой)

Изображение

// samplegl2.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "samplegl2.h"
#include <gl/gl.h>
#include <gl/glu.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst;		// current instance
TCHAR szTitle[MAX_LOADSTRING];	 // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name
// 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, HDC * hDC, HGLRC * hRC);
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
int width = 640, height = 480;

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_SAMPLEGL2, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
  return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SAMPLEGL2));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
}
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_SAMPLEGL2));
wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
//wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SAMPLEGL2);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
// OpenGL Context
HGLRC hRC;
HDC hdc;
//
//   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 | WS_VISIBLE,
	  CW_USEDEFAULT, 0, width, height, NULL, NULL, hInstance, NULL);
   if (!hWnd)
   {
	  return FALSE;
   }
   // enable OpenGL for the window
   EnableOpenGL( hWnd, &hdc, &hRC );
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
 
   return TRUE;
}
GLfloat theta = 0;
void resize(int width, int height)
{
glViewport(0,0,width,height);	  // Reset The Current Viewport
glMatrixMode(GL_PROJECTION);	  // Select The Projection Matrix
glLoadIdentity();		 // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);	   // Select The Modelview Matrix
glLoadIdentity();		 // Reset The Modelview Matrix
}
//
//  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_SIZE:		// Resize The OpenGL Window
  resize(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
  break;
case WM_ERASEBKGND:
  break;
case WM_PAINT:
  //hdc = BeginPaint(hWnd, &ps);
  // OpenGL animation code goes here
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	 glLoadIdentity();
  glTranslatef(0.0f,0.0f,-3.0f);
  glRotatef( theta, 0.1f, 0.5f, 0.2f );
  glBegin (GL_QUADS);
	// Front Face
	glColor3f(1,1,1);
	glNormal3f(0.0, 0.0, 1.0);
	glVertex3f (0.5, 0.5, 0.5);
	glVertex3f (-0.5, 0.5, 0.5);
	glVertex3f (-0.5, -0.5, 0.5);
	glVertex3f (0.5, -0.5, 0.5);
	// Back Face
	glColor3f(0,1,0);
	glNormal3f(0.0, 0.0, -1.0);
	glVertex3f (0.5, 0.5, -0.5);
	glVertex3f (0.5, -0.5, -0.5);
	glVertex3f (-0.5, -0.5, -0.5);
	glVertex3f (-0.5, 0.5, -0.5);
	// Left Face
	glColor3f(0,0,1);
	glNormal3f(-1.0, 0.0, 0.0);
	glVertex3f (-0.5, 0.5, 0.5);
	glVertex3f (-0.5, 0.5, -0.5);
	glVertex3f (-0.5, -0.5, -0.5);
	glVertex3f (-0.5, -0.5, 0.5);
	// Right face
	glColor3f(1,0,1);
	glNormal3f(1.0, 0.0, 0.0);
	glVertex3f(0.5, 0.5, 0.5);
	glVertex3f (0.5, -0.5, 0.5);
	glVertex3f (0.5, -0.5, -0.5);
	glVertex3f (0.5, 0.5, -0.5);
	// Top Face
	glColor3f(1,1,0);
	glNormal3f(0.0, 1.0, 0.0);
	glVertex3f (-0.5, 0.5, -0.5);
	glVertex3f (-0.5, 0.5, 0.5);
	glVertex3f (0.5, 0.5, 0.5);
	glVertex3f (0.5, 0.5, -0.5);
	// Bottom Face
	glColor3f(0,1,1);
	glNormal3f(0.0, -1.0, 0.0);
	glVertex3f (-0.5, -0.5, -0.5);
	glVertex3f (0.5, -0.5, -0.5);
	glVertex3f (0.5, -0.5, 0.5);
	glVertex3f (-0.5, -0.5, 0.5);
  glEnd();
  theta += 1.0f;
  SwapBuffers( hdc );
  // TODO: Add any drawing code here...
  //EndPaint(hWnd, &ps);
  Sleep(10);
  break;
case WM_DESTROY:
  DisableOpenGL( hWnd, hdc, hRC );
  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;
}

// Enable OpenGL
void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int format;

// get the device context (DC)
*hDC = GetDC( hWnd );
RECT rect;
// set the pixel format for the DC
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 = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ChoosePixelFormat( *hDC, &pfd );
SetPixelFormat( *hDC, format, &pfd );

// create and enable the render context (RC)
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
glFrontFace(GL_CW);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
{
  GLfloat ambient[4] = {0.3f, 0.3f, 0.3f, 1.0f};
  GLfloat light0[4] = {-0.5f, 0.2f, 1.0f, 0.01f};
  glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  glLightfv(GL_LIGHT0,GL_POSITION,light0);
  glLightfv(GL_LIGHT0, GL_SPECULAR, ambient);
}
glEnable(GL_COLOR_MATERIAL);
GetClientRect(hWnd,&rect);
resize(rect.right,rect.bottom);
}
// Disable OpenGL
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
}






Темы с аналогичным тегами OpenGL, Rotating Cube, куб, вращающийся

Количество пользователей, читающих эту тему: 1

0 пользователей, 1 гостей, 0 скрытых пользователей

Рейтинг@Mail.ru