C programming

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
for my previous post,i have already done this.

Code:
glColor3f(1.0,1.0,0.0);
		GLfloat vertices[] = {
			minX,minY,minZ,1.0,
			maxX,minY,minZ,1.0,
			maxX,maxY,minZ,1.0,
			minX,maxY,minZ,1.0,
			minX,minY,maxZ,1.0,
			maxX,minY,maxZ,1.0,
			maxX,maxY,maxZ,1.0,
			minX,maxY,maxZ,1.0,
		};
		float sx,sy,sz;//size of the cube
		sx = maxX - minX;
		sy = maxY - minY;
		sz = maxZ - minY;
		float cx,cy,cz;//center of the object
		cx = (maxX+minX)/2;
		cy = (maxY+minY)/2;
		cz = (maxZ+maxZ)/2;
		glutSolidCube(1.0);//prepare the cube

OpenGL Programming/Bounding box - Wikibooks, open books for an open world

We analyze the object:
Minimum coordinates in X, Y and Z
Maximum coordinates in X, Y and Z
We compute the size necessary for the object: max-min in X, Y and Z
We compute the center of the object: (min+max)/2 in X, Y and Z
We compute of cube around the object:
We prepare a cube of size 1 (1x1x1), centered on the origin
We scale it by the size of the object
We center it on the object
To draw the cube:
We draw the front face using 1 looping line
We draw the back face using 1 looping line
We draw 4 orthogonal lines to join both faces

I am up to the stage of the cube,could it be using the looping function?
what kind of cube do there need?
I still need to translate it to the center of the object,

update::

Code:
	float sx,sy,sz;//size of the cube
		sx = maxX - minX;
		sy = maxY - minY;
		sz = maxZ - minY;
		float cx,cy,cz;//center of the object
		cx = (maxX+minX)/2;
		cy = (maxY+minY)/2;
		cz = (maxZ+maxZ)/2;
		glScalef(sx,sy,sz);
		glTranslatef(cx,cy,cz);
		glutWireCube(10.0);//prepare the cube

i didn't use frontface ,back face and line loop method,glutWireCube also can work right?

I want to try the line loop method.
 
Last edited:

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
What is the exact difference in terms of coding that you did, which resulted in the diminishing rendering ?

could be the reshape func

void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //<--- add
glLoadIdentity(); //<--- add
gluPerspective(45.0,(double)w / (double)h,1.0,200.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, (GLsizei)w, (GLsizei)h); // Set our viewport to the size of our window
glLoadIdentity();
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,546
Reaction score
1,299
for my previous post,i have already done this.

Code:
glColor3f(1.0,1.0,0.0);
		GLfloat vertices[] = {
			minX,minY,minZ,1.0,
			maxX,minY,minZ,1.0,
			maxX,maxY,minZ,1.0,
			minX,maxY,minZ,1.0,
			minX,minY,maxZ,1.0,
			maxX,minY,maxZ,1.0,
			maxX,maxY,maxZ,1.0,
			minX,maxY,maxZ,1.0,
		};
		float sx,sy,sz;//size of the cube
		sx = maxX - minX;
		sy = maxY - minY;
		sz = maxZ - minY;
		float cx,cy,cz;//center of the object
		cx = (maxX+minX)/2;
		cy = (maxY+minY)/2;
		cz = (maxZ+maxZ)/2;
		glutSolidCube(1.0);//prepare the cube

OpenGL Programming/Bounding box - Wikibooks, open books for an open world



I am up to the stage of the cube,could it be using the looping function?
what kind of cube do there need?
I still need to translate it to the center of the object,

update::

Code:
	float sx,sy,sz;//size of the cube
		sx = maxX - minX;
		sy = maxY - minY;
		sz = maxZ - minY;
		float cx,cy,cz;//center of the object
		cx = (maxX+minX)/2;
		cy = (maxY+minY)/2;
		cz = (maxZ+maxZ)/2;
		glScalef(sx,sy,sz);
		glTranslatef(cx,cy,cz);
		glutWireCube(10.0);//prepare the cube

