#include "glew.h"
#include "glfw3.h"
#include "SOIL2.h"
GLchar * vertextSrc = " #version 330 core \n \
layout (location = 0) in vec3 position; \
layout (location = 1) in vec3 color; \
layout (location = 2) in vec2 texcoord;\
out vec3 outcolor; \
out vec2 outtexcoord; \
void main(){gl_Position = vec4(position, 1.0f);outcolor = color;outtexcoord = texcoord;}";
GLchar * fragmentSrc = "#version 330 core \n \
in vec3 outcolor;\
in vec2 outtexcoord;\
out vec4 color; \
uniform sampler2D ourTexture;\
uniform sampler2D ourTexture2;\
void main(){ \
vec2 tempTexcoord = vec2(outtexcoord.x, 1.0f - outtexcoord.y);\
color = mix( texture(ourTexture, tempTexcoord), texture(ourTexture2,tempTexcoord) ,0.2); \
}";
int main()
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(500,600,"Texture", NULL,NULL);
glfwMakeContextCurrent(window);
glewInit();
GLfloat vertexArray[] = {
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 2.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, 0.0f, 0.0f, 0.0f, 2.0f,
};
GLuint earrays[] = {
0,1,3,
1,2,3
};
GLuint VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(arrays), arrays, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(earrays), earrays, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3,GL_FLOAT,false, 8* sizeof(GLfloat),(GLvoid*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glVertexAttribPointer(2, 2, GL_FLOAT, false, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
GLuint Vertexshader, fragmentShader, programShader;
Vertexshader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
programShader = glCreateProgram();
GLint succeed;
char info[512];
glShaderSource(Vertexshader,1,&vertextSrc,(GLint*)0);
glCompileShader(Vertexshader);
glGetShaderiv(Vertexshader, GL_COMPILE_STATUS, &succeed);
if (!succeed)
{
glGetShaderInfoLog(Vertexshader, 512, NULL, info);
}
glShaderSource(fragmentShader, 1, &fragmentSrc, (GLint*)0);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &succeed);
if (!succeed)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, info);
}
glAttachShader(programShader,Vertexshader);
glAttachShader(programShader, fragmentShader);
glLinkProgram(programShader);
glDeleteShader(Vertexshader);
glDeleteShader(fragmentShader);
GLuint texture, texture2;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
int width, height;
unsigned char* image = SOIL_load_image("container.jpg", &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);
glGenTextures(1, &texture2);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char* image2 = SOIL_load_image("awesomeface.png", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image2);
glBindTexture(GL_TEXTURE_2D, 0);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(programShader);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(programShader,"ourTexture"),0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glUniform1i(glGetUniformLocation(programShader, "ourTexture2"), 1);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}