Changeset 4046b51 in opengl-game


Ignore:
Timestamp:
Aug 9, 2017, 2:43:13 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
237ec28
Parents:
363d5ff
Message:

Dynamically allocate the points and colors arrays for the ball and begin writing the logic to create circles of varying smoothness in OpenGL

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pong.cpp

    r363d5ff r4046b51  
    1515GLuint createArrayBuffer(GLuint points_vbo, GLuint colors_vbo);
    1616
     17GLfloat* createCirclePoints(unsigned int smoothness, GLfloat centerX, GLfloat centerY, GLfloat radius);
     18GLfloat* createCircleColors(unsigned int smoothness);
     19
     20const double PI = 3.1415926535897;
    1721const bool FULLSCREEN = false;
    1822
     
    8387   // glFrontFace(GL_CW);
    8488
    85    GLfloat points[] = {
    86        0.0f,  0.5f,  0.5f,
    87       -0.5f, -0.5f,  0.5f,
    88        0.5f, -0.5f,  0.5f,
    89    };
    90 
    91    GLfloat colors[] = {
    92       1.0, 0.0, 0.0,
    93       0.0, 0.0, 1.0,
    94       0.0, 1.0, 0.0,
    95    };
     89   unsigned int numBallPoints = 3;
     90
     91   GLfloat* points = createCirclePoints(numBallPoints, 0.0f, 0.0f, 0.2f);
     92   GLfloat* colors = createCircleColors(numBallPoints);
    9693
    9794   GLfloat points_paddle[] = {
     
    120117   };
    121118
    122    GLuint ball_points_vbo = createDataBuffer(sizeof(points), points);
    123    GLuint ball_colors_vbo = createDataBuffer(sizeof(colors), colors);
     119   GLuint ball_points_vbo = createDataBuffer(numBallPoints*3*sizeof(GLfloat), points);
     120   GLuint ball_colors_vbo = createDataBuffer(numBallPoints*3*sizeof(GLfloat), colors);
    124121   GLuint ball_vao = createArrayBuffer(ball_points_vbo, ball_colors_vbo);
    125122
     
    166163      glDrawArrays(GL_TRIANGLES, 0, sizeof(points_paddle)/sizeof(GLfloat)/3);
    167164
    168       model[12] = last_position + speed*elapsed_seconds;
     165      // model[12] = last_position + speed*elapsed_seconds;
     166      model[12] = 0.0f;
    169167      last_position = model[12];
    170168      model[13] = 0.0f;
     
    175173      glEnableVertexAttribArray(1);
    176174
    177       glDrawArrays(GL_TRIANGLES, 0, sizeof(points)/sizeof(GLfloat)/3);
     175      glDrawArrays(GL_TRIANGLES, 0, numBallPoints);
    178176
    179177      glfwPollEvents();
     
    198196
    199197   glfwTerminate();
     198
     199   delete points;
     200   delete colors;
     201
    200202   return 0;
    201203}
     
    246248   return vao;
    247249}
     250
     251GLfloat* createCirclePoints(unsigned int smoothness, GLfloat centerX, GLfloat centerY, GLfloat radius) {
     252   GLfloat* points = new GLfloat[smoothness*3];
     253
     254   points[0] = 0.0f;
     255   points[1] = 0.5f;
     256   points[2] = 0.5f;
     257   points[3] = -0.5f;
     258   points[4] = -0.5f;
     259   points[5] = 0.5f;
     260   points[6] = 0.5f;
     261   points[7] = -0.5f;
     262   points[8] = 0.5f;
     263
     264   cout << "Sphere center: (" << centerX << ", " << centerY << ")" << endl;
     265
     266   smoothness *= 2;
     267   for (unsigned int i=0; i<smoothness; i++) {
     268      double radians = PI/2 + 2*PI * i/smoothness;
     269      cout << radians << endl;;     
     270      GLfloat x = centerX + cos(radians)*radius;
     271      GLfloat y = centerY + sin(radians)*radius;
     272      cout << "point " << i << ": (" << x << ", " << y << ")" << endl;
     273   }
     274
     275   return points;
     276}
     277
     278GLfloat* createCircleColors(unsigned int smoothness) {
     279   GLfloat* colors = new GLfloat[smoothness*3];
     280
     281   for (unsigned int i=0; i<smoothness; i++) {
     282      colors[i*3] = 0.0f;
     283      colors[i*3+1] = 1.0f;
     284      colors[i*3+2] = 0.0f;
     285   }
     286   colors[smoothness*3-3] = 0.0f;
     287   colors[smoothness*3-2] = 0.0f;
     288   colors[smoothness*3-1] = 1.0f;
     289
     290   return colors;
     291}
Note: See TracChangeset for help on using the changeset viewer.