วันพุธที่ 14 ธันวาคม พ.ศ. 2559

โปรแกรมเปลี่ยน wall ไปเป็น awesome face โดยใช้ sine

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;



uniform float xOffset;



void main()

{

    gl_Position = vec4(position.x + xOffset , position.y , position.z , 1.0f);

    ourColor = color;

 TexCoord = vec2( 1 - texCoord.x ,  texCoord.y );



}




*********************************************************************************
Shader.frag



#version 330 core



in vec3 ourColor;



in vec2 TexCoord;



out vec4 color;



uniform sampler2D ourTexture1;

uniform sampler2D ourTexture2;



uniform float value;





void main()

{

    color = mix( texture( ourTexture1 , TexCoord ) , texture( ourTexture2 , TexCoord ) , value ) ;



}



*********************************************************************************
TestApp.h



#pragma once



#include <iostream>

#include <GL/glew.h>

#include "Shader.h"

#include "Texture.h"



class TestApp

{

public:

 TestApp();

 void Update();

 

 

 ~TestApp();



private:

 GLuint VBO, VAO, EBO;

 Shader* ourShader;

 Texture* texture;

 Texture* texture2;

};



***********************************************************************
TestApp.cpp



#include "TestApp.h"





#include <GL/glew.h>

#include <glfw3.h>

#include <SOIL.h>

#include "Shader.h"



TestApp::TestApp()

{







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



 

 GLfloat vertices[] = {

  // Positions          // Color

  -0.5f, -0.5f, 0.0f,   1.0f, 0.0f, 0.0f,    3.0f , 3.0f , // Bottom Left

  -0.5f, 0.5f, 0.0f,   0.0f, 1.0f, 0.0f,     3.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 , 3.0f  // Top Right

 };



 GLuint indices[] = {  // Note that we start from 0!

  0, 1, 3,   // First Triangle

  1, 2, 3    // Second Triangle

 };



 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);







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

 glEnableVertexAttribArray(0);



 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);



 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





 texture = new Texture("awesome_face.jpg");



 texture2 = new Texture("wall.jpg");



}



void TestApp::Update()

{

 glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

 glClear(GL_COLOR_BUFFER_BIT);







 ourShader->Use();







 glActiveTexture(GL_TEXTURE0);

 glBindTexture(GL_TEXTURE_2D, texture->getTextureID() );

 glUniform1i(glGetUniformLocation(ourShader->Program, "ourTexture1"), 0);





 glActiveTexture(GL_TEXTURE1);

 glBindTexture(GL_TEXTURE_2D, texture2->getTextureID());

 glUniform1i(glGetUniformLocation(ourShader->Program, "ourTexture2"), 1);



 float timeValue = glfwGetTime();

 GLfloat value = (sin(timeValue) / 2) + 0.5;

 glUniform1f(glGetUniformLocation(ourShader->Program, "value"), value);





 // Now draw the triangle

 glBindVertexArray(VAO);

 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

 glBindVertexArray(0);

}



TestApp::~TestApp()

{

 delete ourShader;



 delete texture;



 delete texture2;



 // Properly de-allocate all resources once they've outlived their purpose

 glDeleteVertexArrays(1, &this->VAO);

 glDeleteBuffers(1, &this->VBO);

 glDeleteBuffers(1, &this->EBO);

 // Terminate GLFW, clearing any resources allocated by GLFW.

}



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

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