diff --git a/README.md b/README.md index 58d1a2b..74d469a 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,9 @@ Code here is published under the [AGPL-v3](https://www.gnu.org/licenses/agpl-3.0 # TODO -- Allow changing the move/rotation speed(or just a multiplier) -- Change camera position to be cow point of view - DONE, need to do the sub thing - - Add popping-floating text of funny cow thoughts -- Add multiple objects(that is not the cow)(is grass an object?) - - at least 3 - - on needs to be metalic - User controlled light source - Add light source - move the light - change intensity +- Add popping-floating text of funny cow thoughts when in cow view - Add Textures and stuff :)(optional) \ No newline at end of file diff --git a/cow.c b/cow.c index b42da32..f4a21f1 100644 --- a/cow.c +++ b/cow.c @@ -11,7 +11,7 @@ const vec3 headLimit = { 30.0, 30.0, 45.0 }; vec3 tailRot = { 0.0, 0.0, 0.0 }; const vec3 tailLimit = { 90.0, 90.0, 0.0 }; -vec3 cowPos = { 0.0, 6.0, 0.0 }; +vec3 cowPos = { 0.0, 4.9, 0.0 }; float cowRot = 0.0; // rotation along the Y axis const float COW_ROT_SPEED = 5.0; const float COW_MOVE_SPEED = 1.0; diff --git a/mmn_17.c b/mmn_17.c index be429e4..6b29ace 100644 --- a/mmn_17.c +++ b/mmn_17.c @@ -31,44 +31,10 @@ struct { int cowControl; int filters; } menus; // to simply hold the menues in a labed manner -GLuint grassList = -1; - -void generateGrassVertexList() { - grassList = glGenLists(1); - glNewList(grassList, GL_COMPILE); - glDisable(GL_CULL_FACE); - for(float x = -50.0; x < 50.0; x += 1.0) { - for(float z = -50.0; z < 50.0; z += 1.0) { - glPushMatrix(); - glTranslatef(x, 0.0, z); - glRotatef(rand() % 360, 0.0, 1.0, 0.0); - glBegin(GL_TRIANGLES); - glVertex3f(0.0, 0.0, 0.0); - glVertex3f(0.5, 0.0, 0.0); - glVertex3f(0.0, 1.5, 0.0); - glVertex3f(0.0, 0.0, 0.0); - glVertex3f(0.0, 0.0, 0.5); - glVertex3f(0.0, 1.5, 0.0); - glEnd(); - glPopMatrix(); - } - } - glEnable(GL_CULL_FACE); - glEndList(); -} - -vec3 rotateByCameraRotation(vec3 v) { - vec3 r; - // rotate along the x axis(yz plane) - r.z = cosf(camRot.x) * v.z + sinf(camRot.x) * v.y; - r.y = -sinf(camRot.x) * v.z + cosf(camRot.x) * v.y; - v = r; - // rotate along the y axis(xy plane) - r.z = cosf(camRot.y) * v.z + sinf(camRot.y) * v.x; - r.x = -sinf(camRot.y) * v.z + cosf(camRot.y) * v.x; - return r; -} +struct { + GLuint grass; +} lists; // Ui stuff int lastMouseButton = GLUT_LEFT_BUTTON; @@ -90,10 +56,90 @@ ui_slider sliders[] = { /* pos */ { { 0.9, 0.9, 0.0 }, { -50.0, -70.0, 0.0 } }, /* size */ { 100.0, 10.0, 10.0 }, 0.3 + }, + { // drag speed + /* pos */ { { 0.9, 0.9, 0.0 }, { -50.0, -120.0, 0.0 } }, + /* size */ { 100.0, 10.0, 10.0 }, + 0.5 + }, + { // rotation speed + /* pos */ { { 0.9, 0.9, 0.0 }, { -50.0, -170.0, 0.0 } }, + /* size */ { 100.0, 10.0, 10.0 }, + 0.3 } - }; +void generateGrassVertexList() { + lists.grass = glGenLists(1); + glNewList(lists.grass, GL_COMPILE); + glDisable(GL_CULL_FACE); + for(float x = -50.0; x < 50.0; x += 1.0) { + for(float z = -50.0; z < 50.0; z += 1.0) { + float tx = -0.03 * (rand() % 21), tz = -0.03 * (rand() % 21); + glPushMatrix(); + glTranslatef(x, 0.0, z); + glRotatef(rand() % 360, 0.0, 1.0, 0.0); + glBegin(GL_TRIANGLES); + glColor3f(0.0, 0.3, 0.0); + glVertex3f(0.0, 0.0, 0.0); + glColor3f(0.0, 0.5, 0.0); + glVertex3f(0.5, 0.0, 0.0); + glVertex3f(tx, 1.5, tz); + glColor3f(0.0, 0.2, 0.0); + glVertex3f(0.0, 0.0, 0.0); + glVertex3f(0.0, 0.0, 0.5); + glColor3f(0.0, 0.5, 0.0); + glVertex3f(tx, 1.5, tz); + glEnd(); + glPopMatrix(); + } + } + glEnable(GL_CULL_FACE); + glEndList(); +} + +void drawTree(float rad, float height) { + // Bark + glEnable(GL_COLOR_MATERIAL); + glColor3f(0.3, 0.1, 0.3); + glPushMatrix(); + glTranslatef(0.0, height, 0.0); + glRotatef(90.0, 1.0, 0.0, 0.0); + glutSolidCylinder(rad, height, 16, 3); + glPopMatrix(); + glColor3f(0.0, 1.0, 0.0); + glPushMatrix(); + glTranslatef(0.0, height, 0.0); + + vec3 ts[] = { + vec3_new(1.0, 0.0, 0.0), vec3_new(-1.0, 0.0, 0.0), + vec3_new(0.0, 0.0, -1.0), vec3_new(0.0, 0.0, 1.0), + vec3_new(0.0, 1.0, 0.0), + }; + for(int i = 0; i < 5; i += 1) { + vec3 t = ts[i]; + glPushMatrix(); + glTranslatef(t.x * rad, t.y * rad, t.z * rad); + glutSolidSphere(rad, 8, 8); + glPopMatrix(); + } + glPopMatrix(); + glColor3f(1.0, 1.0, 1.0); + glDisable(GL_COLOR_MATERIAL); +} + +vec3 rotateByCameraRotation(vec3 v) { + vec3 r; + // rotate along the x axis(yz plane) + r.z = cosf(camRot.x) * v.z + sinf(camRot.x) * v.y; + r.y = -sinf(camRot.x) * v.z + cosf(camRot.x) * v.y; + v = r; + // rotate along the y axis(xy plane) + r.z = cosf(camRot.y) * v.z + sinf(camRot.y) * v.x; + r.x = -sinf(camRot.y) * v.z + cosf(camRot.y) * v.x; + return r; +} + void drawUi(void) { glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); @@ -121,6 +167,12 @@ void drawUi(void) { glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, t); glRasterPos2f(wWidth * 0.9 - 75.0, wHeight * 0.9 - 10.0); glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (unsigned char*)"r\ng\nb"); + unsigned char td[] = "Drag speed"; + unsigned char tr[] = "Rotate speed"; + glRasterPos2f(wWidth * 0.9 - glutBitmapLength(GLUT_BITMAP_TIMES_ROMAN_24, td) * 0.5, wHeight * 0.9 - 100.0); + glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, td); + glRasterPos2f(wWidth * 0.9 - glutBitmapLength(GLUT_BITMAP_TIMES_ROMAN_24, tr) * 0.5, wHeight * 0.9 - 150.0); + glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, tr); if(settings.showHelp) { glColor3f(0.3, 0.3, 0.3); @@ -165,6 +217,7 @@ void drawWorld(void) { gluPerspective(90, wWidth / wHeight , 0.5, 500.0); // Lights glEnable(GL_LIGHTING); + glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); // Ambient light GLfloat ambient[] = { sliders[0].value, sliders[1].value, sliders[2].value, 1.0 }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); @@ -192,15 +245,32 @@ void drawWorld(void) { glTexCoord2f(1.0, 0.0); glVertex3f(50.0, 0.0, -50.0); glEnd(); glDisable(GL_TEXTURE_2D); - // Add a simple cube to see where the cow was originally facing + glPushMatrix(); + glTranslatef(25.0, 0.0, 25.0); + drawTree(3.0, 20.0); + glTranslatef(-50.0, 0.0, 0.0); + drawTree(2.0, 19.0); + glPopMatrix(); + // Scary shady cube glPushMatrix(); glTranslatef(0.0, 10.0, 50.0); glEnable(GL_COLOR_MATERIAL); - glColor3f(1.0, 1.0, 1.0); + glColor3f(0.05, 0.05, 0.1); + GLfloat mat[] = { 0.25, 0.25, 0.25, 0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6 }; + glMaterialf(GL_FRONT, GL_SHININESS, mat[9] * 128.0); + glMaterialfv(GL_FRONT, GL_AMBIENT, mat); + glMaterialfv(GL_FRONT, GL_DIFFUSE, mat + 3); + glMaterialfv(GL_FRONT, GL_SPECULAR, mat + 6); glutSolidCube(20.0); + GLfloat matReset[] = {0.0, 0.0, 0.0}; + glMaterialf(GL_FRONT, GL_SHININESS, 0.0); + glMaterialfv(GL_FRONT, GL_AMBIENT, matReset); + glMaterialfv(GL_FRONT, GL_DIFFUSE, matReset); + glMaterialfv(GL_FRONT, GL_SPECULAR, matReset); glPopMatrix(); glColor3f(0.1, 0.3, 0.0); - glCallList(grassList); + glCallList(lists.grass); + glColor3f(1.0, 1.0, 1.0); glDisable(GL_COLOR_MATERIAL); } @@ -254,11 +324,12 @@ void mouseMotionEvent(int x, int y) { } else { // rotate the camera - camRot.y -= (x - lastMouseX) * PI * 0.01; + float speed = sliders[4].value * 0.095 + 0.005; + camRot.y -= (x - lastMouseX) * speed; if(camRot.y > 2 * PI) camRot.y -= 2 * PI; else if(camRot.y < 0.0) camRot.y += 2 * PI; - camRot.x = fmin(PI * 0.5, fmax(-PI * 0.5, camRot.x - (y - lastMouseY) * 0.01 * PI)); + camRot.x = fmin(PI * 0.5, fmax(-PI * 0.5, camRot.x - (y - lastMouseY) * speed)); } } @@ -270,9 +341,9 @@ void mouseMotionEvent(int x, int y) { moveX.y = 0; moveY.y = 0; } - - camCenter = vec3_add(camCenter, vec3_mult(moveX, (lastMouseX - x) * 0.5)); - camCenter = vec3_add(camCenter, vec3_mult(moveY, (y - lastMouseY) * 0.5)); + float speed = sliders[3].value * 0.9 + 0.1; + camCenter = vec3_add(camCenter, vec3_mult(moveX, (lastMouseX - x) * speed)); + camCenter = vec3_add(camCenter, vec3_mult(moveY, (y - lastMouseY) * speed)); } lastMouseX = x; lastMouseY = y; glutPostRedisplay();