Finish Textures
This commit is contained in:
parent
4f871d12bc
commit
65c81c4c58
3
Makefile
3
Makefile
@ -18,3 +18,6 @@ run: build
|
|||||||
clean:
|
clean:
|
||||||
$(RM) $(P)
|
$(RM) $(P)
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
all: build
|
BIN
awesomeface.png
Normal file
BIN
awesomeface.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 58 KiB |
BIN
container.jpg
Normal file
BIN
container.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 181 KiB |
112
main.c
112
main.c
@ -6,6 +6,10 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include "lib.h"
|
#include "lib.h"
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#define STBI_FAILURE_USERMSG
|
||||||
|
#include "stb_image.h"
|
||||||
|
|
||||||
int SCREEN_WIDTH = 1024;
|
int SCREEN_WIDTH = 1024;
|
||||||
int SCREEN_HEIGHT = 768;
|
int SCREEN_HEIGHT = 768;
|
||||||
|
|
||||||
@ -46,62 +50,84 @@ int main(void) {
|
|||||||
GLuint shaderProgram =
|
GLuint shaderProgram =
|
||||||
compileShaderProgram("shaders/main.vert", "shaders/main.frag", NULL);
|
compileShaderProgram("shaders/main.vert", "shaders/main.frag", NULL);
|
||||||
|
|
||||||
// float vertices[] = {
|
stbi_set_flip_vertically_on_load(true);
|
||||||
// 0.5f, 0.5f, 0.0f, // top right
|
GLuint texture1, texture2;
|
||||||
// 0.5f, -0.5f, 0.0f, // bottom right
|
glGenTextures(1, &texture1);
|
||||||
// -0.5f, -0.5f, 0.0f, // bottom left
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||||
// -0.5f, 0.5f, 0.0f // top left
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
// };
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
// unsigned int indices[] = { // note that we start from 0!
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||||
// 0, 1, 3, // first triangle
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
// 1, 2, 3 // second triangle
|
int w1, h1, c1;
|
||||||
// };
|
unsigned char* container = stbi_load("container.jpg", &w1, &h1, &c1, 0);
|
||||||
|
if (container == NULL) {
|
||||||
|
fprintf(stderr, "Failed to load image\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w1, h1, 0, GL_RGB, GL_UNSIGNED_BYTE, container);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
// float vertices[] = {
|
glGenTextures(1, &texture2);
|
||||||
// -0.5f, -0.5f, 0.0f,
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
||||||
// 0.5f, -0.5f, 0.0f,
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
// 0.0f, 0.5f, 0.0f
|
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);
|
||||||
|
int w2, h2, c2;
|
||||||
|
unsigned char* smiley = stbi_load("awesomeface.png", &w2, &h2, &c2, 0);
|
||||||
|
if (smiley == NULL) {
|
||||||
|
fprintf(stderr, "Failed to load image\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w2, h2, 0, GL_RGBA, GL_UNSIGNED_BYTE, smiley);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
stbi_image_free(container);
|
||||||
|
stbi_image_free(smiley);
|
||||||
|
|
||||||
float v1[] = {
|
float v1[] = {
|
||||||
-1.0f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
// Position // Color // UV
|
||||||
0.0f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||||
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.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, 1.0f
|
||||||
};
|
};
|
||||||
|
|
||||||
float v2[] = {
|
unsigned int indices[] = {
|
||||||
0.0f, -0.5f, 0.0f,
|
0, 1, 3, // first triangle
|
||||||
1.0f, -0.5f, 0.0f,
|
1, 2, 3 // second triangle
|
||||||
0.5f, 0.5f, 0.0f
|
|
||||||
};
|
};
|
||||||
|
|
||||||
GLuint vaos[2];
|
GLuint vaos[2];
|
||||||
GLuint vbos[2];
|
GLuint vbos[2];
|
||||||
unsigned int VBO1, VBO2;
|
unsigned int EBO;
|
||||||
// unsigned int EBO;
|
|
||||||
glGenVertexArrays(2, vaos);
|
glGenVertexArrays(2, vaos);
|
||||||
// glGenBuffers(1, &EBO);
|
glGenBuffers(1, &EBO);
|
||||||
glGenBuffers(2, vbos);
|
glGenBuffers(2, vbos);
|
||||||
|
|
||||||
glBindVertexArray(vaos[0]);
|
glBindVertexArray(vaos[0]);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
|
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(v1), v1, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(v1), v1, GL_STATIC_DRAW);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
|
|
||||||
glBindVertexArray(vaos[1]);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(v2), v2, GL_STATIC_DRAW);
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float)));
|
||||||
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
glUseProgram(shaderProgram);
|
||||||
|
int t1Loc = glGetUniformLocation(shaderProgram, "t1");
|
||||||
|
glUniform1i(t1Loc, 0);
|
||||||
|
int t2Loc = glGetUniformLocation(shaderProgram, "t2");
|
||||||
|
glUniform1i(t2Loc, 1);
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
processInput(window);
|
processInput(window);
|
||||||
|
|
||||||
@ -116,11 +142,16 @@ int main(void) {
|
|||||||
int offsetLoc = glGetUniformLocation(shaderProgram, "offset");
|
int offsetLoc = glGetUniformLocation(shaderProgram, "offset");
|
||||||
glUniform1f(offsetLoc, 0.5f);
|
glUniform1f(offsetLoc, 0.5f);
|
||||||
|
|
||||||
glBindVertexArray(vaos[0]);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||||
|
glActiveTexture(GL_TEXTURE1);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
||||||
|
|
||||||
glBindVertexArray(vaos[1]);
|
glBindVertexArray(vaos[0]);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
|
// glBindVertexArray(vaos[1]);
|
||||||
|
// glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
@ -130,8 +161,7 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
glDeleteVertexArrays(2, vaos);
|
glDeleteVertexArrays(2, vaos);
|
||||||
glDeleteBuffers(1, &VBO1);
|
glDeleteBuffers(2, vbos);
|
||||||
glDeleteBuffers(1, &VBO2);
|
|
||||||
glDeleteProgram(shaderProgram);
|
glDeleteProgram(shaderProgram);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
in vec3 vertexPos;
|
in vec3 vPos;
|
||||||
in vec3 vertexColor;
|
in vec3 vCol;
|
||||||
|
in vec2 tCoord;
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
// uniform vec4 ourColor;
|
uniform sampler2D t1;
|
||||||
|
uniform sampler2D t2;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
FragColor = vec4(vertexPos, 1);
|
FragColor = mix(texture(t1, tCoord), texture(t2, tCoord), 0.2);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec3 aColor;
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec2 aTexCoord;
|
||||||
|
|
||||||
out vec3 vertexPos;
|
out vec3 vPos;
|
||||||
out vec3 vertexColor;
|
out vec3 vCol;
|
||||||
|
out vec2 tCoord;
|
||||||
uniform float offset;
|
uniform float offset;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||||
vertexPos = vec3(abs(aPos.x), abs(aPos.y), abs(aPos.z));
|
vPos = aPos;
|
||||||
|
vCol = aColor;
|
||||||
|
tCoord = aTexCoord;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user