TextureSquare




//—-textureSquare.cpp
// 중간생략…
GLuint SetSquareData()
{
 // Vertice Positions
 squareVertices.push_back(glm::vec3(-0.75f, -0.75f, 0.0f));
 squareVertices.push_back(glm::vec3(0.75f, -0.75f, 0.0f));
 squareVertices.push_back(glm::vec3(0.75f, 0.75f, 0.0f));
 squareVertices.push_back(glm::vec3(-0.75f, 0.75f, 0.0f));
 // Vertices Textures
 squareTextureCoords.push_back(glm::vec2(0.0f, 0.0f));
 squareTextureCoords.push_back(glm::vec2(1.0f, 0.0f));
 squareTextureCoords.push_back(glm::vec2(1.0f, 1.0f));
 squareTextureCoords.push_back(glm::vec2(0.0f, 1.0f));

 // Vertice Indices
 squareIndices.push_back(0);
 squareIndices.push_back(1);
 squareIndices.push_back(2);
 squareIndices.push_back(0);
 squareIndices.push_back(2);
 squareIndices.push_back(3);


 // VAO
 GLuint vao;
 glGenVertexArrays(1, &vao);
 glBindVertexArray(vao);


 // VBO
 GLuint vbo[2];
 glGenBuffers(2, &vbo[0]);
 glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
 glBufferData(GL_ARRAY_BUFFER, 4*sizeof(glm::vec3), &squareVertices[0], GL_STATIC_DRAW);
 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
 glEnableVertexAttribArray(0);
 glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
 glBufferData(GL_ARRAY_BUFFER, 4*sizeof(glm::vec2), &squareTextureCoords[0], GL_STATIC_DRAW);
 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
 glEnableVertexAttribArray(1);


 // IBO
 GLuint ibo;
 glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*sizeof(int), &squareIndices[0], GL_STATIC_DRAW);


 squareVertices.clear();
 squareTextureCoords.clear();


 return vao;
}




//————-TransformVertexShader.vert
#version 330


// Input vertex data
layout (location = 0) in vec3 vPosition; // vertex position (in model space)
layout (location = 1) in vec2 vTexCoord; // texture coordinate (in model space)


// Output data will be interpolated for each fragment.
out vec2 TexCoordPass;


// Values that stay constant for the whole mesh.
uniform mat4 gMVP;


void main()
{
 // Output position of the vertex, in clip space : gMVP * position
 gl_Position =  gMVP * vec4(vPosition,1);


 TexCoordPass = vTexCoord;
}




//———-TextureFragmentShader.frag
#version 330


// Interpolated values from the vertex shaders
in vec2 TexCoordPass;


// Ouput data
out vec4 Color;


// Values that stay constant for the whole mesh.
uniform sampler2D gTextureSampler;


void main()
{
 // Output color = color of the texture at the specified UV
 Color = texture2D(gTextureSampler, TexCoordPass);
}




Leave a Reply

Your email address will not be published. Required fields are marked *