Helps port part of the Java code to C++

Asked

Viewed 112 times

1

I am in a project and my 'responsibility' is to port the Java code to C++ that uses Opengl. I’ve done almost everything, but it doesn’t work. I think the problem is in this class that I could not port very well [I changed the code in java and gave the same problem that happens in the code in C++: Nothing appears]. My difficulty is to understand how this class "Bufferutils" and the "Floatbuffer" works. I believe that if it were in C++ it would be easier [I already program with Opengl in C and C++ and I already used techniques that worked, but this one did not =| ]

The code I’m doubting is this:

import org.lwjgl.opengl.GL11;
import org.lwjgl.BufferUtils;
import java.nio.FloatBuffer;

public class Tesselator
{
    private static final int MAX_VERTICES = 100000;
    private FloatBuffer vertexBuffer;
    private FloatBuffer texCoordBuffer;
    private FloatBuffer colorBuffer;
    private int vertices;
    private float u;
    private float v;
    private float r;
    private float g;
    private float b;
    private boolean hasColor;
    private boolean hasTexture;

    public Tesselator() {
        this.vertexBuffer = BufferUtils.createFloatBuffer(300000);
        this.texCoordBuffer = BufferUtils.createFloatBuffer(200000);
        this.colorBuffer = BufferUtils.createFloatBuffer(300000);
        this.vertices = 0;
        this.hasColor = false;
        this.hasTexture = false;
    }

    public void flush() {
        this.vertexBuffer.flip();
        this.texCoordBuffer.flip();
        this.colorBuffer.flip();
        GL11.glVertexPointer(3, 0, this.vertexBuffer);
        if (this.hasTexture) {
            GL11.glTexCoordPointer(2, 0, this.texCoordBuffer);
        }
        if (this.hasColor) {
            GL11.glColorPointer(3, 0, this.colorBuffer);
        }
        GL11.glEnableClientState(32884);
        if (this.hasTexture) {
            GL11.glEnableClientState(32888);
        }
        if (this.hasColor) {
            GL11.glEnableClientState(32886);
        }
        GL11.glDrawArrays(7, 0, this.vertices);
        GL11.glDisableClientState(32884);
        if (this.hasTexture) {
            GL11.glDisableClientState(32888);
        }
        if (this.hasColor) {
            GL11.glDisableClientState(32886);
        }
        this.clear();
    }

    private void clear() {
        this.vertices = 0;
        this.vertexBuffer.clear();
        this.texCoordBuffer.clear();
        this.colorBuffer.clear();
    }

    public void init() {
        this.clear();
        this.hasColor = false;
        this.hasTexture = false;
    }

    public void tex(final float u, final float v) {
        this.hasTexture = true;
        this.u = u;
        this.v = v;
    }

    public void color(final float r, final float g, final float b) {
        this.hasColor = true;
        this.r = r;
        this.g = g;
        this.b = b;
    }

    public void vertex(final float x, final float y, final float z) {

        this.vertexBuffer.put(this.vertices * 3 + 0, x);
        this.vertexBuffer.put(this.vertices * 3 + 1, y);
        this.vertexBuffer.put(this.vertices * 3 + 2, z);

        if (this.hasTexture) {
            this.texCoordBuffer.put(this.vertices * 2 + 0, this.u).put(this.vertices * 2 + 1, this.v);
        }
        if (this.hasColor) {
            this.colorBuffer.put(this.vertices * 3 + 0, this.r).put(this.vertices * 3 + 1, this.g).put(this.vertices * 3 + 2, this.b);
        }
        ++this.vertices;
        if (this.vertices == MAX_VERTICES) {
            this.flush();
        }
    }
}

And the code that I "carried" was this:

#ifndef TESSELATOR_HPP
#define TESSELATOR_HPP

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <vector>

#define MAX_VERTICES 100000

class Tesselator{
    std::vector<float> vertexBuffer = std::vector<float>();
    std::vector<float> texCoordBuffer = std::vector<float>();
    std::vector<float> colorBuffer = std::vector<float>();

    int vertices = 0;
    float u = 0;
    float v = 0;
    float r,g,b = 0;
    bool hasColor = false;
    bool hasTexture = false;

    public:

        Tesselator();

        void flush();
        void clear();
        void init();
        void tex(float u, float v);
        void color(float r, float g, float b);
        void vertex(float x, float y, float z);
};

#endif /* TESSELATOR_HPP */ 

#include "Tesselator.hpp"

Tesselator::Tesselator(){

    vertexBuffer.reserve(300000);
    texCoordBuffer.reserve(200000);
    colorBuffer.reserve(300000);

    init();
}

void Tesselator::flush(){

    glVertexPointer(3, GL_FLOAT, 0, &vertexBuffer[0]);

    if(hasTexture){
        glTexCoordPointer(2, GL_FLOAT, 0, &texCoordBuffer[0]);
    }
    if (hasColor){
        glColorPointer(3, GL_FLOAT, 0, &colorBuffer[0]);
    }
    glEnableClientState(GL_VERTEX_ARRAY);

    if(hasTexture)
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    if(hasColor)
        glEnableClientState(GL_COLOR_ARRAY);

    glDrawArrays(GL_QUADS, 0, vertices);

    glDisableClientState(GL_VERTEX_ARRAY);

    if(hasTexture)
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    if(hasColor)
        glDisableClientState(GL_COLOR_ARRAY);

    clear();
}

void Tesselator::clear(){
    vertices = 0;

    //~ vertexBuffer.clear();
    vertexBuffer.resize(vertexBuffer.capacity());
    //~ texCoordBuffer.clear();
    texCoordBuffer.resize(texCoordBuffer.capacity());
    //~ colorBuffer.clear();
    colorBuffer.resize(colorBuffer.capacity());
}

void Tesselator::init(){
    clear();
    hasColor = false;
    hasTexture = false;
}

void Tesselator::tex(float u, float v){
    hasTexture = true;
    this->u = u;
    this->v = v;
}

void Tesselator::color(float r, float g, float b){
    hasColor = true;
    this->r = r;
    this->g = g;
    this->b = b;
}

void Tesselator::vertex(float x, float y, float z){
    vertexBuffer[vertices * 3 + 0] =  x;
    vertexBuffer[vertices * 3 + 1] =  y;
    vertexBuffer[vertices * 3 + 2] =  z;

    vertices += 1;

    if(vertices == MAX_VERTICES){
        flush();
    }
}

In case anyone can tell me where I’m going wrong I’ll be very grateful.

  • I don’t understand. You’re carrying a bug code?

  • No, Java code works great. But in C++ code it doesn’t work =. Have any idea what it might be?

  • No, I’m sorry. I was just trying to clarify.

  • I wonder if someone could give me a hand?

  • Wait a while. There is still time for more people to appear. A lot of people only enter during the night.

  • Okay, thank you very much.

  • No one... this is so wrong??

  • 1

    From what I understand you are also trying to port the graphical part of the application that was implemented in Java to C++/Opengl? In this case you can port the logic of the program but when the graphical part is better to reimplement from scratch in C++ using the version of the program in Java only as inspiration to make an equal interface.

  • @Rodrigosidneypereira said so himself "porting the Java code to C++"... His problem is that there’s a part that he didn’t understand about Java code (the "Bufferutils" and the "Floatbuffer"), if he understands this he’ll probably be able to carry, the question may be a little poorly explained in the code and title part, but I think I can understand what he needs ;)

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.