Commit 1854e7ac authored by Jordan Justen's avatar Jordan Justen
Browse files

wflgears wip


Signed-off-by: Jordan Justen's avatarJordan Justen <jordan.l.justen@intel.com>
parent 1e9817ee
......@@ -21,3 +21,27 @@ install(
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT utils
)
# ----------------------------------------------------------------------------
# Target: wflgears (executable)
# ----------------------------------------------------------------------------
add_executable(wflgears wflgears.c wflgears_legacy.c wutils.c)
target_link_libraries(wflgears ${waffle_libname} m)
set_target_properties(wflgears
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
if(waffle_on_mac)
set_target_properties(wflgears
PROPERTIES
COMPILE_FLAGS "-ObjC"
)
endif()
install(
TARGETS wflgears
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT utils
)
/*
* 3-D gear wheels. This program is in the public domain.
*
* Command line options:
* -info print GL implementation information
* -exit automatically exit after 30 seconds
* -noanim turn off animation
*
*
* Brian Paul
*
* Conversion to Waffle by Jordan Justen
*/
/* Conversion to GLUT by Mark J. Kilgard */
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "glut_wrap.h"
#ifndef M_PI
#define M_PI 3.14159265
#endif
#include "wflgears.h"
const char *wutils_utility_name = "wflgears";
const char *wutils_utility_Name = "Wflgears";
const char *usage_message =
"Usage:\n"
" wflgears <Required Parameters> [Options]\n"
"\n"
"Description:\n"
" Create an OpenGL or OpenGL ES context and print information about it.\n"
"\n"
"Required Parameters:\n"
" -p, --platform\n"
" One of: android, cgl, gbm, glx, wayland or x11_egl\n"
"\n"
" -a, --api\n"
" One of: gl, gles1, gles2 or gles3\n"
"\n"
"Options:\n"
" -V, --version\n"
" For example --api=gl --version=3.2 would request OpenGL 3.2.\n"
"\n"
" --profile\n"
" One of: core, compat or none\n"
"\n"
" -v, --verbose\n"
" Print more information.\n"
"\n"
" --forward-compatible\n"
" Create a forward-compatible context.\n"
"\n"
" --debug-context\n"
" Create a debug context.\n"
"\n"
" -h, --help\n"
" Print wflgears usage information.\n"
"\n"
"Examples:\n"
" wflgears --platform=glx --api=gl\n"
" wflgears --platform=x11_egl --api=gl --version=3.2 --profile=core\n"
" wflgears --platform=wayland --api=gles3\n"
" wflgears --platform=gbm --api=gl --version=3.2 --verbose\n"
" wflgears -p gbm -a gl -V 3.2 -v\n"
;
static GLint T0 = 0;
static GLint Frames = 0;
static GLint autoexit = 0;
static GLint win = 0;
static GLboolean Visible = GL_TRUE;
static GLboolean Animate = GL_TRUE;
static GLfloat viewDist = 40.0;
/**
Draw a gear wheel. You'll probably want to call this function when
building a display list since we do a lot of trig here.
Input: inner_radius - radius of hole at center
outer_radius - radius at center of teeth
width - width of gear
teeth - number of teeth
tooth_depth - depth of tooth
**/
static void
gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
GLint teeth, GLfloat tooth_depth)
void
wflgears_post_draw(struct waffle_window *window)
{
GLint i;
GLfloat r0, r1, r2;
GLfloat angle, da;
GLfloat u, v, len;
r0 = inner_radius;
r1 = outer_radius - tooth_depth / 2.0;
r2 = outer_radius + tooth_depth / 2.0;
da = 2.0 * M_PI / teeth / 4.0;
glShadeModel(GL_FLAT);
glNormal3f(0.0, 0.0, 1.0);
/* draw front face */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
if (i < teeth) {
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
}
}
glEnd();
/* draw front sides of teeth */
glBegin(GL_QUADS);
da = 2.0 * M_PI / teeth / 4.0;
for (i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
}
glEnd();
glNormal3f(0.0, 0.0, -1.0);
/* draw back face */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
if (i < teeth) {
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
}
}
glEnd();
/* draw back sides of teeth */
glBegin(GL_QUADS);
da = 2.0 * M_PI / teeth / 4.0;
for (i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
}
glEnd();
/* draw outward faces of teeth */
glBegin(GL_QUAD_STRIP);
for (i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
u = r2 * cos(angle + da) - r1 * cos(angle);
v = r2 * sin(angle + da) - r1 * sin(angle);
len = sqrt(u * u + v * v);
u /= len;
v /= len;
glNormal3f(v, -u, 0.0);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
glNormal3f(cos(angle), sin(angle), 0.0);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
glNormal3f(v, -u, 0.0);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
glNormal3f(cos(angle), sin(angle), 0.0);
bool ok;
static bool window_shown = false;
if (!window_shown) {
ok = waffle_window_show(window);
if (!ok) {
error_printf("Wflgears", "Error showing window");
}
window_shown = true;
}
glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
glEnd();
ok = waffle_window_swap_buffers(window);
if (!ok) {
error_printf("Wflgears", "Error swapping buffers");
}
glShadeModel(GL_SMOOTH);
Frames++;
/* draw inside radius cylinder */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glNormal3f(-cos(angle), -sin(angle), 0.0);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
{
GLint t = elapsed_ms();
if (t - T0 >= 5000) {
GLfloat seconds = (t - T0) / 1000.0;
GLfloat fps = Frames / seconds;
printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
fflush(stdout);
T0 = t;
Frames = 0;
}
}
glEnd();
}
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
static GLint gear1, gear2, gear3;
static GLfloat angle = 0.0;
static void
cleanup(void)
static bool
display_wflgears(struct waffle_window *window, const struct wutils_config_attrs *config)
{
glDeleteLists(gear1, 1);
glDeleteLists(gear2, 1);
glDeleteLists(gear3, 1);
glutDestroyWindow(win);
if (config->api != WAFFLE_CONTEXT_OPENGL) {
error_printf("Wflgears", "OpenGL ES is not currently supported");
}
if (config->profile == WAFFLE_CONTEXT_CORE_PROFILE) {
error_printf("Wflgears", "Core profiles are not currently supported");
}
T0 = elapsed_ms();
return display_wflgears_legacy_gl(window);
}
static void
draw(void)
int
main(int argc, char **argv)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
bool ok;
int i;
glTranslatef(0.0, 0.0, -viewDist);
struct options opts = {0};
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
int32_t init_attrib_list[3];
glPushMatrix();
glTranslatef(-3.0, -2.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glCallList(gear1);
glPopMatrix();
struct waffle_display *dpy;
struct waffle_config *config;
struct waffle_context *ctx;
struct waffle_window *window;
glPushMatrix();
glTranslatef(3.1, -2.0, 0.0);
glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
glCallList(gear2);
glPopMatrix();
#ifdef __APPLE__
cocoa_init();
#endif
glPushMatrix();
glTranslatef(-3.1, 4.2, 0.0);
glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
glCallList(gear3);
glPopMatrix();
ok = parse_args(argc, argv, &opts);
if (!ok)
exit(EXIT_FAILURE);
glPopMatrix();
i = 0;
init_attrib_list[i++] = WAFFLE_PLATFORM;
init_attrib_list[i++] = opts.platform;
init_attrib_list[i++] = WAFFLE_NONE;
glutSwapBuffers();
ok = waffle_init(init_attrib_list);
if (!ok)
error_waffle();
Frames++;
dpy = waffle_display_connect(NULL);
if (!dpy)
error_waffle();
{
GLint t = glutGet(GLUT_ELAPSED_TIME);
if (t - T0 >= 5000) {
GLfloat seconds = (t - T0) / 1000.0;
GLfloat fps = Frames / seconds;
printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
fflush(stdout);
T0 = t;
Frames = 0;
if ((t >= 999.0 * autoexit) && (autoexit)) {
cleanup();
exit(0);
}
if (!waffle_display_supports_context_api(dpy, opts.context_api)) {
error_printf("Wflgears", "Display does not support %s",
waffle_enum_to_string(opts.context_api));
}
}
}
glGetError = waffle_get_proc_address("glGetError");
if (!glGetError)
error_get_gl_symbol("glGetError");
static void
idle(void)
{
static double t0 = -1.;
double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
if (t0 < 0.0)
t0 = t;
dt = t - t0;
t0 = t;
glGetIntegerv = waffle_get_proc_address("glGetIntegerv");
if (!glGetIntegerv)
error_get_gl_symbol("glGetIntegerv");
angle += 70.0 * dt; /* 70 degrees per second */
angle = fmod(angle, 360.0); /* prevents eventual overflow */
glGetString = waffle_get_proc_address("glGetString");
if (!glGetString)
error_get_gl_symbol("glGetString");
glutPostRedisplay();
}
glGetStringi = waffle_get_proc_address("glGetStringi");
static void
update_idle_func(void)
{
if (Visible && Animate)
glutIdleFunc(idle);
else
glutIdleFunc(NULL);
}
const struct wutils_config_attrs config_attrs = {
.api = opts.context_api,
.profile = opts.context_profile,
.major = opts.context_major,
.minor = opts.context_minor,
.forward_compat = opts.context_forward_compatible,
.debug = opts.context_debug,
};
wutils_create_context(dpy, config_attrs, &ctx, &config);
/* change view angle, exit upon ESC */
/* ARGSUSED1 */
static void
key(unsigned char k, int x, int y)
{
switch (k) {
case 'z':
view_rotz += 5.0;
break;
case 'Z':
view_rotz -= 5.0;
break;
case 'd':
viewDist += 1.0;
break;
case 'D':
viewDist -= 1.0;
break;
case 'a':
Animate = !Animate;
update_idle_func();
break;
case 27: /* Escape */
cleanup();
exit(0);
break;
default:
return;
}
glutPostRedisplay();
}
window = waffle_window_create(config, WINDOW_WIDTH, WINDOW_HEIGHT);
if (!window)
error_waffle();
/* change view angle */
/* ARGSUSED1 */
static void
special(int k, int x, int y)
{
switch (k) {
case GLUT_KEY_UP:
view_rotx += 5.0;
break;
case GLUT_KEY_DOWN:
view_rotx -= 5.0;
break;
case GLUT_KEY_LEFT:
view_roty += 5.0;
break;
case GLUT_KEY_RIGHT:
view_roty -= 5.0;
break;
default:
return;
}
glutPostRedisplay();
}
ok = waffle_make_current(dpy, window, ctx);
if (!ok)
error_waffle();
/* new window size or exposure */
static void
reshape(int width, int height)
{
GLfloat h = (GLfloat) height / (GLfloat) width;
ok = display_wflgears(window, &config_attrs);
if (!ok)
error_waffle();
glViewport(0, 0, (GLint) width, (GLint) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -h, h, 5.0, 200.0);
glMatrixMode(GL_MODELVIEW);
}
ok = waffle_window_destroy(window);
if (!ok)
error_waffle();
static void
init(int argc, char *argv[])
{
static GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0};
static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
GLint i;
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
/* make the gears */
gear1 = glGenLists(1);
glNewList(gear1, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
gear(1.0, 4.0, 1.0, 20, 0.7);
glEndList();
gear2 = glGenLists(1);
glNewList(gear2, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
gear(0.5, 2.0, 2.0, 10, 0.7);
glEndList();
gear3 = glGenLists(1);
glNewList(gear3, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
gear(1.3, 2.0, 0.5, 10, 0.7);
glEndList();
glEnable(GL_NORMALIZE);
for ( i=1; i<argc; i++ ) {
if (strcmp(argv[i], "-info")==0) {
printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
}
else if ( strcmp(argv[i], "-exit")==0) {
autoexit = 30;
printf("Auto Exit after %i seconds.\n", autoexit );
}
else if (strcmp(argv[i], "-noanim") == 0) {
Animate = GL_FALSE;
}
}
}
ok = waffle_context_destroy(ctx);
if (!ok)
error_waffle();
ok = waffle_config_destroy(config);
if (!ok)
error_waffle();
static void
visible(int vis)
{
Visible = vis;
update_idle_func();
}
ok = waffle_display_disconnect(dpy);
if (!ok)
error_waffle();
int main(int argc, char *argv[])
{
glutInitWindowSize(300, 300);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
win = glutCreateWindow("Gears");
init(argc, argv);
glutDisplayFunc(draw);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutVisibilityFunc(visible);
update_idle_func();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
#ifdef __APPLE__
cocoa_finish();
#endif
return EXIT_SUCCESS;
}
/*
* 3-D gear wheels. This program is in the public domain.
*
* Brian Paul
*
* Conversion to Waffle by Jordan Justen
*/
#pragma once
#include "wutils.h"
#define WINDOW_WIDTH 300
#define WINDOW_HEIGHT 300
#ifndef M_PI