screenspace uv
This commit is contained in:
parent
56070f31e4
commit
c118d92fa7
1 changed files with 50 additions and 81 deletions
127
main.c
127
main.c
|
|
@ -2,77 +2,8 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
float tanf(float in) {
|
||||
return in;
|
||||
}
|
||||
|
||||
void perspective(float fovY, float aspect, float zNear, float zFar, float* matrix) {
|
||||
float f = 1.0f / tanf(fovY * 0.5f * (3.14159265358979323846f / 180.0f));
|
||||
matrix[0] = f / aspect;
|
||||
matrix[1] = 0;
|
||||
matrix[2] = 0;
|
||||
matrix[3] = 0;
|
||||
|
||||
matrix[4] = 0;
|
||||
matrix[5] = f;
|
||||
matrix[6] = 0;
|
||||
matrix[7] = 0;
|
||||
|
||||
matrix[8] = 0;
|
||||
matrix[9] = 0;
|
||||
matrix[10] = (zFar + zNear) / (zNear - zFar);
|
||||
matrix[11] = -1;
|
||||
|
||||
matrix[12] = 0;
|
||||
matrix[13] = 0;
|
||||
matrix[14] = (2 * zFar * zNear) / (zNear - zFar);
|
||||
matrix[15] = 0;
|
||||
}
|
||||
|
||||
void drawCube() {
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
// Back
|
||||
glColor3f(0, 1, 0);
|
||||
glVertex3f(-1, -1, -1);
|
||||
glVertex3f(-1, 1, -1);
|
||||
glVertex3f( 1, 1, -1);
|
||||
glVertex3f( 1, -1, -1);
|
||||
|
||||
// Top
|
||||
glColor3f(0, 0, 1);
|
||||
glVertex3f(-1, 1, -1);
|
||||
glVertex3f(-1, 1, 1);
|
||||
glVertex3f( 1, 1, 1);
|
||||
glVertex3f( 1, 1, -1);
|
||||
|
||||
// Bottom
|
||||
glColor3f(1, 1, 0);
|
||||
glVertex3f(-1, -1, -1);
|
||||
glVertex3f( 1, -1, -1);
|
||||
glVertex3f( 1, -1, 1);
|
||||
glVertex3f(-1, -1, 1);
|
||||
|
||||
// Right
|
||||
glColor3f(1, 0, 1);
|
||||
glVertex3f(1, -1, -1);
|
||||
glVertex3f(1, 1, -1);
|
||||
glVertex3f(1, 1, 1);
|
||||
glVertex3f(1, -1, 1);
|
||||
|
||||
// Left
|
||||
glColor3f(0, 1, 1);
|
||||
glVertex3f(-1, -1, -1);
|
||||
glVertex3f(-1, -1, 1);
|
||||
glVertex3f(-1, 1, 1);
|
||||
glVertex3f(-1, 1, -1);
|
||||
|
||||
glEnd();
|
||||
}
|
||||
|
||||
void checkGlError() {
|
||||
const char *err = SDL_GetError();
|
||||
if (*err != 0) {
|
||||
|
|
@ -80,13 +11,47 @@ void checkGlError() {
|
|||
}
|
||||
}
|
||||
|
||||
const char *vertex_shader_source = "";
|
||||
const char *fragment_shader_source =
|
||||
"#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"in vec2 uv;\n"
|
||||
"\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" FragColor = vec4(gl_FragCoord.xy / 800, 0.0, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
GLuint create_shader() {
|
||||
GLuint vshader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vshader, 1, &vertex_shader_source, NULL); // vertex_shader_source is a GLchar* containing glsl shader source code
|
||||
glCompileShader(vshader);
|
||||
return vshader;
|
||||
GLuint compile_shader(GLuint type, const char *src) {
|
||||
GLuint id = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(id, 1, &src, 0);
|
||||
glCompileShader(id);
|
||||
|
||||
int result;
|
||||
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
|
||||
if (result == GL_FALSE) {
|
||||
int length;
|
||||
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &result);
|
||||
char *message = alloca(length * sizeof(char));
|
||||
glGetShaderInfoLog(id, length, &length, message);
|
||||
printf("failed to compile %s shader\n", type == GL_VERTEX_SHADER ? "vertex" : "fragment");
|
||||
printf("%s\n", message);
|
||||
glDeleteShader(id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
GLuint create_shader(const char *fragment_src) {
|
||||
GLuint program = glCreateProgram();
|
||||
GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fragment_src);
|
||||
|
||||
glAttachShader(program, fragment_shader);
|
||||
glLinkProgram(program);
|
||||
glValidateProgram(program);
|
||||
glDeleteShader(fragment_shader);
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
@ -112,6 +77,10 @@ int main() {
|
|||
SDL_Event e;
|
||||
int running = 1;
|
||||
float angle = 0.0;
|
||||
|
||||
GLuint shader = create_shader(fragment_shader_source);
|
||||
glUseProgram(shader);
|
||||
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_EVENT_QUIT) running = false;
|
||||
|
|
@ -119,18 +88,18 @@ int main() {
|
|||
checkGlError();
|
||||
|
||||
// Rendorrrr
|
||||
glViewport(0, 0, 800, 600);
|
||||
glViewport(0, 0, 800, 800);
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); // Dark gray background
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
//drawCube();
|
||||
// Front
|
||||
glBegin(GL_QUADS);
|
||||
glColor3f(1, 0, 0);
|
||||
glVertex2f(-1, -1);
|
||||
glVertex2f(-1, 1);
|
||||
glVertex2f( 1, 1);
|
||||
glVertex2f(-1,-1);
|
||||
glVertex2f( 1,-1);
|
||||
glVertex2f( 1, 1);
|
||||
|
||||
glEnd();
|
||||
|
||||
SDL_GL_SwapWindow(window);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue