opengl 球纹理旋转源代码
專注java已6年,歡迎加入java核心技術(shù)QQ群:135138817,每周五晚有群主進行技術(shù)講座。
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <gl/glaux.h>
#include <stdio.h>
#include <io.h>
HWND?? hWnd;
HDC??? hDC;
HGLRC?? hRC=NULL;?? //定義渲染環(huán)境
HINSTANCE hInstance;?? //得到程序的例子
RECT?? rect;
int??? sw = 640;
int??? sh = 480;
bool?? fullscreen = 1;
GLfloat?? aspect;
GLfloat xrot;???? // X軸旋轉(zhuǎn) ( 新 )
GLfloat yrot;???? // Y軸旋轉(zhuǎn) ( 新 )
GLfloat zrot;???? // Z軸旋轉(zhuǎn) ( 新 )
GLuint texture[1];??? //紋理的存儲空間 ( 新 )
#pragma comment( lib, "opengl32.lib" )??????? // 鏈接時使用OpenGL32.lib
#pragma comment( lib, "glu32.lib" )???????? // 鏈接時使用GLu32.lib
#pragma comment( lib, "glaux.lib" )???????? // 鏈接時使用GLaux.lib
AUX_RGBImageRec *LoadBMP(char *Filename)???? // 讀取位圖圖象
{
FILE *File=NULL;????????? // 文件句柄
if (!Filename)?????????? // 確定文件名已給出
{
?? return NULL;????????? // 如果文件名未給出則返回NULL
}
File=fopen(Filename,"r");??????? // 檢測文件是否存在
if (File)??????????? // 文件是否存在?
{
?? fclose(File);????????? // 關(guān)閉文件
?? return auxDIBImageLoad(Filename);???? // 讀取位圖并返回一個指針
}
return NULL;?????????? //如果調(diào)用文件失敗則返回NULL
}
int LoadGLTextures()????????? //調(diào)用Bitmap并轉(zhuǎn)換成紋理
{
int Status=FALSE;????????? //狀態(tài)確定
AUX_RGBImageRec *TextureImage[1];????? //為紋理創(chuàng)建存儲空間
memset(TextureImage,0,sizeof(void *)*1);?????????? //將指針設(shè)為NULL
//讀取位圖,檢查錯誤。如果位圖不存在則退出
if (TextureImage[0]=LoadBMP("Data/TX.bmp"))
{
?? Status=TRUE;????????? //設(shè)Status為TRUE
?? glGenTextures(1, &texture[0]);????? //創(chuàng)建紋理
?? //用位圖中的數(shù)據(jù)進行典型的紋理生成
?? glBindTexture(GL_TEXTURE_2D, texture[0]);
?? glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
?? glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
?? glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
if (TextureImage[0])????????? //紋理是否存在
{
?? if (TextureImage[0]->data)??????? //紋理圖象是否存在
?? {
??? free(TextureImage[0]->data);????? //釋放紋理圖象所占用內(nèi)存
?? }
?? free(TextureImage[0]);???????? //釋放圖象結(jié)構(gòu)
}
return Status;?????????? //返回Status的值
}
void SceneInit(int w, int h)
{
LoadGLTextures();
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);??????? //允許平滑著色
glClearColor( 0.0, 0.0, 0.0, 0.5 );
glClearDepth(1.0f);????????? //設(shè)置深度緩沖區(qū)
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);???????? //深度測試的類型
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void SceneResizeViewport(GLsizei w, GLsizei h)
{
if(h==0)
{
?? h=1;
}
aspect = (GLfloat)w/(GLfloat)h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);?????? //選擇投影矩陣
glLoadIdentity();
gluPerspective( 45.0f, aspect, 0.1f, 100.0f );
glMatrixMode(GL_MODELVIEW);??
glLoadIdentity();
}
void SceneShow(GLvoid)????????? //這里進行所有的繪圖工作
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清屏和清除深度緩沖區(qū)
glLoadIdentity();????????? //重置當(dāng)前Modelview矩陣
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
//glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
GLUquadricObj *quadObj = gluNewQuadric();//創(chuàng)建一個二次曲面物體
??? gluQuadricTexture(quadObj,GL_TRUE);??????? //啟用該二次曲面的紋理
??? glBindTexture(GL_TEXTURE_2D, texture[0]);//綁定紋理
?????
???? gluSphere(quadObj,1,20,20); //畫圓
gluDeleteQuadric(quadObj);
??? glEnd();
???
xrot+=0.3f;
yrot+=0.2f;
zrot+=0.4f;
}
void EnableOpenGL()
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
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 = 16;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat( hDC, &pfd );
SetPixelFormat( hDC, iFormat, &pfd );
hRC = wglCreateContext( hDC );
wglMakeCurrent( hDC, hRC );
}
// 取消 OpenGL
void DisableOpenGL()
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
}
bool ChangeResolution(int w, int h, int bitdepth)
{
DEVMODE devMode;
int?? modeSwitch;
int?? closeMode = 0;
EnumDisplaySettings(NULL, closeMode, &devMode);
devMode.dmBitsPerPel = bitdepth;
devMode.dmPelsWidth = w;
devMode.dmPelsHeight = h;
devMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
modeSwitch = ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
if(modeSwitch == DISP_CHANGE_SUCCESSFUL)
{
?? return true;
}
else
{
?? ChangeDisplaySettings(NULL, 0);
?? return false;
}
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
??????? WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
?? case WM_CREATE:
??? GetWindowRect(hWnd, &rect);
??? sw = rect.right - rect.left;
??? sh = rect.bottom - rect.top;
??? SceneResizeViewport(sw, sh);
??? return 0;
?? case WM_SIZE:
??? if(!fullscreen)
??? {
???? GetWindowRect(hWnd, &rect);
???? sw = rect.right - rect.left;
???? sh = rect.bottom - rect.top;
???? if(sw>0 && sh>0)
????? SceneResizeViewport(sw, sh);
??? }
??? else
??? {
???? SceneResizeViewport(GetSystemMetrics( SM_CXSCREEN ),
????????? GetSystemMetrics( SM_CYSCREEN ));
??? }
??? return 0;
?? case WM_CLOSE:
??? ShowWindow (hWnd, SW_HIDE);
??? PostQuitMessage( 0 );
??? return 0;
?? case WM_DESTROY:
??? return 0;
?? case WM_KEYDOWN:
??? switch( wParam )
??? {
??? case VK_ESCAPE:
???? PostMessage(hWnd, WM_CLOSE, 0, 0);
??? break;
??? }
??? return 0;
?? default:
?? return DefWindowProc( hWnd,message, wParam, lParam );
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
???????????????????? HINSTANCE hPrevInstance,
???????????????????? LPSTR???? lpCmdLine,
???????????????????? int?????? nCmdShow)
{
?? WNDCLASS wc;
MSG msg;
bool bQuit = false;
if (MessageBox(NULL,"是否選擇全屏顯示模式?", "全屏方式運行?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
?? fullscreen=0;??????? //窗口模式
}
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = "Name";
RegisterClass( &wc );
if(fullscreen)
{
?? ChangeResolution(640, 480, 16);
?? hWnd = CreateWindow(
???????? "Name",
???????? "Lesson1",
???????? WS_POPUP | WS_CLIPSIBLINGS | WS_VISIBLE,
???????? 0, 0,
???????? GetSystemMetrics( SM_CXSCREEN ),
???????? GetSystemMetrics( SM_CYSCREEN ),
???????? NULL, NULL,
???????? hInstance,
???????? NULL );
}else
{
?? hWnd = CreateWindow(
??????? "Name",
??????? "Lesson1",
??????? WS_TILEDWINDOW | WS_VISIBLE,
??????? GetSystemMetrics( SM_CXSCREEN )/2-sw/2,
??????? GetSystemMetrics( SM_CYSCREEN )/2-sh/2,
??????? sw,
??????? sh,
??????? NULL, NULL,
??????? hInstance,
??????? NULL );
?? ChangeDisplaySettings(NULL, 0);
}
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
//Initialisation
EnableOpenGL();
SceneInit(sw, sh);
if(!fullscreen)
{
?? GetWindowRect(hWnd, &rect);
?? sw = rect.right - rect.left;
?? sh = rect.bottom - rect.top;
?? if(sw>0 && sh>0)
??? SceneResizeViewport(sw, sh);
}
else
{
?? SceneResizeViewport(GetSystemMetrics( SM_CXSCREEN ),
??????? GetSystemMetrics( SM_CYSCREEN ));
}
while ( !bQuit )
{
?? if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
??? if ( msg.message == WM_QUIT )
???? bQuit = true;
??? else
??? {
???? TranslateMessage( &msg );
???? DispatchMessage( &msg );
??? }
?? else
?? {
??? // OpenGL 動畫
??? SceneShow();
??? SwapBuffers(hDC);
?? }
}
//關(guān)閉,退出程序
DisableOpenGL();
ShowWindow (hWnd, SW_HIDE);
DestroyWindow( hWnd );
ChangeDisplaySettings(NULL, 0);
return msg.wParam;
return 0;
}
轉(zhuǎn)載于:https://www.cnblogs.com/myzhijie/articles/1658569.html
總結(jié)
以上是生活随笔為你收集整理的opengl 球纹理旋转源代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 掘开是什么意思解释
- 下一篇: MHT格式文件更换默认图标及打开后显示乱