render image to fullscreen quad

This commit is contained in:
ktyl 2021-07-06 21:20:01 +01:00
parent ea9c3fdd3a
commit 8857b057e0
7 changed files with 7821 additions and 15 deletions

3
res/container.jpg Normal file
View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:126baccff187648acfad78c57560035f5c783cc6ec92a37a93a5b1edc97af10e
size 184939

View File

@ -2,8 +2,11 @@
out vec4 FragColor; out vec4 FragColor;
in vec3 ourColor; in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main() void main()
{ {
FragColor = vec4(ourColor, 1.0); FragColor = texture(ourTexture, TexCoord);
} }

View File

@ -1,11 +1,14 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; // position has attribute position 0 layout (location = 0) in vec3 aPos; // position has attribute position 0
layout (location = 1) in vec3 aColor; // color has attribute position 1 layout (location = 1) in vec3 aColor; // color has attribute position 1
layout (location = 2) in vec2 aTexCoord; // texture coordinate
out vec3 ourColor; out vec3 ourColor;
out vec2 TexCoord;
void main() void main()
{ {
gl_Position = vec4(aPos, 1.0); gl_Position = vec4(aPos, 1.0);
ourColor = aColor; ourColor = aColor;
TexCoord = aTexCoord;
} }

View File

@ -1,13 +1,14 @@
#include "main.h" #include "main.h"
#include "gfx.h" #include "gfx.h"
#include "stb_image.h"
float vertices[] = { float vertices[] = {
// position color // position color uvs
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, // top right 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top left -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
}; };
unsigned int indices[] = { unsigned int indices[] = {
@ -28,6 +29,33 @@ int main()
gfxInit(); gfxInit();
SDL_Window* window = getWindow(); SDL_Window* window = getWindow();
// generate opengl texture
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image data from file
const char* imageFile = "res/tex.png";
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(1);
unsigned char* data = stbi_load(imageFile, &width, &height, &nrChannels, 0);
if (!data)
{
fputs("failed to load texture data: ", stderr);
fputs(imageFile, stderr);
fputs("\n", stderr);
exit(1);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
// free loaded image data
stbi_image_free(data);
unsigned int shaderProgram = compileShaderProgram(); unsigned int shaderProgram = compileShaderProgram();
// vertex array object // vertex array object
@ -52,12 +80,16 @@ int main()
// set vertex attributes // set vertex attributes
int stride = 8*sizeof(float);
// position attribute // position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)0); // TODO: wtf glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); // TODO: wtf
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// color attribute // color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
// uv attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
@ -69,10 +101,10 @@ int main()
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// update uniforms // update uniforms
float time = (float)SDL_GetTicks() / 1000.0f; //float time = (float)SDL_GetTicks() / 1000.0f;
float greenValue = (sin(time)/2.0f)+0.5f; //float greenValue = (sin(time)/2.0f)+0.5f;
int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor"); //int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); //glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
// draw // draw
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

2
src/stb_image.c Normal file
View File

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

7763
src/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
* [x] basic opengl initialisation * [x] basic opengl initialisation
* [ ] render a texture to a full-screen quad * [x] render a texture to a full-screen quad
* [ ] output image to a file * [ ] output image to a file
* [ ] render image with compute shader * [ ] render image with compute shader