Created
October 31, 2011 02:05
-
-
Save davidwparker/1326745 to your computer and use it in GitHub Desktop.
OpenGL Screencast 14: textures part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| /* | |
| * main() | |
| * ---- | |
| * Start up GLUT and tell it what to do | |
| */ | |
| int main(int argc,char* argv[]) | |
| { | |
| initializeGlobals(); | |
| /* screencast specific variables */ | |
| windowName = "OpenGL screenscasts 14: Textures part 1"; | |
| screencastID = 14; | |
| dim = 4; | |
| fov = 50; | |
| th = -25; | |
| ph = 15; | |
| ecX = -0.4; | |
| ecZ = -2.0; | |
| distance = 2; | |
| glutInit(&argc,argv); | |
| glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); | |
| glutInitWindowSize(windowWidth,windowHeight); | |
| glutCreateWindow(windowName); | |
| glutDisplayFunc(display); | |
| glutReshapeFunc(displayReshape); | |
| glutKeyboardFunc(windowKey); | |
| glutSpecialFunc(windowSpecial); | |
| initializeTextures(); | |
| redisplayAll(); | |
| glutMainLoop(); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void drawScreencast14(void) | |
| { | |
| drawLight(); | |
| currentTexture = textures[TEX_CRATE]; | |
| cube(0,0,0, 1,1,1, 0); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* TEXTURES */ | |
| unsigned int textures[7]; | |
| int currentTexture=TEX_DEFAULT; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void initializeTextures(void) | |
| { | |
| /* | |
| TEX_DEFAULT 0 | |
| TEX_BRICK 1 | |
| TEX_CRATE 2 | |
| TEX_ICE 3 | |
| TEX_FIRE 4 | |
| TEX_EARTH 5 | |
| TEX_WOOD 6 | |
| */ | |
| textures[TEX_BRICK] = loadTexBMP("txBrick14.bmp"); | |
| textures[TEX_CRATE] = loadTexBMP("txCrate.bmp"); | |
| textures[TEX_ICE] = loadTexBMP("txIce7.bmp"); | |
| textures[TEX_FIRE] = loadTexBMP("txLava1.bmp"); | |
| textures[TEX_EARTH] = loadTexBMP("txRock5.bmp"); | |
| textures[TEX_WOOD] = loadTexBMP("txWood3.bmp"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Target to build | |
| #TARGET = 011 012 013 014 | |
| TARGET = 014 | |
| #EXECS = ./011 ./012 ./013 ./014 | |
| EXECS = ./executables/014 | |
| # Libraries - LINUX | |
| #LIBS=-lglut -lGLU | |
| # Libraries - OSX | |
| LIBS=-framework OpenGL -framework GLUT | |
| all: $(TARGET) | |
| # Generic compile rules | |
| .c.o: | |
| gcc -c -g -O -Wall $< | |
| # Generic compile and link | |
| %: %.c screencasts.a | |
| gcc -Wall -O3 -o ./executables/$@ $^ $(LIBS) | |
| clean: | |
| rm -f $(EXECS) *.o *.a | |
| # without .h => globals.o | |
| screencasts.a:globals.o print.o error.o fatal.o textures.o shapes.o models.o interaction.o initialization.o draw.o display.o | |
| ar -rcs screencasts.a $^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void cube(double x,double y,double z, | |
| double dx,double dy,double dz, | |
| double th) | |
| { | |
| /* Cube vertices */ | |
| GLfloat vertA[3] = { 0.5, 0.5, 0.5}; | |
| GLfloat vertB[3] = {-0.5, 0.5, 0.5}; | |
| GLfloat vertC[3] = {-0.5,-0.5, 0.5}; | |
| GLfloat vertD[3] = { 0.5,-0.5, 0.5}; | |
| GLfloat vertE[3] = { 0.5, 0.5,-0.5}; | |
| GLfloat vertF[3] = {-0.5, 0.5,-0.5}; | |
| GLfloat vertG[3] = {-0.5,-0.5,-0.5}; | |
| GLfloat vertH[3] = { 0.5,-0.5,-0.5}; | |
| glPushMatrix(); | |
| /* Transform */ | |
| glTranslated(x,y,z); | |
| glRotated(th,0,1,0); | |
| glScaled(dx,dy,dz); | |
| glEnable(GL_TEXTURE_2D); | |
| /* using the current texture */ | |
| glBindTexture(GL_TEXTURE_2D,currentTexture); | |
| /* Cube */ | |
| glBegin(GL_QUADS); | |
| /* front => ABCD yellow */ | |
| glNormal3f(0,0,1); | |
| if (screencastID < 14) | |
| glColor3f(1.0,1.0,0.0); | |
| glTexCoord2f(0,0); glVertex3fv(vertA); | |
| glTexCoord2f(1,0); glVertex3fv(vertB); | |
| glTexCoord2f(1,1); glVertex3fv(vertC); | |
| glTexCoord2f(0,1); glVertex3fv(vertD); | |
| glEnd(); | |
| /* back => FEHG red */ | |
| glBegin(GL_QUADS); | |
| glNormal3f(0,0,-1); | |
| if (screencastID < 14) | |
| glColor3f(1.0,0.0,0.0); | |
| glTexCoord2f(0,0); glVertex3fv(vertF); | |
| glTexCoord2f(1,0); glVertex3fv(vertE); | |
| glTexCoord2f(1,1); glVertex3fv(vertH); | |
| glTexCoord2f(0,1); glVertex3fv(vertG); | |
| //glEnd(); | |
| //glBegin(GL_QUADS); | |
| /* right => EADH green */ | |
| glNormal3f(1,0,0); | |
| //if (screencastID < 14) | |
| glColor3f(0.0,1.0,0.0); | |
| glTexCoord2f(0,0); glVertex3fv(vertE); | |
| glTexCoord2f(1,0); glVertex3fv(vertA); | |
| glTexCoord2f(1,1); glVertex3fv(vertD); | |
| glTexCoord2f(0,1); glVertex3fv(vertH); | |
| glEnd(); | |
| glDisable(GL_TEXTURE_2D); | |
| glBegin(GL_QUADS); | |
| /* left => BFGC blue */ | |
| glNormal3f(-1,0,0); | |
| if (screencastID < 14) | |
| glColor3f(0.0,0.0,1.0); | |
| glVertex3fv(vertB); | |
| glVertex3fv(vertF); | |
| glVertex3fv(vertG); | |
| glVertex3fv(vertC); | |
| /* top => EFBA turquoise */ | |
| glNormal3f(0,1,0); | |
| if (screencastID < 14) | |
| glColor3f(0.0,1.0,1.0); | |
| glVertex3fv(vertE); | |
| glVertex3fv(vertF); | |
| glVertex3fv(vertB); | |
| glVertex3fv(vertA); | |
| /* bottom => DCGH pink */ | |
| glNormal3f(0,-1,0); | |
| if (screencastID < 14) | |
| glColor3f(1.0,0.0,1.0); | |
| glVertex3fv(vertD); | |
| glVertex3fv(vertC); | |
| glVertex3fv(vertG); | |
| glVertex3fv(vertH); | |
| glEnd(); | |
| glPopMatrix(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| /* | |
| * Reverse n bytes | |
| */ | |
| static void reverseBytes(void* x,const int n) | |
| { | |
| int k; | |
| char* ch = (char*)x; | |
| for (k=0;k<n/2;k++) | |
| { | |
| char tmp = ch[k]; | |
| ch[k] = ch[n-1-k]; | |
| ch[n-1-k] = tmp; | |
| } | |
| } | |
| /* | |
| * Load texture from BMP file | |
| */ | |
| unsigned int loadTexBMP(char* file) | |
| { | |
| unsigned int texture; /* Texture name */ | |
| FILE* f; /* File pointer */ | |
| unsigned short magic; /* Image magic */ | |
| unsigned int dx,dy,size; /* Image dimensions */ | |
| unsigned short nbp,bpp; /* Planes and bits per pixel */ | |
| unsigned char* image; /* Image data */ | |
| unsigned int k; /* Counter */ | |
| /* Open file */ | |
| f = fopen(file,"rb"); | |
| if (!f) fatal("Cannot open file %s\n",file); | |
| /* Check image magic */ | |
| if (fread(&magic,2,1,f)!=1) fatal("Cannot read magic from %s\n",file); | |
| if (magic!=0x4D42 && magic!=0x424D) fatal("Image magic not BMP in %s\n",file); | |
| /* Seek to and read header */ | |
| if (fseek(f,16,SEEK_CUR) || fread(&dx ,4,1,f)!=1 || fread(&dy ,4,1,f)!=1 || | |
| fread(&nbp,2,1,f)!=1 || fread(&bpp,2,1,f)!=1 || fread(&k,4,1,f)!=1) | |
| fatal("Cannot read header from %s\n",file); | |
| /* Reverse bytes on big endian hardware (detected by backwards magic) */ | |
| if (magic==0x424D) { | |
| reverseBytes(&dx,4); | |
| reverseBytes(&dy,4); | |
| reverseBytes(&nbp,2); | |
| reverseBytes(&bpp,2); | |
| reverseBytes(&k,4); | |
| } | |
| /* Check image parameters */ | |
| if (dx<1 || dx>65536) fatal("%s image width out of range: %d\n",file,dx); | |
| if (dy<1 || dy>65536) fatal("%s image height out of range: %d\n",file,dy); | |
| if (nbp!=1) fatal("%s bit planes is not 1: %d\n",file,nbp); | |
| if (bpp!=24) fatal("%s bits per pixel is not 24: %d\n",file,bpp); | |
| if (k!=0) fatal("%s compressed files not supported\n",file); | |
| #ifndef GL_VERSION_2_0 | |
| /* OpenGL 2.0 lifts the restriction that texture size must be a power of two */ | |
| for (k=1;k<dx;k*=2); | |
| if (k!=dx) fatal("%s image width not a power of two: %d\n",file,dx); | |
| for (k=1;k<dy;k*=2); | |
| if (k!=dy) fatal("%s image height not a power of two: %d\n",file,dy); | |
| #endif | |
| /* Allocate image memory */ | |
| size = 3*dx*dy; | |
| image = (unsigned char*) malloc(size); | |
| if (!image) fatal("Cannot allocate %d bytes of memory for image %s\n",size,file); | |
| /* Seek to and read image */ | |
| if (fseek(f,20,SEEK_CUR) || fread(image,size,1,f)!=1) | |
| fatal("Error reading data from image %s\n",file); | |
| fclose(f); | |
| /* Reverse colors (BGR -> RGB) */ | |
| for (k=0;k<size;k+=3) { | |
| unsigned char temp = image[k]; | |
| image[k] = image[k+2]; | |
| image[k+2] = temp; | |
| } | |
| /* Sanity check */ | |
| errCheck("loadTexBMP"); | |
| /* Generate 2D texture */ | |
| glGenTextures(1,&texture); | |
| glBindTexture(GL_TEXTURE_2D,texture); | |
| /* Copy image */ | |
| glTexImage2D(GL_TEXTURE_2D,0,3,dx,dy,0,GL_RGB,GL_UNSIGNED_BYTE,image); | |
| if (glGetError()) fatal("Error in glTexImage2D %s %dx%d\n",file,dx,dy); | |
| /* Scale linearly when image size doesn't match */ | |
| glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); | |
| glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); | |
| /* Free image memory */ | |
| free(image); | |
| /* Return texture name */ | |
| return texture; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment