render image to fullscreen quad
This commit is contained in:
		
							parent
							
								
									ea9c3fdd3a
								
							
						
					
					
						commit
						8857b057e0
					
				
							
								
								
									
										3
									
								
								res/container.jpg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								res/container.jpg
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| version https://git-lfs.github.com/spec/v1 | ||||
| oid sha256:126baccff187648acfad78c57560035f5c783cc6ec92a37a93a5b1edc97af10e | ||||
| size 184939 | ||||
| @ -2,8 +2,11 @@ | ||||
| out vec4 FragColor; | ||||
| 
 | ||||
| in vec3 ourColor; | ||||
| in vec2 TexCoord; | ||||
| 
 | ||||
| uniform sampler2D ourTexture; | ||||
| 
 | ||||
| void main() | ||||
| { | ||||
|     FragColor = vec4(ourColor, 1.0); | ||||
|     FragColor = texture(ourTexture, TexCoord); | ||||
| } | ||||
|  | ||||
| @ -1,11 +1,14 @@ | ||||
| #version 330 core | ||||
| layout (location = 0) in vec3 aPos;         // position has attribute position 0 | ||||
| layout (location = 1) in vec3 aColor;       // color has attribute position 1 | ||||
| layout (location = 2) in vec2 aTexCoord;    // texture coordinate | ||||
| 
 | ||||
| out vec3 ourColor; | ||||
| out vec2 TexCoord; | ||||
| 
 | ||||
| void main() | ||||
| { | ||||
|     gl_Position = vec4(aPos, 1.0); | ||||
|     ourColor = aColor; | ||||
|     TexCoord = aTexCoord; | ||||
| } | ||||
|  | ||||
							
								
								
									
										54
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								src/main.c
									
									
									
									
									
								
							| @ -1,13 +1,14 @@ | ||||
| #include "main.h" | ||||
| 
 | ||||
| #include "gfx.h" | ||||
| #include "stb_image.h" | ||||
| 
 | ||||
| float vertices[] = { | ||||
|     // position             color
 | ||||
|      0.5f,  0.5f,  0.0f,    1.0f, 1.0f, 1.0f,  // top right
 | ||||
|      0.5f, -0.5f,  0.0f,    1.0f, 0.0f, 0.0f,  // bottom right
 | ||||
|     -0.5f, -0.5f,  0.0f,    0.0f, 1.0f, 0.0f,  // bottom left
 | ||||
|     -0.5f,  0.5f,  0.0f,    0.0f, 0.0f, 1.0f   // top left
 | ||||
|     // position             color               uvs
 | ||||
|      1.0f,  1.0f,  0.0f,    1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
 | ||||
|      1.0f, -1.0f,  0.0f,    0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
 | ||||
|     -1.0f, -1.0f,  0.0f,    0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
 | ||||
|     -1.0f,  1.0f,  0.0f,    1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left
 | ||||
| }; | ||||
| 
 | ||||
| unsigned int indices[] = { | ||||
| @ -28,6 +29,33 @@ int main() | ||||
|     gfxInit(); | ||||
|     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(); | ||||
| 
 | ||||
|     // vertex array object
 | ||||
| @ -52,12 +80,16 @@ int main() | ||||
| 
 | ||||
|     // set vertex attributes
 | ||||
| 
 | ||||
|     int stride = 8*sizeof(float); | ||||
|     // 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); | ||||
|     // 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); | ||||
|     // uv attribute
 | ||||
|     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float))); | ||||
|     glEnableVertexAttribArray(2); | ||||
| 
 | ||||
|     glUseProgram(shaderProgram); | ||||
| 
 | ||||
| @ -69,10 +101,10 @@ int main() | ||||
|         glClear(GL_COLOR_BUFFER_BIT); | ||||
| 
 | ||||
|         // update uniforms
 | ||||
|         float time = (float)SDL_GetTicks() / 1000.0f; | ||||
|         float greenValue = (sin(time)/2.0f)+0.5f; | ||||
|         int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor"); | ||||
|         glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); | ||||
|         //float time = (float)SDL_GetTicks() / 1000.0f;
 | ||||
|         //float greenValue = (sin(time)/2.0f)+0.5f;
 | ||||
|         //int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
 | ||||
|         //glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
 | ||||
| 
 | ||||
|         // draw
 | ||||
|         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); | ||||
|  | ||||
							
								
								
									
										2
									
								
								src/stb_image.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								src/stb_image.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | ||||
| #define STB_IMAGE_IMPLEMENTATION | ||||
| #include "stb_image.h" | ||||
							
								
								
									
										7763
									
								
								src/stb_image.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7763
									
								
								src/stb_image.h
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user