Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Java Cylinder with OpenGL  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to create a cylinder but it doesnt work.I am beginner and i am seriously struggling with this. Here is what I have:
package Objects;


public class SCylinder extends SObject{
private float radius;
private int slices;
private int stacks;

public SCylinder(){
super();
init();
update();
}

public SCylinder(float radius){
super();
init();
this.radius = radius;
update();
}

public SCylinder(float radius, int slices, int stacks){
super();
this.radius = radius;
this.slices = slices;
this.stacks = stacks;
update();
}

private void init(){
this.radius = 1;
this.slices = 20;
this.stacks = 20;
}

@Override
protected void genData() {
            int i,j,k;
             
                numVertices = 10000;
               
               

                // Generate vertices coordinates, normal values, and texture coordinates
                //numVertices = (slices + 1) * (stacks - 1) + 2;
                 vertices = new float[numVertices * 3];
                 normals = new float[numVertices * 3];
                //textures = new float[numVertices * 2];


                int theta = 0;
                            // vertices body
                  k = 1;
                  for (i = 0; i < slices; i++) {
                     normals[3 * k] = cos(theta * i);
                     normals[3 * k + 1] = sin(theta * i);
                     normals[3 * k + 2] = 0;

                     vertices[3 * k] = radius * normals[3 * k];
                     vertices[3 * k + 1] = radius * normals[3 * k + 1];
                     vertices[3 * k + 2] = .50f;
                     k++;
                   // end of for vertices on body
                  }
                  for (i = 0; i < slices; i++) {
                     normals[3 * k] = cos(theta * i);
                     normals[3 * k + 1] = sin(theta * i);
                     normals[3 * k + 2] = 0;

                     vertices[3 * k] = radius * normals[3 * k];
                     vertices[3 * k + 1] = radius * normals[3 * k + 1];
                     vertices[3 * k + 2] = -.50f;

                     k++;
                  } // end of for vertices on body

                        // Generate indices for triangular mesh
                 numIndices = 100000;
                 indices = new  int[numIndices];

                k = 0;

                for (i = 0; i < slices; ++i) {
                   
                     int i1 = i;
                     int i2 = (i1 + 1) % slices;
                     int i3 = i1 + slices;
                     int i4 = i2 + slices;
                    
                     indices[k++] = i1;
                     indices[k++] = i3;
                     indices[k++] = i2;
                    

                     indices[k++] = i4;
                     indices[k++] = i2;
                     indices[k++] = i3;
                    
                    
                }

        }       
               
               
     

public void setRadius(float radius){
this.radius = radius;
updated = false;
}

public void setSlices(int slices){
this.slices = slices;
updated = false;
}

public void setStacks(int stacks){
this.stacks = stacks;
updated = false;
}

public float getRadius(){
return radius;
}

public int getSlices(){
return slices;
}

public int getStacks(){
return stacks;
}
}
 
Saloon Keeper
Posts: 4745
117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"it doesn't work" doesn't give us much to go by. What does the code do now, and what do you think it should be doing instead?
 
Marshal
Posts: 60124
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always use the code button; then your code would look so much better, like this:-Why have you got that init() method? Why not use this(1, 20, 20); or similar in your constructors instead? What does the update() method do? Why are you using a float for radius? I think floats should not be used for radii or anything (if possible) because of their poor precision. What does the array of vertices mean? Are you trying to put the three coordinates into the same array? That is a recipe for disaster. How are you calculating sin and cos in lines 55‑56? What is the value of θ there? Why have you got that array at all? Why don't you calculate the coordinates whenever they are required?
The array makes this look like something out of an animation program where a cylinder reduces to a many‑sided prism.
 
Keit Mg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the java class SSphere I have to create a class SCylinder that does the same but for a cylinder. After that I use this class in my mail file in the display() and init(). I believe my problem is somewhere in the for loops where i have to generate the vertices and normals.
 
Campbell Ritchie
Marshal
Posts: 60124
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really supposed to write a sphere class like that? Obviously the special requirements of that framework necessitate what one would regard as dubious coding style in a general‑purpose class.

I think you will find some of your problems will become clearer if you go through our questions, including that about the value of θ. You haven't told us what is going wrong.
 
Keit Mg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whe I use tgis class for creating the cylinder:
For example:

Sobject cylinder=new SCylinder(1,40,40);
createObject(gl, cylinder);


Nothing is displayed.I dont understand how to index the triagle mesh for a cylinder.
 
Keit Mg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Keit Mg wrote:Whe I use tgis class for creating the cylinder:
For example:

Sobject cylinder=new SCylinder(1,40,40);
createObject(gl, cylinder);


Nothing is displayed.I dont understand how to index the triagle mesh for a cylinder.




I want to draw this:

image.png
[Thumbnail for image.png]
 
Campbell Ritchie
Marshal
Posts: 60124
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks even more complicated than the things you get in animation apps.
So you have a set of vertices forming a ring at one end and a set of vertices forming a ring at the other end but turned through θ ÷ 2. I suggest you start by working out the coordinates for one vertex at one end and one at the other end. (Or similar.) Forget about the array for the time being; you can come back to that later.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!