วันจันทร์ที่ 12 ธันวาคม พ.ศ. 2559

4.class Texture



Texture.h

#pragma once

#include <iostream>

#include <GL/glew.h>

#include <SOIL.h>

class Texture
{
public:
Texture(std::string fileName);

GLuint getTextureID();

void bindTexture();

~Texture();

private:
GLuint texture;


};

*****************************************************************

Texture.cpp

#include "Texture.h"



Texture::Texture(std::string fileName)
{
glGenTextures(1, &this->texture);
glBindTexture(GL_TEXTURE_2D, this->texture); // All upcoming GL_TEXTURE_2D operations now have effect on this texture object
  // Set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load image, create texture and generate mipmaps
int width, height;
unsigned char* image = SOIL_load_image(fileName.c_str(), &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, s
}


void Texture::bindTexture()
{
glBindTexture(GL_TEXTURE_2D, this->texture);

}


GLuint Texture::getTextureID()
{
return this->texture;
}


Texture::~Texture()
{
}

*******************************************************************

shader.vert

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;

out vec3 ourColor;
out vec2 TexCoord;


void main()
{
    gl_Position = vec4(position.x  , position.y , position.z , 1.0f);
    ourColor = color;
TexCoord = texCoord;
}

**********************************************************************

shader.frag

#version 330 core

in vec3 ourColor;

in vec2 TexCoord;

out vec4 color;




uniform sampler2D ourTexture;

void main()
{


color = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0f);


}

****************************************************************************

TestApp.h

#pragma once

#include <iostream>
#include <GL/glew.h>
#include "Shader.h"
#include "Texture.h"

class TestApp
{
public:
TestApp();
void Update();

void putTextureInRectangleProgram();

void changeGreenToBlackProgram();

~TestApp();

private:
GLuint VBO, VAO, EBO;

Shader* ourShader;

Texture* texture;
};

**********************************************************************


TestApp.cpp

#include "TestApp.h"


#include <GL/glew.h>
#include <glfw3.h>

#include "Shader.h"



GLfloat offset = 0.0f;

TestApp::TestApp()
{

ourShader = new Shader("shader.vert", "shader.frag");


GLfloat vertices[] = {
// Positions          // Colors
0.5f, 0.5f, 0.0f,   1.0f, 0.0f, 0.0f,      1.0f, 1.0f,      // Bottom Left
0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,     1.0f, 0.0f,      // Top Left
-0.5f,  -0.5f, 0.0f,   0.0f, 0.0f, 1.0f ,  0.0f, 0.0f,           // Top Right
-0.5f,  0.5f, 0.0f,   0.0f, 0.0f, 1.0f ,   0.0f, 1.0f     // Top Right
};

GLuint indices[] = {  // Note that we start from 0!
0, 1, 3,   // First Triangle
1, 2, 3    // Second Triangle
};




// generate vao , vbo , veo
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);




// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);


glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);


// Vertices
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

// Colors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)) );
glEnableVertexAttribArray(1);

// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);


// Unbind
glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO



// After Unbind VAO
// Load and create a texture 
texture = new Texture("container.jpg");







}

void TestApp::Update()
{

glClearColor(0.7f, 0.3f, 0.3f, 1.0f);     // Background color
glClear(GL_COLOR_BUFFER_BIT);



texture->bindTexture();

ourShader->Use();



// Now draw the triangle
glBindVertexArray(VAO);
// with ebo
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);



glBindVertexArray(0);
}

TestApp::~TestApp()
{
delete ourShader;

delete texture;

// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(1, &this->VAO);
glDeleteBuffers(1, &this->VBO);
glDeleteBuffers(1, &this->EBO);

}

******************************************************************

ผลที่ได้








ไม่มีความคิดเห็น:

แสดงความคิดเห็น