📄 main.c
字号:
/* Copyright (C) Gabor Nagy, 2000. * All rights reserved worldwide. * * This software is provided "as is" without express or implied * warranties. You may freely copy and compile this source into * applications you distribute provided that the copyright text * below is included in the resulting source code, for example: * "Portions Copyright (C) Gabor Nagy, 2000" *//*==============================================================================*//* Real-time glass *//* *//* - The Main *//* *//* AUTHOR: Gabor Nagy *//* DATE: 2000-May-02 14:49:29 *//* v1.0 *//* *//* For Game Programming Graphics Gems *//*==============================================================================*/#include <stdio.h>#include <math.h>#include <GL/glut.h>#include <stdlib.h>#ifdef _WIN32#include <windows.h>#include <time.h>#else#include <sys/time.h>#include <sys/param.h>#endif#include "str.h"#include "obj.h"E3dModel* Scn_GlassModel=NULL;E3dModel* Scn_BulbGlassModel=NULL;E3dModel* Scn_BulbBlackModel=NULL;E3dModel* Scn_BulbCopperModel=NULL;E3dModel* Scn_ClothModel=NULL;E3dModel* Scn_TableModel=NULL;E3dLight Sh_Light;enum{ ShDOLLY_CAMERA=0, ShMOVE_LIGHT};int Sh_MiddleMouseMode=ShDOLLY_CAMERA;int Sh_DisplayMode=0;#ifdef _WIN32float Sh_StartTime;#elseunsigned long Sh_StartTime;#endifunsigned long Sh_Frame=0;unsigned long Sh_NumOfFrames=200;// Shadow map//unsigned long* Sh_ShadowMapImage=NULL;int Sh_ShadowMapXSize=128,Sh_ShadowMapYSize=128;int Sh_TexXSize, Sh_TexYSize;GLubyte* Sh_TexImage;GLuint Sh_EnvMapTexId, Sh_WoodTexId;float Scn_CameraX=0.0, Scn_CameraY=0.0, Scn_CameraZ=0.0;E3dLight Scn_Headlight;// Viewing//float Sh_Longitude=152.0, Sh_Latitude=-36.0;float Sh_ViewDistance = 20.0;float Sh_ZNear=0.1, Sh_ZFar=8192.0;float Sh_WindowAspect = 4.0/3.0;long Sh_WindowXSize, Sh_WindowYSize;int Sh_PrevMX, Sh_PrevMY;EBool Sh_LeftButton = FALSE, Sh_MiddleButton = FALSE;int Sh_MainMenu, Sh_DisplayMenu;int Sh_CurrentGlassObject=-1;/*======================================*//* Setlect blocker object *//*======================================*/void ShCB_SetlectGlassObject(int LValue){ E3dModel* LModel; E3dMesh* LMesh; E3dPolyGroup* LPolyGroup; E3dMaterial* LMaterial; if(Sh_CurrentGlassObject!=LValue) { switch(LValue) { case 0: if(Scn_GlassModel!=NULL) E3d_ModelFree(Scn_GlassModel); if((LModel=E3d_ReadOBJFile("objects/BulbGlass.obj")) != NULL) { Scn_GlassModel = LModel; if((LMesh=(E3dMesh*)(LModel->Geometry))!=NULL) { LPolyGroup=LMesh->PolyGroups[0]; LPolyGroup->Material=LMaterial=E3d_MaterialAllocate(); LMaterial->Ambient[0]=0.0; LMaterial->Ambient[1]=0.0; LMaterial->Ambient[2]=0.0; LMaterial->Diffuse[0]=3.0;LMaterial->Diffuse[1]=3.0;LMaterial->Diffuse[2]=3.0;LMaterial->Diffuse[3]=0.36; } } if((LModel=E3d_ReadOBJFile("objects/BulbBlack.obj")) != NULL) { Scn_BulbBlackModel = LModel;// Plastic part of light bulb// if((LMesh=(E3dMesh*)(LModel->Geometry))!=NULL) { LPolyGroup=LMesh->PolyGroups[0]; LPolyGroup->Material=LMaterial=E3d_MaterialAllocate(); LMaterial->Ambient[0]=0.0; LMaterial->Ambient[1]=0.0; LMaterial->Ambient[2]=0.0; LMaterial->Diffuse[0]=0.1; LMaterial->Diffuse[1]=0.1; LMaterial->Diffuse[2]=0.1; } E3d_ModelRefreshMatrices(LModel); } if((LModel=E3d_ReadOBJFile("objects/BulbCopper.obj")) != NULL) { Scn_BulbCopperModel = LModel;// Copper part of light bulb// if((LMesh=(E3dMesh*)(LModel->Geometry))!=NULL) { LPolyGroup=LMesh->PolyGroups[0]; LPolyGroup->Material=LMaterial=E3d_MaterialAllocate(); LMaterial->Ambient[0]=2.0; LMaterial->Ambient[1]=1.2; LMaterial->Ambient[2]=0.4; LMaterial->Diffuse[0]=1.0; LMaterial->Diffuse[1]=0.6; LMaterial->Diffuse[2]=0.2; } E3d_ModelRefreshMatrices(LModel); } break; } Sh_CurrentGlassObject=LValue; } glutPostRedisplay();}/*======================================*//* Main menu callback *//*======================================*/void ShCB_MainMenu(int value){ switch(value) { case 5: exit(0); break; }}/*==============================*//* Read raw RGBA file *//*==============================*/char* E3d_LoadImage(char* LFileName){ FILE* LInFile; char* LBuffer=NULL; unsigned long LFileSize; if((LInFile=fopen(LFileName,"r"))!=NULL) {// Determine file length// fseek(LInFile, 0, SEEK_END); LFileSize=ftell(LInFile); fseek(LInFile, 0, SEEK_SET);// Allocate buffer and read file// if((LBuffer=EMalloc(LFileSize))!=NULL) { fread(LBuffer, 1, LFileSize, LInFile); } fclose(LInFile); } return(LBuffer);}/*==============================*//* Initialize textures *//*==============================*/void Sh_InitTextures(void){ int LTexXSize, LTexYSize; GLubyte* LTexData;// Read and set up wood texture// glGenTextures(1,&Sh_WoodTexId); glBindTexture(GL_TEXTURE_2D, Sh_WoodTexId); if((LTexData=(GLubyte*)E3d_LoadImage("Wood.rgba"))!=NULL) { LTexXSize=128;LTexYSize=128; glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 4, LTexXSize, LTexYSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, LTexData ); }// Read and set up environment-map texture// glGenTextures(1,&Sh_EnvMapTexId); glBindTexture(GL_TEXTURE_2D, Sh_EnvMapTexId); if((LTexData=(GLubyte*)E3d_LoadImage("Envmap.rgba"))!=NULL) { LTexXSize=128;LTexYSize=128; glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 4, LTexXSize, LTexYSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, LTexData ); }}/*==============================*//* Render the scene *//*==============================*/void Sh_DrawScene(){ E3dModel* LModel; E3dMesh* LMesh; E3dPolyGroup* LPolyGroup; E3dMaterial* LMaterial; glEnable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); E3d_GLUpdateLight(0, &Sh_Light);// Draw cloth// if((LModel=Scn_ClothModel)!=NULL) { glPushMatrix(); glDisable(GL_TEXTURE_2D); E3d_DrawModel(LModel, FALSE, &Sh_Light, FALSE); glPopMatrix(); }// Draw table// if((LModel=Scn_TableModel)!=NULL) { glPushMatrix();// Set up wood texture// glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, Sh_WoodTexId); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); E3d_DrawModel(LModel, TRUE, &Sh_Light, FALSE); glPopMatrix(); }// Draw light bulb copper part// if((LModel=Scn_BulbCopperModel)!=NULL) { glPushMatrix(); glEnable(GL_TEXTURE_2D);// Set up shpere-map texture// glBindTexture(GL_TEXTURE_2D, Sh_EnvMapTexId); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_TEXTURE_2D);// Texture-coordinate generation method// glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); E3d_DrawModel(LModel, FALSE, &Sh_Light, FALSE); glPopMatrix(); }// Draw light bulb plastic part// if((LModel=Scn_BulbBlackModel)!=NULL) { glDisable(GL_TEXTURE_2D); glPushMatrix(); E3d_DrawModel(LModel, FALSE, &Sh_Light, FALSE); glPopMatrix(); }// Render glass model// if(Scn_GlassModel!=NULL) { glEnable(GL_BLEND);// Turn off Z update//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -