Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new in OpenGL. I will be glad to get a little help from you.
Is it possible making a dot in screen according to coordinates that came from array in OpenGL.
For example I have
x=2 , y=2, and z=2 
In the array there could be thousands of reference points. I need to make a shape from the points in the array.I am working in a 3D view. is this possible ? Anyone can get me through this? Thanks in advance.

I got his useful.

//#include <windows.h>  // for MS Windows
//improved
#include   // GLUT, include glu.h and gl.h
#include 
#include <iostream>
 
struct Point {
  GLfloat x, y, z;
  Point(GLfloat x, GLfloat y, GLfloat z): x(x), y(y), z(z) {
	//Left Blank
  }
  Point midpoint(Point p) {
	  return Point((x+p.x)/2, (y+p.y)/2, (z+p.z)/2);
  }
};
 
void display(void) {
 
	/* tum pikselleri temizle */
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
	return;
} // end of void display(void)

void foo() {
 
	// The tetrahedron has four vertices.  We also have to keep track of the
  // current point during the plotting.
  static Point vertices[4] = {
    Point(-25, -22, -20),
    Point(-15, -22, -70),
    Point(25, -22, -27),
    Point(0, 45, -5)
  };
  static Point lastPoint = vertices[0];
 
  // Here is the code to draw the "next 500 points".  The closer the point is
  // to the camera, the brighter we make it.  The coloring technique is a
  // quick hack which (unprofessionally) depends on the fact that the range
  // of z-coordinates in the tetrahedron run from -200 to -700.  By adding
  // 700 to any z-value and then dividing by 500, we get values in the range
  // 0 to 1 - just perfect for coloring.
  glBegin(GL_POINTS);
  for (int i = 0; i <= 200; i++) {
    lastPoint = lastPoint.midpoint(vertices[rand() % 4]);
    GLfloat intensity = (600 + lastPoint.z) / 400.0;
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(lastPoint.x, lastPoint.y, lastPoint.z);
  }
  glEnd();
  glFlush();
	
 
}
 
void reshape(GLint w, GLint h) {
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(100.0, GLfloat(w)/GLfloat(h), 10.0, 1500.0);
}
 

void init(void) {
 
	glEnable(GL_DEPTH_TEST);
	return;
} // end of void init(void)

 
int main(int argc, char* argv[]) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(0, 0);
	glutCreateWindow(argv[0]);
	
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutIdleFunc(foo);
	init();
	glutMainLoop();
	return 0;
} // end int main(int, char**)</iostream></windows.h>
Posted
Updated 5-Dec-13 9:14am
v3
Comments
[no name] 2-Dec-13 16:21pm    
what kind of array?? is it like int[][][]? or array list?

1 solution

Caling glvertex for each point should make impact on performance.
I have suggestions to use call list or vertex buffer object to optimize the vertex data transfer, and combination vertex and pixel shader to change the color of vertex based on position of vertex.

Display list is a group of OpenGL commands that have been stored (compiled) for later execution. Once a display list is created, all vertex and pixel data are evaluated and copied into the display list memory on the server machine. It is only one time process.

If you want to change the color of output pixel based on the vertex position, it can be implemented with a vertex shader.

Have a look at these tutorials of call list and vertex buffer objects.

http://www.songho.ca/opengl/gl_displaylist.html

http://nehe.gamedev.net/tutorial/vertex_buffer_objects/22002/">http://nehe.gamedev.net/tutorial/vertex_buffer_objects/22002/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900