计算机图形学的课程设计作业

显示一个Cube立方体,然后可以用鼠标拖动它旋转.

用OpenGL实现再简单不过了.我直接在Nehe的lesson7的程序总修改几下,就可以实现了.95%都是它已经写好了的代码.唯一修改的就是把6个面改成分别使用了六个不同的纹理图片.还增加了鼠标的拖动控制.

下面就是整个OpenGL程序.

/*
 *   a Cube for OpenGL Sample
 */

#include <windows.h>  // Header File For Windows
#include <stdio.h>   // Header File For Standard Input/Output
#include <gl/gl.h>   // Header File For The OpenGL32 Library
#include <gl/glu.h>   // Header File For The GLu32 Library
#include <gl/glaux.h>  // Header File For The Glaux Library

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=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default

GLfloat xrot;    // X Rotation
GLfloat yrot;    // Y Rotation

GLfloat LightAmbient[]=  { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]=  { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };

GLuint texture[6];   // Storage For 6 face Textures

int     LastXPos,LastYPos;
BOOL    IsLBDown = FALSE;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc

AUX_RGBImageRec *LoadBMP(char *Filename)    // Loads A Bitmap Image
{
 FILE *File=NULL;         // File Handle

 if (!Filename)          // Make Sure A Filename Was Given
 {
  return NULL;         // If Not Return NULL
 }

 File=fopen(Filename,"r");       // Check To See If The File Exists

 if (File)           // Does The File Exist?
 {
  fclose(File);         // Close The Handle
  return auxDIBImageLoad(Filename);    // Load The Bitmap And Return A Pointer
 }

 return NULL;          // If Load Failed Return NULL
}

int LoadGLTextures()         // Load Bitmaps And Convert To Textures
{
 int   i;
 char filename[128];
 AUX_RGBImageRec *TextureImage[6];     // Create Storage Space For The Texture

 memset(TextureImage,0,sizeof(void *)*6);            // Set The Pointer To NULL

 // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
 
 for(i=0;i<6;i++)
 {
  sprintf(filename,"Data/%d.bmp",i+1);
  TextureImage[i] = LoadBMP(filename);
  if(!TextureImage[i])
  {
   char msg[256];
   sprintf(msg,"Cannot read the file : %s",filename);
   MessageBox(NULL,msg,"Error",MB_OK);
   return FALSE;
  }
 }

 glGenTextures(6,texture);
 
 for(i=0;i<6;i++)
 {
  glBindTexture(GL_TEXTURE_2D,texture[i]);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data);

  if(TextureImage[i]->data)
   free(TextureImage[i]->data);
  free(TextureImage[i]);
 }

 return TRUE;          // Return The Status
}

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);      // 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
}

int InitGL(GLvoid)          // All Setup For OpenGL Goes Here
{
 if (!LoadGLTextures())        // Jump To Texture Loading Routine
 {
  return FALSE;         // If Texture Didn't Load Return FALSE
 }

 glEnable(GL_TEXTURE_2D);       // Enable Texture Mapping
 glShadeModel(GL_SMOOTH);       // Enable Smooth Shading
 glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
 glClearDepth(1.0f);   

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值