Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kesulitan membuat gear 3d #5

Open
GustavArthur opened this issue Jul 10, 2018 · 3 comments
Open

Kesulitan membuat gear 3d #5

GustavArthur opened this issue Jul 10, 2018 · 3 comments

Comments

@GustavArthur
Copy link
Contributor

Pak, saya mengalami kesusahan untuk membuat gear 3d, saya bingung dengan koordinat bila menggunakan gl polygon. terima kasih

@goFrendiAsgard

@goFrendiAsgard
Copy link

Apa yang dibingungkan? coba sampaikan secara lebih spesifik

@GustavArthur
Copy link
Contributor Author

modelling 3dnya pak

@goFrendiAsgard
Copy link

Masih belum spesifik Mas, modelling 3d di bagian mana yang kalian bingungkan.
Begini saja, saya kasih coding yang pernah saya demokan dulu waktu di kelas. Coba dilihat dan dipelajari.

Kalau masih kesulitan, sebutkan saja bagian koding yang mana yang sulit.

#include "GL/glut.h"
#include "math.h"

void initGL()
{
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  glClearDepth(1.0f);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
  glShadeModel(GL_SMOOTH);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

float deg2rad (float degree) {
  return degree * (22.0/7) / 180;
}

float getX (float sudut, float jariJari) {
  return cos(deg2rad(sudut)) * jariJari;
}

float getY (float sudut, float jariJari) {
  return sin(deg2rad(sudut)) * jariJari;
}

void segiLima (float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float x5, float y5, float z) {
  glBegin(GL_POLYGON);
  glVertex3f(x1,y1,z);
  glVertex3f(x2,y2,z);
  glVertex3f(x3,y3,z);
  glVertex3f(x4,y4,z);
  glVertex3f(x5,y5,z);
  glEnd();
}

void segiEmpatKecil (float x1, float y1, float x2, float y2, float zDepan, float zBelakang) {
  glBegin(GL_QUADS);
  glVertex3f(x1,y1,zDepan);
  glVertex3f(x2,y2,zDepan);
  glVertex3f(x2,y2,zBelakang);
  glVertex3f(x1,y1,zBelakang);
  glEnd();
}

void gear (float diameterLuar, float diameterDalam, float tinggiGigi, float ketebalan, int banyakGigi) {
  float delta = 360.0/banyakGigi;
  float sudut = 0;
  float zDepan = 0.5 * ketebalan;
  float zBelakang = -zDepan;
  for (int i=0; i<banyakGigi; i++) {
    float sudut1 = sudut - 0.5 * delta;
    float sudut2 = sudut + 0.5 * delta;
    float x1 = getX(sudut1, diameterDalam);
    float y1 = getY(sudut1, diameterDalam);
    float x2 = getX(sudut2, diameterDalam);
    float y2 = getY(sudut2, diameterDalam);
    float x3 = getX(sudut2, diameterLuar);
    float y3 = getY(sudut2, diameterLuar);
    float x4 = getX(sudut, diameterLuar + tinggiGigi);
    float y4 = getY(sudut, diameterLuar + tinggiGigi);
    float x5 = getX(sudut1, diameterLuar);
    float y5 = getY(sudut1, diameterLuar);
    glColor3f(1,0,0);
    segiLima (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, zDepan);
    segiLima (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, zBelakang);
    glColor3f(0,1,0);
    segiEmpatKecil(x3, y3, x4, y4, zDepan, zBelakang);
    segiEmpatKecil(x5, y5, x4, y4, zDepan, zBelakang);
    segiEmpatKecil(x1, y1, x2, y2, zDepan, zBelakang);
    sudut += delta;
  }
}

float angle = 0;
void display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef(0, 0, -15);

  glRotatef(45, 0, 1, 0);
  glRotatef(angle, 0, 0, 1);
  gear (3, 1, 1, 0.5, 20);
  // diameter luar, diameter dalam, tinggi gigi, ketebalan, sudut

  angle ++;

  glutSwapBuffers();
}

void timer(int value)
{
  glutPostRedisplay();
  glutTimerFunc(15, timer, 0);
}

void reshape(GLsizei width, GLsizei height)
{
  if (height == 0)
    height = 1;
  GLfloat aspect = (GLfloat)width / (GLfloat)height;
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(50, 50);
  glutCreateWindow("3d-animation");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  initGL();
  glutTimerFunc(0, timer, 0);
  glutMainLoop();
  return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants