日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

opengl 球纹理旋转源代码

發布時間:2023/12/19 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opengl 球纹理旋转源代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

專注java已6年,歡迎加入java核心技術QQ群:135138817,每周五晚有群主進行技術講座。

#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;?? //定義渲染環境
HINSTANCE hInstance;?? //得到程序的例子

RECT?? rect;

int??? sw = 640;
int??? sh = 480;

bool?? fullscreen = 1;

GLfloat?? aspect;

GLfloat xrot;???? // X軸旋轉 ( 新 )
GLfloat yrot;???? // Y軸旋轉 ( 新 )
GLfloat zrot;???? // Z軸旋轉 ( 新 )

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);????????? // 關閉文件
?? return auxDIBImageLoad(Filename);???? // 讀取位圖并返回一個指針
}

return NULL;?????????? //如果調用文件失敗則返回NULL
}

int LoadGLTextures()????????? //調用Bitmap并轉換成紋理
{
int Status=FALSE;????????? //狀態確定

AUX_RGBImageRec *TextureImage[1];????? //為紋理創建存儲空間

memset(TextureImage,0,sizeof(void *)*1);?????????? //將指針設為NULL

//讀取位圖,檢查錯誤。如果位圖不存在則退出
if (TextureImage[0]=LoadBMP("Data/TX.bmp"))
{
?? Status=TRUE;????????? //設Status為TRUE

?? glGenTextures(1, &texture[0]);????? //創建紋理

?? //用位圖中的數據進行典型的紋理生成
?? 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);????? //釋放紋理圖象所占用內存
?? }

?? free(TextureImage[0]);???????? //釋放圖象結構
}

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);????????? //設置深度緩沖區
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); //清屏和清除深度緩沖區
glLoadIdentity();????????? //重置當前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();//創建一個二次曲面物體
??? 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);

?? }
}

//關閉,退出程序
DisableOpenGL();

ShowWindow (hWnd, SW_HIDE);
DestroyWindow( hWnd );

ChangeDisplaySettings(NULL, 0);

return msg.wParam;

return 0;
}

轉載于:https://www.cnblogs.com/myzhijie/articles/1658569.html

總結

以上是生活随笔為你收集整理的opengl 球纹理旋转源代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。