Added texture sqr
This commit is contained in:
parent
753c6cead6
commit
1409eb78ae
@ -18,6 +18,14 @@ unsigned int sqr_indices[] = {
|
|||||||
1, 2, 3 /*br triangle*/
|
1, 2, 3 /*br triangle*/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
float sqr_tex_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*/
|
||||||
|
};
|
||||||
|
|
||||||
const char *basicVertexShaderSource =
|
const char *basicVertexShaderSource =
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
"layout (location = 0) in vec3 aPos;\n"
|
"layout (location = 0) in vec3 aPos;\n"
|
||||||
@ -55,11 +63,12 @@ const char *rgbFragmentShaderSource = "#version 330 core\n"
|
|||||||
" FragColor = vec4(color, 1.0);\n"
|
" FragColor = vec4(color, 1.0);\n"
|
||||||
"}\0";
|
"}\0";
|
||||||
|
|
||||||
const char *rgbPulseFragmentShaderSource = "#version 330 core\n"
|
const char *rgbPulseFragmentShaderSource =
|
||||||
"out vec4 FragColor;\n"
|
"#version 330 core\n"
|
||||||
"in vec3 color;\n"
|
"out vec4 FragColor;\n"
|
||||||
"uniform float u_time;"
|
"in vec3 color;\n"
|
||||||
"void main()\n"
|
"uniform float u_time;"
|
||||||
"{\n"
|
"void main()\n"
|
||||||
" FragColor = vec4(color,0.0)*(sin(u_time)/2.0+0.5);\n"
|
"{\n"
|
||||||
"}\0";
|
" FragColor = vec4(color,0.0)*(sin(u_time)/2.0+0.5);\n"
|
||||||
|
"}\0";
|
||||||
|
|||||||
12
shaders/TexFrag.glsl
Normal file
12
shaders/TexFrag.glsl
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D tex0;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = texture(Tex0, TexCoord) * vec4(ourColor, 1.0);
|
||||||
|
}
|
||||||
14
shaders/TexVert.glsl
Normal file
14
shaders/TexVert.glsl
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
TexCoord = aTexCoord;
|
||||||
|
}
|
||||||
57
src/main.cpp
57
src/main.cpp
@ -2,15 +2,15 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <shapes.h>
|
|
||||||
#include <shader.hpp>
|
#include <shader.hpp>
|
||||||
|
#include <shapes.h>
|
||||||
|
#include <stb_image.h>
|
||||||
|
|
||||||
#define UNUSED(x) (void)(x)
|
#define UNUSED(x) (void)(x)
|
||||||
|
|
||||||
void processInput(GLFWwindow *window);
|
void processInput(GLFWwindow *window);
|
||||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int tmp = 0;
|
int tmp = 0;
|
||||||
|
|
||||||
@ -29,27 +29,55 @@ int main(void) {
|
|||||||
glViewport(0, 0, 800, 600);
|
glViewport(0, 0, 800, 600);
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
|
||||||
unsigned int VBO,VAO,EBO;
|
unsigned int VBO, VAO, EBO;
|
||||||
glGenVertexArrays(1, &VAO);
|
glGenVertexArrays(1, &VAO);
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
glGenBuffers(1, &VBO);
|
glGenBuffers(1, &VBO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(tri_rgb_vertices), tri_rgb_vertices,
|
glBufferData(GL_ARRAY_BUFFER, sizeof(sqr_tex_vertices), sqr_tex_vertices,
|
||||||
GL_STATIC_DRAW);
|
GL_STATIC_DRAW);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float)));
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||||
|
(void *)(3 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
/*
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||||
|
(void *)(6 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
glGenBuffers(1, &EBO);
|
glGenBuffers(1, &EBO);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sqr_indices), sqr_indices, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sqr_indices), sqr_indices,
|
||||||
*/
|
GL_STATIC_DRAW);
|
||||||
|
|
||||||
Shader* shaderProgram = new Shader("./shaders/rgbPulseVert.glsl","./shaders/rgbPulseFrag.glsl");
|
Shader *shaderProgram =
|
||||||
|
new Shader("./shaders/TexVert.glsl", "./shaders/TexFrag.glsl");
|
||||||
|
|
||||||
|
unsigned int texture;
|
||||||
|
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);
|
||||||
|
|
||||||
|
stbi_set_flip_vertically_on_load(true);
|
||||||
|
int width, height, nrChannels;
|
||||||
|
unsigned char *data =
|
||||||
|
stbi_load("./texture/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);
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
processInput(window);
|
processInput(window);
|
||||||
@ -58,17 +86,18 @@ int main(void) {
|
|||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
shaderProgram->use();
|
shaderProgram->use();
|
||||||
shaderProgram->setFloat("u_time",glfwGetTime());
|
shaderProgram->setInt("Tex0",0);
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(shaderProgram);
|
delete (shaderProgram);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
texture/brick.jpg
Normal file
BIN
texture/brick.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 251 KiB |
BIN
texture/container.jpg
Normal file
BIN
texture/container.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 181 KiB |
Loading…
x
Reference in New Issue
Block a user