(同实验一)
// exp_2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include //Header File For The OpenGL32 Library
#include //Header File For The GLu32 Library
#include //Header File For the Glaux Library
#define GLUT_DISABLE_ATEXIT_HACK
#include "GL\GLUT.H"
#pragma warning (disable:4996);
GLfloat rtri;
GLfloat rquad;
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 filter; //Which Filter to Use
GLuint texture[3]; //Storage For 3 Textures
int InitGL(int width, int height);
void DrawGLScene(void);
void myReshape(int w, int h);
void myKeyboard(int key, int x, int y);
void myMovedMouse(int x, int y);
AUX_RGBImageRec *LoadBMP(char *Filename);
int LoadGLTextures();
int main(int argc,char **argv)
{
glutInit(&argc, argv); //Initialize GLUT function callings
glutInitWindowSize(800, 600);//Set window size(width,height) in number of pixels
//Set window position, from the left and top of the screen, in numbers of pixels
glutInitWindowPosition(200, 100);
//Specify a window creation event
glutCreateWindow("3D Graph");
//Specify the drawing function that is called when the window is created or redrew
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(myReshape);
glutSpecialFunc(myKeyboard);
glutMotionFunc(myMovedMouse);
InitGL(800, 600); //Invoke this function for initialization
glutMainLoop(); //Enter the event processing loop
return 0; //Indicate normal termination
}
int InitGL(int width, int height) {
myReshape(width, height);
if (!LoadGLTextures()) //Jump To Texture Loading Routine
{
return FALSE;
//If Textrure 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);
//Depth Buffer Setup
glEnable(GL_DEPTH_TEST); //Enables Depth Testing
glDepthFunc(GL_LEQUAL);
//The Type of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Really Nice Perspective Calculations
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); //Setup The Ambient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); //Setup The Diffuse Light
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition); //Position The Light
glEnable(GL_LIGHT1);
return TRUE;
}
void myReshape(int width, int height) {
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);
glLoadIdentity();
//Reset the Projection Matrix
}
void DrawGLScene(GLvoid)
//Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the Screen and The Depth Buffer
glLoadIdentity();
//Reset The View
glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(rquad, 1.0f, 1.0f, 1.0f); //Rotate The Quad On the X axis (NEW)
glBindTexture(GL_TEXTURE_2D, texture[filter]);
glBegin(GL_QUADS);
//Front Face
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
//Back Face
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
//Top Face
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
//Bottom Face
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
//Right face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glFlush(); //Force to display the new drawings immediately
}
void myKeyboard(int key, int x, int y) {
switch (key) {
case GLUT_KEY_LEFT:
case GLUT_KEY_UP:
rtri -= 0.5f; //Increase the Rotation Variable For the Triangle(NEW)
rquad += 0.85f;
DrawGLScene();
break;
case GLUT_KEY_RIGHT:
case GLUT_KEY_DOWN:
rtri += 0.5f;
rquad -= 0.85f;
DrawGLScene();
break;
}
}
void myMovedMouse(int x, int y) {
}
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 Exists?
{
fclose(file);
//Close Handle
return auxDIBImageLoadA(Filename); //Load The Bitmap and RETURN A Pointer
}
return NULL;
//if Load Failed Return NULL
}
int LoadGLTextures() //Load Bitmaps and convert textures
{
int Status = FALSE;
//Status Indicator
AUX_RGBImageRec *TextureImage[1]; //Create Storage Space for the Texture
memset(TextureImage, 0, sizeof(void*) * 1); //Set the pointer to NULL
//Load The Bitmap,Check For Errors,If Bitmap's not Found Quit
if (TextureImage[0] = LoadBMP("Data/Create.bmp"))
Status = TRUE; //Set The Status To TRUE
glGenTextures(3, &texture[0]); //Create Three Textures
//Create Nearest Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
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[0]->sizeX,
TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
//Create Linear Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
//Create MipMapped Texture
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
if (TextureImage[0]) //If Texture Exists
{
if (TextureImage[0]->data) //If texture Image Exists
{
free(TextureImage[0]->data); //Free the Texture Image Memory
}
free(TextureImage[0]); //Free the Image Structure
}
return Status;
}
解决方法:把glut32.dll复制到C:\Windows\SysWOW64目录下
参考:https://blog.csdn.net/u012130706/article/details/73695406
后来报了第二个错误,是找不到openGL.dll,查找网上信息可知:
错误原因:附加依赖项同时引用了glut.lib和glut32.lib,更改内容如下:
glTexImage2D该句有误,载入图像出错
解决: if (TextureImage[0] = LoadBMP(“C:/Users/Natalie/Documents/Visual Studio 2015/Projects/openGL_SUES/exp_2/Data/Create.bmp”))
*注意文件路径
#pragma warning (disable:4996) 这句表示解决掉fopen等函数安全性问题。
要注意位图的边框、大小否则要出错
按方向键可以实现图像旋转功能
有一些小问题还没有解决,比如不知道为什么该立方体上有一部分色差。(好心人路过的话麻烦看看是怎么回事…我看矩阵没有问题鸭)
glu中提供了一个绘制圆柱体的函数:
void gluCylinder( GLUquadric* quad,
GLdouble base,
GLdouble top,
GLdouble height,
GLint slices,
GLint stacks )
使用方法是:
GLUquadric *pObj;
pObj = gluNewQuadric();
注意之后使用到sin,cos 函数,需要导入cmath头文件。