Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • 2016ws
  • 2017ws
  • 2018ws
  • 2019ws
  • 2020ws
  • 2021ws
  • 2022ws
  • 2023ws
  • 2024ws
9 results

Target

Select target project
  • pgerwinski/hp
  • bwildenhain/hp
  • Daniel.Eisi/hp
  • aahrens/hp
4 results
Select Git revision
  • 2016ws
  • 2017ws
  • 2018ws
  • master
4 results
Show changes
Showing
with 0 additions and 909 deletions
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
phi += dt * omega;
omega += dt * (-g / l) * sin (phi);
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (1.0, 1.0, 0.8);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
phi += dt * omega;
omega += dt * (-g / l) * phi;
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 0.5 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi_with_sin = phi0;
double omega_with_sin= omega0;
double phi_without_sin = phi0;
double omega_without_sin= omega0;
void draw_pendulum (double phi)
{
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, 1.0, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glutSwapBuffers ();
}
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
draw_pendulum (phi_with_sin);
set_material_color (1.0, 0.8, 0.8);
draw_pendulum (phi_without_sin);
set_material_color (0.8, 1.0, 0.8);
double A = phi0;
double B = 0.5 * M_PI; /* 90° */
double phi_analytic = A * sin (sqrt (g / l) * t + B);
draw_pendulum (phi_analytic);
glFlush ();
}
void timer_handler (int value)
{
t += dt;
phi_with_sin += dt * omega_with_sin; /* geg. Differentialgleichung */
omega_with_sin += - dt * g / l * sin (phi_with_sin); /* geg. Differentialgleichung */
/* _ohne_ Kleinwinkelnäherung */
phi_without_sin += dt * omega_without_sin; /* geg. Differentialgleichung */
omega_without_sin += - dt * g / l * phi_without_sin; /* geg. Differentialgleichung */
/* _mit_ Kleinwinkelnäherung */
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutPostRedisplay ();
glutMainLoop ();
return 0;
}
20171106/photo-20171106-170748.jpg

130 KiB

README: Fadenpendel: Aufstellung der Differentialgleichung
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <png.h>
#include "textured-spheres.h"
#ifndef __USE_POSIX
extern int fileno (FILE *stream);
#endif
static png_byte *load_png_file (char *filename, unsigned *w, unsigned *h)
{
FILE *f = fopen (filename, "rb");
if (!f)
return NULL;
/* is it a PNG file? */
png_byte buf[8];
if (fread (buf, 1, 8, f) != 8)
{
fclose (f);
return NULL;
}
if (!png_check_sig (buf, 8))
{
fclose (f);
return NULL;
}
png_struct *png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr)
{
fclose (f);
return NULL;
}
png_info *info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr)
{
fclose (f);
png_destroy_read_struct (&png_ptr, NULL, NULL);
return NULL;
}
if (setjmp (png_jmpbuf (png_ptr)))
{
fclose (f);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
png_init_io (png_ptr, f);
png_set_sig_bytes (png_ptr, 8);
png_read_info (png_ptr, info_ptr);
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int color_type;
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, NULL, NULL, NULL);
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
if (bit_depth == 16)
png_set_strip_16 (png_ptr);
/* expand paletted colors into true RGB triplets */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand (png_ptr);
/* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand (png_ptr);
/* expand paletted or RGB images with transparency to full alpha channels
so the data will be available as RGBA quartets. */
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand (png_ptr);
/* transform grayscale images into rgb */
if (color_type == PNG_COLOR_TYPE_GRAY
|| color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb (png_ptr);
if (color_type & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha (png_ptr);
png_read_update_info (png_ptr, info_ptr);
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, NULL, NULL, NULL);
if (color_type != PNG_COLOR_TYPE_RGB
&& color_type != PNG_COLOR_TYPE_RGB_ALPHA)
{
fclose (f);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
png_uint_32 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
if (row_bytes & 0x01)
row_bytes++;
png_byte *png_pixels = malloc (row_bytes * height * sizeof (png_byte));
if (png_pixels == NULL)
{
fclose (f);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
png_byte **row_pointers = malloc (height * sizeof (png_bytep));
if (row_pointers == NULL)
{
fclose (f);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
free (png_pixels);
png_pixels = NULL;
return NULL;
}
int i;
for (i = 0; i < height; i++)
row_pointers[i] = png_pixels + i * row_bytes;
png_read_image (png_ptr, row_pointers);
png_read_end (png_ptr, info_ptr);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fclose (f);
if (w)
*w = width;
if (h)
*h = height;
free (row_pointers);
return png_pixels;
}
void init_texture (char *image_filename, GLuint *texture)
{
unsigned image_width, image_height;
png_byte *image = load_png_file (image_filename, &image_width, &image_height);
if (!image)
{
fprintf (stderr, "textured-spheres.c: cannot open texture file \"%s\"",
image_filename);
exit (1);
}
glGenTextures (1, texture);
glBindTexture (GL_TEXTURE_2D, *texture);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, image);
}
void draw_textured_sphere (GLuint texture, GLdouble radius, GLint slices, GLint stacks)
{
static GLfloat white_color[] = { 1.0, 1.0, 1.0 };
glMaterialfv (GL_FRONT, GL_AMBIENT, white_color);
glMaterialfv (GL_FRONT, GL_DIFFUSE, white_color);
glBindTexture (GL_TEXTURE_2D, texture);
glEnable (GL_TEXTURE_2D);
GLUquadric *sphere = gluNewQuadric ();
gluQuadricTexture (sphere, GL_TRUE);
gluSphere (sphere, radius, slices, stacks);
glDisable (GL_TEXTURE_2D);
}
#ifndef TEXTURED_SPHERES_H
#define TEXTURED_SPHERES_H
#include <GL/gl.h>
#include <GL/glu.h>
extern void init_texture (char *image_filename, GLuint *texture);
extern void draw_textured_sphere (GLuint texture, GLdouble radius, GLint slices, GLint stacks);
#endif /* TEXTURED_SPHERES_H */
20171108/earth-texture.png

1020 KiB

GNU/Linux
~~~~~~~~~
Compilieren: gcc -Wall -O cube-3c.c opengl-magic.c \
-lGL -lGLU -lglut -o cube-3c
Aufrufen: ./cube-3c
MacOS
~~~~~
Compilieren: gcc -Wall -O cube-3c.c opengl-magic.c \
-framework OpenGL -framework GLUT -o cube-3c
Aufrufen: ./cube-3c
Cygwin mit X11
~~~~~~~~~~~~~~
Compilieren: gcc -Wall -O cube-3c.c opengl-magic.c \
-lGL -lGLU -lglut -o cube-3c
Aufrufen: DISPLAY=:0.0 ./cube-3c
oder:
einmalig: export DISPLAY=:0.0
danach: ./cube-3c
Vorher einmalig: X-Server starten
XWin -multiwindow &
Cygwin ohne X11
~~~~~~~~~~~~~~~
Compilieren: i686-pc-mingw32-gcc -I freeglut/include -L freeglut/lib \
-Wall -O cube-3c.c opengl-magic.c \
-lOpenGL32 -lGLU32 -lfreeglut -o cube-3c
Aufrufen: ./cube-3c
MinGW ohne X11
~~~~~~~~~~~~~~
Compilieren: gcc -I freeglut/include -L freeglut/lib \
-Wall -O cube-3c.c opengl-magic.c \
-lOpenGL32 -lGLU32 -lfreeglut -o cube-3c
Aufrufen: ./cube-3c
% hp-2017ws-p2.pdf - Labor Notes on Low-Level Programming
% Copyright (C) 2014, 2015, 2016, 2017 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
% README: Versuch 2, 8. und 15.11.2017: Basketball-Simulation
\documentclass[a4paper]{article}
\usepackage{pgscript}
\usepackage{multicol}
\usepackage{amsmath}
\usepackage{sfmath}
\sloppy
\pagestyle{empty}
\newcommand{\sep}{~$\cdot$~}
\newcommand{\mylicense}{CC-by-sa (Version 3.0) oder GNU GPL (Version 3 oder höher)}
\begin{document}
\makebox(0,0)[tl]{\includegraphics[scale=0.57]{logo-hochschule-bochum-cvh-text.pdf}}\hfill
\makebox(0,0)[tr]{\includegraphics[scale=0.5]{logo-hochschule-bochum.pdf}}
\par\bigskip\bigskip
\begin{center}
\Large\textbf{Praktikumsversuch 2: Basketball-Simulation}
\par\medskip
\normalsize Hardwarenahe Programmierung / Angewandte Informatik\\
Wintersemester 2017/18\sep
Prof.~Dr.~Peter Gerwinski
\end{center}
Aufgabe: Schreiben Sie ein C-Programm, das
den schrägen Wurf einer Kugel ("`Basketball"')
durch einen horizontalen Ring ("`Korb"') simuliert und visualisiert
-- sowohl ohne als auch mit Berücksichtigung der Luftreibung.
\bigskip
Hinweise:
\vspace*{-\medskipamount}
\begin{multicols}{2}
\begin{itemize}
\item
Die Beispielprogramme \file{pendulum-}\lstinline{*}\file{.c} zeigen,
wie eine Simulation mit Visualisierung
mit Hilfe der 3d-Grafikbibliothek OpenGL
programmiert werden kann.
\item
Als Basketball eignet sich eine Kugel;
alternativ ist auch eine Teekanne zulässig.
Als Basketballkorb eignet sich ein Ring (Torus).
\item
Zum Verschieben der gezeichneten Gegenstände
im dreidimensionalen Zeichenraum
eignet sich die Funktion \lstinline{glTranslatef()}.
Genau wie \lstinline{glRotatef()} wirkt sich auch \lstinline{glTranslatef()}
auf \emph{alle nachfolgenden\/} Zeichenoperationen aus,
und der Wirkungsbereich läßt sich mit
\lstinline{glPushMatrix()} und \lstinline{glPopMatrix()} begrenzen.
\item
\textbf{Bonus:}
Zusätzliche Elemente wie z.\,B.\ das Abprallen des Balls am Brett,
an der Wand oder auf dem Boden sind optional.
Falls Sie über eine Textur für den Basketball verfügen,
zeigt Ihnen das Beispielprogramm \file{orbit-1.c},
wie Sie den Ball damit ausstatten können.
\vfill\columnbreak
\item
In der Differentialgleichung (Bewegungsgleichung) für einen schrägen Wurf
sind die $x$- und $y$-Komponente entkoppelt.
\item
In beiden Komponenten gibt es jeweils einen Anfangsort
und eine Anfangsgeschwindigkeit.
\item
Bei Vernachlässigung der Luftreibung
wirkt in $x$-Richtung keinerlei Kraft;
in $y$-Richtung wirkt die Schwerkraft $F = m\cdot g$
mit $g = 9.81\frac{\text{m}}{\text{s}^2}$.
\item
Die Luftreibung wirkt grundsätzlich der Bewegung entgegengerichtet.
Wir nehmen an, daß die Stärke der Reibungskraft
proportional zur Geschwindigkeit ist (laminarer Strömungswiderstand).
\item
Auch bei der Reibungskraft
können Sie die $x$- und $y$-Komponente entkoppelt rechnen.
\end{itemize}
\bigskip
\strut\hfill\emph{Viel Erfolg!}
\vfill\strut
\end{multicols}
\vfill
\begingroup
\small
\setlength{\leftskip}{3cm}
Stand: 6.\ November 2017
% Soweit nicht anders angegeben:\\
Copyright \copyright\ 2014, 2015, 2016, 2017\quad Peter Gerwinski\\
Lizenz: \mylicense
Sie können diese Praktikumsunterlagen einschließlich Quelltext
% und Beispielprogramme\\
herunterladen unter:\\
\url{https://gitlab.cvh-server.de/pgerwinski/hp}
\endgroup
\end{document}
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
20171108/moon-texture.png

447 KiB

#if defined(__APPLE__) || defined(MACOSX)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include "opengl-magic.h"
void init_opengl (int *argcp, char **argv, char *window_name)
{
glutInit (argcp, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (1024, 768);
glutCreateWindow (window_name);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (20.0, -1.33333, 3.0, 7.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -5.0);
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
static GLfloat light0_position[] = { 1.0, 0.0, 1.0, 0.0 };
glLightfv (GL_LIGHT0, GL_POSITION, light0_position);
}
void set_material_color (float r, float g, float b)
{
GLfloat color[] = { r, g, b };
glMaterialfv (GL_FRONT, GL_AMBIENT, color);
glMaterialfv (GL_FRONT, GL_DIFFUSE, color);
}
#if defined(__APPLE__) || defined(MACOSX)
void glutSolidCylinder (double radius, double height, int slices, int stacks)
{
GLUquadricObj *q = gluNewQuadric ();
gluCylinder (q, radius, radius, height, slices, stacks);
gluDeleteQuadric (q);
}
#endif
#ifndef OPENGL_MAGIC_H
#define OPENGL_MAGIC_H
extern void init_opengl (int *argcp, char **argv, char *window_name);
extern void set_material_color (float r, float g, float b);
#ifdef __MACOSX__
extern void glutSolidCylinder (double radius, double height, int slices, int stacks);
#endif
#endif /* OPENGL_MAGIC_H */
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "opengl-magic.h"
#include "textured-spheres.h"
float t = 0.0;
GLuint earth_texture, moon_texture;
void draw_earth (void)
{
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glRotatef (100.0 * t, 0.0, 0.0, 1.0);
draw_textured_sphere (earth_texture, 0.25512, 63, 20);
glPopMatrix ();
}
void draw_moon (void)
{
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glRotatef (-90, 0.0, 0.0, 1.0);
draw_textured_sphere (moon_texture, 0.06952, 31, 10);
glPopMatrix ();
}
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glRotatef (23.44, 1.0, 0.0, -1.0);
draw_earth ();
glRotatef (5.145, 0.0, 1.0, 1.0);
glRotatef (30.0 * t, 0.0, -1.0, 0.0);
glTranslatef (0.9, 0.0, 0.0);
draw_moon ();
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += 0.05;
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Orbit");
init_texture ("earth-texture.png", &earth_texture);
init_texture ("moon-texture.png", &moon_texture);
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
phi += dt * omega;
omega += dt * (-g / l) * sin (phi);
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (1.0, 1.0, 0.8);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
phi += dt * omega;
omega += dt * (-g / l) * phi;
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 0.5 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi_with_sin = phi0;
double omega_with_sin= omega0;
double phi_without_sin = phi0;
double omega_without_sin= omega0;
void draw_pendulum (double phi)
{
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, 1.0, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glutSwapBuffers ();
}
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
draw_pendulum (phi_with_sin);
set_material_color (1.0, 0.8, 0.8);
draw_pendulum (phi_without_sin);
set_material_color (0.8, 1.0, 0.8);
double A = phi0;
double B = 0.5 * M_PI; /* 90° */
double phi_analytic = A * sin (sqrt (g / l) * t + B);
draw_pendulum (phi_analytic);
glFlush ();
}
void timer_handler (int value)
{
t += dt;
phi_with_sin += dt * omega_with_sin; /* geg. Differentialgleichung */
omega_with_sin += - dt * g / l * sin (phi_with_sin); /* geg. Differentialgleichung */
/* _ohne_ Kleinwinkelnäherung */
phi_without_sin += dt * omega_without_sin; /* geg. Differentialgleichung */
omega_without_sin += - dt * g / l * phi_without_sin; /* geg. Differentialgleichung */
/* _mit_ Kleinwinkelnäherung */
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutPostRedisplay ();
glutMainLoop ();
return 0;
}