本文主要是介绍跟我一起学OpenGL- texture(纹理),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前面学习了画普通的图形,但是颜色比较单一,有什么好的方法让图形颜色更加丰富,更加真实呢?texture(中文译为纹理)就可以扮演这一角色,本节则简单说明texture的使用。
运行下面的程序,便可出现一个黑白相间的棋盘:
1 #include <GL/glew.h> 2 #include <GL/freeglut.h>3 #include <iostream>4 using namespace std;5 #define BUFFER_OFFSET(x) ((const void*) (x))67 enum VAO_IDs {Triangles, NumVAOs};8 enum Buffer_IDs {ArrayBuffer, NumBuffers};9 enum Attrib_IDs {vPosition = 0};1011 GLuint VAOs[NumVAOs];12 GLuint Buffers[NumBuffers];13 GLuint program;1415 GLushort indices[] = {16 0, 1, 3, /* first triangle */17 1, 2, 3 /* second triangle */18 };1920 GLuint tex;2122 const GLchar* vertex_shader = "#version 300 es \n"23 "layout(location=0) in vec3 vPosition;\n"24 "layout(location=1) in vec2 texCo;\n"25 "out vec2 TexC;\n"26 "void main()\n"27 "{\n"28 "gl_Position = vec4(vPosition, 1.0);\n"29 "TexC = vec2(texCo.x, texCo.y);\n"30 "}\n";3132 const GLchar* fragment_shader = "#version 300 es \n"33 "precision highp float; \n"34 "uniform sampler2D color;\n"35 "layout(location=0)out vec4 fColor;\n"36 "in vec2 TexC;\n"37 "void main()\n"38 "{\n"39 "fColor = texture2D(color, TexC);\n"40 "}\n";4142 void init_shader()43 {44 // vertex shader45 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);46 glShaderSource(vertexShader, 1, &vertex_shader, NULL);47 glCompileShader(vertexShader);48 // check vertex shader compiling status49 GLint compiled;50 GLchar log[256];51 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compiled);52 if (!compiled)53 {54 glGetShaderInfoLog(vertexShader, 256, NULL, log);55 std::cerr << "vertex shader compile failed" << log << std::endl;56 }5758 // fragment shader59 GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);60 glShaderSource(fragmentShader, 1, &fragment_shader, NULL);61 glCompileShader(fragmentShader);62 // check fragment shader compiling status63 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compiled);64 if (!compiled)65 {66 glGetShaderInfoLog(fragmentShader, 256, NULL, log);67 std::cerr << "fragment shader compile failed" << log << std::endl;68 }6970 // link shaders71 program = glCreateProgram();72 glAttachShader(program, vertexShader);73 glAttachShader(program, fragmentShader);74 glLinkProgram(program);7576 cout << "prog=" << program << endl;77 //check link status78 GLint linked;79 glGetProgramiv(program, GL_LINK_STATUS, &linked);80 if (!linked)81 {82 glGetProgramInfoLog(program, 256, NULL, log);83 std::cerr << "shader linking failed" << log << std::endl;84 }85 // delete shader86 glDeleteShader(vertexShader);87 glDeleteShader(fragmentShader);88 }8990 #define HEIGHT 891 #define WIDTH 892 unsigned char pic[HEIGHT][WIDTH][4];9394 static void gen_texture(void)95 {96 int i, j, count;97 for(i = 0; i < HEIGHT; i++)98 {99 for(j = 0; j < WIDTH; j++)
100 {
101 count = (((i & 0x1) == 0) ^ ((j & 0x1) == 0)) * 255;
102 pic[i][j][0] = count;
103 pic[i][j][1] = count;
104 pic[i][j][2] = count;
105 pic[i][j][3] = 255;
106 }
107 }
108 }
109
110 void init_data()
111 {
112 glClearColor(0, 0, 0, 0);
113 glClear(GL_COLOR_BUFFER_BIT);
114
115 gen_texture();
116
117 glGenVertexArrays(NumVAOs, VAOs);
118 glBindVertexArray(VAOs[Triangles]);
119
120 const GLfloat vertices[] = {
121 // positions
122 -1.0f, -1.0f, 0.0f, // top right
123 1.0f, -1.0f, 0.0f, // bottom right
124 1.0f, 1.0f, 0.0f, // bottom left
125 -1.0f, 1.0f, 0.0f, // top left
126
127 // texture coords
128 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f
129 };
130
131 glGenBuffers(NumBuffers, Buffers);
132 glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
133 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
134
135 GLuint ebo;
136 glGenBuffers(1, &ebo);
137 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
138 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
139
140 glGenTextures(1, &tex);
141 glBindTexture(GL_TEXTURE_2D, tex);
142
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
145
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, pic);
149
150 glUseProgram(program);
151
152 GLint texloc = glGetAttribLocation(program, "texCo");
153 cout << "texloc=" << texloc << endl;
154 GLint posloc = glGetAttribLocation(program, "vPosition");
155 cout << "posloc=" << posloc << endl;
156
157 glEnableVertexAttribArray(posloc);
158 glVertexAttribPointer(posloc, 3, GL_FLOAT, GL_FALSE, 0, 0);
159
160 glEnableVertexAttribArray(texloc);
161 glVertexAttribPointer(texloc, 2, GL_FLOAT, GL_FALSE, 0, (void*)(12*sizeof(float)));
162 }
163
164 void display()
165 {
166 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
167 glFlush();
168 }
169
170 int main(int argc, char* argv[])
171 {
172 glutInit(&argc, argv);
173 glutInitDisplayMode(GLUT_RGBA);
174 glutInitWindowSize(300 ,300);
175 glutInitWindowPosition(100, 100);
176 glutCreateWindow("Learn opengl: texture");
177
178 const char* version = (const char*)glGetString(GL_VERSION);
179 cout << "version=" << version << endl;
180 const char* vendor = (const char*)glGetString(GL_VENDOR);
181 cout << "vendor=" << vendor << endl;
182
183 const char* render = (const char*)glGetString(GL_RENDERER);
184 cout << "render=" << render << endl;
185
186 const char* extension = (const char*)glGetString(GL_EXTENSIONS);
187 cout << "extension=" << extension << endl;
188
189 glewExperimental = GL_TRUE;
190 if(glewInit())
191 {
192 std::cout << "Unable to Initialize" << std::endl;
193 exit(1);
194 }
195 init_shader();
196 init_data();
197 glutDisplayFunc(display);
198 glutMainLoop();
199 }
如图所示:
上面的程序会产生8x8大小的texture,黑白相间。
这篇关于跟我一起学OpenGL- texture(纹理)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!