]> git.sesse.net Git - movit/commitdiff
Move the widgets into its own source file.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 13:16:50 +0000 (15:16 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 13:16:50 +0000 (15:16 +0200)
Makefile
test.cpp
texture_enum.h [new file with mode: 0644]
util.h
widgets.cpp [new file with mode: 0644]
widgets.h [new file with mode: 0644]

index deaf0663ac658efade94db1ddd1ab152debf75d5..4667f41dd3cd38d303aeefa1f286ff43f13a9ca9 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CC=gcc
 CXX=g++
 CXXFLAGS=-Wall
 LDFLAGS=-lSDL -lSDL_image -lGL
-OBJS=test.o util.o
+OBJS=test.o util.o widgets.o
 
 test: $(OBJS)
        $(CXX) -o test $(OBJS) $(LDFLAGS)
index 6de5452a7270601cf12d1c5d8f3390da6cdf2057..974fe53998c5ad5198919cd26acd67fdba8b58da 100644 (file)
--- a/test.cpp
+++ b/test.cpp
@@ -5,8 +5,6 @@
 #define HEIGHT 720
 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
 
-#define HSV_WHEEL_SIZE 128
-
 #include <math.h>
 #include <time.h>
 
 #include <GL/glext.h>
 
 #include "util.h"
-
-#ifdef NDEBUG
-#define check_error()
-#else
-#define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at line %d\n", err, __LINE__); exit(1); } }
-#endif
+#include "widgets.h"
+#include "texture_enum.h"
 
 unsigned char result[WIDTH * HEIGHT * 4];
 
@@ -87,13 +81,6 @@ private:
        std::vector<Effect *> effects;
 };
 
-enum textures {
-       SOURCE_IMAGE = 1,
-       SRGB_LUT = 2,
-       SRGB_REVERSE_LUT = 3,
-       HSV_WHEEL = 4,
-};
-
 GLhandleARB read_shader(const char* filename, GLenum type)
 {
        static char buf[131072];
@@ -192,137 +179,6 @@ void draw_picture_quad(GLint prog, int frame)
        check_error();
 }
 
-void draw_hsv_wheel(float y, float rad, float theta, float value)
-{
-       glUseProgramObjectARB(0);
-       check_error();
-       glActiveTexture(GL_TEXTURE0);
-       check_error();
-       glEnable(GL_TEXTURE_2D);
-       check_error();
-       glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
-       check_error();
-       glActiveTexture(GL_TEXTURE1);
-       check_error();
-       glBindTexture(GL_TEXTURE_2D, 0);
-       check_error();
-       glActiveTexture(GL_TEXTURE0);
-       glEnable(GL_BLEND);
-       check_error();
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       check_error();
-
-       // wheel
-       glBegin(GL_QUADS);
-
-       glTexCoord2f(0.0f, 1.0f);
-       glVertex2f(0.0f, y);
-
-       glTexCoord2f(1.0f, 1.0f);
-       glVertex2f(0.2f * 9.0f / 16.0f, y);
-
-       glTexCoord2f(1.0f, 0.0f);
-       glVertex2f(0.2f * 9.0f / 16.0f, y + 0.2f);
-
-       glTexCoord2f(0.0f, 0.0f);
-       glVertex2f(0.0f, y + 0.2f);
-
-       glEnd();
-
-       // wheel selector
-       glDisable(GL_TEXTURE_2D);
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glPointSize(5.0f);
-       glBegin(GL_POINTS);
-       glVertex2f((0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f, y + 0.1f - rad * sin(theta) * 0.1f);
-       glEnd();
-       
-       // value slider
-       glDisable(GL_TEXTURE_2D);
-       glBegin(GL_QUADS);
-
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glVertex2f(0.22f * 9.0f / 16.0f, y);
-       glVertex2f(0.24f * 9.0f / 16.0f, y);
-
-       glColor3f(1.0f, 1.0f, 1.0f);
-       glVertex2f(0.24f * 9.0f / 16.0f, y + 0.2f);
-       glVertex2f(0.22f * 9.0f / 16.0f, y + 0.2f);
-
-       glEnd();
-
-       // value selector
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glPointSize(5.0f);
-       glBegin(GL_POINTS);
-       glVertex2f(0.23f * 9.0f / 16.0f, y + value * 0.2f);
-       glEnd();
-
-       glColor3f(1.0f, 1.0f, 1.0f);
-}
-
-void draw_saturation_bar(float y)
-{
-       glUseProgramObjectARB(0);
-       check_error();
-
-       // value slider
-       glDisable(GL_TEXTURE_2D);
-       glBegin(GL_QUADS);
-
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glVertex2f(0.0f * 9.0f / 16.0f, y + 0.02f);
-       glVertex2f(0.0f * 9.0f / 16.0f, y);
-
-       glColor3f(1.0f, 1.0f, 1.0f);
-       glVertex2f(0.2f * 9.0f / 16.0f, y);
-       glVertex2f(0.2f * 9.0f / 16.0f, y + 0.02f);
-
-       glEnd();
-
-       // value selector
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glPointSize(5.0f);
-       glBegin(GL_POINTS);
-       glVertex2f(0.2f * (saturation / 4.0f) * 9.0f / 16.0f, y + 0.01f);
-       glEnd();
-
-       glColor3f(1.0f, 1.0f, 1.0f);
-}
-
-void make_hsv_wheel_texture()
-{
-       static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
-       for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
-               for (int x = 0; x < HSV_WHEEL_SIZE; ++x) {
-                       float yf = 2.0f * y / (float)(HSV_WHEEL_SIZE) - 1.0f;
-                       float xf = 2.0f * x / (float)(HSV_WHEEL_SIZE) - 1.0f;
-                       float rad = hypot(xf, yf);
-                       float theta = atan2(yf, xf);
-
-                       float r, g, b;
-                       hsv2rgb(theta, rad, 1.0f, &r, &g, &b);
-                       hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 0] = lrintf(r * 255.0f);
-                       hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 1] = lrintf(g * 255.0f);
-                       hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 2] = lrintf(b * 255.0f);
-
-                       if (rad > 1.0f) {
-                               hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
-                       } else {
-                               hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
-                       }
-               }
-               printf("\n");
-       }
-
-       glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
-       check_error();
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-       check_error();
-       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
-       check_error();
-}
-
 void update_hsv()
 {
        hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
@@ -340,21 +196,6 @@ void update_hsv()
        printf("\n");
 }
 
