从数组中绘制三角形 [英] Drawing triangles from an array

查看:262
本文介绍了从数组中绘制三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

可以说我有一个浮点数数组:

 浮动 []点= { 1 . 0 . 0 . 1 . 0  1 . 0  1 . 0  1 . 0  1 . 0 };  

这是ex三角形上的一个点,其中前3个浮点是坐标,后2个是纹理坐标,后3个是法线坐标.

如何使用GLSL在OpenGL中快速绘制此图像(如果需要)?

解决方案

我从来没有在OpenGL中做过任何事情.但指的是 MSDN [ 浮动 []点= { 1 .0F, 0 .0F, 0 .5F, 1 .0F, 1 .0F, 1 .0F, 1 .0F, 1 .0F }; glNormal3f(points [ 0 ],points [ 1 ],points [ 3 ],points [ 4 ]); glVertex3f(points [ 5 ],points [ 6 ],points [ // 自定义数据持有者类的定义 TriangleCoordinages { public float NormalX,NormalY,NormalZ; public float TextureX,TextureY; public float LocationX,LocationY,LocationZ; 公共 TriangleCoordinates( float normalX, float normalY, float normalZ , float textureX, float textureY, float locationX, float locationY, float locationZ ){ NormalX = normalX; NormalY = normalY; NormalZ = normalZ; TextureX = textureX; TextureY = textureY; LocationX = locationX; LocationY = locationY; LocationZ = locationZ; } } // 数据持有者对象的实例化 TriangleCoordinates triangle = TriangleCoordinates( 1 .0F, 0 .0F, 0 .5F, 1 .0F, 1 .0F, 1 .0F, 1 .0F, 1 .0F ); // 在数据持有者对象中使用数据 glNormal3f(triangle.NormalX,triangle.NormalY,triangle.NormalZ); glTexCoord2f(triangle.TextureX,triangle.TextureY); glVertex3f(triangle.LocationX,triangle.LocationY,triangle.LocationZ);


Hi.

Lets say i have an array of floats:

float[] points = {1.0, 0.0, 0.5,
1.0, 1.0,
1.0, 1.0, 1.0};



This is one point on ex a triangle, where the first 3 floats are coordinates, the 2 next texture coordinates, and the last 3 normal coordinates.

How do i fast draw this in OpenGL using GLSL(If needed)?

I''ve never done anything in OpenGL. But referring to MSDN[^], I suggest

float[] points = {
    1.0F, 0.0F, 0.5F,
    1.0F, 1.0F,
    1.0F, 1.0F, 1.0F
};

glNormal3f(points[0], points[1], points[2]);
glTexCoord2f(points[3], points[4]);
glVertex3f(points[5], points[6], points[7]);


points is an array. You can access its elements using the correct index in square brackets. Be careful not to use a non-existent index for it will result in an exception.

Have you considered to use a custom object with more descriptive naming for the floats? (I don''t know the performance penalty, though). It could read something like that:

// Definition of your custom data holder class
class TriangleCoordinages
{
    public float NormalX, NormalY, NormalZ;
    public float TextureX, TextureY;
    public float LocationX, LocationY, LocationZ;

    public TriangleCoordinates(
        float normalX, float normalY, float normalZ,
        float textureX, float textureY,
        float locationX, float locationY, float locationZ
    ){
        NormalX = normalX;
        NormalY = normalY;
        NormalZ = normalZ;
        TextureX = textureX;
        TextureY = textureY;
        LocationX = locationX;
        LocationY = locationY;
        LocationZ = locationZ;
    }
}

// Instantiation of a data holder object
TriangleCoordinates triangle = new TriangleCoordinates(
    1.0F, 0.0F, 0.5F,
    1.0F, 1.0F,
    1.0F, 1.0F, 1.0F
);

// Using data within the data holder object
glNormal3f(triangle.NormalX, triangle.NormalY, triangle.NormalZ);
glTexCoord2f(triangle.TextureX, triangle.TextureY);
glVertex3f(triangle.LocationX, triangle.LocationY, triangle.LocationZ);


这篇关于从数组中绘制三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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