1

I just want to do horizontally moving wheel. There's a code but it doesn't work - wheel just moving as a circle, leaving traces. I've tried to glTranslatef(0.0f, moveY, 0.0f); but it doesn't work too.

#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>
#include <GL\GL.h>

#define window_width  1080  
#define window_height 720 

GLfloat angle; // wheel's rotator
GLfloat moveX; // wheel's mover by X

void DrawCircle(float cx, float cy, float r, int num_segments) {
    glLoadIdentity();

    glTranslatef(0, 0, -10); // set place where wheel starts to draw

    glRotatef(angle, 0.0f, 0.0f, -0.1f); // rotate our wheel. it works correctly I think

    glTranslatef(moveX, 0.0f, 0.0f); // but it makes me wonder. moving circle by X doesn't work 

    // part of a circle
    glBegin(GL_TRIANGLE_FAN);
    glColor3f(255, 255, 255);
    for (int ii = 0; ii < num_segments; ii++) {
        double twicePi = 2.0 * 3.142;
        float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);
        float x = r * cosf(theta);
        float y = r * sinf(theta);
        glVertex2f(x + cx, y + cy);
    }

    glEnd();

    // second part of a circle
    int i, x, y;
    double radius = 1.20;    

    glColor3ub(0.0, 0.0, 0.0);
    double twicePi = 2.0 * 3.142;
    x = 0, y = 0;
    glBegin(GL_TRIANGLE_FAN); 
    glVertex2f(x, y); 
    for (i = 0; i <= 20; i++) {
        glVertex2f(
            (x + (radius * cos(i * twicePi / 20))),
            (y + (radius * sin(i * twicePi / 20)))
        );
    }
    glEnd();
    // there's an end of drawning of a circle

    // this is where wheel starts to rotate and move
    angle += 0.02f;
    moveX += 0.00005f;



    // there are spokes of a wheel
    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, -1.2f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, 1.2f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.2f, 0.0, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.2f, 0.0, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.f, 1.f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.f, 1.f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.f, -1.f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.f, -1.f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.2f, 0.5f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.2f, -0.5f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.2f, -0.5f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.2f, 0.5f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.2f, -0.5f, 0.0);
    glEnd();

}


void main_loop_function() {
    DrawCircle(0.0, 0.0, 1.45, 200);
    glutSwapBuffers();
}

void GL_Setup(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(45, (float) width / height, 0.1, 100);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(window_width, window_height);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("trata");
    glutIdleFunc(main_loop_function);
    GL_Setup(window_width, window_height);
    glutMainLoop();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
phen0menon
  • 2,354
  • 2
  • 17
  • 36

1 Answers1

3

This is just a quick fix to make your code work so you can fuhrtherly develop and debug it. I see a number of critical issues in your code:

(0) Mixing POSIX paths with Windows paths:

#include <GL/freeglut.h>
#include <GL\GL.h>

(1) including custom headers and expecting the community to guess what is it about. I commented it out.

//#include "stdafx.h" 

(2) Miswriting casts as functions - it should be:

    float theta = 2.0f * 3.1415926f * (float)ii / (float)num_segments;

(3) No glutDisplayFunc in your code. You should register something like:

glutDisplayFunc(main_display_function);

before registering glutIdleFunc(). Then, in your code you should define it somewhat in the following way:

void main_display_function() {
    DrawCircle(0.0, 0.0, 1.45, 200);
    glutSwapBuffers();    
}

(4) Even in that function you don't clear the "window content" before redrawing:

void DrawCircle(float cx, float cy, float r, int num_segments) {
    glClearColor (0., 0., 0., 1);
    glClear (GL_COLOR_BUFFER_BIT);
    {…   …   … } //the rest of your code
}

(5) Passing color values of wrong data type:

    glColor3f(255, 255, 255);  //unsigned byte passed, float expected!

or

    glColor3ub(0.0, 0.0, 0.0); //float passed, unsigned byte expected!

There are few more issues which directly address your question - you will easily understand and debug them yourself, once you fix the program to work correctly (the order of calling glRotatef and the second glTranslatef, correctly computing movex, drawing beams or spokes). Please, also take a look at this SO post.

PS. Your using a C++ compiler to build this program doesn't make it a C++ code. There is not a single line of C++ specific code in your program. Also, both OpenGL and GLUT are plain-C APIs.

Community
  • 1
  • 1
user3078414
  • 1,942
  • 2
  • 16
  • 24
  • oh my god `glClear (GL_COLOR_BUFFER_BIT);` was very useful. ty again! – phen0menon Jun 09 '16 at 14:19
  • There's still much code which is inefficient, such as dividing constants inside the loop: `twicePi / 20`, or redundant: all the repeated `glColor3f()` calls for the spokes, when you in fact use only one color for all... – user3078414 Jun 09 '16 at 16:39
  • sorry i'm new in opengl. what's wrong with `twicePi /20`? – phen0menon Jun 09 '16 at 17:32
  • You divide two constants for 20 times. Like you have calculated `twicePi` outside the loop to prevent multiplying constants, you could have also defined `float rad = twicePi/20.`(or whichever number of fans) outside the loop. This has nothing to do with OpenGL. In every computer language or API it would be the same, because it is _math_. – user3078414 Jun 09 '16 at 18:00