i didn't use frontface ,back face and line loop method,glutWireCube also can work right?

I want to try the line loop method.

Once you find out the min and max of all the vertices you are trying to bound within a box, you can easily use GL_LINES to plot the edges of the box. Why do you need to think so hard ?
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
I recalled on lab8 for data structure I wrote some code for graphs an adjaceny matrix in java.

along the line teacher says need adjacency matrix,


Code:
public class Graph {


    private int graphSize = 0;
    private int adjacencyMatrix[][];


    public Graph(int size) {

        //create an adjacency matrix based on the graph size
        graphSize = size;
        adjacencyMatrix = new int[size][size];
        for (int i = 0; i < adjacencyMatrix.length; i++) {
            for (int j = 0; j < adjacencyMatrix[0].length; j++) {
                adjacencyMatrix[i][j] = 0;
            }
        }
    }



    public void printAdjacencyMatrix() {
        if (!isEmpty()) {
            for (int i = 0; i < adjacencyMatrix.length; i++) {
                for (int j = 0; j < adjacencyMatrix[0].length; j++) {
                    System.out.print(adjacencyMatrix[i][j] + " ");
                }
                System.out.println();
            }
        }
    }


    /*
     * insert the edges given the adjacency matrix.
     * it will be arranged as of undirected graph order
     */
    public void insertEdge(int nodeID, int edge) {
        //take in nodeID and its correspodning edges
        //iterate through the adjacencymatrix
        //set adjacencymatrix based on undirect graph pending
        if (!isEmpty()) {
            adjacencyMatrix[nodeID][edge] = 1;
            //since must be undirected so
            //adjacency matrix finally cloned
            adjacencyMatrix[edge][nodeID] = 1;
        }

    }


    public boolean isEmpty() {
        return graphSize == 0;
    }

   
    /*
     * Print out the depth first search tree from vertex v
     */

    public void printDepthFirstSearch(int v) {

        if (!isEmpty()) {
        boolean[] isVisited = new boolean[graphSize];
        //given a verticies
        dfs2(v, isVisited);
        System.out.println();
        }
    }

    private void dfs2(int v, boolean isVisited[]) {
        //this area will be visited starting from the int v
        //for each vertex v visited
        //store the visited vertex into an array
        //if not visited then print it out
        if(!isVisited[v]){
            System.out.print(v + " ");
        }

        isVisited[v] = true;
        //iterate through the adjacency matrix
        //each neighbour of v
        int count = 0;
        for (int neigh = 0; neigh < adjacencyMatrix[v].length; neigh++) {
            if (adjacencyMatrix[v][neigh] == 1) {//if it exist
                if (!isVisited[neigh]) { //if not visited for that particular neighbour
                    count ++;
                    dfs2(neigh, isVisited);
                }
            }
        }
       if(count == 0) System.out.println();

    }


    /*
     *
     * Print the depth first search given the verticies
     * @param v as the starting point
     */
    public void printBreathFirstSearch(int v) {
        if (!isEmpty()) {
        boolean[] isVisited = new boolean[graphSize];
        //given a verticies
        bfs2(v, isVisited);
        System.out.println();
        }
    }


    //the int need to be a node?
    private void bfs2(int v, boolean isVisited[]) {
        Queue1 queue = new Queue1();
        queue.enqueue(v);
        isVisited[v] = true;

        if(!isVisited[v]){
              System.out.print(v + " ");
        }
        while (!queue.isEmpty()) {
            int vertex = Integer.parseInt(queue.dequeue().toString());
            //given the vertex
            
            
            for (int neigh = 0; neigh < adjacencyMatrix[vertex].length;neigh ++ ) {//for each neigh
                if (adjacencyMatrix[vertex][neigh] == 1 && !isVisited[neigh]) {
                        //been visited
                        queue.enqueue(new Integer(neigh));//pending recursive visiting
                        //dequeue to search for more neighbours
                        bfs2(Integer.parseInt(queue.dequeue().toString()), isVisited);
                       
                }
                //create a new line to break
                

            }

            

        }
    }

