/ Published in: C++
simple openGL script that makes a texture plate out of a text file.
Expand |
Embed | Plain Text
#include <string> #include <fstream> #include <cmath> #include <Windows.h> #include <glut.h> #include <GL\GLU.h> using namespace std; const int dimensions = 14; int texture[10][dimensions*2][dimensions*2]; char Ctexture[dimensions][dimensions]; float R[] = {0, 1, 0, 1, 1, 0, .5, 1, .9, .5}; //black, white, blue, red, yellow, green, purple, pink, orange, gray float G[] = {0, 1, 0, 0, 1, 1, 0, 0, .5, .5}; // 0 1 2 3 4 5 6 7 8 9 float B[] = {0, 1, 1, 0, 0, 0, .5, .8, 0, .5}; float plateSize = 1; void load_plate(const char *_filename, int ID){ ifstream myFile; myFile.open(_filename); string fileLine[dimensions]; int i = 0; while(myFile.good()){ getline(myFile, fileLine[i]); i++; } for (int a = 0; a < dimensions; a++) { int e = 0; for (int b = 0; b < dimensions*2-1; b+=2) { Ctexture[a][e] = fileLine[a].at(b); e++; } } for (int a = 0; a < dimensions; a++) { for (int b = 0; b < dimensions; b++) { if (Ctexture[a][b] == '0') { texture[ID][a][b] = 0;} else if (Ctexture[a][b] == '1') { texture[ID][a][b] = 1;} else if (Ctexture[a][b] == '2') { texture[ID][a][b] = 2;} else if (Ctexture[a][b] == '3') { texture[ID][a][b] = 3;} else if (Ctexture[a][b] == '4') { texture[ID][a][b] = 4;} else if (Ctexture[a][b] == '5') { texture[ID][a][b] = 5;} else if (Ctexture[a][b] == '6') { texture[ID][a][b] = 6;} else if (Ctexture[a][b] == '7') { texture[ID][a][b] = 7;} else if (Ctexture[a][b] == '8') { texture[ID][a][b] = 8;} else if (Ctexture[a][b] == '9') { texture[ID][a][b] = 9;} } } myFile.close(); } void drawSquare(float x, float y, float z) { glPushMatrix(); glBegin(GL_QUADS); glVertex3f(x, y, z); glVertex3f(x, y+plateSize/dimensions, z); glVertex3f(x+plateSize/dimensions, y+plateSize/dimensions, z); glVertex3f(x+plateSize/dimensions, y, z); glEnd(); glPopMatrix(); } void Reshape(int height, int width) { glClearColor(0, 0, 0, 1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60, (float)height/(float)width, 1, 100); glMatrixMode(GL_MODELVIEW); } void drawPlate(float x, float y, float z, int ID) { for (int i = 0; i < dimensions; i++) { for (int j = 0; j < dimensions; j++) { glColor3d(R[texture[ID][i][j]], G[texture[ID][i][j]], B[texture[ID][i][j]]); drawSquare(x+(j*(plateSize/dimensions)), y+(i*(plateSize/dimensions)), z); } } } void Display(void){ glClear(GL_COLOR_BUFFER_BIT); load_plate("texture1.txt", 1); drawPlate(0, 0, -4, 1); glutSwapBuffers(); } void Keyboard(unsigned char key, int x, int y) { switch(key){ case'a': plateSize += .05; glutPostRedisplay(); break; case's': plateSize -= .05; glutPostRedisplay(); break; } } int main(int argc, char **argv){ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Texture plate tests"); glutReshapeFunc(Reshape); glutDisplayFunc(Display); glutKeyboardFunc(Keyboard); glutMainLoop(); } texture1.txt -------------------- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
You need to login to post a comment.
