[Code] Code hinzugefügt

This commit is contained in:
Andre Meyering 2018-11-21 15:46:14 +01:00
parent 1bd0638da8
commit 57f5e41f0b

View file

@ -7,7 +7,6 @@
#pragma warning(disable : 4996) #pragma warning(disable : 4996)
void mouse(int button, int state, int x, int y); void mouse(int button, int state, int x, int y);
void key(unsigned char key, int x, int y); void key(unsigned char key, int x, int y);
void init(void); void init(void);
@ -57,19 +56,15 @@ int lights = 0;
int shading = 0; int shading = 0;
float shininess = 2; float shininess = 2;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// FARBEN DER LICHT KOMPONENTEN // FARBEN DER LICHT KOMPONENTEN
float ambietntLightColor[3] = { 0.1f, 0.1f, 0.1f };
float diffuseLightColor[3] = { 0.5f, 0.5f, 0.5 };
float specularLightColor[3] = { 1.0f, 1.0f, 1.0f };
// LICHT POSITION // LICHT POSITION
float lightPosition[4] = { 0, 0, 1, 1 };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -119,8 +114,7 @@ void displaycloud(int modus)
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
range[i] = cpointsmax[i] - cpointsmin[i]; range[i] = cpointsmax[i] - cpointsmin[i];
if (modus > 0) if (modus > 0) {
{
if (modus == 1 || modus == 4) { // Darstellung von Punkten if (modus == 1 || modus == 4) { // Darstellung von Punkten
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
} }
@ -131,14 +125,12 @@ void displaycloud(int modus)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
} }
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
for (i = 0; i < maxcoords + 1; i++) for (i = 0; i < maxcoords + 1; i++) {
{
if (modus > 3) { // Darstellung der Farben aus dem Mesh-File if (modus > 3) { // Darstellung der Farben aus dem Mesh-File
currentColor[0] = ccolors[ccoord[i] * 3]; currentColor[0] = ccolors[ccoord[i] * 3];
currentColor[1] = ccolors[ccoord[i] * 3 + 1]; currentColor[1] = ccolors[ccoord[i] * 3 + 1];
currentColor[2] = ccolors[ccoord[i] * 3 + 2]; currentColor[2] = ccolors[ccoord[i] * 3 + 2];
} } else { // Darstellung der interpolierten Farben entsprechend der Koordinaten
else { // Darstellung der interpolierten Farben entsprechend der Koordinaten
currentColor[0] = (cpoints[ccoord[i] * 3] - cpointsmin[0]) / range[0]; currentColor[0] = (cpoints[ccoord[i] * 3] - cpointsmin[0]) / range[0];
currentColor[1] = (cpoints[ccoord[i] * 3 + 1] - cpointsmin[1]) / range[1]; currentColor[1] = (cpoints[ccoord[i] * 3 + 1] - cpointsmin[1]) / range[1];
currentColor[2] = (cpoints[ccoord[i] * 3 + 2] - cpointsmin[2]) / range[2]; currentColor[2] = (cpoints[ccoord[i] * 3 + 2] - cpointsmin[2]) / range[2];
@ -148,19 +140,14 @@ void displaycloud(int modus)
//////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////
// MATERIAL DEFINTION // MATERIAL DEFINTION
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, currentColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, currentColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, currentColor);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
//////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////
} } else {
else {
glColor3f(currentColor[0], currentColor[1], currentColor[2]); glColor3f(currentColor[0], currentColor[1], currentColor[2]);
} }
@ -186,33 +173,30 @@ void displaycloud(int modus)
} }
glEnd(); glEnd();
} }
} }
void display(void) { void display(void)
{
if (lights == 1) { if (lights == 1) {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LICHT DEFINITION // LICHT DEFINITION
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambietntLightColor);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLightColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLightColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHTING);
// SHADING DEFINTION // SHADING DEFINTION
if (shading == 0) { // Flat Shading if (shading == 0) { // Flat Shading
glShadeModel(GL_FLAT);
} else if (shading == 1) { // Gouraud Shading
glShadeModel(GL_SMOOTH);
} }
else if (shading == 1) { // Gouraud Shading
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} } else {
else {
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
} }
@ -239,8 +223,6 @@ void display(void) {
break; break;
} }
glPushMatrix(); glPushMatrix();
// enable depth buffer and clear color/depth buffer // enable depth buffer and clear color/depth buffer
@ -249,7 +231,6 @@ void display(void) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); // Default: GL_LESS glDepthFunc(GL_LESS); // Default: GL_LESS
glColor3f(0.0, 0.0, 0.0); glColor3f(0.0, 0.0, 0.0);
// center and rotate // center and rotate
glTranslatef(xoff, yoff, 0); glTranslatef(xoff, yoff, 0);
@ -291,7 +272,6 @@ void display(void) {
glutSwapBuffers(); // Buffer for animation needs to be swapped glutSwapBuffers(); // Buffer for animation needs to be swapped
} }
void init(void) void init(void)
{ {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -309,8 +289,6 @@ void init(void)
angle2 = 45; angle2 = 45;
} }
void reshape(int w, int h) void reshape(int w, int h)
{ {
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
@ -350,16 +328,14 @@ void readcloud(char* filename)
fscanf(f, "%s", str); fscanf(f, "%s", str);
printf("Lese Punkte ein...\n"); printf("Lese Punkte ein...\n");
//Punkte einlesen //Punkte einlesen
while (!feof(f) && abbruch == 0) while (!feof(f) && abbruch == 0) {
{
//einlesen //einlesen
if (((i + 1) % 3) == 0) if (((i + 1) % 3) == 0)
fscanf(f, "%f %c", &cpoints[i], str); fscanf(f, "%f %c", &cpoints[i], str);
else else
fscanf(f, "%f", &cpoints[i]); fscanf(f, "%f", &cpoints[i]);
// Extremalwerte initialisieren // Extremalwerte initialisieren
if (i < 3) if (i < 3) {
{
cpointsmax[i % 3] = cpoints[i]; cpointsmax[i % 3] = cpoints[i];
cpointsmin[i % 3] = cpoints[i]; cpointsmin[i % 3] = cpoints[i];
} }
@ -376,14 +352,14 @@ void readcloud(char* filename)
cpoints_n = i - 1; cpoints_n = i - 1;
printf("Es wurden %i Vertices gelesen\n", cpoints_n / 3); 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]); 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; abbruch = 0;
i = 0;
//warten, bis es zu den colors geht //warten, bis es zu den colors geht
while (!feof(f) && str[0] != '[') while (!feof(f) && str[0] != '[')
fscanf(f, "%s", str); fscanf(f, "%s", str);
printf("Lese Farben ein...\n"); printf("Lese Farben ein...\n");
// Farben einlesen // Farben einlesen
while (!feof(f) && abbruch == 0) while (!feof(f) && abbruch == 0) {
{
//einlesen //einlesen
if (((i + 1) % 3) == 0) if (((i + 1) % 3) == 0)
fscanf(f, "%f %c", &ccolors[i], str); fscanf(f, "%f %c", &ccolors[i], str);
@ -395,24 +371,22 @@ void readcloud(char* filename)
i++; i++;
} }
printf("Es wurden %i Farben eingelesen\n\n", (i - 1) / 3); printf("Es wurden %i Farben eingelesen\n\n", (i - 1) / 3);
abbruch = 0; i = 0; abbruch = 0;
i = 0;
//warten, bis es zu den koordinaten geht //warten, bis es zu den koordinaten geht
while (!feof(f) && str[0] != '[') while (!feof(f) && str[0] != '[')
fscanf(f, "%s", str); fscanf(f, "%s", str);
printf("Lese Koordinaten fuer die Dreiecke ein...\n"); printf("Lese Koordinaten fuer die Dreiecke ein...\n");
// Koordinaten einlesen // Koordinaten einlesen
while (!feof(f) && abbruch < 2) while (!feof(f) && abbruch < 2) {
{
//einlesen //einlesen
fscanf(f, "%i %c", &ccoord[i], str); fscanf(f, "%i %c", &ccoord[i], str);
//printf("%i\n",ccoord[i]); //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) //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) if (ccoord[i] == -1) {
{
i--; i--;
abbruch++; abbruch++;
} } else
else
abbruch = 0; abbruch = 0;
i++; i++;
} }
@ -478,7 +452,6 @@ void readcloud(char* filename)
cvnormals[ccoord[i + 2] * 3] = cvnormals[ccoord[i + 2] * 3] + n[0]; cvnormals[ccoord[i + 2] * 3] = cvnormals[ccoord[i + 2] * 3] + n[0];
cvnormals[ccoord[i + 2] * 3 + 1] = cvnormals[ccoord[i + 2] * 3 + 1] + n[1]; cvnormals[ccoord[i + 2] * 3 + 1] = cvnormals[ccoord[i + 2] * 3 + 1] + n[1];
cvnormals[ccoord[i + 2] * 3 + 2] = cvnormals[ccoord[i + 2] * 3 + 2] + n[2]; cvnormals[ccoord[i + 2] * 3 + 2] = cvnormals[ccoord[i + 2] * 3 + 2] + n[2];
} }
counter++; counter++;
if (counter == 3) { if (counter == 3) {
@ -488,33 +461,25 @@ void readcloud(char* filename)
printf("... beendet.\n"); printf("... beendet.\n");
} }
void key(unsigned char k, int x, int y); void key(unsigned char k, int x, int y);
void mouseactive(int x, int y) void mouseactive(int x, int y)
{ {
if (pressedbutton == GLUT_LEFT_BUTTON) if (pressedbutton == GLUT_LEFT_BUTTON) {
{
angle1 = startangle1 + (x - startx) / 10; angle1 = startangle1 + (x - startx) / 10;
angle2 = startangle2 + (y - starty) / 10; angle2 = startangle2 + (y - starty) / 10;
} }
if (pressedbutton == GLUT_RIGHT_BUTTON) if (pressedbutton == GLUT_RIGHT_BUTTON) {
{
xoff = startxoff + (float)(x - startx) / 100; xoff = startxoff + (float)(x - startx) / 100;
yoff = startyoff + (float)(y - starty) / 100; yoff = startyoff + (float)(y - starty) / 100;
} }
if (pressedbutton == GLUT_MIDDLE_BUTTON) if (pressedbutton == GLUT_MIDDLE_BUTTON) {
{
zoff = startzoff + ((float)(y - startz) / 100); zoff = startzoff + ((float)(y - startz) / 100);
} }
glutPostRedisplay(); glutPostRedisplay();
} }
void mouse(int button, int state, int x, int y) void mouse(int button, int state, int x, int y)
{ {
if (state == GLUT_DOWN) if (state == GLUT_DOWN) {
{
pressedbutton = button; pressedbutton = button;
startx = x; startx = x;
starty = y; starty = y;
@ -524,13 +489,10 @@ void mouse(int button, int state, int x, int y)
startxoff = xoff; startxoff = xoff;
startyoff = yoff; startyoff = yoff;
startzoff = zoff; startzoff = zoff;
} } else
else
pressedbutton = 0; pressedbutton = 0;
} }
void MainMenu(int value) void MainMenu(int value)
{ {
switch (value) { switch (value) {
@ -538,7 +500,6 @@ void MainMenu(int value)
case 2: case 2:
key('q', 0, 0); key('q', 0, 0);
break; break;
} }
} }
@ -546,7 +507,6 @@ void submenu1(int value)
{ {
} }
void define_menu() void define_menu()
{ {
} }
@ -589,20 +549,16 @@ void key(unsigned char k, int x, int y)
if (shading == 1) { if (shading == 1) {
shading = 0; shading = 0;
printf(" Shading = FLAT\n"); printf(" Shading = FLAT\n");
} } else if (shading == 0) {
else if (shading == 0) {
shading = 1; shading = 1;
printf(" Shading = GOURAUD\n"); printf(" Shading = GOURAUD\n");
} }
break; break;
default: default:
if (k > '0' - 1 && k < '7') if (k > '0' - 1 && k < '7') {
{
displaymodus = k - '0'; displaymodus = k - '0';
printf("Display-Modus: %i\n", displaymodus); printf("Display-Modus: %i\n", displaymodus);
} } else {
else
{
printf("Taste %c mit Steuerzeichen %i nicht belegt\n", k, k); printf("Taste %c mit Steuerzeichen %i nicht belegt\n", k, k);
} }
break; break;