📄 main.cpp
字号:
#include <windows.h> // Header File For Windows
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include "Ocean.h"
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=FALSE; // Fullscreen Flag Set To Fullscreen Mode By Default
float tran_x=0, tran_y=0, tran_z=0, rotx = 0, roty = 0;
T_Ocean* ocean ;
void drawOcean(T_Ocean* ocean);
void drawBreakingWave(T_Ocean* ocean, float particleSize);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
void drawOcean(T_Ocean* ocean)
{
GLfloat ambient[]={1.0,1.0,1.0,1.0};
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 1.0 };
GLfloat light_one[]={0.0f,0.5f,1.0f,1.0f};//光源颜色R,G,B,ALPHA
//GLfloat light_position1[] = {100,200.0,500, 1.0};
GLfloat light_position1[] = {0.0f,500.0f,512.0f,1.0};
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_one);
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glPushMatrix();
for (int i = 0 ;i<ocean->oceanSizeX; i++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int j = 0; j<ocean->oceanSizeY + 1; j++)
{
glNormal3d(
ocean->oceanBigNormals[i][j][0],
ocean->oceanBigNormals[i][j][1],
ocean->oceanBigNormals[i][j][2]);
glVertex3f(
ocean->oceanGridPosition[i][j][0],
ocean->oceanGridPosition[i][j][1]/**ocean->oceanWaveHeight*/,
ocean->oceanGridPosition[i][j][2]);
glNormal3d(
ocean->oceanBigNormals[i+1][j][0],
ocean->oceanBigNormals[i+1][j][1],
ocean->oceanBigNormals[i+1][j][2]);
glVertex3f(
ocean->oceanGridPosition[i+1][j][0],
ocean->oceanGridPosition[i+1][j][1]/**ocean->oceanWaveHeight*/,
ocean->oceanGridPosition[i+1][j][2]);
}
glEnd();
}
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHT1);
glDisable(GL_LIGHTING);
glDisable(GL_NORMALIZE);
}
void drawOceanCopy(T_Ocean* ocean, int copyX, int copyY)
{
for (int i = 0; i<copyX;i++)
{
for (int j = 0; j<copyY;j++)
{
glPushMatrix();
glTranslatef(ocean->oceanSizeX*ocean->oceanGridSize*i,0,ocean->oceanSizeY*ocean->oceanGridSize*j);
drawOcean(ocean);
drawBreakingWave(ocean, 0.2f);
glPopMatrix();
}
}
}
void drawBreakingWave(T_Ocean* ocean, float particleSize)
{
glDisable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
for (int i = 0; i< ocean->oceanSizeX; i++)
{
for (int j = 0; j<ocean->oceanSizeY; j++)
{
for (int k = 0; k< ocean->particleNum; k++)//遍历所有粒子
{
if (ocean->oceanBigNormals[i][j][0]<0 && ocean->oceanBigNormals[i][j][1]<0 && (ocean->oceanGridPosition[i][j][1])>10)
{
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(ocean->oceanParticle[i][j][k].particlePosition.x+particleSize,
ocean->oceanParticle[i][j][k].particlePosition.y + particleSize, ocean->oceanParticle[i][j][k].particlePosition.z);
glVertex3f(ocean->oceanParticle[i][j][k].particlePosition.x-particleSize,
ocean->oceanParticle[i][j][k].particlePosition.y + particleSize, ocean->oceanParticle[i][j][k].particlePosition.z);
glVertex3f(ocean->oceanParticle[i][j][k].particlePosition.x+particleSize,
ocean->oceanParticle[i][j][k].particlePosition.y - particleSize, ocean->oceanParticle[i][j][k].particlePosition.z);
glVertex3f(ocean->oceanParticle[i][j][k].particlePosition.x-particleSize,
ocean->oceanParticle[i][j][k].particlePosition.y - particleSize, ocean->oceanParticle[i][j][k].particlePosition.z);
glEnd();
}
}
}
}
glDisable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
}
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glClearColor(0.5f,0.6f,0.8f,1.0f);
//ocean = new T_Ocean();
ocean = new T_Ocean(64,64,2.0f,0.08,0.08,0.28,0.4f);
ocean->oceanWindScaleX = 100.0f;
ocean->oceanWindScaleY = 100.0f;
ocean->oceanWaveHeight = 0.1f;
enableBreakingWave(ocean);
//setOceanWaveHeight(ocean,0.4f);
return TRUE; // Initialization Went OK
}
int renderGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();// Reset The Current Modelview Matrix
glTranslatef(-100.0f + tran_x, -50.0f + tran_y, -300.0f + tran_z);
glRotatef(30 + rotx,1.0f, 0.0f, 0.0f);
glRotatef(0.0f + roty,0.0f, 1.0f, 0.0f);
//glRotatef(0,0.0f, 0.0f, 1.0f);
return TRUE;
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // 使用投影的矩阵模式才能实现平移的键盘响应。WHY?
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective (70.0, (GLdouble)width/(GLdouble)height, 3.0, 1000.0);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
{
if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}
if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL
}
if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}
if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}
if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
}
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = "OpenGL"; // Set The Class Name
if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (fullscreen) // Attempt Fullscreen Mode?
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -