u02
This commit is contained in:
parent
c986407284
commit
94a2fee653
123
src/main.c
123
src/main.c
|
@ -1,5 +1,6 @@
|
|||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
@ -21,6 +22,40 @@ GLuint initialWindowHeight = 600;
|
|||
int frameCount = 0;
|
||||
struct timespec last_time, current_time;
|
||||
|
||||
typedef struct ColorRGB {
|
||||
GLfloat r;
|
||||
GLfloat g;
|
||||
GLfloat b;
|
||||
} ColorRGB;
|
||||
|
||||
// Color Conversion Functions from https://gist.github.com/ciembor/1494530
|
||||
GLfloat hueToRgb(GLfloat p, GLfloat q, GLfloat t) {
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1./6) return p + (q - p) * 6 * t;
|
||||
if (t < 1./2) return q;
|
||||
if (t < 2./3) return p + (q - p) * (2./3 - t) * 6;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
// Color Conversion Functions by Copilot
|
||||
ColorRGB hslToRgb(GLfloat h, GLfloat s, GLfloat l) {
|
||||
ColorRGB result = {0, 0, 0};
|
||||
|
||||
if(0 == s) {
|
||||
result.r = result.g = result.b = l; // achromatic
|
||||
} else {
|
||||
float q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
float p = 2 * l - q;
|
||||
result.r = hueToRgb(p, q, h + 1./3);
|
||||
result.g = hueToRgb(p, q, h);
|
||||
result.b = hueToRgb(p, q, h - 1./3);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void initialiseStatusDisplay() {
|
||||
clock_gettime(CLOCK_MONOTONIC, &last_time);
|
||||
}
|
||||
|
@ -72,19 +107,72 @@ void init(void) {
|
|||
INFO("Shader Program Done.");
|
||||
|
||||
// create triangle buffer
|
||||
GLfloat triangleVertices[] =
|
||||
{
|
||||
//X //Y // R //G /B
|
||||
0.0f, 0.5f, 0.0f, 1.0f, 1.0f,
|
||||
-0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 1.0f, 1.0f, 0.0f
|
||||
/**
|
||||
* -0.35 0.35
|
||||
* | -0.2 0.2 |
|
||||
* | | | |
|
||||
*
|
||||
* +----+ +----+ --- 0.6
|
||||
* | | | |
|
||||
* | | | |
|
||||
* | +--------+ | --- 0.1
|
||||
* | |
|
||||
* | +--------+ | --- -0.1
|
||||
* | | | |
|
||||
* | | | |
|
||||
* +----+ +----+ --- -0.6
|
||||
*
|
||||
* +------------------+ --- -0.7
|
||||
* | |
|
||||
* +------------------+ --- -0.9
|
||||
*
|
||||
*/
|
||||
|
||||
GLfloat vertices[] = {
|
||||
// X // Y
|
||||
|
||||
// left vertical bar
|
||||
-0.35f, -0.6f,
|
||||
-0.35f, 0.6f,
|
||||
-0.2f, 0.6f,
|
||||
|
||||
-0.35f, -0.6f,
|
||||
-0.2f, 0.6f,
|
||||
-0.2f, -0.6f,
|
||||
|
||||
// right vertical bar
|
||||
0.35f, -0.6f,
|
||||
0.35f, 0.6f,
|
||||
0.2f, 0.6f,
|
||||
|
||||
0.35f, -0.6f,
|
||||
0.2f, 0.6f,
|
||||
0.2f, -0.6f,
|
||||
|
||||
// middle bar
|
||||
-0.2f, -0.1f,
|
||||
-0.2f, 0.1f,
|
||||
0.2f, 0.1f,
|
||||
|
||||
-0.2f, -0.1f,
|
||||
0.2f, 0.1f,
|
||||
0.2f, -0.1f,
|
||||
|
||||
// bottom bar
|
||||
-0.35f, -0.7f,
|
||||
-0.35f, -0.9f,
|
||||
0.35f, -0.9f,
|
||||
|
||||
-0.35f, -0.7f,
|
||||
0.35f, -0.9f,
|
||||
0.35f, -0.7f
|
||||
};
|
||||
|
||||
DEBUG("Creating vertext buffer");
|
||||
GLuint vertexBuffer;
|
||||
glGenBuffers(1, &vertexBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
DEBUG("Creating vertex array object");
|
||||
|
@ -99,15 +187,11 @@ void init(void) {
|
|||
2, // number of values to read
|
||||
GL_FLOAT, // type of value
|
||||
GL_FALSE, // if values are normalised
|
||||
5 * sizeof(GLfloat), // stride - distance between vertices
|
||||
2 * sizeof(GLfloat), // stride - distance between vertices
|
||||
0 // start offset
|
||||
);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// vertex color data
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
|
@ -119,13 +203,26 @@ void init(void) {
|
|||
INFO("--- Initialisation done ---");
|
||||
}
|
||||
|
||||
GLfloat currentHue = 0.0f;
|
||||
|
||||
void draw(void) {
|
||||
updateStatusDisplay();
|
||||
|
||||
currentHue += 0.001f;
|
||||
if (currentHue > 1.0) currentHue = 0.0f;
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glUseProgram(program);
|
||||
|
||||
GLuint colorLocation = glGetUniformLocation(program, "uColor");
|
||||
ColorRGB color = hslToRgb(currentHue, 1.0f, 0.5f);
|
||||
glUniform3f(colorLocation, color.r, color.g, color.b);
|
||||
|
||||
GLuint positionLocation = glGetUniformLocation(program, "uPosition");
|
||||
glUniform2f(positionLocation, cos(currentHue * 2 * M_PI) * 0.2f, sin(currentHue * 2 * M_PI) * 0.2f);
|
||||
|
||||
glBindVertexArray(vertexArrayObject);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 48);
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec2 aPosition;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
uniform vec3 uColor;
|
||||
uniform vec2 uPosition;
|
||||
|
||||
out vec3 vertexColor;
|
||||
|
||||
void main() {
|
||||
vertexColor = aColor;
|
||||
gl_Position = vec4(aPosition, 0.0, 1.0);
|
||||
vertexColor = uColor;
|
||||
gl_Position = vec4(uPosition + aPosition, 0.0, 1.0);
|
||||
}
|
Loading…
Reference in New Issue