    public void printConnectedComponents() {
        if (!isEmpty()) {
        boolean[] isVisited = new boolean[graphSize];
        for(int neigh = 0;neigh < adjacencyMatrix.length;neigh++){
            //break
            dfs2(neigh,isVisited);
        }
        System.out.println();
        }
    }

}

I also wrote before some code for reading graphs from a text files:

Code:
 /*
     * Read the graph from a text file.
     */
    public static void readFile() {

        int nodeID, size, numOfEdge,edge, count = 0;
        try {

            System.out.println("Enter the file name:");
            Scanner in = new Scanner(new File(console.nextLine()));

            size = in.nextInt();//verticies size
            g = new Graph(size);
            //not useful in this case as can get size from the lab8
            for (int j = 0; j < size; j++) {
                //first ndode past through already
                nodeID = in.nextInt();
                count++;
                //check the number of edges
                numOfEdge = in.nextInt();
                for (int k = 0; k < numOfEdge; k++) {
                    //insert the edge with regards to the node id
                    edge = in.nextInt();
                    g.insertEdge(nodeID, edge);
                }
            }
           // if need for debugging just use this method
           // g.printAdjacencyMatrix();

            //send the vertices and its edges to the adjacency matrix
        } catch (FileNotFoundException ex) {
  
            System.out.println("File not found!");
        }

        System.out.println(count + " nodes read");
        
    }

the format is like that:

6
0 2 1 2
1 2 0 2
2 3 0 1 3
3 1 2
4 1 5
5 1 4

still trying to figure out between graphs and half-edge data structure,
btw my mesh is only need triangle only.no need for rect,poly,tetra etcs.


speaking of which,

i had a easy time converting this java graph to C++,
please suggest if it can be useful

Code:
class Graph {
    int graphSize;
    int **adjacencyMatrix;
    Graph(int size) {
        //create an adjacency matrix based on the graph size
        graphSize = size;
        adjacencyMatrix = new int*[size];
		for(int i=0; i<size; ++i)
		adjacencyMatrix[i]=new int[size];        
    }
    void printAdjacencyMatrix() {
        if (!isEmpty()) {
            for (int i = 0; i < graphSize; i++) {
                for (int j = 0; j < graphSize; j++) {
					cout << adjacencyMatrix[i][j] + " " << endl;
                }
				cout << endl;
            }
        }
    }
    /*
     * insert the edges given the adjacency matrix.
     * it will be arranged as of undirected graph order
     */
     void insertEdge(int nodeID, int edge) {
        //take in nodeID and its correspodning edges
        //iterate through the adjacencymatrix
        //set adjacencymatrix based on undirect graph pending
        if (!isEmpty()) {
            adjacencyMatrix[nodeID][edge] = 1;
            //since must be undirected so
            //adjacency matrix finally cloned
            adjacencyMatrix[edge][nodeID] = 1;
        }

    }


    bool isEmpty() {
        return graphSize == 0;
    }

   
    /*
     * Print out the depth first search tree from vertex v
     */

    void printDepthFirstSearch(int v) {
        if (!isEmpty()) {
		bool* isVisited = new bool[graphSize]();   
		isVisited = new bool[graphSize];
        //given a verticies
        dfs2(v, isVisited);
		}
    }

    void dfs2(int v, bool isVisited[]) {
        //this area will be visited starting from the int v
        //for each vertex v visited
        //store the visited vertex into an array
        //if not visited then print it out
        if(!isVisited[v]){
			cout << v + " " << endl;
        }

        isVisited[v] = true;
        //iterate through the adjacency matrix
        //each neighbour of v
        int count = 0;
        for (int neigh = 0; neigh < graphSize; neigh++) {
            if (adjacencyMatrix[v][neigh] == 1) {//if it exist
                if (!isVisited[neigh]) { //if not visited for that particular neighbour
                    count ++;
                    dfs2(neigh, isVisited);
                }
            }
        }
       if(count == 0) cout << endl;
    }


