G
G
gibsonen2018-02-14 23:35:46
Java
gibsonen, 2018-02-14 23:35:46

Texture mapping in open gl?

Tell me how to apply the name.TIF texture to my model?
Read the image file and put it in the image buffer:

public static ByteBuffer loadTexture(String path) {
        BufferedImage image = null;
        try {
            InputStream in = new FileInputStream(path);
            image = ImageIO.read(in);
        } catch (IOException ex) {
            throw new RuntimeException("Failed to load a texture file!"
                    + System.lineSeparator() + ex.getMessage());
        }
        if (image != null) {
            AffineTransform transform = AffineTransform.getScaleInstance(1f, -1f);
            transform.translate(0, -image.getHeight());
            AffineTransformOp operation = new AffineTransformOp(transform,
                    AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            image = operation.filter(image, null);
            int width = image.getWidth();
            int height = image.getHeight();
            int[] pixels = new int[width * height];
            image.getRGB(0, 0, width, height, pixels, 0, width);
            ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4);
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int pixel = pixels[y * width + x];
                    buffer.put((byte) ((pixel >> 16) & 0xFF));
                    buffer.put((byte) ((pixel >> 8) & 0xFF));
                    buffer.put((byte) (pixel & 0xFF));
                    buffer.put((byte) ((pixel >> 24) & 0xFF));
                }
            }
            buffer.flip();
            return buffer;
        } else {
            throw new RuntimeException("File extension not supported!"
                    + System.lineSeparator() + "The following file extensions "
                    + "are supported: "
                    + Arrays.toString(ImageIO.getReaderFileSuffixes()));
        }
    }

The class responsible for rendering the model (reduced as much as possible) builds a model from an obj file using triangles:
private final int vaoTriangles;
    private final int shaderProgram;
    private ObjModel objModel;//модель обж файла

    SceneRenderer(ObjModel objModel) throws Exception {
        this.objModel = objModel;
        glEnable(GL_DEPTH_TEST);
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glPointSize(10);

        String fragmentSource = IOUtils.toString(getClass().getResourceAsStream("shader.frag"));
        String vertexSource = IOUtils.toString(getClass().getResourceAsStream("shader.vert"));
        shaderProgram = Util.createShaderProgram(vertexSource, fragmentSource);

        int vertexBuffer = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, Geometry.objFileObject(objModel).rewind(), GL_STATIC_DRAW);

        int normalBuffer = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
        glBufferData(GL_ARRAY_BUFFER, Geometry.getNormals(objModel).rewind(), GL_STATIC_DRAW);


        vaoTriangles = glGenVertexArrays();
        glBindVertexArray(vaoTriangles);

        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
        glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, false, 0, 0);
        glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
        glVertexAttribPointer(normalLocation, 3, GL_FLOAT, false, 0, 0)

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }

    void render() throws Exception {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer

        FloatBuffer vMatrix = BufferUtils.createFloatBuffer(16);
        new Matrix4f()
                .lookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
                        0.0f, 0.0f, 0.0f,
                        0.0f, 1.0f, 0.0f).get(vMatrix);

        FloatBuffer mMatrix = BufferUtils.createFloatBuffer(16);
        new Matrix4f().translate(modelPosition)
                .rotateX(modelRotation.x)
                .rotateY(modelRotation.y)
                .rotateZ(modelRotation.z)
                .get(mMatrix);
        glUseProgram(shaderProgram);
        glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "P"), false, pMatrix);
        glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "V"), false, vMatrix);
        glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "M"), false, mMatrix);
        glUniform3f(glGetUniformLocation(shaderProgram, "light_worldspace"), lightPosition.x, lightPosition.y, lightPosition.z);

        glBindVertexArray(vaoTriangles);

        glEnableVertexAttribArray(vertexLocation);
        glEnableVertexAttribArray(normalLocation);
        glDrawArrays(GL_TRIANGLES, 0, objModel.getIdx().size());
    }
}

Well, the fragment shader, in which the color is filled:
#version 330 core

in vec3 normal_modelspace;
in vec3 vertex_modelspace;

out vec3 color;
uniform vec3 light_worldspace;

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;

  color = vec3(0.0,1.0,0.0) * (cosTheta + ambient);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question