- #include <windows.h>
- #include <gl/gl.h>
- #include <gl/glu.h>
- #include <gl/glut.h>
- #include <math.h>
- #include <iostream.h>
- unsigned char tex1[128*128*3];
- GLuint TexID;
- int typ=1;
- int mode=GL_POLYGON;
- GLfloat ul [3]={ -5.0f , -2.5f , -15.0f }; // unten links
- GLfloat uml[3]={ -2.5f , -2.5f , -15.0f }; // unten mitte links
- GLfloat um [3]={ 0.0f , -2.5f , -15.0f }; // unten mitte
- GLfloat umr[3]={ 2.5f , -2.5f , -15.0f }; // unten mitte rechts
- GLfloat ur [3]={ 5.0f , -2.5f , -15.0f }; // unten rechts
- GLfloat mm [3]={ 0.0f , 0.8f , -15.0f }; // schnittpunkt der diagonalen (ca.)
- GLfloat ol [3]={ -2.5f , 2.5f , -15.0f }; // oben links
- GLfloat or [3]={ 2.5f , 2.5f , -15.0f }; // oben rechts
- // Light values and coordinates
- GLfloat Light0Pos[] = { -100.0f, 50.0f, 0.0f, 1.0f };
- GLfloat Light0Ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
- GLfloat Light0Diffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
- GLfloat Light0Specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
- GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- /////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////// R E N D E R S C E N E /////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////
- // Called to draw scene
- void RenderScene(void){
- glColor3ub(255,255,255);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the window
- glPushMatrix();
- glColor3ub(255,255,255);
- glNormal3f( 0.0f , 0.0f , 1.0f ); // Reflektions-Vektor
- glBindTexture( GL_TEXTURE_2D, TexID );
- if (mode==GL_POLYGON){ // Texturen auf Lieneien sind keine gute idee^^
- glEnable(GL_TEXTURE_2D);
- }
- if(typ==1){
- glBegin(mode); //schräg gelegtes viereck als referenz
- glTexCoord2f(0,0);
- glVertex3f( -5.0f , -2.5f , -15.0f );
- glTexCoord2f(1,0);
- glVertex3f( 5.0f , -2.5f , -15.0f );
- glTexCoord2f(1,1);
- glVertex3f( 5.0f , 5.0f , -30.0f );
- glTexCoord2f(0,1);
- glVertex3f( -5.0f , 5.0f , -30.0f );
- glEnd();
- }
- if(typ==2){
- glBegin(mode); //normales trapez
- glTexCoord2f(0,0);
- glVertex3fv(ul);
- glTexCoord2f(1,0);
- glVertex3fv(ur);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glEnd();
- }
- if (typ==3){ // trapez aus 3 dreiecken
- glBegin(mode);
- glTexCoord2f(0,0);
- glVertex3fv(ul);
- glTexCoord2f(0.5,0);
- glVertex3fv(um);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glEnd();
- glBegin(mode);
- glTexCoord2f(0.5,0);
- glVertex3fv(um);
- glTexCoord2f(1,0);
- glVertex3fv(ur);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glEnd();
- glBegin(mode);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glTexCoord2f(0.5,0);
- glVertex3fv(um);
- glEnd();
- }
- if (typ==4){ //paralellogramm mit dreieck
- glBegin(mode);
- glTexCoord2f(0,0);
- glVertex3fv(ul);
- glTexCoord2f(0.5,0);
- glVertex3fv(um);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glEnd();
- glBegin(mode);
- glTexCoord2f(0.5,0);
- glVertex3fv(um);
- glTexCoord2f(1,0);
- glVertex3fv(ur);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glEnd();
- }
- if (typ==5){ //ein viereck mt je einem dreieck links und rechts
- glBegin(mode);
- glTexCoord2f(0,0);
- glVertex3fv(ul);
- glTexCoord2f(0.25,0);
- glVertex3fv(uml);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glEnd();
- glBegin(mode);
- glTexCoord2f(0.25,0);
- glVertex3fv(uml);
- glTexCoord2f(0.75,0);
- glVertex3fv(umr);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glEnd();
- glBegin(mode);
- glTexCoord2f(0.75,0);
- glVertex3fv(umr);
- glTexCoord2f(1,0);
- glVertex3fv(ur);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glEnd();
- }
- if (typ==6){ //trapez aus 2 dreiecken
- glBegin(mode);
- glTexCoord2f(0,0);
- glVertex3fv(ul);
- glTexCoord2f(1,0);
- glVertex3fv(ur);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glEnd();
- glBegin(mode);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glTexCoord2f(0,0);
- glVertex3fv(ul);
- glEnd();
- }
- if (typ==7){ //trapez aus 4 dreiecken
- glBegin(mode);
- glTexCoord2f(0,0);
- glVertex3fv(ul);
- glTexCoord2f(1,0);
- glVertex3fv(ur);
- glTexCoord2f(0.5,0.5);
- glVertex3fv(mm);
- glEnd();
- glBegin(mode);
- glTexCoord2f(1,0);
- glVertex3fv(ur);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glTexCoord2f(0.5,0.5);
- glVertex3fv(mm);
- glEnd();
- glBegin(mode);
- glTexCoord2f(1,1);
- glVertex3fv(or);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glTexCoord2f(0.5,0.5);
- glVertex3fv(mm);
- glEnd();
- glBegin(mode);
- glTexCoord2f(0,1);
- glVertex3fv(ol);
- glTexCoord2f(0,0);
- glVertex3fv(ul);
- glTexCoord2f(0.5,0.5);
- glVertex3fv(mm);
- glEnd();
- }
- glDisable(GL_TEXTURE_2D);
- glPopMatrix();
- glutSwapBuffers();
- }
- void SetupRC(){
- glGenTextures(1, &TexID);
- FILE *in;
- int OK;
- in=fopen("test.raw","rb");
- OK=fread(tex1,128*128*3,1,in);
- fclose(in);
- glBindTexture(GL_TEXTURE_2D, TexID);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
- glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
- glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128,0, GL_RGB, GL_UNSIGNED_BYTE, tex1);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // Black background
- glEnable(GL_DEPTH_TEST); // Enable Depth Testing // Hidden surface removal
- glEnable(GL_LIGHTING); // Enable lighting
- glEnable(GL_COLOR_MATERIAL); // Enable color tracking
- // Setup and enable light 0
- glLightfv(GL_LIGHT0,GL_AMBIENT,Light0Ambient);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,Light0Diffuse);
- glLightfv(GL_LIGHT0,GL_SPECULAR,Light0Specular);
- glEnable(GL_LIGHT0);
- // All materials hereafter have full specular reflectivity
- // with a high shine
- glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
- glMateriali(GL_FRONT,GL_SHININESS,128);
- glFrontFace(GL_CCW); // Counter clock-wise polygons face out
- glEnable(GL_CULL_FACE); // Do not calculate inside of jet
- glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); // Set Material properties to follow glColor values
- glColor3ub(0, 0, 255); // Set drawing color to blue
- }
- void ChangeSize(int w, int h){
- glViewport(0, 0, w, h); // Set Viewport to window dimensions
- // Reset projection matrix stack
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0f,(GLfloat)w/(GLfloat)h,1.0f, 1000.0f);
- // Reset Texture Model view matrix stack
- glMatrixMode(GL_TEXTURE);
- glLoadIdentity;
- // Reset Model view matrix stack
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glLightfv(GL_LIGHT0,GL_POSITION,Light0Pos);
- }
- void NormalKeys(unsigned char key, int x, int y){
- if(key == '1'){
- typ=1;
- }
- if(key == '2'){
- typ=2;
- }
- if(key == '3'){
- typ=3;
- }
- if(key == '4'){
- typ=4;
- }
- if(key == '5'){
- typ=5;
- }
- if(key == '6'){
- typ=6;
- }
- if(key == '7'){
- typ=7;
- }
- if(key == 27){ // ESC-taste
- exit(0);
- }
- if(key == ' '){
- if (mode==GL_POLYGON){mode=GL_LINE_LOOP;}else{mode=GL_POLYGON;}
- }
- glutPostRedisplay(); // Refresh the Window
- }
- void MenueFunktion(int was){
- if (was==99){exit(0);}
- glutPostRedisplay();
- }
- int main(int argc, char* argv[]){
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutCreateWindow("Texturen Test");
- // glutGameModeString("640x480:16@60"); //für neuere glut versionen...
- // glutEnterGameMode();
- // glutFullScreen(); //für ältere glut versionen
- glutCreateMenu(MenueFunktion);
- glutAddMenuEntry("<ESC> Beenden",99);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- glutReshapeFunc(ChangeSize);
- glutKeyboardFunc(NormalKeys);
- glutDisplayFunc(RenderScene);
- SetupRC();
- glutMainLoop();
- return 0;
- }