    /*
     *
     * Print the depth first search given the verticies
     * @param v as the starting point
     */
    void printBreathFirstSearch(int v) {
        if (!isEmpty()) {
        bool* isVisited = new bool[graphSize]();   
        //given a verticies
        bfs2(v, isVisited);
        cout << endl;
        }
    }


    //the int need to be a node?
    void bfs2(int v, bool isVisited[]) {
        queue<int> q;
        q.push(v);
        isVisited[v] = true;
        if(!isVisited[v]){
              cout << v + " " << endl;  
        }
        while (!q.empty()) {
			int vertex = q.pop;
            //given the vertex
            for (int neigh = 0; neigh < graphSize ;neigh ++ ) {//for each neigh
                if (adjacencyMatrix[vertex][neigh] == 1 && !isVisited[neigh]) {
                        //been visited
						q.push(neigh);
                        //dequeue to search for more neighbours
                        bfs2(q.pop(),isVisited);
                }
                //create a new line to break
			}
        }
    }

    void printConnectedComponents() {
        if (!isEmpty()) {
        bool* isVisited = new bool[graphSize]();  
        for(int neigh = 0;neigh < graphSize;neigh++){
            //break
            dfs2(neigh,isVisited);
        }
        cout << endl;
        }
    }

};
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,546
Reaction score
1,299
speaking of which,

i had a easy time converting this java graph to C++,
please suggest if it can be useful

Code:
class Graph {
...

};

Sorry I won't have the time to recommend you on your codes if it is suitable. I believe you are learnt enough to make the necessary judgement.

I would believe you will find it more productive if my advices are on something more substantial and concrete.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
2d6w77q.png


if I do a line it will look like zig zag.

54j0vq.png
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Would I be able to infer what codes u wrote from the screen capture?

i just give you this portion

