OpenGL从具有X,Y和Z坐标的数组中创建形状 [英] OpenGL making a shape from an array that has X , Y and Z coordinates

查看:70
本文介绍了OpenGL从具有X,Y和Z坐标的数组中创建形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenGL的新手。我很乐意得到你的帮助。

是否有可能根据OpenGL阵列中的坐标在屏幕上制作一个点。

例如我有

 x = 2,y = 2,z = 2 

在数组中可能有数千个参考点。我需要从数组中的点创建一个形状。我正在3D视图中工作。这可能吗 ?有谁能帮我解决这个问题?在此先感谢。



我有用了。



 //#包括< windows.h> //对于MS Windows 
//改进
#include // GLUT,包括glu.h和gl.h
#include
#include< iostream>

struct Point {
GLfloat x,y,z;
点(GLfloat x,GLfloat y,GLfloat z):x(x),y​​(y),z(z){
//左空白
}
点中点(点p){
返回Point((x + px)/ 2,(y + py)/ 2,(z + pz)/ 2);
}
};

无效显示(无效){

/ * tum pikselleri temizle * /
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

返回;
} //结束void显示(void)

void foo(){

//四面体有四个顶点。我们还必须在绘图期间跟踪
//当前点。
静态点顶点[4] = {
点(-25,-22,-20),
点(-15,-22,-70),
点( 25,-22,-27),
点(0,45,-5)
};
static Point lastPoint = vertices [0];

//这是绘制下一个500点的代码。点b
越靠近相机,我们就越亮。着色技术是
//快速黑客(非专业)取决于四面体中z坐标的范围
//从-200到-700的范围。通过将
// 700添加到任何z值然后除以500,我们得到
// 0到1范围内的值 - 非常适合着色。
glBegin(GL_POINTS);
for(int i = 0; i< = 200; i ++){
lastPoint = lastPoint.midpoint(vertices [rand()%4]);
GLfloat烈度=(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);
返回;
} //结束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(显示);
glutReshapeFunc(重塑);
glutIdleFunc(foo);
init();
glutMainLoop();
返回0;
} // end int main(int,char **)< / iostream>< /windows.h>

解决方案

为每个点调用glvertex会对性能产生影响。

我建议使用调用列表或顶点缓冲区对象来优化顶点数据传输,并组合顶点和像素着色器来改变颜色基于顶点位置的顶点。



显示列表是一组已存储(编译)以供以后执行的OpenGL命令。创建显示列表后,将评估所有顶点和像素数据并将其复制到服务器计算机上的显示列表内存中。这只是一次性过程。



如果你想根据顶点位置改变输出像素的颜色,可以用顶点着色器实现。



看看这些调用列表和顶点缓冲对象的教程。



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 /

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>

解决方案

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/


这篇关于OpenGL从具有X,Y和Z坐标的数组中创建形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