read shaders from glsl file, u03-3

This commit is contained in:
Luca Conte 2024-04-04 10:11:27 +02:00
parent c8faac75da
commit 5bcda78e4d
6 changed files with 266 additions and 0 deletions

19
u03-3/Makefile Normal file
View File

@ -0,0 +1,19 @@
GLEW_LIBS=$(shell pkgconf glew --libs)
GLFW_LIBS=$(shell pkgconf glfw3 --libs)
OBJ = main.o
cg1.out: $(OBJ) vertexShader.c fragmentShader.c
gcc -o $@ $(OBJ) -lm $(GLEW_LIBS) $(GLFW_LIBS)
%Shader.c: %Shader.glsl
xxd -i $? > $@
main.o: vertexShader.c fragmentShader.c
%.o: %.c
gcc -c $<
clean:
rm vertexShader.c fragmentShader.c cg1.out *.o

16
u03-3/fragmentShader.c Normal file
View File

@ -0,0 +1,16 @@
unsigned char fragmentShader_glsl[] = {
0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x33, 0x33, 0x30,
0x20, 0x63, 0x6f, 0x72, 0x65, 0x0a, 0x69, 0x6e, 0x20, 0x66, 0x6c, 0x6f,
0x61, 0x74, 0x20, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x56,
0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d,
0x61, 0x69, 0x6e, 0x28, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x67, 0x6c, 0x5f,
0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20,
0x76, 0x65, 0x63, 0x34, 0x28, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x65, 0x63,
0x33, 0x28, 0x31, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20,
0x30, 0x2e, 0x30, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, 0x28, 0x30,
0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30,
0x29, 0x2c, 0x20, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x56,
0x61, 0x6c, 0x75, 0x65, 0x29, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b,
0x0a, 0x7d
};
unsigned int fragmentShader_glsl_len = 146;

View File

@ -0,0 +1,5 @@
#version 330 core
in float fragmentValue;
void main() {
gl_FragColor = vec4(mix(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), fragmentValue), 1.0);
}

197
u03-3/main.c Normal file
View File

@ -0,0 +1,197 @@
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "vertexShader.c"
#include "fragmentShader.c"
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define NUM_EDGES 20
#define RADIUS 0.7f
#define INNER_RADIUS 0.2f
typedef struct {
GLfloat x;
GLfloat y;
GLfloat value;
} Point;
GLuint program;
GLuint vao;
void init(void) {
// create and compile vertex shader
GLchar *vertexText = malloc(vertexShader_glsl_len);
memcpy(vertexText, vertexShader_glsl, vertexShader_glsl_len);
const GLchar *vertexTextConst = vertexText;
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexTextConst, &vertexShader_glsl_len);
glCompileShader(vertexShader);
GLint status;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
if (!status) {
printf("Error compiling vertex shader: ");
GLchar infoLog[1024];
glGetShaderInfoLog(vertexShader, 1024, NULL, infoLog);
printf("%s",infoLog);
}
// create and compile fragment shader
GLchar *fragmentText = malloc(fragmentShader_glsl_len);
memcpy(fragmentText, fragmentShader_glsl, fragmentShader_glsl_len);
const GLchar *fragmentTextConst = fragmentText;
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentTextConst, &fragmentShader_glsl_len);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
if (!status) {
printf("Error compiling fragment shader: ");
GLchar infoLog[1024];
glGetShaderInfoLog(fragmentShader, 1024, NULL, infoLog);
printf("%s",infoLog);
}
// create and link shader program
program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (!status) {
printf("Error linking program: ");
GLchar infoLog[1024];
glGetProgramInfoLog(program, 1024, NULL, infoLog);
printf("%s",infoLog);
}
glValidateProgram(program);
glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
if (!status) {
printf("Error validating program: ");
GLchar infoLog[1024];
glGetProgramInfoLog(program, 1024, NULL, infoLog);
printf("%s",infoLog);
}
size_t size = sizeof(Point) * (NUM_EDGES + 1) * 2;
Point* triangleVertices = (Point *) malloc(size);
float alpha;
for (int i = 0; i <= NUM_EDGES; i++) {
alpha = ((float) i) / NUM_EDGES * 3.14159f * 2;
GLfloat dx = cos(alpha);
GLfloat dy = sin(alpha);
triangleVertices[2 * i].x = INNER_RADIUS * cos(alpha);
triangleVertices[2 * i].y = INNER_RADIUS * sin(alpha);
// triangleVertices[2 * i].value = ((float)i) / NUM_EDGES;
triangleVertices[2 * i].value = 1.0f;
triangleVertices[2 * i + 1].x = RADIUS * cos(alpha);
triangleVertices[2 * i + 1].y = RADIUS * sin(alpha);
// triangleVertices[2 * i + 1].value = ((float)i) / NUM_EDGES;
triangleVertices[2 * i + 1].value = 0.0f;
}
GLuint triangleVertexBufferObject;
glGenBuffers(1, &triangleVertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, size, triangleVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// create vertex array object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject);
glVertexAttribPointer(
0,
2,
GL_FLOAT,
GL_FALSE,
sizeof(Point),
0
);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
1,
1,
GL_FLOAT,
GL_FALSE,
sizeof(Point),
(GLvoid*)(2 * sizeof(GLfloat))
);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
}
void draw(void) {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (NUM_EDGES + 1) * 2);
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
int main(void) {
printf("Hello World!\n");
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(800, 800, "Computergrafik 1", NULL, NULL);
if (!window) {
printf("Failed to create window\n");
glfwTerminate();
return -1;
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwMakeContextCurrent(window);
glewInit();
init();
while (!glfwWindowShouldClose(window)) {
draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}

21
u03-3/vertexShader.c Normal file
View File

@ -0,0 +1,21 @@
unsigned char vertexShader_glsl[] = {
0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x33, 0x33, 0x30,
0x20, 0x63, 0x6f, 0x72, 0x65, 0x0a, 0x6c, 0x61, 0x79, 0x6f, 0x75, 0x74,
0x20, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d,
0x20, 0x30, 0x29, 0x20, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20,
0x61, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x6c,
0x61, 0x79, 0x6f, 0x75, 0x74, 0x20, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x31, 0x29, 0x20, 0x69, 0x6e, 0x20,
0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78,
0x56, 0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x66,
0x6c, 0x6f, 0x61, 0x74, 0x20, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e,
0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64,
0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x66,
0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65,
0x20, 0x3d, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x56, 0x61, 0x6c,
0x75, 0x65, 0x3b, 0x0a, 0x09, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69,
0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28,
0x61, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x30,
0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b, 0x0a, 0x7d
};
unsigned int vertexShader_glsl_len = 215;

8
u03-3/vertexShader.glsl Normal file
View File

@ -0,0 +1,8 @@
#version 330 core
layout (location = 0) in vec2 aPosition;
layout (location = 1) in float vertexValue;
out float fragmentValue;
void main() {
fragmentValue = vertexValue;
gl_Position = vec4(aPosition, 0.0, 1.0);
}