返回顶部

收藏

openGL 从一个文本文件,作出纹理板

更多
#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

标签:opengl

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2018-08-25 11:50:39 【OpenGL-ES】模型变换与坐标系统
  2. 博主 发表 2018-07-31 14:36:29 年轻人的第一篇OpenGL ES 2.0教程
  3. 杨磊 发表 2018-07-19 09:07:45 WebGL入门与进阶3
  4. 杨磊 发表 2018-07-17 01:10:23 WebGL入门与进阶2
  5. 杨磊 发表 2018-07-11 09:56:01 WebGL入门与进阶1
  6. 博主 发表 2018-05-19 14:04:17 【OpenGL-ES】二维纹理
  7. 博主 发表 2018-03-18 08:53:23 【OpenGL ES】入门及绘制一个三角形
  8. 博主 发表 2018-03-17 13:52:42 【OpenGL】入门及三角形与简单纹理绘制
  9. 博主 发表 2017-05-24 04:02:00 今日力推:Android OpenGL / 微信防撤回 / Whatʼs new in Swift
  10. mortoray 发表 2016-11-10 14:53:35 What is a normal map? An object normal map?
  11. chu 发表 2016-11-13 12:50:19 解决RDP无法启动OpenGL程序的问题
  12. guowei 发表 2017-01-10 03:39:22 OpenGL ES (2) 之 GLSL

发表评论