Code:
void disp(void){
	glEnable(GL_DEPTH_TEST); 
	// Just clean the screen
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	// setup the perspective projection
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 
	gluPerspective(60, 1, .1, 10); 

	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
	//eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX, upY, upZ
	gluLookAt(0,0,2,0,0,0,0,1,0); 
	// rotate and scale the object
	glRotatef(x_angle, 0, 1,0); 
	glRotatef(y_angle, 1,0,0); 
	glScalef(scale_size, scale_size, scale_size); 
	float minX = -0.432809;
	float minY = -0.4454530;
	float minZ = -0.360542;
	float maxX = 0.432955;
	float maxY = 0.499768;
	float maxZ = 0.360529;
	float sx,sy,sz,cx,cy,cz;
	sx = maxX - minX;
	sy = maxY - minY;
	sz = maxZ - minZ;
	cx = (maxX + minX)/2;
	cy = (maxY + minY)/2;
	cz = (maxZ + minZ)/2;
	glScalef(0.1,0.1,0.1);
	glColor3f(1.0, 1.0, 1.0);    
	glBegin(GL_LINES);
		glVertex3f(minX, minY, minZ);
		glVertex3f(maxX, minY, minZ);
		glVertex3f(maxX, maxY, minZ);
		glVertex3f(maxX, maxY, maxZ);
		glVertex3f(minX, minY, maxZ);
		glVertex3f(minX, maxY, maxZ);
		glVertex3f(minX, maxY, minZ);
		glVertex3f(maxX, minY, maxZ);			
	glEnd();
	glutSolid
	// swap the buffers
	glutSwapBuffers(); 
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,546
Reaction score
1,299
i just give you this portion

Code:
void disp(void){
	...
	glBegin(GL_LINES);
		glVertex3f(minX, minY, minZ);
		glVertex3f(maxX, minY, minZ);
		glVertex3f(maxX, maxY, minZ);
		glVertex3f(maxX, maxY, maxZ);
		glVertex3f(minX, minY, maxZ);
		glVertex3f(minX, maxY, maxZ);
		glVertex3f(minX, maxY, minZ);
		glVertex3f(maxX, minY, maxZ);			
	glEnd();
	...
}

Before I start answering your doubts. I would like you to keep this thing in mind, "SPOON FEED" me. I wouldn't spoon feed you because you are the learner. You have to spoon feed someone if you want someone to get as much information as possible to help you, not that I need to ask you what you need to provide. I hope we can have a common understanding on this. :)

Now, I would expect at least you know how GL_LINES is used. If not, please do read up on the OpenGL reference or at least good on how it is used.

A common question, how many edges form a box ? Do you have sufficient lines to form a box at all ? I only see 4 edges drawn which is exactly what you are doing in your codes.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
8 edges form a box

teacher say 2 poly to form top face and bottom
then use gl line to joint them

btw if i want to draw two triangle or two line seperate how do I do it?
so that it won't overlap?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,546
Reaction score
1,299
8 edges form a box

teacher say 2 poly to form top face and bottom
then use gl line to joint them

btw if i want to draw two triangle or two line seperate how do I do it?
so that it won't overlap?

Either you use GL_POLYGON or GL_LINES, given only a few lines, I doubt it makes much difference in performance or ease, so just choose what you prefer.

Don't get your last question. If you want to draw 2 lines separate on paper, what will you do ? What is so special about these 2 lines ?
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
most of problems like cannot read file larger than >1,scaling dimishing is removed,aabb is drawn,and finally there is GUI menu which makes my testing much more easier.

i sorted my writing in small parts and creating folders and batch files to run them.

file,
box,
wire,
fscanf,
line,
menu,
mesh,
min_max,

i just trying out the smaller parts first and the writing my original code last week.I stopped using the visual studio and used bcc32 tools to do my work since last week.

today i come across this called UNIT Testing,i realise i just have been doing this,it is a lifesaver,then I came across this on my course note that I should TEST before CODE.

all i need to do is to use the code that i tested out which works and rewrite them(refactor) all at one go,compared to the time when I use visual studio and change one or two lines and compile which takes up a lots of seconds.
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,546
Reaction score
1,299
most of problems like cannot read file larger than >1,scaling dimishing is removed,aabb is drawn,and finally there is GUI menu which makes my testing much more easier.

i sorted my writing in small parts and creating folders and batch files to run them.

file,
box,
wire,
fscanf,
line,
menu,
mesh,
min_max,

i just trying out the smaller parts first and the writing my original code last week.I stopped using the visual studio and used bcc32 tools to do my work since last week.

today i come across this called UNIT Testing,i realise i just have been doing this,it is a lifesaver,then I came across this on my course note that I should TEST before CODE.

all i need to do is to use the code that i tested out which works and rewrite them(refactor) all at one go,compared to the time when I use visual studio and change one or two lines and compile which takes up a lots of seconds.

Test before coding is industrially known as TDD, Test Driven Development. It's one kind of technique in Agile development methodology. Are you doing that ?

Writing Unit Testing and Integration Testing is part and parcel of most development works. Especially useful in complex system development since they help to ensure correctness in a modular manner during the whole development cycle.

However that has nothing to do with with IDE is used or which compiler is used.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Test before coding is industrially known as TDD, Test Driven Development. It's one kind of technique in Agile development methodology. Are you doing that ?

Writing Unit Testing and Integration Testing is part and parcel of most development works. Especially useful in complex system development since they help to ensure correctness in a modular manner during the whole development cycle.

However that has nothing to do with with IDE is used or which compiler is used.

i am writing small parts(modules) and the parts which i see fits i just put inside(integrate) my main code at the end of the day.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,546
Reaction score
1,299
i am writing small parts(modules) and the parts which i see fits i just put inside(integrate) my main code at the end of the day.

I don't think you are doing TDD. It's just that you have testing codes after you code. That's fine actually. You should be doing modular coding since it able you to see clearer on how you code and the functional schematics of your application.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
I don't think you are doing TDD. It's just that you have testing codes after you code. That's fine actually. You should be doing modular coding since it able you to see clearer on how you code and the functional schematics of your application.

test code before i code also,

how i can use SVN?
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top