#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "shader.h"
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
void framebuffer_size_callback(GLFWwindow
* window
, int width
, int height
);
void processInput(GLFWwindow
*window
);
const unsigned int SCR_WIDTH
= 800;
const unsigned int SCR_HEIGHT
= 600;
int main(void)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR
, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR
, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE
, GLFW_OPENGL_CORE_PROFILE
);
GLFWwindow
* window
= glfwCreateWindow(SCR_WIDTH
, SCR_HEIGHT
, "window", NULL, NULL);
if (window
== NULL)
{
std
::cout
<< "Failed to create GLFW window" << std
::endl
;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window
);
glfwSetFramebufferSizeCallback(window
, framebuffer_size_callback
);
if (!gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
))
{
std
::cout
<< "Failed to initialize GLAD" << std
::endl
;
return -1;
}
Shader
ourShader("D:\\OpenGL\\Project1\\4.1.texture.vs", "D:\\OpenGL\\Project1\\4.1.texture.fs");
float vertices
[] = {
0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f
};
unsigned int indices
[] = {
0, 1, 3,
1, 2, 3
};
unsigned int VBO
, VAO
, EBO
;
glGenVertexArrays(1, &VAO
);
glGenBuffers(1, &VBO
);
glGenBuffers(1, &EBO
);
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
, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT
, GL_FALSE
, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int texture1
, texture2
;
glGenTextures(1, &texture1
);
glBindTexture(GL_TEXTURE_2D
, texture1
);
glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
int width
, height
, nrChannels
;
stbi_set_flip_vertically_on_load(true);
unsigned char * data
= stbi_load("D:\\OpenGL\\Project1\\container.jpg", &width
, &height
, &nrChannels
, 0);
if (data
)
{
glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, width
, height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, data
);
glGenerateMipmap(GL_TEXTURE_2D
);
}
else
{
std
::cout
<< "Failed to load texture" << std
::endl
;
}
stbi_image_free(data
);
glGenTextures(1, &texture2
);
glBindTexture(GL_TEXTURE_2D
, texture2
);
glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
data
= stbi_load("D:\\OpenGL\\Project1\\awesomeface.png", &width
, &height
, &nrChannels
, 0);
if (data
)
{
glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, width
, height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
glGenerateMipmap(GL_TEXTURE_2D
);
}
else
{
std
::cout
<< "Failed to load texture" << std
::endl
;
}
stbi_image_free(data
);
ourShader
.use();
ourShader
.setInt("texture1", 0);
ourShader
.setInt("texture2", 1);
while (!glfwWindowShouldClose(window
))
{
processInput(window
);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT
);
glActiveTexture(GL_TEXTURE0
);
glBindTexture(GL_TEXTURE_2D
, texture1
);
glActiveTexture(GL_TEXTURE1
);
glBindTexture(GL_TEXTURE_2D
, texture2
);
glm
::mat4 transform
= glm
::mat4(1.0f);
transform
= glm
::translate(transform
, glm
::vec3(0.5f, -0.5f, 0.0f));
transform
= glm
::rotate(transform
, (float)glfwGetTime(), glm
::vec3(0.0f, 0.0f, 1.0f));
unsigned int transformLoc
= glGetUniformLocation(ourShader
.ID
, "transform");
glUniformMatrix4fv(transformLoc
, 1, GL_FALSE
, glm
::value_ptr(transform
));
glBindVertexArray(VAO
);
glDrawElements(GL_TRIANGLES
, 6, GL_UNSIGNED_INT
, 0);
transform
= glm
::mat4(1.0f);
transform
= glm
::translate(transform
, glm
::vec3(-0.5f, 0.5f, 0.0f));
float scaleAmount
= sin(glfwGetTime());
transform
= glm
::scale(transform
, glm
::vec3(scaleAmount
, scaleAmount
, scaleAmount
));
glUniformMatrix4fv(transformLoc
, 1, GL_FALSE
, glm
::value_ptr(transform
));
glDrawElements(GL_TRIANGLES
, 6, GL_UNSIGNED_INT
, 0);
glfwSwapBuffers(window
);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO
);
glDeleteBuffers(1, &VBO
);
glDeleteBuffers(1, &EBO
);
glfwTerminate();
return 0;
}
void processInput(GLFWwindow
*window
)
{
if (glfwGetKey(window
, GLFW_KEY_ESCAPE
) == GLFW_PRESS
)
glfwSetWindowShouldClose(window
, true);
}
void framebuffer_size_callback(GLFWwindow
* window
, int width
, int height
)
{
glViewport(0, 0, width
, height
);
}
顶点着色器
#version 330 core
layout
(location
= 0) in vec3 aPos
;
layout
(location
= 1) in vec2 aTexCoord
;
out vec2 TexCoord
;
uniform mat4 transform
;
void main()
{
gl_Position
= transform
* vec4(aPos
, 1.0);
TexCoord
= vec2(aTexCoord
.x
, aTexCoord
.y
);
}
片元着色器
#version 330 core
out vec4 FragColor
;
in vec2 TexCoord
;
uniform sampler2D texture1
;
uniform sampler2D texture2
;
void main()
{
FragColor
= mix(texture(texture1
, TexCoord
), texture(texture2
, TexCoord
), 0.2);
}