📄 texturedquad.cpp
字号:
#include "objects/texturedQuad.h"
#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
using namespace std;
TexturedQuad::TexturedQuad(float x, float y, float z, float rotx, float roty, float rotz):
Rendereable(x,y,z,rotx,roty,rotz)
{
SDL_Surface* surface;
GLenum texture_format;
GLint nOfColors;
string bitmapName = "resources/sample.bmp";
surface = SDL_LoadBMP(bitmapName.c_str());
if (surface == NULL) {
cerr << "No se puede cargar el bitmap: "<< bitmapName.c_str() << endl;
exit( 0 );
}
// Verificar si el ancho es una potencia de dos
if ( (surface->w & (surface->w - 1)) != 0 ) {
cerr << "Atencion: " << bitmapName.c_str() << " no posee ancho en potencias de dos" << endl;
}
// Tambien verificar si el alto es una potencia de dos
if ( (surface->h & (surface->h - 1)) != 0 ) {
cerr << "Atencion: " << bitmapName.c_str() << " no posee alto en potencias de dos" << endl;
}
//n鷐ero bytes colores por pixel
nOfColors = surface->format->BytesPerPixel;
if (nOfColors == 4) // tiene alfa
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
else if (nOfColors == 3) // no tiene alfa
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
else
{
cerr << "Atencion: la imagen no esta en truecolor. El archivo puede estar da馻do" << endl;
// hay que prestar atenci髇 a este error y tomar una descic髇 acorde
texture_format = GL_RGB;
}
//habilito las texturas
glEnable(GL_TEXTURE_2D);
//genero un nuevo identificador de texturas
glGenTextures( 1, &this->textureId );
//indico que textura es la seleccionada
glBindTexture( GL_TEXTURE_2D, this->textureId );
// Set the texture's stretching properties
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//cargo la textura
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
texture_format, GL_UNSIGNED_BYTE, surface->pixels );
//deshabilito las texturas
glDisable(GL_TEXTURE_2D);
if ( surface ) {
SDL_FreeSurface( surface );
}
}
TexturedQuad::~TexturedQuad()
{}
void TexturedQuad::draw()
{
glPushMatrix();
glTranslatef( this->x, this->y, this->z );
if( this->rotx != 0 )
{
glRotatef( this->rotx, 1.f,0.f,0.f );
}
if( this->roty != 0 )
{
glRotatef( this->roty, 0.f,1.f,0.f );
}
if( this->rotz != 0 )
{
glRotatef( this->rotz, 0.f,0.f,1.f );
}
glColor3f( 1.f,1.f,1.f );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, this->textureId );
glBegin( GL_QUADS );
//Top-left vertex (corner)
glTexCoord2i( 1, 0 );
glVertex3f( -1.4f, 1.f, 0.f );
//Bottom-left vertex (corner)
glTexCoord2i( 1, 1 );
glVertex3f( -1.4f, -1.f, 0.f );
//Bottom-right vertex (corner)
glTexCoord2i( 0, 1 );
glVertex3f( 1.4f, -1.f, 0.f );
//Top-right vertex (corner)
glTexCoord2i( 0, 0 );
glVertex3f( 1.4f, 1.f, 0.f );
glEnd();
glDisable( GL_TEXTURE_2D );
glPopMatrix();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -