[Vorlesung] Ergänze originalen Code (Lösungen) und Folien

This commit is contained in:
Andre Meyering 2018-11-14 13:59:53 +01:00
parent 21d5ac32b8
commit f13697a35c
14 changed files with 162283 additions and 0 deletions

160897
Code_Original/bones.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,51 @@
#include <glut.h>
int width = 640;
int height = 480;
void init(int argc, char** argv) {
glutInit(&argc, argv); // Initialisierung der GLUT Bibliothek
glutInitDisplayMode(GLUT_SINGLE); // Initialisierung des Single Buffer Modes
glutInitWindowSize(width, height); // Fenstergröße in Pixel (Breite, Hoehe)
glutInitWindowPosition(100, 100); // Fensterposition in Pixel, ausgehend vom Ursprung des Window Systems
glViewport(0,0,width,height);
glutCreateWindow("Hello world"); // Erstellen des Fensters
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
char *myText = "Hello World!";
int j;
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f((width/2)-(width/4), (height/2)-(height/4), 0.0);
glVertex3f((width/2)+(width/4), (height/2)-(height/4), 0.0);
glVertex3f((width/2)+(width/4), (height/2)+(height/4), 0.0);
glVertex3f((width/2)-(width/4), (height/2)+(height/4), 0.0);
glEnd();
glColor3f(1.0,1.0,1.0);
glRasterPos2i((width/2)-(width/4)+10,(height/2)-(height/4)+10);
for (j=0; j<strlen(myText); j++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,myText[j]);
}
glFlush();
}
int main(int argc, char** argv)
{
init(argc, argv);
glutDisplayFunc(display); // Callback-Funktion für das Fenster
glutMainLoop(); // Abgabe der Kontrolle an GLUT-Bibliothek
return 0;
}

View file

@ -0,0 +1,91 @@
#include <glut.h>
int width = 800;
int height = 600;
void init(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glViewport(0,0,width,height);
glutCreateWindow("Culling");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glOrtho(-2.0, 2.0,-2.0, 2.0, -2.0, 2.0);
gluLookAt(0.25,0.5,0.1,0,0,0,0,1,0);
////////////////////////////////////////////////////////////////////
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
////////////////////////////////////////////////////////////////////
}
void display(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
// FRONT
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// BACK
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, -0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
// LEFT
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
// RIGHT
glVertex3f( 0.5f, -0.5f, -0.5f);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
// BOTTOM
// glVertex3f(-0.5f, -0.5f, 0.5f);
// glVertex3f(-0.5f, -0.5f, -0.5f);
// glVertex3f( 0.5f, -0.5f, -0.5f);
// glVertex3f( 0.5f, -0.5f, 0.5f);
// to turn around the bottom orientation: define points in different order
// glVertex3f( 0.5f, -0.5f, 0.5f);
// glVertex3f( 0.5f, -0.5f, -0.5f);
// glVertex3f(-0.5f, -0.5f, -0.5f);
// glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
init(argc, argv);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

View file

@ -0,0 +1,183 @@
#include <glut.h>
#include <math.h>
#include <stdio.h>
#pragma warning(disable:4996)
#define PI 3.14159265358979323846
int width = 800;
int height = 600;
void init(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glViewport(0, 0, width, height);
glutCreateWindow("Programmieruebung Kreis-Plotter");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void drawCoordinateSystem(float* origin, int xLength, int yLength, int numTicksX, int numTicksY, float minX, float maxX, float minY, float maxY) {
float xTickDistance = (float) xLength / (float) numTicksX;
float yTickDistance = (float) yLength / (float) numTicksY;
int k;
float j;
float resX, resY;
char label[10];
resX = (maxX - minX) / xLength;
resY = (maxY - minY) / yLength;
glColor3f(0.0, 0.0, 0.0);
/* drawing the coordinate system */
glBegin(GL_LINES);
// x axis
glVertex2f(origin[0], origin[1]);
glVertex2f(origin[0] + xLength, origin[1]);
// y axis
glVertex2f(origin[0], origin[1]);
glVertex2f(origin[0], origin[1] + yLength);
// arrow head at x axis
glVertex2f(origin[0] + xLength, origin[1]);
glVertex2f(origin[0] + xLength - 15, origin[1] - 10);
glVertex2f(origin[0] + xLength, origin[1]);
glVertex2f(origin[0] + xLength - 15, origin[1] + 10);
// arrow head at y axis
glVertex2f(origin[0], origin[1] + yLength);
glVertex2f(origin[0] - 10, origin[1] + yLength - 15);
glVertex2f(origin[0], origin[1] + yLength);
glVertex2f(origin[0] + 10, origin[1] + yLength - 15);
glEnd();
// drawing ticks at x axis
for (j = 0; j < xLength; j = j + xTickDistance) {
glBegin(GL_LINES);
glVertex2f(origin[0] + j, origin[1] - 5);
glVertex2f(origin[0] + j, origin[1] + 5);
glEnd();
sprintf(label, "%5.2f", minX + (j*resX));
glRasterPos2i((int) round(origin[0] + j - (8 * 5)), (int) round(origin[1] - 13 - 10));
for (k = 0; k < 5; k++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, label[k]);
}
}
// drawing ticks at y axis
for (j = 0; j < yLength; j = j + yTickDistance) {
glBegin(GL_LINES);
glVertex2f(origin[0] - 5, origin[1] + j);
glVertex2f(origin[0] + 5, origin[1] + j);
glEnd();
sprintf(label, "%5.2f", minY + (j*resY));
glRasterPos2i((int) round(origin[0] - (8 * 5) - 10), (int) round(origin[1] + j - 13 / 2));
for (k = 0; k < 5; k++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, label[k]);
}
}
}
void drawCircle(float origin[2], float center[2], float radius, GLint numPoints)
{
//////////////////////////////////////////////////////////////////////////////////////////////
// - calculate numPoints points on the surface of the circle with given center and radius
// hint: see circle equation in parametric form
// - position points in the coordinate system starting at given origin
// - draw the points by OpenGL commands
float i;
float x, y;
glBegin(GL_POINTS);
for (i = 0.0; i < 2 * (float) PI; i = i + (2 * (float) PI) / (float) numPoints) {
x = origin[0] + center[0] + radius * (float) cos(i);
y = origin[1] + center[1] + radius * (float) sin(i);
glVertex2f(x, y);
}
glEnd();
//////////////////////////////////////////////////////////////////////////////////////////////
}
void display(void)
{
int n;
int xTickDistance = 50;
int yTickDistance = 50;
int numPoints = 30;
float radius[] = { 50, 100, 150 };
float center[] = { 300, 200 };
float origin[] = { 100, 100 };
GLfloat c[] = { 0.0,0.0,0.0 };
int xLength = 600;
int yLength = 400;
float size = 2;
/* settings for the coordinate system */
int numTicksX = 6;
int numTicksY = 4;
float minX = 0;
float maxX = 600;
float minY = 0;
float maxY = 400;
/* clearing the background color */
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
drawCoordinateSystem(origin, xLength, yLength, numTicksX, numTicksY, minX, maxX, minY, maxY);
for (n = 0; n < 3; n++) {
///////////////////////////////////////////////////////////////////////////////
// - set the point size different for each loop iteration
// - set the color different for each loop iteration
size = size * 2;
glPointSize(size); // set the point size
memset(c, 0, sizeof(c)); // reset array to all 0
c[n] = 1.0; // set color channel n to full intensity
glColor3fv(c);
//////////////////////////////////////////////////////////////////////////////
drawCircle(origin, center, radius[n], numPoints);
}
glFlush();
}
int main(int argc, char** argv)
{
init(argc, argv);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

View file

@ -0,0 +1,397 @@
#include <glut.h>
#include <math.h>
#include <stdio.h>
#pragma warning(disable:4996)
void mouse(int button, int state, int x, int y);
void key(unsigned char key, int x, int y);
void init(void);
void reshape(int, int);
void display(void);
int main(int, char **);
void define_menu();
void idle();
void timer(int value);
void readcloud(char* filename);
void mouseactive(int x, int y);
void mouse(int button, int state, int x, int y);
float cpoints[3 * 60000];
float ccolors[3 * 60000];
int ccoord[10 * 3 * 60000];
int maxcoords = 0;
float cpointsmax[3];
float cpointsmin[3];
int cpoints_n = 0;
float xoff;
float yoff;
float zoff;
float zoom;
int angle1;
int angle2;
const float stepsize = 0.05;
const float anglestepsize = 0.01;
int displaymodus = 1;
int pressedbutton = 0;
int startx, starty;
int startangle1;
int startangle2;
float startxoff;
float startyoff;
float startzoff;
int main(int argc, char** argv)
{
readcloud("C:\\tmp\\bones.txt"); // change this in case the point cloud is saved somewhere else.
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Doublebuffer for animation
glutInitWindowSize(800, 800);
glutInitWindowPosition(400, 100);
glutCreateWindow("Mesh Visualization");
init();
glutMouseFunc(mouse);
glutMotionFunc(mouseactive);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
printf("\n\nSTEUERUNG\nAnzeigemodi:\n");
printf("'0' nur Box\n'1' Points, Farbwerte nach Koordinate\n'2' Wireframe, Farbwerte nach Koordinate\n'3' Filled, Farbwerte nach Koordinate\n");
printf("'4' Points, Farbwerte aus Datei\n'5' Wireframe, Farbwerte aus Datei\n'6' Filled, Farbwerte aus Datei\n\n\n");
printf("Transformationen:\n linke Maustaste und x-y-Bewegung -> Rotation\n mittlere Maustaste und y-Richtung -> Zoom (entspricht einer Skalierung)\n");
printf(" rechte Maustaste und x-y-Bewegung -> Translation\n\n");
glutMainLoop();
return 0;
}
void displaycloud(int modus)
{
int i = 0;
float range[3];
for (i = 0; i < 3; i++)
range[i] = cpointsmax[i] - cpointsmin[i];
if (modus > 0)
{
if (modus == 1 || modus == 4) { // Display only the vertices
//////////////////////
// TODO: set fill mode to render only the vertices
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
//////////////////////
}
if (modus == 2 || modus == 5) { // Display the outlines of the polygons
//////////////////////
// TODO: set fill mode to render only outlines
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//////////////////////
}
if (modus == 3 || modus == 6) { // Display filled polygons
//////////////////////
// TODO: set fill mode to render filled polygons
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
/////////////////////
}
glBegin(GL_TRIANGLES); // using the polygone mode "GL_TRIANGLES"
for (i = 0; i < maxcoords + 1; i++)
{
if (modus > 3) { // Displaying colors saved in the mesh file (node wise definition!)
glColor3f(ccolors[ccoord[i] * 3], ccolors[ccoord[i] * 3 + 1], ccolors[ccoord[i] * 3 + 2]);
}
else { // Displaying interpolated colors according to the x-/y-/z-value of the point coordinates (node wise definition!)
glColor3f((cpoints[ccoord[i] * 3] - cpointsmin[0]) / range[0], (cpoints[ccoord[i] * 3 + 1] - cpointsmin[1]) / range[1], (cpoints[ccoord[i] * 3 + 2] - cpointsmin[2]) / range[2]);
}
// Definition of vertices
/////////////////////////////////////////////////////////////////////
// TODO: definition of vertices.
// note the data structures: cpoints holds all coordinate components in a 1D array:
// node j_x can be accessed via cpoints[j*3]
// node j_y can be accessed via cpoints[j*3+1]
// node j_z can be accessed via cpoints[j*3+2]
// note the data structure: ccoord holds the indices into the cpoints array in a 1D array:
// one triangle consists of 3 consecutive entries of ccoord, i.e.
// first triangle consists of the vertices referenced in ccoord[0], ccords[1], ccoord[2].
glVertex3f(cpoints[ccoord[i] * 3], cpoints[ccoord[i] * 3 + 1], cpoints[ccoord[i] * 3 + 2]);
/////////////////////////////////////////////////////////////////////
}
glEnd();
}
}
void display(void)
{
glPushMatrix();
gluLookAt(0, -zoom, 0, 0, zoom*zoom + 5, 0, 0.0, 0.0, 1.0);
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glColor3f(0.0, 0.0, 0.0);
// center and rotate
glTranslatef(xoff, yoff, zoff);
glRotatef(angle2, 1.0, 0.0, 0.0);
glRotatef(angle1, 0.0, 0.0, 1.0);
glTranslatef(-(cpointsmax[0] - cpointsmin[0]) / 2 - cpointsmin[0], -(cpointsmax[1] - cpointsmin[1]) / 2 - cpointsmin[1], -(cpointsmax[2] - cpointsmin[2]) / 2 - cpointsmin[2]);
//display
displaycloud(displaymodus);
// draw box
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(cpointsmax[0], cpointsmax[1], cpointsmax[2]);
glVertex3f(cpointsmin[0], cpointsmax[1], cpointsmax[2]);
glVertex3f(cpointsmin[0], cpointsmin[1], cpointsmax[2]);
glVertex3f(cpointsmax[0], cpointsmin[1], cpointsmax[2]);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(cpointsmax[0], cpointsmax[1], cpointsmin[2]);
glVertex3f(cpointsmin[0], cpointsmax[1], cpointsmin[2]);
glVertex3f(cpointsmin[0], cpointsmin[1], cpointsmin[2]);
glVertex3f(cpointsmax[0], cpointsmin[1], cpointsmin[2]);
glEnd();
glBegin(GL_LINES);
glVertex3f(cpointsmax[0], cpointsmax[1], cpointsmax[2]);
glVertex3f(cpointsmax[0], cpointsmax[1], cpointsmin[2]);
glVertex3f(cpointsmin[0], cpointsmax[1], cpointsmax[2]);
glVertex3f(cpointsmin[0], cpointsmax[1], cpointsmin[2]);
glVertex3f(cpointsmin[0], cpointsmin[1], cpointsmax[2]);
glVertex3f(cpointsmin[0], cpointsmin[1], cpointsmin[2]);
glVertex3f(cpointsmax[0], cpointsmin[1], cpointsmax[2]);
glVertex3f(cpointsmax[0], cpointsmin[1], cpointsmin[2]);
glEnd();
glPopMatrix();
glPopMatrix();
glutSwapBuffers(); // Buffer for animation needs to be swapped
}
void init(void)
{
glClearColor(0.99, 0.99, 0.99, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
xoff = 0.0;
yoff = (cpointsmax[1] - cpointsmin[1]);
zoff = 0.0;
zoom = 1;
angle1 = -45;
angle2 = 45;
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glClear(GL_COLOR_BUFFER_BIT);
}
void idle()
{
}
void timer(int value)
{
}
void readcloud(char* filename)
{
int i = 0;
int j = 0;
FILE * f;
int abbruch = 0;
char str[200] = "";
printf("Lese '%s' ein\n", filename);
f = fopen(filename, "r");
printf("Ueberspringe Kopf...\n");
// Kopf Überspringen
while (!feof(f) && str[0] != '[')
fscanf(f, "%s", str);
printf("Lese Punkte ein...\n");
//Punkte einlesen
while (!feof(f) && abbruch == 0)
{
//einlesen
if (((i + 1) % 3) == 0)
fscanf(f, "%f %c", &cpoints[i], str);
else
fscanf(f, "%f", &cpoints[i]);
// Extremalwerte initialisieren
if (i < 3)
{
cpointsmax[i % 3] = cpoints[i];
cpointsmin[i % 3] = cpoints[i];
}
//Abbruch, wenn alle Punkte 0 sind, (nicht ganz sauber, aber funktioniert, wenn nicht zufällig der Urspung ein gültiger Punkt ist)
if (i > 3 && cpoints[i - 2] == 0 && cpoints[i - 1] == 0 && cpoints[i] == 0)
abbruch = 1;
//Extremalwerte gegebenenfalls erneuern
if (cpoints[i] > cpointsmax[i % 3] && cpoints[i] != 0)
cpointsmax[i % 3] = cpoints[i];
if (cpoints[i] < cpointsmin[i % 3] && cpoints[i] != 0)
cpointsmin[i % 3] = cpoints[i];
i++;
}
cpoints_n = i - 1;
printf("Es wurden %i Vertices gelesen\n", cpoints_n / 3);
printf("Koordinaten sind in den Intervallen [%f,%f] [%f,%f] [%f,%f]\n\n", cpointsmin[0], cpointsmax[0], cpointsmin[1], cpointsmax[1], cpointsmin[2], cpointsmax[2]);
abbruch = 0; i = 0;
//warten, bis es zu den colors geht
while (!feof(f) && str[0] != '[')
fscanf(f, "%s", str);
printf("Lese Farben ein...\n");
// Farben einlesen
while (!feof(f) && abbruch == 0)
{
//einlesen
if (((i + 1) % 3) == 0)
fscanf(f, "%f %c", &ccolors[i], str);
else
fscanf(f, "%f", &ccolors[i]);
//Abbruch, wenn alle farben 0 sind, (nicht ganz sauber, aber funktioniert, wenn nicht zufällig schwarz eine gültige Farbe ist)
if (i > 3 && ccolors[i - 2] == 0 && ccolors[i - 1] == 0 && ccolors[i] == 0)
abbruch = 1;
i++;
}
printf("Es wurden %i Farben eingelesen\n\n", (i - 1) / 3);
abbruch = 0; i = 0;
//warten, bis es zu den koordinaten geht
while (!feof(f) && str[0] != '[')
fscanf(f, "%s", str);
printf("Lese Koordinaten fuer die Dreiecke ein...\n");
// Koordinaten einlesen
while (!feof(f) && abbruch < 2)
{
//einlesen
fscanf_s(f, "%i %c", &ccoord[i], str);
//printf("%i\n",ccoord[i]);
//Abbruch, wenn alle Dreiecke 0 sind, (nicht ganz sauber, aber funktioniert, wenn nicht zufällig der Urspung ein gültiger Punkt ist)
if (ccoord[i] == -1)
{
i--;
abbruch++;
}
else
abbruch = 0;
i++;
}
maxcoords = i - 1;
printf("Es wurden %i Dreiecke eingelesen\n", (maxcoords + 1) / 3);// drei Punkte bilden ein Dreieck
fclose(f);
printf("Einlesen beendet\n\n");
}
void key(unsigned char k, int x, int y);
void mouseactive(int x, int y)
{
if (pressedbutton == GLUT_LEFT_BUTTON)
{
angle1 = startangle1 + (x - startx) / 10;
angle2 = startangle2 + (y - starty) / 10;
}
if (pressedbutton == GLUT_RIGHT_BUTTON)
{
xoff = startxoff + (float)(x - startx) / 100;
zoff = startzoff - (float)(y - starty) / 100;
}
if (pressedbutton == GLUT_MIDDLE_BUTTON)
{
yoff = startyoff + ((float)(y - starty) / 100);
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
pressedbutton = button;
startx = x;
starty = y;
startangle1 = angle1;
startangle2 = angle2;
startxoff = xoff;
startyoff = yoff;
startzoff = zoff;
}
else
pressedbutton = 0;
}
void MainMenu(int value)
{
switch (value) {
case 2:
key('q', 0, 0);
break;
}
}
void
submenu1(int value)
{
}
void define_menu()
{
}
void key(unsigned char k, int x, int y)
{
switch (k) {
case 8: //BACKSPACE
init();
break;
case 27:
case 'q':
case 'Q':
exit(0);
default:
if (k > '0' - 1 && k < '7')
{
displaymodus = k - '0';
printf("Display-Modus: %i\n", displaymodus);
}
else
{
printf("Taste %c mit Steuerzeichen %i nicht belegt\n", k, k);
}
break;
}
glutPostRedisplay();
}

View file

@ -0,0 +1,84 @@
#include <glut.h>
int width = 800;
int height = 600;
void init(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glViewport(0,0,width,height);
glutCreateWindow("Culling");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glOrtho(-2.0, 2.0,-2.0, 2.0, -2.0, 2.0);
gluLookAt(0.25,0.5,0.1,0,0,0,0,1,0);
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
}
void display(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
// FRONT
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// BACK
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, -0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
// LEFT
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
// RIGHT
glVertex3f( 0.5f, -0.5f, -0.5f);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
// BOTTOM
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
init(argc, argv);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

View file

@ -0,0 +1,183 @@
#include <glut.h>
#include <math.h>
#include <stdio.h>
#pragma warning(disable:4996)
#define PI 3.14159265358979323846
int width = 800;
int height = 600;
void init(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glViewport(0, 0, width, height);
glutCreateWindow("Programmieruebung Kreis-Plotter");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void drawCoordinateSystem(float* origin, int xLength, int yLength, int numTicksX, int numTicksY, float minX, float maxX, float minY, float maxY) {
float xTickDistance = (float) xLength / (float) numTicksX;
float yTickDistance = (float) yLength / (float) numTicksY;
int k;
float j;
float resX, resY;
char label[10];
resX = (maxX - minX) / xLength;
resY = (maxY - minY) / yLength;
glColor3f(0.0, 0.0, 0.0);
/* drawing the coordinate system */
glBegin(GL_LINES);
// x axis
glVertex2f(origin[0], origin[1]);
glVertex2f(origin[0] + xLength, origin[1]);
// y axis
glVertex2f(origin[0], origin[1]);
glVertex2f(origin[0], origin[1] + yLength);
// arrow head at x axis
glVertex2f(origin[0] + xLength, origin[1]);
glVertex2f(origin[0] + xLength - 15, origin[1] - 10);
glVertex2f(origin[0] + xLength, origin[1]);
glVertex2f(origin[0] + xLength - 15, origin[1] + 10);
// arrow head at y axis
glVertex2f(origin[0], origin[1] + yLength);
glVertex2f(origin[0] - 10, origin[1] + yLength - 15);
glVertex2f(origin[0], origin[1] + yLength);
glVertex2f(origin[0] + 10, origin[1] + yLength - 15);
glEnd();
// drawing ticks at x axis
for (j = 0; j < xLength; j = j + xTickDistance) {
glBegin(GL_LINES);
glVertex2f(origin[0] + j, origin[1] - 5);
glVertex2f(origin[0] + j, origin[1] + 5);
glEnd();
sprintf(label, "%5.2f", minX + (j*resX));
glRasterPos2i((int) round(origin[0] + j - (8 * 5)), (int) round(origin[1] - 13 - 10));
for (k = 0; k < 5; k++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, label[k]);
}
}
// drawing ticks at y axis
for (j = 0; j < yLength; j = j + yTickDistance) {
glBegin(GL_LINES);
glVertex2f(origin[0] - 5, origin[1] + j);
glVertex2f(origin[0] + 5, origin[1] + j);
glEnd();
sprintf(label, "%5.2f", minY + (j*resY));
glRasterPos2i((int) round(origin[0] - (8 * 5) - 10), (int) round(origin[1] + j - 13 / 2));
for (k = 0; k < 5; k++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, label[k]);
}
}
}
void drawCircle(float origin[2], float center[2], float radius, GLint numPoints)
{
//////////////////////////////////////////////////////////////////////////////////////////////
// - calculate numPoints points on the surface of the circle with given center and radius
// hint: see circle equation in parametric form
// - position points in the coordinate system starting at given origin
// - draw the points by OpenGL commands
//////////////////////////////////////////////////////////////////////////////////////////////
}
void display(void)
{
int n;
int xTickDistance = 50;
int yTickDistance = 50;
int numPoints = 30;
float radius[] = { 50, 100, 150 };
float center[] = { 300, 200 };
float origin[] = { 100, 100 };
GLfloat c[] = { 0.0,0.0,0.0 };
int xLength = 600;
int yLength = 400;
float size = 2;
/* settings for the coordinate system */
int numTicksX = 6;
int numTicksY = 4;
float minX = 0;
float maxX = 600;
float minY = 0;
float maxY = 400;
/* clearing the background color */
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
drawCoordinateSystem(origin, xLength, yLength, numTicksX, numTicksY, minX, maxX, minY, maxY);
for (n = 0; n < 3; n++) {
///////////////////////////////////////////////////////////////////////////////
// - set the point size different for each loop iteration
// - set the color different for each loop iteration
//////////////////////////////////////////////////////////////////////////////
drawCircle(origin, center, radius[n], numPoints);
}
glFlush();
}
int main(int argc, char** argv)
{
init(argc, argv);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

View file

@ -0,0 +1,397 @@
#include <glut.h>
#include <math.h>
#include <stdio.h>
#pragma warning(disable:4996)
void mouse(int button, int state, int x, int y);
void key(unsigned char key, int x, int y);
void init(void);
void reshape(int, int);
void display(void);
int main(int, char **);
void define_menu();
void idle();
void timer(int value);
void readcloud(char* filename);
void mouseactive(int x, int y);
void mouse(int button, int state, int x, int y);
float cpoints[3 * 60000];
float ccolors[3 * 60000];
int ccoord[10 * 3 * 60000];
int maxcoords = 0;
float cpointsmax[3];
float cpointsmin[3];
int cpoints_n = 0;
float xoff;
float yoff;
float zoff;
float zoom;
int angle1;
int angle2;
const float stepsize = 0.05;
const float anglestepsize = 0.01;
int displaymodus = 1;
int pressedbutton = 0;
int startx, starty;
int startangle1;
int startangle2;
float startxoff;
float startyoff;
float startzoff;
int main(int argc, char** argv)
{
readcloud("C:\\tmp\\bones.txt"); // change this in case the point cloud is saved somewhere else.
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Doublebuffer for animation
glutInitWindowSize(800, 800);
glutInitWindowPosition(400, 100);
glutCreateWindow("Mesh Visualization");
init();
glutMouseFunc(mouse);
glutMotionFunc(mouseactive);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
printf("\n\nSTEUERUNG\nAnzeigemodi:\n");
printf("'0' nur Box\n'1' Points, Farbwerte nach Koordinate\n'2' Wireframe, Farbwerte nach Koordinate\n'3' Filled, Farbwerte nach Koordinate\n");
printf("'4' Points, Farbwerte aus Datei\n'5' Wireframe, Farbwerte aus Datei\n'6' Filled, Farbwerte aus Datei\n\n\n");
printf("Transformationen:\n linke Maustaste und x-y-Bewegung -> Rotation\n mittlere Maustaste und y-Richtung -> Zoom (entspricht einer Skalierung)\n");
printf(" rechte Maustaste und x-y-Bewegung -> Translation\n\n");
glutMainLoop();
return 0;
}
void displaycloud(int modus)
{
int i = 0;
float range[3];
for (i = 0; i < 3; i++)
range[i] = cpointsmax[i] - cpointsmin[i];
if (modus > 0)
{
if (modus == 1 || modus == 4) { // Display only the vertices
//////////////////////
// TODO: set fill mode to render only the vertices
//////////////////////
}
if (modus == 2 || modus == 5) { // Display the outlines of the polygons
//////////////////////
// TODO: set fill mode to render only outlines
//////////////////////
}
if (modus == 3 || modus == 6) { // Display filled polygons
//////////////////////
// TODO: set fill mode to render filled polygons
/////////////////////
}
glBegin(GL_TRIANGLES); // using the polygone mode "GL_TRIANGLES"
for (i = 0; i < maxcoords + 1; i++)
{
if (modus > 3) { // Displaying colors saved in the mesh file (node wise definition!)
glColor3f(ccolors[ccoord[i] * 3], ccolors[ccoord[i] * 3 + 1], ccolors[ccoord[i] * 3 + 2]);
}
else { // Displaying interpolated colors according to the x-/y-/z-value of the point coordinates (node wise definition!)
glColor3f((cpoints[ccoord[i] * 3] - cpointsmin[0]) / range[0], (cpoints[ccoord[i] * 3 + 1] - cpointsmin[1]) / range[1], (cpoints[ccoord[i] * 3 + 2] - cpointsmin[2]) / range[2]);
}
// Definition of vertices
/////////////////////////////////////////////////////////////////////
// TODO: definition of vertices.
// note the data structures: cpoints holds all coordinate components in a 1D array:
// node j_x can be accessed via cpoints[j*3]
// node j_y can be accessed via cpoints[j*3+1]
// node j_z can be accessed via cpoints[j*3+2]
// note the data structure: ccoord holds the indices into the cpoints array in a 1D array:
// one triangle consists of 3 consecutive entries of ccoord, i.e.
// first triangle consists of the vertices referenced in ccoord[0], ccords[1], ccoord[2].
/////////////////////////////////////////////////////////////////////
}
glEnd();
}
}
void display(void)
{
glPushMatrix();
gluLookAt(0, -zoom, 0, 0, zoom*zoom + 5, 0, 0.0, 0.0, 1.0);
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glColor3f(0.0, 0.0, 0.0);
// center and rotate
glTranslatef(xoff, yoff, zoff);
glRotatef(angle2, 1.0, 0.0, 0.0);
glRotatef(angle1, 0.0, 0.0, 1.0);
glTranslatef(-(cpointsmax[0] - cpointsmin[0]) / 2 - cpointsmin[0], -(cpointsmax[1] - cpointsmin[1]) / 2 - cpointsmin[1], -(cpointsmax[2] - cpointsmin[2]) / 2 - cpointsmin[2]);
//display
displaycloud(displaymodus);
// draw box
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(cpointsmax[0], cpointsmax[1], cpointsmax[2]);
glVertex3f(cpointsmin[0], cpointsmax[1], cpointsmax[2]);
glVertex3f(cpointsmin[0], cpointsmin[1], cpointsmax[2]);
glVertex3f(cpointsmax[0], cpointsmin[1], cpointsmax[2]);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(cpointsmax[0], cpointsmax[1], cpointsmin[2]);
glVertex3f(cpointsmin[0], cpointsmax[1], cpointsmin[2]);
glVertex3f(cpointsmin[0], cpointsmin[1], cpointsmin[2]);
glVertex3f(cpointsmax[0], cpointsmin[1], cpointsmin[2]);
glEnd();
glBegin(GL_LINES);
glVertex3f(cpointsmax[0], cpointsmax[1], cpointsmax[2]);
glVertex3f(cpointsmax[0], cpointsmax[1], cpointsmin[2]);
glVertex3f(cpointsmin[0], cpointsmax[1], cpointsmax[2]);
glVertex3f(cpointsmin[0], cpointsmax[1], cpointsmin[2]);
glVertex3f(cpointsmin[0], cpointsmin[1], cpointsmax[2]);
glVertex3f(cpointsmin[0], cpointsmin[1], cpointsmin[2]);
glVertex3f(cpointsmax[0], cpointsmin[1], cpointsmax[2]);
glVertex3f(cpointsmax[0], cpointsmin[1], cpointsmin[2]);
glEnd();
glPopMatrix();
glPopMatrix();
glutSwapBuffers(); // Buffer for animation needs to be swapped
}
void init(void)
{
glClearColor(0.99, 0.99, 0.99, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
xoff = 0.0;
yoff = (cpointsmax[1] - cpointsmin[1]);
zoff = 0.0;
zoom = 1;
angle1 = -45;
angle2 = 45;
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glClear(GL_COLOR_BUFFER_BIT);
}
void idle()
{
}
void timer(int value)
{
}
void readcloud(char* filename)
{
int i = 0;
int j = 0;
FILE * f;
int abbruch = 0;
char str[200] = "";
printf("Lese '%s' ein\n", filename);
f = fopen(filename, "r");
printf("Ueberspringe Kopf...\n");
// Kopf Überspringen
while (!feof(f) && str[0] != '[')
fscanf(f, "%s", str);
printf("Lese Punkte ein...\n");
//Punkte einlesen
while (!feof(f) && abbruch == 0)
{
//einlesen
if (((i + 1) % 3) == 0)
fscanf(f, "%f %c", &cpoints[i], str);
else
fscanf(f, "%f", &cpoints[i]);
// Extremalwerte initialisieren
if (i < 3)
{
cpointsmax[i % 3] = cpoints[i];
cpointsmin[i % 3] = cpoints[i];
}
//Abbruch, wenn alle Punkte 0 sind, (nicht ganz sauber, aber funktioniert, wenn nicht zufällig der Urspung ein gültiger Punkt ist)
if (i > 3 && cpoints[i - 2] == 0 && cpoints[i - 1] == 0 && cpoints[i] == 0)
abbruch = 1;
//Extremalwerte gegebenenfalls erneuern
if (cpoints[i] > cpointsmax[i % 3] && cpoints[i] != 0)
cpointsmax[i % 3] = cpoints[i];
if (cpoints[i] < cpointsmin[i % 3] && cpoints[i] != 0)
cpointsmin[i % 3] = cpoints[i];
i++;
}
cpoints_n = i - 1;
printf("Es wurden %i Vertices gelesen\n", cpoints_n / 3);
printf("Koordinaten sind in den Intervallen [%f,%f] [%f,%f] [%f,%f]\n\n", cpointsmin[0], cpointsmax[0], cpointsmin[1], cpointsmax[1], cpointsmin[2], cpointsmax[2]);
abbruch = 0; i = 0;
//warten, bis es zu den colors geht
while (!feof(f) && str[0] != '[')
fscanf(f, "%s", str);
printf("Lese Farben ein...\n");
// Farben einlesen
while (!feof(f) && abbruch == 0)
{
//einlesen
if (((i + 1) % 3) == 0)
fscanf(f, "%f %c", &ccolors[i], str);
else
fscanf(f, "%f", &ccolors[i]);
//Abbruch, wenn alle farben 0 sind, (nicht ganz sauber, aber funktioniert, wenn nicht zufällig schwarz eine gültige Farbe ist)
if (i > 3 && ccolors[i - 2] == 0 && ccolors[i - 1] == 0 && ccolors[i] == 0)
abbruch = 1;
i++;
}
printf("Es wurden %i Farben eingelesen\n\n", (i - 1) / 3);
abbruch = 0; i = 0;
//warten, bis es zu den koordinaten geht
while (!feof(f) && str[0] != '[')
fscanf(f, "%s", str);
printf("Lese Koordinaten fuer die Dreiecke ein...\n");
// Koordinaten einlesen
while (!feof(f) && abbruch < 2)
{
//einlesen
fscanf_s(f, "%i %c", &ccoord[i], str);
//printf("%i\n",ccoord[i]);
//Abbruch, wenn alle Dreiecke 0 sind, (nicht ganz sauber, aber funktioniert, wenn nicht zufällig der Urspung ein gültiger Punkt ist)
if (ccoord[i] == -1)
{
i--;
abbruch++;
}
else
abbruch = 0;
i++;
}
maxcoords = i - 1;
printf("Es wurden %i Dreiecke eingelesen\n", (maxcoords + 1) / 3);// drei Punkte bilden ein Dreieck
fclose(f);
printf("Einlesen beendet\n\n");
}
void key(unsigned char k, int x, int y);
void mouseactive(int x, int y)
{
if (pressedbutton == GLUT_LEFT_BUTTON)
{
angle1 = startangle1 + (x - startx) / 10;
angle2 = startangle2 + (y - starty) / 10;
}
if (pressedbutton == GLUT_RIGHT_BUTTON)
{
xoff = startxoff + (float)(x - startx) / 100;
zoff = startzoff - (float)(y - starty) / 100;
}
if (pressedbutton == GLUT_MIDDLE_BUTTON)
{
yoff = startyoff + ((float)(y - starty) / 100);
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
pressedbutton = button;
startx = x;
starty = y;
startangle1 = angle1;
startangle2 = angle2;
startxoff = xoff;
startyoff = yoff;
startzoff = zoff;
}
else
pressedbutton = 0;
}
void MainMenu(int value)
{
switch (value) {
case 2:
key('q', 0, 0);
break;
}
}
void
submenu1(int value)
{
}
void define_menu()
{
}
void key(unsigned char k, int x, int y)
{
switch (k) {
case 8: //BACKSPACE
init();
break;
case 27:
case 'q':
case 'Q':
exit(0);
default:
if (k > '0' - 1 && k < '7')
{
displaymodus = k - '0';
printf("Display-Modus: %i\n", displaymodus);
}
else
{
printf("Taste %c mit Steuerzeichen %i nicht belegt\n", k, k);
}
break;
}
glutPostRedisplay();
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.