Creating a 3D Solid Sphere in OpenGL

This is a discussion on Creating a 3D Solid Sphere in OpenGL within the C++ Programming forums, part of the General Programming Boards category; Hi guys! I'm working on a new OpenGL problem today: Creating a 3D Solid Sphere in OpenGL. I've used the ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    78

    Creating a 3D Solid Sphere in OpenGL

    Hi guys!


    I'm working on a new OpenGL problem today: Creating a 3D Solid Sphere in OpenGL.

    I've used the following code to define a class '3DSphere.cpp' (according to Link).

    Code:
    /*
     * 3DSphere.cpp
     *
     *  Created on: Oct 30, 2012
     *      Author: guptaa
     */
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <gl.h>
    #include <glu.h>
    #include <glut.h>
    
    #include <math.h>
    #include <vector.h>
    
    
    class 3DSphere
    {
    
    protected:
        vector<GLfloat> vertices;
        vector<GLfloat> normals;
        vector<GLfloat> texcoords;
        vector<GLushort> indices;
    
    public:
        void SolidSphere(float radius, unsigned int rings, unsigned int sectors)
        {
            float const R = 1./(float)(rings-1);
            float const S = 1./(float)(sectors-1);
            int r, s;
    
            sphere_vertices.resize(rings * sectors * 3);
            sphere_normals.resize(rings * sectors * 3);
            sphere_texcoords.resize(rings * sectors * 2);
            vector<GLfloat>::iterator v = sphere_vertices.begin();
            vector<GLfloat>::iterator n = sphere_normals.begin();
            vector<GLfloat>::iterator t = sphere_texcoords.begin();
            for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
                    float const y = sin( -M_PI_2 + M_PI * r * R );
                    float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R );
                    float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R );
    
                    *t++ = s*S;
                    *t++ = r*R;
    
                    *v++ = x * radius;
                    *v++ = y * radius;
                    *v++ = z * radius;
    
                    *n++ = x;
                    *n++ = y;
                    *n++ = z;
            }
    
            sphere_indices.resize(rings * sectors * 4);
            std:vector<GLushort>::iterator i = sphere_indices.begin();
            for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
                    *i++ = r * sectors + s;
                    *i++ = r * sectors + (s+1);
                    *i++ = (r+1) * sectors + (s+1);
                    *i++ = (r+1) * sectors + s;
            }
        }
    
        void draw(GLfloat x, GLfloat y, GLfloat z)
        {
            glMatrixMode(GL_MODELVIEW);
            glPushMatrix();
            glTranslatef(x,y,z);
    
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_NORMAL_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
            glVertexPointer(3, GL_FLOAT, 0, &sphere_vertices[0]);
            glNormalPointer(GL_FLOAT, 0, &sphere_normals[0]);
            glTexCoordPointer(2, GL_FLOAT, 0, &sphere_texcoords[0]);
            glDrawElements(GL_QUADS, sphere_indices.size()/4, GL_UNSIGNED_SHORT, sphere_indices);
            glPopMatrix();
        }
    }

    However, I receive the following error (ref: line 21 - opening of 3DSphere class):
    invalid suffix "DSphere" on integer constant
    It seems a C error rather than an OpenGL one.

    Also - where does the actual drawing take place? There is no main function here!

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    First, this is C++ code, so this post should be moved to the C++ board.

    Second, you cannot start an identifier with a digit. Only A-Za-z_ (and I think $) are allowed for the first character of an identifier.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by christop View Post
    First, this is C++ code, so this post should be moved to the C++ board.

    Second, you cannot start an identifier with a digit. Only A-Za-z_ (and I think $) are allowed for the first character of an identifier.

    Thanks for letting me know. I hope someone can shift this thread into another section.

    I changed the name to just 'Sphere.cpp'. And now the rest of the error have started appearing:

    Code:
    expected identifier before numeric constant    Sphere.cpp    /CreateSyntheticObjects    line 21    C/C++ Problem
    expected unqualified-id before numeric constant    Sphere.cpp    /CreateSyntheticObjects    line 21    C/C++ Problem
    Invalid arguments '
    Candidates are:
    void glDrawElements(unsigned int, int, unsigned int, const void *)
    
    invalid suffix "DSphere" on integer constant    Sphere.cpp    /CreateSyntheticObjects    line 21    C/C++ Problem
    Method 'begin' could not be resolved    Sphere.cpp    /CreateSyntheticObjects    line 40    Semantic Error
    
    Method 'resize' could not be resolved    Sphere.cpp    /CreateSyntheticObjects    line 37    Semantic Error
    Method 'size' could not be resolved    Sphere.cpp    /CreateSyntheticObjects    line 83    Semantic Error
    Symbol 'sphere_indices' could not be resolved    Sphere.cpp    /CreateSyntheticObjects    line 60    Semantic Error
    Symbol 'sphere_normals' could not be resolved    Sphere.cpp    /CreateSyntheticObjects    line 38    Semantic Error
    Symbol 'sphere_texcoords' could not be resolved    Sphere.cpp    /CreateSyntheticObjects    line 39    Semantic Error
    Symbol 'sphere_vertices' could not be resolved    Sphere.cpp    /CreateSyntheticObjects    line 37    Semantic Error

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Moved to C++ programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL, problem creating rendering context
    By therealsabin in forum C++ Programming
    Replies: 3
    Last Post: 08-03-2011, 05:56 AM
  2. 3-dimensional solid program
    By thekautz in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2009, 03:21 PM
  3. Creating OpenGL Texture
    By Kernel Sanders in forum Game Programming
    Replies: 9
    Last Post: 12-15-2008, 04:08 PM
  4. Replies: 3
    Last Post: 04-04-2002, 04:27 PM
  5. Direct X + C = Solid App?
    By fatpotatohead in forum Game Programming
    Replies: 1
    Last Post: 09-03-2001, 04:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21