-void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
-{
-       if (xf < 0.2f && yf < 0.2f) {
-               float xp = 2.0f * xf / 0.2f - 1.0f;
-               float yp = -(2.0f * yf / 0.2f - 1.0f);
-               *rad = hypot(xp, yp);
-               *theta = atan2(yp, xp);
-               if (*rad > 1.0) {
-                       *rad = 1.0;
-               }
-       } else if (xf >= 0.22f && xf <= 0.24f) {
-               *value = yf / 0.2f;
-       }
-}
-
 void mouse(int x, int y)
 {
        float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
@@ -562,7 +403,7 @@ int main(int argc, char **argv)
                draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
                draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
                draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
-               draw_saturation_bar(0.6f);
+               draw_saturation_bar(0.6f, saturation);
 
                SDL_GL_SwapBuffers();
                check_error();
diff --git a/texture_enum.h b/texture_enum.h
new file mode 100644 (file)
index 0000000..e1e956f
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef _TEXTURE_ENUM_H
+#define _TEXTURE_ENUM_H 1
+
+enum textures {
+       SOURCE_IMAGE = 1,
+       SRGB_LUT = 2,
+       SRGB_REVERSE_LUT = 3,
+       HSV_WHEEL = 4,
+};
+
+#endif // !defined(_TEXTURE_ENUM_H)
+
diff --git a/util.h b/util.h
index 0d499453048d2e9bebb2ea50c8d56fd75c01c67d..3abd8d124a6e11f6087f0e6ccb62765db238b39b 100644 (file)
--- a/util.h
+++ b/util.h
@@ -1,7 +1,16 @@
 #ifndef _UTIL_H
 #define _UTIL_H 1
 
+#include <stdio.h>
+#include <stdlib.h>
+
 // assumes h in [0, 2pi> or [-pi, pi>
 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b);
 
+#ifdef NDEBUG
+#define check_error()
+#else
+#define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at line %d\n", err, __LINE__); exit(1); } }
+#endif
+
 #endif // !defined(_UTIL_H)
diff --git a/widgets.cpp b/widgets.cpp
new file mode 100644 (file)
index 0000000..529212f
--- /dev/null
@@ -0,0 +1,158 @@
+#define GL_GLEXT_PROTOTYPES 1
+
+#include <math.h>
+#include <GL/gl.h>
+#include <GL/glext.h>
+
+#include "widgets.h"
+#include "util.h"
+#include "texture_enum.h"
+
+#define HSV_WHEEL_SIZE 128
+
+void draw_hsv_wheel(float y, float rad, float theta, float value)
+{
+       glUseProgramObjectARB(0);
+       check_error();
+       glActiveTexture(GL_TEXTURE0);
+       check_error();
+       glEnable(GL_TEXTURE_2D);
+       check_error();
+       glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
+       check_error();
+       glActiveTexture(GL_TEXTURE1);
+       check_error();
+       glBindTexture(GL_TEXTURE_2D, 0);
+       check_error();
+       glActiveTexture(GL_TEXTURE0);
+       glEnable(GL_BLEND);
+       check_error();
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       check_error();
+
+       // wheel
+       glBegin(GL_QUADS);
+
+       glTexCoord2f(0.0f, 1.0f);
+       glVertex2f(0.0f, y);
+
+       glTexCoord2f(1.0f, 1.0f);
+       glVertex2f(0.2f * 9.0f / 16.0f, y);
+
+       glTexCoord2f(1.0f, 0.0f);
+       glVertex2f(0.2f * 9.0f / 16.0f, y + 0.2f);
+
+       glTexCoord2f(0.0f, 0.0f);
+       glVertex2f(0.0f, y + 0.2f);
+
+       glEnd();
+
+       // wheel selector
+       glDisable(GL_TEXTURE_2D);
+       glColor3f(0.0f, 0.0f, 0.0f);
+       glPointSize(5.0f);
+       glBegin(GL_POINTS);
+       glVertex2f((0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f, y + 0.1f - rad * sin(theta) * 0.1f);
+       glEnd();
+       
+       // value slider
+       glDisable(GL_TEXTURE_2D);
+       glBegin(GL_QUADS);
+
+       glColor3f(0.0f, 0.0f, 0.0f);
+       glVertex2f(0.22f * 9.0f / 16.0f, y);
+       glVertex2f(0.24f * 9.0f / 16.0f, y);
+
+       glColor3f(1.0f, 1.0f, 1.0f);
+       glVertex2f(0.24f * 9.0f / 16.0f, y + 0.2f);
+       glVertex2f(0.22f * 9.0f / 16.0f, y + 0.2f);
+
+       glEnd();
+
+       // value selector
+       glColor3f(0.0f, 0.0f, 0.0f);
+       glPointSize(5.0f);
+       glBegin(GL_POINTS);
+       glVertex2f(0.23f * 9.0f / 16.0f, y + value * 0.2f);
+       glEnd();
+
+       glColor3f(1.0f, 1.0f, 1.0f);
+}
+
+void draw_saturation_bar(float y, float saturation)
+{
+       glUseProgramObjectARB(0);
+       check_error();
+
+       // value slider
+       glDisable(GL_TEXTURE_2D);
+       glBegin(GL_QUADS);
+
+       glColor3f(0.0f, 0.0f, 0.0f);
+       glVertex2f(0.0f * 9.0f / 16.0f, y + 0.02f);
+       glVertex2f(0.0f * 9.0f / 16.0f, y);
+
+       glColor3f(1.0f, 1.0f, 1.0f);
+       glVertex2f(0.2f * 9.0f / 16.0f, y);
+       glVertex2f(0.2f * 9.0f / 16.0f, y + 0.02f);
+
+       glEnd();
+
+       // value selector
+       glColor3f(0.0f, 0.0f, 0.0f);
+       glPointSize(5.0f);
+       glBegin(GL_POINTS);
+       glVertex2f(0.2f * (saturation / 4.0f) * 9.0f / 16.0f, y + 0.01f);
+       glEnd();
+
+       glColor3f(1.0f, 1.0f, 1.0f);
+}
+
+void make_hsv_wheel_texture()
+{
+       static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
+       for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
+               for (int x = 0; x < HSV_WHEEL_SIZE; ++x) {
+                       float yf = 2.0f * y / (float)(HSV_WHEEL_SIZE) - 1.0f;
+                       float xf = 2.0f * x / (float)(HSV_WHEEL_SIZE) - 1.0f;
+                       float rad = hypot(xf, yf);
+                       float theta = atan2(yf, xf);
+
+                       float r, g, b;
+                       hsv2rgb(theta, rad, 1.0f, &r, &g, &b);
+                       hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 0] = lrintf(r * 255.0f);
+                       hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 1] = lrintf(g * 255.0f);
+                       hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 2] = lrintf(b * 255.0f);
+
+                       if (rad > 1.0f) {
+                               hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
+                       } else {
+                               hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
+                       }
+               }
+               printf("\n");
+       }
+
+       glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       check_error();
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
+       check_error();
+}
+
+void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
+{
+       if (xf < 0.2f && yf < 0.2f) {
+               float xp = 2.0f * xf / 0.2f - 1.0f;
+               float yp = -(2.0f * yf / 0.2f - 1.0f);
+               *rad = hypot(xp, yp);
+               *theta = atan2(yp, xp);
+               if (*rad > 1.0) {
+                       *rad = 1.0;
+               }
+       } else if (xf >= 0.22f && xf <= 0.24f) {
+               *value = yf / 0.2f;
+       }
+}
+
diff --git a/widgets.h b/widgets.h
new file mode 100644 (file)
index 0000000..9ae00cf
--- /dev/null
+++ b/widgets.h
@@ -0,0 +1,9 @@
+#ifndef _WIDGETS_H
+#define _WIDGETS_H 1
+
+void draw_hsv_wheel(float y, float rad, float theta, float value);
+void draw_saturation_bar(float y, float saturation);
+void make_hsv_wheel_texture();
+void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value);
+
+#endif // !defined(_WIDGETS_H)