Opengl connected line node graph

hey guys, i am trying to create a graph with nodes and lines connecting the nodes all together ( it is going to be used for showing path methods such as a*) and i need a little help. i am trying to connect the nodes with the lines and i appear to be only getting squares, what would you suggest would be a better method to my current line draw? here is my current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "Timer.h"
#include <windows.h>
#include <time.h>
#include "sdl_opengl.h"
#include "sdl.h"
#include "AStar.h"
#include <iostream>

const int iSCREEN_WIDTH = 800;
const int iSCREEN_HEIGHT = 600;
const int iNODE_COUNT_X = 26;
const int iNODE_COUNT_Y = 26;
sNode m_nodes[iNODE_COUNT_Y][iNODE_COUNT_X];

int InitGL(int width, int height)
{
	int error;

	error = SDL_Init (SDL_INIT_EVERYTHING);

	//gl and SDL configurationss
	SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 32);
	SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);

	//Use Hardware acceleration
	unsigned int flags = SDL_OPENGL | SDL_HWSURFACE;

	//Vid mode
	SDL_SetVideoMode(width, height, 32, flags);

	//Window color
	glClearColor (0.0, 0.0, 0.0, 0.0);

	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho (0.0, width, height, 0.0, 0.0, 10.0);

	glDisable(GL_TEXTURE_2D);

	return error;
}

bool HandleInput()
{
	SDL_Event event;
	while (SDL_PollEvent (&event))
	{
		switch(event.type)
		{
	case SDL_QUIT:
		{
				return false;
		}
	case SDL_KEYDOWN:
				{
	switch (event.key.keysym.sym)
			{
	case SDLK_ESCAPE:
		{
	return false;
			}
		}
	break;
			}
		}
	}

	return true;
}

void GenerateNodes()
{
	const float fNodeSpacingX = iSCREEN_WIDTH / iNODE_COUNT_X + 2;
	const float fNodeSpacingY = iSCREEN_WIDTH / iNODE_COUNT_Y + 2;
	int iNodeCount = 0;

	for(int y = 0; y < iNODE_COUNT_Y; ++y)
	{
		for(int x = 0; x < iNODE_COUNT_X; ++x)
		{
    	m_nodes[y][x].vPosition.x = fNodeSpacingX * x;
    	m_nodes[y][x].vPosition.y = fNodeSpacingY * y;
    	std::cout << iNODE_COUNT_X << std::endl << iNODE_COUNT_Y ;
    	//give unique ID to all nodessssssssssssss
    	m_nodes[y][x].id = iNodeCount++;
		}
	}
}

void DrawNodes()
{
	glColor4f( 1.0f, 0.0f, 0.0f, 1.0f );

	//Point size
	glPointSize( 3.5f );

	//drawing points
	glBegin(GL_POINTS);
	for(int y = 0; y < iNODE_COUNT_Y; ++y)
	{
		for(int x = 0; x < iNODE_COUNT_X; ++x)
		{
			glVertex2f(m_nodes[y][x].vPosition.x, m_nodes[y][x].vPosition.y);
		}
	}

	glEnd();
	glBegin(GL_LINES);
	for(int y = 0; y < iNODE_COUNT_Y; ++y)
	{
		for(int x = 0; x < iNODE_COUNT_X; ++x)
		{
			glVertex2f(m_nodes[y][x].vPosition.x, m_nodes[y][x].vPosition.y);

		}

	}
	for(int x = 0; x < iNODE_COUNT_X; ++x)
	{
		for(int y = 0; y < iNODE_COUNT_Y; ++y)
		{
			glVertex2f(m_nodes[y][x].vPosition.x, m_nodes[y][x].vPosition.y);
		}
	}
glEnd();
}

int SDL_main(int argc, char* argv[])
{
	InitGL(iSCREEN_WIDTH, iSCREEN_HEIGHT);

	TimerInit();
	srand(time(NULL));

	GenerateNodes();
	//smoothing
		glShadeModel(GL_SMOOTH);
		
		glEnable(GL_POINT_SMOOTH);
		
		glEnable(GL_LINE_SMOOTH);


    	while(HandleInput())
    	{
    		float dt = TickTimer();
    		
    		glClear(GL_COLOR_BUFFER_BIT);
    
    		DrawNodes();
    
    		SDL_GL_SwapBuffers();

    	}
    
    	return 0;
}
A line needs two vertices. Example:
a-b-c
| |/
d-e

That graph has 6 edges, so you have to give 2*6 vertices:
a b b c a d b e c e d e
hmm i did
1
2
3
glVertex2f(n1->vPosition.x, n1->vPosition.y); glVertex2f(n2->vPosition.x, n2->vPosition.y);
			glVertex2f(n1->vPosition.x, n1->vPosition.y); glVertex2f(n3->vPosition.x, n3->vPosition.y);
	

that appears to add lines.
i have yet to add edges, so when it comes to me creating a highlighted path for pathfinding would this be acceptable?
Also how would i go by adding edges themselves?
Last edited on
That is a data structure decision.

What you have now is a non-sparse square grid http://en.wikipedia.org/wiki/Lattice_graph
and currently you would not need the 2D array, because you can compute pos.x, pos.y and id from (x,y).

If you had a sparse grid, you could use the 2D array only, if sNode has a bool flag to show that it is a real node.

You could have a list of nodes and a list of edges. Each edge points to two nodes. Each node has a list of edges that connect to it. Much fun, but you should study graph algorithms to find out the best solution for your case.
Topic archived. No new replies allowed.