生活随笔
收集整理的這篇文章主要介紹了
texture 纹理(贴图)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
紋理
紋理是一個2D圖片(甚至也有1D和3D的紋理),它可以用來添加物體的細節。
這是兩張照片疊加的效果
由下面兩張疊加而成
源代碼
shaders類在自定義著色器 中有完整的源代碼。
下邊我們使用stb_image.h來解析圖片。
#define GLEW_STATIC #include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<iostream>
#include "Shaders.h"#define STB_IMAGE_IMPLEMENTATION#include "stb_image.h"using namespace std
;void processInput(GLFWwindow
);void processInput(GLFWwindow
*window
) {if (glfwGetKey(window
, GLFW_KEY_ESCAPE
) == GLFW_PRESS
) {glfwSetWindowShouldClose(window
, true);}
}
float vertices
[]{0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
unsigned int indices
[]{0, 1, 2, 2, 3, 0
};int main() {glfwInit();glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR
, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR
, 3);glfwWindowHint(GLFW_OPENGL_PROFILE
, GLFW_OPENGL_CORE_PROFILE
);GLFWwindow
*window
= glfwCreateWindow(800, 600, "Test window", NULL, NULL);if (window
== NULL) {cout
<< "open window failed." << endl
;glfwTerminate();}glfwMakeContextCurrent(window
);glewExperimental
= true;if (glewInit() != GLEW_OK
) {cout
<< "glew init failed." << endl
;glfwTerminate();return -1;}glViewport(0, 0, 800, 600);unsigned int VAO
;glGenVertexArrays(1, &VAO
);glBindVertexArray(VAO
);unsigned int VBO
; glGenBuffers(1, &VBO
);glBindBuffer(GL_ARRAY_BUFFER
, VBO
);glBufferData(GL_ARRAY_BUFFER
, sizeof(vertices
), vertices
, GL_STATIC_DRAW
);unsigned int EBO
;glGenBuffers(1, &EBO
);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER
, EBO
);glBufferData(GL_ELEMENT_ARRAY_BUFFER
, sizeof(indices
), indices
, GL_STATIC_DRAW
);Shaders
*shaders
= new Shaders("./shaders/vertexSource.txt", "./shaders/fragmentSource.txt");glVertexAttribPointer(0, 3, GL_FLOAT
, GL_FALSE
, 8 * sizeof(float), (void *) 0);glEnableVertexAttribArray(0);glVertexAttribPointer(1, 3, GL_FLOAT
, GL_FALSE
, 8 * sizeof(float), (void *) (3 * sizeof(float)));glEnableVertexAttribArray(1);glVertexAttribPointer(2, 2, GL_FLOAT
, GL_FALSE
, 8 * sizeof(float), (void *) (6 * sizeof(float)));glEnableVertexAttribArray(2);unsigned int TexBufferA
;unsigned int TexBufferB
;glGenTextures(1, &TexBufferA
);glBindTexture(GL_TEXTURE_2D
, TexBufferA
);int width
, height
, nrchannels
;stbi_set_flip_vertically_on_load(true);unsigned char *data1
= stbi_load("container.jpg", &width
, &height
, &nrchannels
, 0);cout
<<width
<<" "<<height
<<endl
;if (data1
) {glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, width
, height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, data1
);glGenerateMipmap(GL_TEXTURE_2D
);} else {cout
<< "load image failed." << endl
;}stbi_image_free(data1
);glBindTexture(GL_TEXTURE_2D
,0);glGenTextures(1, &TexBufferB
);glBindTexture(GL_TEXTURE_2D
, TexBufferB
);unsigned char *data2
= stbi_load("ground-texture.jpg", &width
, &height
, &nrchannels
, 0);cout
<<width
<<" "<<height
<<endl
;if (data2
) {glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, width
, height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, data2
);glGenerateMipmap(GL_TEXTURE_2D
);} else {cout
<< "load image failed." << endl
;}stbi_image_free(data2
);glBindTexture(GL_TEXTURE_2D
,0);while (!glfwWindowShouldClose(window
)) {processInput(window
);glClearColor(0.2, 0.3, 0.3, 1.0);glClear(GL_COLOR_BUFFER_BIT
);glActiveTexture(GL_TEXTURE0
);glBindTexture(GL_TEXTURE_2D
, TexBufferA
);glActiveTexture(GL_TEXTURE3
);glBindTexture(GL_TEXTURE_2D
, TexBufferB
);glBindVertexArray(VAO
);shaders
->use();glUniform1i(glGetUniformLocation(shaders
->ID
, "ourTexture1"), 0);glUniform1i(glGetUniformLocation(shaders
->ID
, "ourTexture3"), 3);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER
, EBO
);glDrawElements(GL_TRIANGLES
, 6, GL_UNSIGNED_INT
, 0);glBindVertexArray(0);glfwSwapBuffers(window
);glfwPollEvents();}glfwTerminate();return 0;
}
shaders
頂點著色器
#version 330 core
layout
(location
= 0) in vec3 aPos
;
layout
(location
= 1) in vec3 aColor
;
layout
(location
= 2) in vec2 aTexCoord
;
out vec4 vertexColor
;
out vec2 TexCoord
;
void main()
{
gl_Position
= vec4(aPos
.x
, aPos
.y
, aPos
.z
, 1.0);vertexColor
= vec4(aColor
.x
,aColor
.y
,aColor
.z
,1.0);TexCoord
= aTexCoord
;
}
片元著色器
#version 330 core
out vec4 FragColor
;
in vec4 vertexColor
;
in vec2 TexCoord
;
uniform sampler2D ourTexture1
;
uniform sampler2D ourTexture3
;
void main()
{ FragColor
= mix(texture(ourTexture1
,TexCoord
) , texture(ourTexture3
,TexCoord
),0.55);
}
stb_image.h 代碼
總結
以上是生活随笔為你收集整理的texture 纹理(贴图)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。