投影纹理
#pragma once
#include <windows.h>
#include<fstream>
#include <iostream>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include "MyMatrix4X4.h"
using namespace std;
static int win;
GLuint pTexture;/***纹理标识***/
int imgWid=64,imgHei=64;/***纹理源大小***/
static GLubyte texSourIma[64][64][4];/***纹理源图像***/
MyVector3D_f cameraPosition(25.0,35.0,25.0);/***摄像机位置***/
MyMatrix4X4_f cameraView,cameraProjection;/***视图矩阵和投影矩阵***/
void initTexture()
{
for (int i =0;i<64;i++)
{
for (int j=0;j<64;j++)
{
texSourIma[i][j][0]=(GLubyte)(i%2)*255;
texSourIma[i][j][1]=(GLubyte)(i%2)*255;
texSourIma[i][j][2]=(GLubyte)(i%2)*255;
texSourIma[i][j][3]=(GLubyte)255;
}
}
glGenTextures(1,&pTexture);
glBindTexture(GL_TEXTURE_2D,pTexture);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,imgWid,imgHei,0,GL_RGBA,GL_UNSIGNED_BYTE,texSourIma);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
cameraProjection.setPerspective(30.0, 1, 0.1, 900.0);
cameraView.setLookAt(cameraPosition,MyVector3D_f(0,0,0),MyVector3D_f(0,1,0));
}
void init(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor (1.0, 1.0, 1.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
initTexture();
}
void drawAxis()
{
glPushMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);
glDisable(GL_LIGHTING);
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(100,0,0);
glEnd();
// glColor3f(0,0,0);
// glBegin(GL_LINES);
// glVertex3d(0,0,0);
// glVertex3d(0,100,0);
// glEnd();
glColor3f(0,0,1);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,100);
glEnd();
glEnable(GL_LINE_STIPPLE);
glLineStipple (3, 0x1111);
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(-100,0,0);
glEnd();
// glColor3f(0,0,0);
// glBegin(GL_LINES);
// glVertex3d(0,0,0);
// glVertex3d(0,-100,0);
// glEnd();
glColor3f(0,0,1);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,-100);
glEnd();
glDisable(GL_LINE_STIPPLE);
glPointSize(5.0f);
glEnable(GL_LIGHTING);
glPopAttrib();
glPopMatrix();
}
void scene()
{
glPushMatrix();
glTranslated(0,-10,0);
glColor4f(0.3f,0.3f,0.3f,1);
glBegin(GL_QUADS);
glVertex3f(-15,2,-15);
glVertex3f(-15,2, 15);
glVertex3f( 15,2, 15);
glVertex3f( 15,2,-15);
glEnd();
glPopMatrix();
glColor3f(1.0,0.5,0.0);
glutSolidTeapot(5);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
//gluLookAt (25.0,35.0,5.0, 0.0, 0.0, 0.0, 0.0, 1.0,0.0);
GLfloat SPlane[]={1, 0, 0, 0};
GLfloat TPlane[]={0, 1, 0, 0};
GLfloat RPlane[]={0, 0, 1, 0};
GLfloat QPlane[]={0, 0, 0, 1};
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_Q);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
//坐标设置,这里也可以把纹理矩阵textMat的列分别传入STRQ方向,效果一样
glTexGenfv(GL_S, GL_EYE_PLANE, SPlane);
glTexGenfv(GL_T, GL_EYE_PLANE, TPlane);
glTexGenfv(GL_R, GL_EYE_PLANE, RPlane);
glTexGenfv(GL_Q, GL_EYE_PLANE, QPlane);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, pTexture);
MyMatrix4X4_f bias(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
MyMatrix4X4_f textMat=bias*cameraProjection*cameraView;
glMatrixMode(GL_TEXTURE);//纹理矩阵设置
glPushMatrix();
glLoadMatrixf(textMat.m);
// glPopMatrix();//如果不注释掉,结果是效果图2
glMatrixMode(GL_MODELVIEW);
gluLookAt (25.0,35.0,25.0, 0.0, 0.0, 0.0, 0.0, 1.0,0.0);
scene();
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);
drawAxis();
glPopMatrix();
glFlush();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 0.1, 900.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB |GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
win=glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
正确效果图1错误效果图2