纹理矩阵
使用纹理矩阵实现之前的广告牌效果
取消之前cpu的顶点uv赋值
直接使用纹理矩阵操作
glMatrixMode(GL_TEXTURE);
glTranslatef(0,0.001f,0);
#include <windows.h>
#include <tchar.h>
#include <math.h>
#include "FreeImage.h"
#include "CELLMath.hpp"
#include "OpenGLWindow.h"
#include <gl/GLU.h>
#include <vector>
using namespace CELL;
struct Vertex
{
float x, y, z;
float u, v;
};
class SamplerTexture :public OpenGLWindow
{
GLuint _texture;
float _textureV;
public:
SamplerTexture()
{
_textureV=0;
}
virtual void onShutdownGL()
{
glDeleteTextures(1,&_texture);
}
virtual FIBITMAP*readImage(const char* fileName)
{
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0);
if (fifmt == FIF_UNKNOWN)
{
return 0;
}
FIBITMAP *dib = FreeImage_Load(fifmt, fileName,0);
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
FIBITMAP* temp = dib;
dib = FreeImage_ConvertTo32Bits(dib);
FreeImage_Unload(temp);
BYTE* pixels = (BYTE*)FreeImage_GetBits(dib);
int width = FreeImage_GetWidth(dib);
int height = FreeImage_GetHeight(dib);
for (int i = 0 ;i < width * height * 4 ; i+=4 )
{
BYTE temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}
return dib;
}
virtual void onInitGL()
{
FIBITMAP* arrayImage[1];
arrayImage[0] = readImage( "1.png" );
glGenTextures(1,&_texture);
glBindTexture(GL_TEXTURE_2D,_texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
BYTE* pixels = (BYTE*)FreeImage_GetBits(arrayImage[0]);
int width = FreeImage_GetWidth(arrayImage[0]);
int height = FreeImage_GetHeight(arrayImage[0]);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,GL_RGBA, GL_UNSIGNED_BYTE, pixels);
FreeImage_Unload(arrayImage[0]);
}
virtual void render()
{
#define M_PI (3.14159265358979323846)
//! 指定以下的操作针对投影矩阵
glMatrixMode(GL_PROJECTION);
//! 将投影举证清空成单位矩阵
glLoadIdentity();
gluPerspective(60,double(_width)/double(_height),0.1,1000);
_textureV += 0.001f;
Vertex cubeVertices[] =
{
{
-1.0f,-0.125f, 1.0f ,0, 0 },
{
1.0f,-0.125f, 1.0f ,1, 0 },
{
1.0f, 0.125f, 1.0f ,1, 0.25f },
{
-1.0f, 0.125f, 1.0f ,0, 0.25f },
};
glMatrixMode(GL_TEXTURE);
glTranslatef(0,0.001f,0);
glMatrixMode(GL_MODELVIEW