I'm in a project and my 'responsibility' is to carry the Java code for C ++ that uses OpenGL. I've done almost everything, but it does not work. I think the problem is in this class that I could not do 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 "FloatBuffer" works. I think that if it were in C ++ it would be easier [already programming with OpenGL in C and C ++ and I already used techniques that worked, but this did not = | ]
The code I'm in doubt 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 I "ported" 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();
}
}
If someone can tell me where I am wrong I will be very grateful.