Nove repo opengl-learning
This commit is contained in:
87
lessons/1. Getting started/5. Textures/src/Shader.cpp
Normal file
87
lessons/1. Getting started/5. Textures/src/Shader.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath)
|
||||
{
|
||||
std::string v = readFile(vertexShaderSourcePath);
|
||||
std::string f = readFile(fragmentShaderSourcePath);
|
||||
const char *vertexSource = v.c_str();
|
||||
const char *fragmentSource = f.c_str();
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str());
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
20
lessons/1. Getting started/5. Textures/src/Shader.h
Normal file
20
lessons/1. Getting started/5. Textures/src/Shader.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath);
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
private:
|
||||
std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
184
lessons/1. Getting started/5. Textures/src/main.cpp
Normal file
184
lessons/1. Getting started/5. Textures/src/main.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include "Shader.h"
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
float ratio = 0.2;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height);
|
||||
void proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader myShader("../src/shaders/vertexShader.vert", "../src/shaders/fragmentShader.frag");
|
||||
|
||||
float vertices[] = {
|
||||
// positions // colors // texture coords
|
||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
||||
};
|
||||
unsigned int indices[] = {
|
||||
0, 1, 3,
|
||||
1, 2, 3
|
||||
};
|
||||
|
||||
uint VAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
uint EBO;
|
||||
glGenBuffers(1, &EBO);
|
||||
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(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint texture1 = load_texture(TEXTURES_DIR "container.jpg", false);
|
||||
uint texture2 = load_texture(TEXTURES_DIR "awesomeface.png");
|
||||
|
||||
myShader.Use();
|
||||
myShader.setInt("texture1", 0); // set Texture units IDs
|
||||
myShader.setInt("texture2", 1);
|
||||
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
myShader.Use();
|
||||
|
||||
myShader.setFloat("ratio", ratio);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, texture2);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Window!", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
|
||||
if (ratio < 1.0)
|
||||
ratio += 0.001f;
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
|
||||
if (ratio > 0.0)
|
||||
ratio -= 0.001f;
|
||||
}
|
||||
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bites (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec4 outColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
uniform sampler2D texture2;
|
||||
uniform float ratio;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), ratio);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
|
||||
out vec4 outColor;
|
||||
out vec2 TexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos.xyz, 1.0);
|
||||
outColor = vec4(aColor, 1.0f);
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
Reference in New Issue
Block a user