Advertisement
Guest User

GLUT Problem

a guest
Apr 14th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. //Drawing Function:
  2.  
  3. void DrawGeo::DrawTexture(){
  4.     glBindTexture(GL_TEXTURE_2D, tex->textureID);
  5.     glBegin(GL_QUADS);
  6.         glColor3f(1,1,1);
  7.  
  8.         glTexCoord2f(5,5);
  9.         glVertex3f(   -5,-10,  -5);
  10.  
  11.         glTexCoord2f(-5,5);
  12.         glVertex3f(    -5, -10, 5);
  13.  
  14.         glTexCoord2f(-5,-5);
  15.         glVertex3f(    5, -10,  5);
  16.  
  17.         glTexCoord2f(5,-5);
  18.         glVertex3f(   5,-10,   -5);
  19.     glEnd();
  20.     glBindTexture(GL_TEXTURE_2D, 0);
  21. }
  22.  
  23.  
  24. //Texture Header:
  25.  
  26. #pragma once
  27. #include <iostream>
  28. class Texture{
  29. private:
  30.    
  31.  
  32. public:
  33.     unsigned int textureID;
  34.     Texture(void* data, int w, int h, int format);
  35.    
  36.     static Texture* loadBMP(const char* filename);
  37. };
  38.  
  39.  
  40.  
  41. //Texture CPP:
  42.  
  43.  
  44.  
  45. #define _CRT_SECURE_NO_DEPRECATE
  46. #include "Texture.h"
  47. #include <glut.h>
  48. #include <stdio.h>
  49. using namespace std;
  50.  
  51. Texture::Texture(void* data, int w, int h, int format){
  52.     glGenTextures(1, &textureID);
  53.     glBindTexture(GL_TEXTURE_2D, textureID);
  54.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, format, GL_UNSIGNED_BYTE, data);
  55.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  56.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  57.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //Can swap linear for something else to minecraftify: GL_NEAREST
  58.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //GL_LINEAR
  59.     glBindTexture(GL_TEXTURE_2D, 0);
  60. }
  61.  
  62.  
  63. Texture* Texture::loadBMP(const char* filename){
  64.     //http://en.wikipedia.org/wiki/BMP_file_format
  65.  
  66.  
  67.     FILE* fp;
  68.     fp = fopen(filename, "r");
  69.     if (!fp){
  70.         cout << "\""<< filename << "\" Could not be opened" << endl;
  71.         fclose(fp);
  72.         return NULL;
  73.     }
  74.  
  75.     char* headerField = new char[3];
  76.     headerField[2] = 0;
  77.     fread(headerField, 2, sizeof(char), fp);
  78.     if (strcmp(headerField, "BM")){
  79.         delete [] headerField;
  80.         cout << "File isn't a bitmap" << endl;
  81.         fclose(fp);
  82.         return NULL;
  83.     }
  84.  
  85.     unsigned int bmpDataLocation, bmpWidth, bmpHeight;
  86.     unsigned short numColourPlanes, bitsPerPixel;
  87.     unsigned int compressionMethod, bmpDataSize;
  88.  
  89.     fseek(fp, 0x000a, SEEK_SET);
  90.     fread(&bmpDataLocation, 1, sizeof(unsigned int), fp);
  91.  
  92.     fseek(fp, 0x0012, SEEK_SET);
  93.     fread(&bmpWidth, 1, sizeof(unsigned int), fp);
  94.     fread(&bmpHeight, 1, sizeof(unsigned int), fp);
  95.     fread(&numColourPlanes, 1, sizeof(unsigned short), fp);
  96.     fread(&bitsPerPixel, 1, sizeof(unsigned short), fp);
  97.     fread(&compressionMethod, 1, sizeof(unsigned int), fp);
  98.     fread(&bmpDataSize, 1, sizeof(unsigned int), fp);
  99.     if (numColourPlanes != 1 || bitsPerPixel != 24 || compressionMethod != 0){
  100.         delete [] headerField;
  101.         cout << "File isn't a raw bmp24" << endl;
  102.         fclose(fp);
  103.         return NULL;
  104.     }
  105.  
  106.     unsigned char* bmpData = new unsigned char[bmpDataSize];
  107.     fseek(fp, bmpDataLocation, SEEK_SET);
  108.     fread(bmpData, bmpDataSize, sizeof(unsigned char), fp);
  109.  
  110.     fclose(fp);
  111.  
  112.     return new Texture(bmpData, bmpWidth, bmpHeight, GL_RGB);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement