Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Drawing Function:
- void DrawGeo::DrawTexture(){
- glBindTexture(GL_TEXTURE_2D, tex->textureID);
- glBegin(GL_QUADS);
- glColor3f(1,1,1);
- glTexCoord2f(5,5);
- glVertex3f( -5,-10, -5);
- glTexCoord2f(-5,5);
- glVertex3f( -5, -10, 5);
- glTexCoord2f(-5,-5);
- glVertex3f( 5, -10, 5);
- glTexCoord2f(5,-5);
- glVertex3f( 5,-10, -5);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- //Texture Header:
- #pragma once
- #include <iostream>
- class Texture{
- private:
- public:
- unsigned int textureID;
- Texture(void* data, int w, int h, int format);
- static Texture* loadBMP(const char* filename);
- };
- //Texture CPP:
- #define _CRT_SECURE_NO_DEPRECATE
- #include "Texture.h"
- #include <glut.h>
- #include <stdio.h>
- using namespace std;
- Texture::Texture(void* data, int w, int h, int format){
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, format, GL_UNSIGNED_BYTE, data);
- 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); //Can swap linear for something else to minecraftify: GL_NEAREST
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //GL_LINEAR
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- Texture* Texture::loadBMP(const char* filename){
- //http://en.wikipedia.org/wiki/BMP_file_format
- FILE* fp;
- fp = fopen(filename, "r");
- if (!fp){
- cout << "\""<< filename << "\" Could not be opened" << endl;
- fclose(fp);
- return NULL;
- }
- char* headerField = new char[3];
- headerField[2] = 0;
- fread(headerField, 2, sizeof(char), fp);
- if (strcmp(headerField, "BM")){
- delete [] headerField;
- cout << "File isn't a bitmap" << endl;
- fclose(fp);
- return NULL;
- }
- unsigned int bmpDataLocation, bmpWidth, bmpHeight;
- unsigned short numColourPlanes, bitsPerPixel;
- unsigned int compressionMethod, bmpDataSize;
- fseek(fp, 0x000a, SEEK_SET);
- fread(&bmpDataLocation, 1, sizeof(unsigned int), fp);
- fseek(fp, 0x0012, SEEK_SET);
- fread(&bmpWidth, 1, sizeof(unsigned int), fp);
- fread(&bmpHeight, 1, sizeof(unsigned int), fp);
- fread(&numColourPlanes, 1, sizeof(unsigned short), fp);
- fread(&bitsPerPixel, 1, sizeof(unsigned short), fp);
- fread(&compressionMethod, 1, sizeof(unsigned int), fp);
- fread(&bmpDataSize, 1, sizeof(unsigned int), fp);
- if (numColourPlanes != 1 || bitsPerPixel != 24 || compressionMethod != 0){
- delete [] headerField;
- cout << "File isn't a raw bmp24" << endl;
- fclose(fp);
- return NULL;
- }
- unsigned char* bmpData = new unsigned char[bmpDataSize];
- fseek(fp, bmpDataLocation, SEEK_SET);
- fread(bmpData, bmpDataSize, sizeof(unsigned char), fp);
- fclose(fp);
- return new Texture(bmpData, bmpWidth, bmpHeight, GL_RGB);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement