Hello Guest

Recent Posts

Pages: 1 ... 8 9 [10]
91
OpenGL / Re: How to select polygons that fall into a frustum?
« Last post by KaiHH on April 14, 2018, 18:15:38 »
But is there a function in Lwigl that finds A B C D?

Are you kidding? You just wrote that function in GLSL. I quote your code from the other thread:
Code: [Select]
float distancePointPlane(vec3 point,mat3 matr) {
        float v1Y0Y = matr[1][1] - matr[0][1];
        float v2Z0Z = matr[2][2] - matr[0][2];
        float v2Y0Y = matr[2][1] - matr[0][1];
        float v1Z0Z = matr[1][2] - matr[0][2];
        float v2X0X = matr[2][0] - matr[0][0];
        float v1X0X = matr[1][0] - matr[0][0];
        float a = v1Y0Y * v2Z0Z - v2Y0Y * v1Z0Z;
        float b = v1Z0Z * v2X0X - v2Z0Z * v1X0X;
        float c = v1X0X * v2Y0Y - v2X0X * v1Y0Y;
        float d = -(a * matr[0][0] + b * matr[0][1] + c * matr[0][2]);
        float s=0.0;
        return distancePointPlane(a, point[1], point[2], a, b, c, d);
    }
It should be very very easy to convert that to Java.
92
OpenGL / Re: How to select polygons that fall into a frustum?
« Last post by gibson on April 14, 2018, 18:13:40 »
Many thanks, like would understand!!! But is there a function in Lwigl that finds A B C D?

I am assuming that you extracted the six frustum planes from an existing view-projection matrix.
If that is the case and if your question was how to color all the fragments of the rendered model that fall inside of that frustum, then just do this:
1. transform the model-space vertices of the model by the model matrix and the camera's view-projection matrix as you would normally do
2. also transform the model-space vertices by the model matrix and the view-projection matrix describing your additional frustum
3. output the homogeneous vector from that 2. step as interpolated varying/out variable from your vertex shader to your fragment shader
4. in your fragment shader, color the fragment when their x, y and z coordinates all are within -w to w. This is basically what OpenGL does with your camera-transformed vertex positions written to gl_Position, just that it performs clipping based on that.

In the case where you really only have the six planes, simply only transform the model-space vertices only by the model matrix in step 2 and perform a point/plane distance test with all the six faces and output those six distances to the fragment shader as interpolated float values. Then, inside the fragment shader, test whether any of those distances is negative, in which case that fragment got effectively clipped by that plane.
93
OpenGL / Re: Problem in the shader when rendering?
« Last post by KaiHH on April 14, 2018, 17:59:19 »
Your solution would become much more elegant, if you did not submit three points on the plane for each of those 6 planes, but instead used 6 vec4's each holding the factors a, b, c and d of the plane equation `a*x + b*y + c*z + d = 0`, making your `distancePointPlane(vec3 point,mat3 matr)` (which computes the plane equation from three points on the plane) completely superfluous.
And as I said in your previous post (which you should just have continued answering to), you can perfectly compute the distances IN THE VERTEX SHADER and only perform the `distance >= 0.0` test ON THE INTERPOLATED VARYING `distance` in the fragment shader. This will be correct, since OpenGL performs perspective-correct interpolation of that varying based on your camera projection. This will save you A LOT of computations in the fragment shader.

All of this was already mentioned in my answer to your initial post. Please read it.
94
OpenGL / Problem in the shader when rendering?
« Last post by gibson on April 14, 2018, 17:38:51 »
I moved on. I figured out how to determine if the point is inside the frustum or not. I did it this way: I just calculated the distance to each of the 6 planes, if it turns out to be negative, then the point does not lie inside the frustum. Implemented this all in a fragmented shader.

Below is the implementation of this shader. I transmit 6 square matrices of size 3 A B C D E F - each of them contains 3 vertices of the plane. In the shader, a method was written that finds the distance between the point and the plane of the
Code: [Select]
distancePointPlane. When the program is started, it independently selects a polygon or does not draw with color. With the transfer of the coordinates of vertices, problems should not exist. The problem at the level of computation in the shader. Help.

Code: [Select]
version 330 core
in vec3 normal_modelspace;
in vec3 vertex_modelspace;
in vec2 TexCoord;
in vec4 vertexColor;
in vec3 coord;
out vec4 color;


float distancePointPlane(float pointX, float pointY, float pointZ, float a, float b, float c, float d) {
        float denom = sqrt(a * a + b * b + c * c);
        return float((a * pointX + b * pointY + c * pointZ + d) / denom);

    }

float distancePointPlane(vec3 point,mat3 matr) {
        float v1Y0Y = matr[1][1] - matr[0][1];
        float v2Z0Z = matr[2][2] - matr[0][2];
        float v2Y0Y = matr[2][1] - matr[0][1];
        float v1Z0Z = matr[1][2] - matr[0][2];
        float v2X0X = matr[2][0] - matr[0][0];
        float v1X0X = matr[1][0] - matr[0][0];
        float a = v1Y0Y * v2Z0Z - v2Y0Y * v1Z0Z;
        float b = v1Z0Z * v2X0X - v2Z0Z * v1X0X;
        float c = v1X0X * v2Y0Y - v2X0X * v1Y0Y;
        float d = -(a * matr[0][0] + b * matr[0][1] + c * matr[0][2]);
        float s=0.0;
        return distancePointPlane(a, point[1], point[2], a, b, c, d);
    }
uniform mat3 A;
uniform mat3 B;
uniform mat3 C;
uniform mat3 D;
uniform mat3 E;
uniform mat3 F;

uniform vec3 light_worldspace;

uniform sampler2D ourTexture;

void main() {
  vec3 n = normalize(normal_modelspace);
  vec3 l = normalize(light_worldspace - vertex_modelspace);
  float cosTheta = clamp( dot( n, l), 0,1 );
  float ambient = 0.05;

  float aF= distancePointPlane(vertex_modelspace,A);//нахожу расстояние от вершины модели до плоскости а
  float bF= distancePointPlane(vertex_modelspace,B);
  float cF= distancePointPlane(vertex_modelspace,C);
  float dF= distancePointPlane(vertex_modelspace,D);
  float eF= distancePointPlane(vertex_modelspace,E);
  float fF= distancePointPlane(vertex_modelspace,F);
  if(aF>=0.0 && bF>=0.0 && cF>=0.0 && dF>=0.0 && eF>=0.0 && fF>=0.0){
   color = vertexColor;
  }else{
    color = texture(ourTexture, TexCoord)*(cosTheta + ambient);
  }
}

95
OpenGL / Re: How to select polygons that fall into a frustum?
« Last post by KaiHH on April 13, 2018, 21:29:48 »
I am assuming that you extracted the six frustum planes from an existing view-projection matrix.
If that is the case and if your question was how to color all the fragments of the rendered model that fall inside of that frustum, then just do this:
1. transform the model-space vertices of the model by the model matrix and the camera's view-projection matrix as you would normally do
2. also transform the model-space vertices by the model matrix and the view-projection matrix describing your additional frustum
3. output the homogeneous vector from that 2. step as interpolated varying/out variable from your vertex shader to your fragment shader
4. in your fragment shader, color the fragment when their x, y and z coordinates all are within -w to w. This is basically what OpenGL does with your camera-transformed vertex positions written to gl_Position, just that it performs clipping based on that.

In the case where you really only have the six planes, simply only transform the model-space vertices only by the model matrix in step 2 and perform a point/plane distance test with all the six faces and output those six distances to the fragment shader as interpolated float values. Then, inside the fragment shader, test whether any of those distances is negative, in which case that fragment got effectively clipped by that plane.
96
OpenGL / [Solved] How can I convert pixel data to bytebuffer?
« Last post by Harang on April 13, 2018, 18:09:28 »
I parsed a sprite file and I want to convert the pixel data of it to bytebuffer for creating a texture.

ByteBuffer buffer = ByteBuffer.allocate(width * height * 4);
      for(int b : data) {
         buffer.put((byte) palette[b * 3]); // R
         buffer.put((byte) palette[b * 3 + 1]); // G
         buffer.put((byte) palette[b * 3 + 2]); // B
         if(b == 254) { // If b equals 0xFE     // A
            buffer.put((byte) 0);
         } else {
            buffer.put((byte) 255);
         }
      }
      buffer.flip();

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

But I can't see anything except the black screen.
How can I convert the RGBA pixel data to a texture?

Thank you.

----------------------------------------------------------------------------------------------------------------------------------------------------------


int[] pixels = new int[f_width * f_height * 4];
      int p = 0;
      for(int b : data) {
         pixels[p++] = palette[b * 3];
         pixels[p++] = palette[b * 3 + 1];
         pixels[p++] = palette[b * 3 + 2];
         if(b == 254) {
            pixels[p++] = 0;
         } else {
            pixels[p++] = 255;
         }
      }

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, f_width, f_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);


=> The result was the same.

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Finally, I solved the problem.

int[] pixels = new int[f_width * f_height];
      int p = 0;
      for(int b : data) {
         if(b == 254) {
            pixels[p++] = palette[b * 3] | palette[b * 3 + 1] << 8 | palette[b * 3 + 2] << 16 | 0 << 24; // ABGR
         } else {
            pixels[p++] = palette[b * 3] | palette[b * 3 + 1] << 8 | palette[b * 3 + 2] << 16 | 255 << 24;
         }
      }

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, f_width, f_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
 
97
OpenGL / How to select polygons that fall into a frustum?
« Last post by gibson on April 13, 2018, 17:23:13 »
I made selective rays (6 planes were obtained). It is now necessary to color the triangles that are inside this truncated pyramid. How do you advise it? Tell me not the geometry, but how it is done programmatically in the shader. Given: the matrix P, the matrix of the view, the matrix the matrix of the model M, the coordinates of the vertices of the planes (their 8 is origin and the dir (used the function unproject)), the coordinates of the vertices of the model and its normals I got.
98
Use Pixel Buffer Objects, create two of them for double buffering. You now only need one texture and can stream video using opengl. This is the method I use.

The concept is mentioned here : http://www.songho.ca/opengl/gl_pbo.html
99
I am developing and video editing software... thats why I need to update the texture.
100
why when you unbind you use
Code: [Select]
glDisable(GL_TEXTURE_2D); you disabled the Textures not unbind it you must call
Code: [Select]
glBindTexture(GL_TEXTURE_2D, 0);and sorry for this question but What type of project you do that require you to reload and read new Image every frame ?!! that's performance killing.
Pages: 1 ... 8 9 [10]