Thread: Please could you explain this code?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    Please could you explain this code?

    Please could you explain the following code in detail as i am so baffled and i do not know what this code exactly does. This is a game that basically draws multiple 3d objects i don't understand how and where the drawings were achieved so could you please explain thank you in advance

    here is the code:

    Code:
    #ifndef _GAME_OBJECTS_H_ 
    #define _GAME_OBJECTS_H_ 
    
    
    class GameObjects 
    {
    public:
    	GameObjects();
    	virtual void Update()=0;  
    	virtual void Draw()=0; 
    
    	const float* GetPos();
    	void SetPos(float x, float y, float z );
    
    	
    	bool m_deleteMe;
    protected:
    	float m_pos[3];
    };
    
    #endif
    This is a cpp file that i think it is also linked to the drawing of the 3d object

    Code:
    GameObjects::GameObjects()
    {
    	m_deleteMe = false; 
    	m_pos[0] = m_pos[1] = m_pos[2] = 0.0f; 
    }
    
    const float* GameObjects::GetPos(){
    	return m_pos; 
    }
    
    void GameObject::SetPos(float x, float y, float z ){ 
    	m_pos[0] = x;
    	m_pos[1] = y;
    	m_pos[2] = z;
    }
    This is the 1st 3d object that I do not understand how it was drawn

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include "glut.h"
    
    #include "Object.h" 
    
    
    Object::Object()
    {
    	m_pos[2] = -2.8;
    }
    void Object::Update(){
    
    }
    
    
    void Object::Draw(){
    	
    	float color[] = {0.2,1.0,0.2,1.0};
    	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
    
    	glPushMatrix();
    	glTranslatef(m_pos[0],m_pos[1],m_pos[2]);
    
    	glPushMatrix();
    	glutSolidCone(0.4,0.4f,12,12);
    	glPopMatrix();
    
    	for(int i=0; i<4; i++){
    		glPushMatrix();
    		glTranslatef(0,0,-0.15*(i+1));
    		glutSolidCone(0.3f,0.4f,12,12);
    		glPopMatrix();
    	}
    	glPushMatrix();
    	glTranslatef(0,0,-0.15*5);
    	glutSolidCone(0.5f,0.4f,12,12);
    	glPopMatrix();
    
    	glPopMatrix();
    
    }

    Please help guys

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What does object.h file contains?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Quote Originally Posted by ssharish2005 View Post
    What does object.h file contains?

    ssharish
    That's what it has

    Code:
    #ifndef _OBJECT_H_
    #define _OBJECT_H_
    
    #include "GameObjects.h"
    
    
    class Object : public GameObjects
    {
    public:
    	Object();
    	void Update();
    	void Draw();
    };
    
    #endif

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Ok class object inherits from class GamesObject. That kind of makes sense now. So its quite diffcult to explain the whole code. I would appreciate if you point out which part of the code you dont understand. You will have to ask smart question rather than just asking anything.

    It will take me ages to explain you that code. And i do not have that much time

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    like ssharish2005 said, there are many things going on in there. The short version is that object:: Draw() is the one that tell openGL how to "plot" your 3D object-- in this case, it's a solid cone since you're calling glutSolidCone(). OpenGL uses stacks of matrices to manage your object and how you view the object. since matrices are stored in stack, you need to push and pop them.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Quote Originally Posted by ssharish2005 View Post
    Ok class object inherits from class GamesObject. That kind of makes sense now. So its quite diffcult to explain the whole code. I would appreciate if you point out which part of the code you dont understand. You will have to ask smart question rather than just asking anything.

    It will take me ages to explain you that code. And i do not have that much time

    ssharish
    Thank you for the response,

    Could you please explain how the 3d object was drawn, and translated into positioned?

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Mehdi-110 View Post
    Thank you for the response,

    Could you please explain how the 3d object was drawn, and translated into positioned?
    for that particulate code check out: glutSolidCone
    OpenGL can draw different object as well.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Quote Originally Posted by nimitzhunter View Post
    for that particulate code check out: glutSolidCone
    OpenGL can draw different object as well.
    Thank you for that,

    but could you briefly explain how would one figure out the base , height , slices , stacks of glutSolidCone, would it be possible to initially draw it on a graph paper? or how would would initially figure it out before actually translating it into code? and could you also briefly explain how the colouring of that object was achieved, thank you so much for you help

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You should start reading this book to understand the opengl uasage.
    Chapter 1. Introduction to OpenGL

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Quote Originally Posted by ssharish2005 View Post
    You should start reading this book to understand the opengl uasage.
    Chapter 1. Introduction to OpenGL

    ssharish
    Thank you so much for your help,

    could please explain the loop there, what does it do

  11. #11
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Mehdi-110 View Post
    Thank you so much for your help,

    could please explain the loop there, what does it do
    be honest here, is this a homework? ssharish gave you the link, go read it and play around.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please explain me this C code fragment
    By stefboombastic in forum C Programming
    Replies: 6
    Last Post: 12-16-2010, 01:29 PM
  2. Help please explain what code does
    By cybersasho in forum C Programming
    Replies: 3
    Last Post: 10-08-2010, 05:27 AM
  3. Can someone explain a line of code for me please?
    By drkidd22 in forum C Programming
    Replies: 8
    Last Post: 12-09-2009, 10:36 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM