world_of_cow/cow.c
2023-07-22 15:47:51 +03:00

78 lines
1.9 KiB
C

#include <GL/freeglut_std.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include "cow.h"
#include "vec.h"
#include <math.h>
#include <stdio.h>
vec3 headRot;
const vec3 headLimit = { 90.0, 90.0 ,90.0 };
vec3 cowPos = { 0.0, 6.0, 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;
enum COW_CONTROL control;
float deg2rad(float deg) {
return deg * 3.14 / 180.0;
}
void cowMove(char c) {
// check for rotations
cowRot += ((c == 'e') - (c == 'q')) * COW_ROT_SPEED;
if(cowRot > 180.0) { cowRot -= 360.0; }
else if(cowRot < -180.0) { cowRot += 360.0; }
printf("rot: %f\n", cowRot);
// movement
vec3 dir = vec3_new((c == 'd') - (c == 'a'), 0.0, (c == 'w') - (c == 's'));
vec3 r = vec3_splat(0.0);
float rot = deg2rad(cowRot);
r.z = cosf(rot) * dir.z + sinf(rot) * dir.x;
r.x = -sinf(rot) * dir.z + cosf(rot) * dir.x;
cowPos = vec3_add(cowPos, vec3_mult(r, COW_MOVE_SPEED));
glutPostRedisplay();
}
void drawCow() {
glMatrixMode(GL_MODELVIEW);
glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
// Main cow position - as center of cow body
glTranslatef(cowPos.x, cowPos.y, cowPos.z);
glRotatef(cowRot, 0.0, 1.0, 0.0);
// Draw cow body
glPushMatrix();
glColor3f(0.8,0.4,0.11);
glScalef(1.0, 1.0, 1.5);
glutSolidSphere(4.0, 16, 16);
glPopMatrix();
// head
glPushMatrix();
glTranslatef(0.0, 0.0, 5.0);
glRotatef(headRot.y, 0.0, 1.0, 0.0);
glRotatef(headRot.x, 1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, 1.0);
glRotatef(headRot.z, 0.0, 0.0 ,1.0);
glutSolidSphere(2.0, 16, 16);
glPopMatrix();
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
}
void onCowKeyboardInput(char key) {
switch(control) {
case COW_CONTROL_MOVE:
cowMove(key);
break;
default:
break;
}
}
void updateCowControl(enum COW_CONTROL c) {
control = c;
}