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*/
|
||||
};
|
||||
|
||||
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 =
|
||||
"#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;\n"
|
||||
@ -55,7 +63,8 @@ const char *rgbFragmentShaderSource = "#version 330 core\n"
|
||||
" FragColor = vec4(color, 1.0);\n"
|
||||
"}\0";
|
||||
|
||||
const char *rgbPulseFragmentShaderSource = "#version 330 core\n"
|
||||
const char *rgbPulseFragmentShaderSource =
|
||||
"#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"in vec3 color;\n"
|
||||
"uniform float u_time;"
|
||||
|
||||
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;
|
||||
}
|
||||
53
src/main.cpp
53
src/main.cpp
@ -2,15 +2,15 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <shapes.h>
|
||||
#include <shader.hpp>
|
||||
#include <shapes.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
void processInput(GLFWwindow *window);
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||
|
||||
|
||||
int main(void) {
|
||||
int tmp = 0;
|
||||
|
||||
@ -36,20 +36,48 @@ int main(void) {
|
||||
glGenBuffers(1, &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);
|
||||
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);
|
||||
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);
|
||||
|
||||
/*
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||
(void *)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glGenBuffers(1, &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)) {
|
||||
processInput(window);
|
||||
@ -58,11 +86,12 @@ int main(void) {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
shaderProgram->use();
|
||||
shaderProgram->setFloat("u_time",glfwGetTime());
|
||||
shaderProgram->setInt("Tex0",0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
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