孙其功陪你学之——opengl纹理贴图(例程)
2013-08-09 15:13
459 查看
本人亲测例程(Opengl宝典提供——略有修改),仅供学习参考
#include <glut.h>//此头文件需注意
#include "glext.h"//此头文件位置需注意
#include <math.h>
#ifndef M_PI
# define M_PI 3.141592649
#endif /* !M_PI */
#include "bitmap.h"
#include "bitmap.c"
/*
* Globals...
*/
int Width; /* Width of window */
int Height; /* Height of window */
BITMAPINFO *TexInfo; /* Texture bitmap information */
GLubyte *TexBits; /* Texture bitmap pixel bits */
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
/*
* Functions...
*/
void Redraw(void);
void Resize(int width, int height);
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void SetupRC()
{
// Black background
glClearColor(1.0f, 0.0f, 1.0f, 1.0f );
// Set drawing color to green
//glColor3f(0.0f, 1.0f, 0.0f);
}
/*
* 'main()' - Open a window and display a textured sky.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line arguments */
char *argv[]) /* I - Command-line arguments */
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
glutInitWindowSize(792, 573);
glutCreateWindow("冰心玉壶");
glutSpecialFunc(SpecialKeys);
glutReshapeFunc(Resize);
glutDisplayFunc(Redraw);
TexBits = LoadDIBitmap("poot.bmp", &TexInfo);
SetupRC();
glutMainLoop();
return (0);
}
/*
* 'Redraw()' - Redraw the window...
*/
void
Redraw(void)
{
static GLfloat dark[4] = { 0.0, 0.0, 0.0, 1.0 };
static GLfloat normal[4] = { 0.5, 0.5, 0.5, 1.0 };
static GLfloat bright[4] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat pos[4] = { -0.2588, 0.0, 0.9659, 0.0 };
/* Clear the window */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Setup for drawing... */
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPushMatrix();
glTranslatef(0.0, 0.0, -50.0);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
/* First draw the pot without texturing and with specular highlights */
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, dark);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dark);
glLightfv(GL_LIGHT0, GL_SPECULAR, bright);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glMaterialfv(GL_FRONT, GL_AMBIENT, dark);
glMaterialfv(GL_FRONT, GL_DIFFUSE, dark);
glMaterialfv(GL_FRONT, GL_SPECULAR, bright);
glMateriali(GL_FRONT, GL_SHININESS, 128);
glColor3f(1.0, 1.0, 1.0);
glutSolidTeapot(10.0);
/* Define the 2D texture image. */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TexInfo->bmiHeader.biWidth,
TexInfo->bmiHeader.biHeight, 0, GL_BGR_EXT,
GL_UNSIGNED_BYTE, TexBits);
/* Draw the teapot with texturing, adding the textured colors... */
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, normal);
glLightfv(GL_LIGHT0, GL_AMBIENT, normal);
glLightfv(GL_LIGHT0, GL_DIFFUSE, normal);
glLightfv(GL_LIGHT0, GL_SPECULAR, dark);
glMaterialfv(GL_FRONT, GL_AMBIENT, normal);
glMaterialfv(GL_FRONT, GL_DIFFUSE, normal);
glMaterialfv(GL_FRONT, GL_SPECULAR, dark);
glMateriali(GL_FRONT, GL_SHININESS, 0);
glutSolidTeapot(10.0);
glPopMatrix();
glFinish();
}
void
Resize(int width, /* I - Width of window */
int height) /* I - Height of window */
{
/* Save the new width and height */
Width = width;
Height = height;
/* Reset the viewport... */
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (float)width / (float)height, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
#include <glut.h>//此头文件需注意
#include "glext.h"//此头文件位置需注意
#include <math.h>
#ifndef M_PI
# define M_PI 3.141592649
#endif /* !M_PI */
#include "bitmap.h"
#include "bitmap.c"
/*
* Globals...
*/
int Width; /* Width of window */
int Height; /* Height of window */
BITMAPINFO *TexInfo; /* Texture bitmap information */
GLubyte *TexBits; /* Texture bitmap pixel bits */
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
/*
* Functions...
*/
void Redraw(void);
void Resize(int width, int height);
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void SetupRC()
{
// Black background
glClearColor(1.0f, 0.0f, 1.0f, 1.0f );
// Set drawing color to green
//glColor3f(0.0f, 1.0f, 0.0f);
}
/*
* 'main()' - Open a window and display a textured sky.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line arguments */
char *argv[]) /* I - Command-line arguments */
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
glutInitWindowSize(792, 573);
glutCreateWindow("冰心玉壶");
glutSpecialFunc(SpecialKeys);
glutReshapeFunc(Resize);
glutDisplayFunc(Redraw);
TexBits = LoadDIBitmap("poot.bmp", &TexInfo);
SetupRC();
glutMainLoop();
return (0);
}
/*
* 'Redraw()' - Redraw the window...
*/
void
Redraw(void)
{
static GLfloat dark[4] = { 0.0, 0.0, 0.0, 1.0 };
static GLfloat normal[4] = { 0.5, 0.5, 0.5, 1.0 };
static GLfloat bright[4] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat pos[4] = { -0.2588, 0.0, 0.9659, 0.0 };
/* Clear the window */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Setup for drawing... */
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPushMatrix();
glTranslatef(0.0, 0.0, -50.0);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
/* First draw the pot without texturing and with specular highlights */
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, dark);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dark);
glLightfv(GL_LIGHT0, GL_SPECULAR, bright);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glMaterialfv(GL_FRONT, GL_AMBIENT, dark);
glMaterialfv(GL_FRONT, GL_DIFFUSE, dark);
glMaterialfv(GL_FRONT, GL_SPECULAR, bright);
glMateriali(GL_FRONT, GL_SHININESS, 128);
glColor3f(1.0, 1.0, 1.0);
glutSolidTeapot(10.0);
/* Define the 2D texture image. */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TexInfo->bmiHeader.biWidth,
TexInfo->bmiHeader.biHeight, 0, GL_BGR_EXT,
GL_UNSIGNED_BYTE, TexBits);
/* Draw the teapot with texturing, adding the textured colors... */
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, normal);
glLightfv(GL_LIGHT0, GL_AMBIENT, normal);
glLightfv(GL_LIGHT0, GL_DIFFUSE, normal);
glLightfv(GL_LIGHT0, GL_SPECULAR, dark);
glMaterialfv(GL_FRONT, GL_AMBIENT, normal);
glMaterialfv(GL_FRONT, GL_DIFFUSE, normal);
glMaterialfv(GL_FRONT, GL_SPECULAR, dark);
glMateriali(GL_FRONT, GL_SHININESS, 0);
glutSolidTeapot(10.0);
glPopMatrix();
glFinish();
}
void
Resize(int width, /* I - Width of window */
int height) /* I - Height of window */
{
/* Save the new width and height */
Width = width;
Height = height;
/* Reset the viewport... */
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (float)width / (float)height, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
相关文章推荐
- 孙其功陪你学之——OpenGL纹理贴图
- Win32 OpenGL编程(16) 纹理贴图
- OpenGL教程翻译 第十六课 基本的纹理贴图
- opengl学习笔记——纹理贴图
- opengl纹理贴图(续)
- OpenGL系统设计-纹理贴图(4)_包含JPEG解压缩解码
- openGL学习笔记6 bmp 2D纹理贴图
- OpenGL基本概念入门5——纹理贴图2
- OpenGL纹理贴图流程
- OpenGL中用bmp图片做纹理贴图的三种方法
- windows下opengl多重纹理贴图
- OpenGL——纹理贴图
- OpenGL系统设计-纹理贴图(1)
- OpenGL纹理贴图 JPEG纹理
- android ndk调用OpenGL 实现纹理贴图Texture
- OpenGL基本概念入门5——纹理贴图1
- 孙其功陪你学之——OpenGL加载OBJ模型文件并进行纹理修饰
- OpenGL深入探索——纹理加载(ImageMagick)与贴图
- OpenGL系统设计-纹理贴图(3)
- OpenGL纹理贴图纠结的步骤