Jump to content
  • Advertisement

Recommended Posts

Not long ago, I create a nice OBJ loader that loads 3D Studio Max files. Only problem is, is that although it works and works great, I wasn't using Vertex Buffers. Now that I applied Vertex Buffers, it seems to only use the first color of the texture and spread it all across the poiygon. I examined my code over and over again, and the Vertex Buffer code is correct. But when I comment out all of my vertex buffer code, it works as intended. I practically given up on fixing it on my own, so hopefully you guys will be able to figure out what is wrong.

    public static final int BYTES_PER_FLOAT = 4;
    public static final int POSITION_COMPONENT_COUNT_3D = 4;
    public static final int COLOR_COMPONENT_COUNT = 4;
    public static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    public static final int NORMAL_COMPONENT_COUNT = 3;

    public static final int POSITION_COMPONENT_STRIDE_2D = POSITION_COMPONENT_COUNT_2D * BYTES_PER_FLOAT;
    public static final int POSITION_COMPONENT_STRIDE_3D = POSITION_COMPONENT_COUNT_3D * BYTES_PER_FLOAT;
    public static final int COLOR_COMPONENT_STRIDE = COLOR_COMPONENT_COUNT * BYTES_PER_FLOAT;
    public static final int TEXTURE_COORDINATE_COMPONENT_STRIDE = TEXTURE_COORDINATES_COMPONENT_COUNT * BYTES_PER_FLOAT;
    public static final int NORMAL_COMPONENT_STRIDE = NORMAL_COMPONENT_COUNT * BYTES_PER_FLOAT;

    int loadFile()
    {
        ArrayList<Vertex3D> tempVertexArrayList = new ArrayList<Vertex3D>();
        ArrayList<TextureCoord2D> tempTextureCoordArrayList = new ArrayList<TextureCoord2D>();
        ArrayList<Vector3D> tempNormalArrayList = new ArrayList<Vector3D>();
        ArrayList<Face3D> tempFaceArrayList = new ArrayList<Face3D>();
        StringBuilder body = new StringBuilder();

        try {
            InputStream inputStream = context.getResources().openRawResource(resourceID);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String nextLine;
            String subString;
            String[] stringArray;
            String[] stringArray2;
            int[] indexNumberList = new int[3];
            int[] textureCoordNumberList = new int[3];
            int[] normalNumberList = new int[3];
            int i = 0;
            int j = 0;
            int k = 0;
            try {
                while ((nextLine = bufferedReader.readLine()) != null) {
                    if (nextLine.startsWith("v ")) {
                        subString = nextLine.substring(1).trim();
                        stringArray = subString.split(" ");
                        try {

                            tempVertexArrayList.add(new Vertex3D(Float.parseFloat(stringArray[0]),
                                    Float.parseFloat(stringArray[1]),
                                    Float.parseFloat(stringArray[2]), 1f));
                        }
                        catch(NumberFormatException e){
                            Log.d(TAG, "Error: Invalid number format in loading vertex list");
                            return 0;
                        }
                        String x = String.valueOf(tempVertexArrayList.get(i).x);
                        String y = String.valueOf(tempVertexArrayList.get(i).y);
                        String z = String.valueOf(tempVertexArrayList.get(i).z);
                        //Log.d(TAG, "vertex " + String.valueOf(i) + ": " + x + ", " + y + ", " + z);
                        i++;
                    }

                    if (nextLine.startsWith("vn ")) {
                        subString = nextLine.substring(2).trim();
                        stringArray = subString.split(" ");
                        try {
                            if(reverseNormals){
                                tempNormalArrayList.add(new Vector3D(-Float.parseFloat(stringArray[0]),
                                        -Float.parseFloat(stringArray[1]),
                                        -Float.parseFloat(stringArray[2])));
                            }
                            else{
                                tempNormalArrayList.add(new Vector3D(Float.parseFloat(stringArray[0]),
                                        Float.parseFloat(stringArray[1]),
                                        Float.parseFloat(stringArray[2])));
                            }

                        }
                        catch(NumberFormatException e){
                            Log.d(TAG, "Error: Invalid number format in loading normal list");
                            return 0;
                        }
                        String nx = String.valueOf(tempNormalArrayList.get(j).x);
                        String ny = String.valueOf(tempNormalArrayList.get(j).y);
                        String nz = String.valueOf(tempNormalArrayList.get(j).z);
                        //Log.d(TAG, "normal " + String.valueOf(j) + ": " + nx + ", " + ny + ", " + nz);
                        j++;
                    }

                    if (nextLine.startsWith("vt ")) {
                        subString = nextLine.substring(2).trim();
                        stringArray = subString.split(" ");
                        try {
                            tempTextureCoordArrayList.add(new TextureCoord2D(Float.parseFloat(stringArray[0]),
                                    Float.parseFloat(stringArray[1])));
                        }
                        catch(NumberFormatException e){
                            Log.d(TAG, "Error: Invalid number format in loading texture coordinate list");
                            return 0;
                        }
                        String tu = String.valueOf(tempTextureCoordArrayList.get(k).tu);
                        String tv = String.valueOf(tempTextureCoordArrayList.get(k).tv);
                        //Log.d(TAG, "texture coord " + String.valueOf(k) + ": " + tu + ", " + tv);
                        k++;
                    }

                    if (nextLine.startsWith("f ")) {
                        subString = nextLine.substring(1).trim();
                        stringArray = subString.split(" ");
                        for (int index = 0; index <= 2; index++) {
                            stringArray2 = stringArray[index].split("/");
                            try {
                                indexNumberList[index] = Integer.parseInt(stringArray2[0]) - 1;
                                if(indexNumberList[index] < 0){
                                    Log.d(TAG, "Error: indexNumberList[] is less than zero");
                                    return 0;
                                }
                            }
                            catch(NumberFormatException e){
                                Log.d(TAG, "Error: Invalid number format in loading indexNumberList[]");
                                return 0;
                            }

                            try{
                                textureCoordNumberList[index] = Integer.parseInt(stringArray2[1]) - 1;
                                if(textureCoordNumberList[index] < 0){
                                    Log.d(TAG, "Error: textureCoordNumberList[] is less than zero");
                                    return 0;
                                }
                            }
                            catch(NumberFormatException e){
                                Log.d(TAG, "Error: Invalid number format in loading textureCoordNumberList[]");
                                return 0;
                            }

                            try{
                                normalNumberList[index] = Integer.parseInt(stringArray2[2]) - 1;
                                if(normalNumberList[index] < 0){
                                    Log.d(TAG, "Error: normalNumberList[] is less than zero");
                                    return 0;
                                }
                            }
                            catch(NumberFormatException e){
                                Log.d(TAG, "Error: Invalid number format in loading normalNumberList[]");
                                return 0;
                            }
                        }
                        tempFaceArrayList.add(new Face3D(indexNumberList[0], textureCoordNumberList[0], normalNumberList[0],
                                indexNumberList[1], textureCoordNumberList[1], normalNumberList[1],
                                indexNumberList[2], textureCoordNumberList[2], normalNumberList[2]));
                    }

                    body.append(nextLine);
                    body.append('\n');
                }

                //Now that everything has successfully loaded, you can now populate the public variables.
                if(tempVertexArrayList != null && tempVertexArrayList.size() != 0)
                    vertexArrayList.addAll(tempVertexArrayList);

                if(tempTextureCoordArrayList != null && tempTextureCoordArrayList.size() != 0)
                    textureCoordArrayList.addAll(tempTextureCoordArrayList);

                if(tempNormalArrayList != null && tempNormalArrayList.size() != 0)
                    normalArrayList.addAll(tempNormalArrayList);

                if(tempFaceArrayList != null && tempFaceArrayList.size() != 0)
                    faceArrayList.addAll(tempFaceArrayList);

                vertexList = new float[faceArrayList.size() * POSITION_COMPONENT_COUNT_3D * NUMBER_OF_SIDES_PER_FACE];
                indexList = new short[faceArrayList.size() * NUMBER_OF_SIDES_PER_FACE];
                colorList = new float[faceArrayList.size() * COLOR_COMPONENT_COUNT * NUMBER_OF_SIDES_PER_FACE];
                textureCoordList = new float[faceArrayList.size() * TEXTURE_COORDINATES_COMPONENT_COUNT * NUMBER_OF_SIDES_PER_FACE];
                normalList = new float[faceArrayList.size() * NORMAL_COMPONENT_COUNT * NUMBER_OF_SIDES_PER_FACE];

                int nextFace = 0;
                int step = POSITION_COMPONENT_COUNT_3D * NUMBER_OF_SIDES_PER_FACE;

                for (int currentVertex = 0; currentVertex < vertexList.length; currentVertex += step){
                    vertexList[currentVertex + 0] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(0)).x;
                    vertexList[currentVertex + 1] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(0)).y;
                    vertexList[currentVertex + 2] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(0)).z;
                    vertexList[currentVertex + 3] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(0)).w;

                    vertexList[currentVertex + 4] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(1)).x;
                    vertexList[currentVertex + 5] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(1)).y;
                    vertexList[currentVertex + 6] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(1)).z;
                    vertexList[currentVertex + 7] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(1)).w;

                    vertexList[currentVertex + 8] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(2)).x;
                    vertexList[currentVertex + 9] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(2)).y;
                    vertexList[currentVertex + 10] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(2)).z;
                    vertexList[currentVertex + 11] = vertexArrayList.get(faceArrayList.get(nextFace).indexNumberList.get(2)).w;

                    nextFace++;
                }

                numberOfVertices = vertexList.length / POSITION_COMPONENT_COUNT_3D;

                nextFace = 0;

                for (int currentIndex = 0; currentIndex < indexList.length; currentIndex += NUMBER_OF_SIDES_PER_FACE){
                    indexList[currentIndex + 0] = faceArrayList.get(nextFace).indexNumberList.get(0).shortValue();
                    indexList[currentIndex + 1] = faceArrayList.get(nextFace).indexNumberList.get(1).shortValue();
                    indexList[currentIndex + 2] = faceArrayList.get(nextFace).indexNumberList.get(2).shortValue();
                }

                step = COLOR_COMPONENT_COUNT * NUMBER_OF_SIDES_PER_FACE;

                for (int currentVertex = 0; currentVertex < colorList.length; currentVertex += step){
                    colorList[currentVertex + 0] = red;
                    colorList[currentVertex + 1] = green;
                    colorList[currentVertex + 2] = blue;
                    colorList[currentVertex + 3] = alpha;

                    colorList[currentVertex + 4] = red;
                    colorList[currentVertex + 5] = green;
                    colorList[currentVertex + 6] = blue;
                    colorList[currentVertex + 7] = alpha;

                    colorList[currentVertex + 8] = red;
                    colorList[currentVertex + 9] = green;
                    colorList[currentVertex + 10] = blue;
                    colorList[currentVertex + 11] = alpha;
                }

                nextFace = 0;
                step = TEXTURE_COORDINATES_COMPONENT_COUNT * NUMBER_OF_SIDES_PER_FACE;

                for (int currentVertex = 0; currentVertex < textureCoordList.length; currentVertex += step){
                    textureCoordList[currentVertex + 0] = textureCoordArrayList.get(faceArrayList.get(nextFace).textureCoordNumberList.get(0)).tu * mult;
                    textureCoordList[currentVertex + 1] = textureCoordArrayList.get(faceArrayList.get(nextFace).textureCoordNumberList.get(0)).tv * mult;

                    textureCoordList[currentVertex + 2] = textureCoordArrayList.get(faceArrayList.get(nextFace).textureCoordNumberList.get(1)).tu * mult;
                    textureCoordList[currentVertex + 3] = textureCoordArrayList.get(faceArrayList.get(nextFace).textureCoordNumberList.get(1)).tv * mult;

                    textureCoordList[currentVertex + 4] = textureCoordArrayList.get(faceArrayList.get(nextFace).textureCoordNumberList.get(2)).tu * mult;
                    textureCoordList[currentVertex + 5] = textureCoordArrayList.get(faceArrayList.get(nextFace).textureCoordNumberList.get(2)).tv * mult;

                    nextFace++;
                }

                nextFace = 0;
                step = NORMAL_COMPONENT_COUNT * NUMBER_OF_SIDES_PER_FACE;

                for (int currentVertex = 0; currentVertex < normalList.length; currentVertex += step){
                    normalList[currentVertex + 0] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(0)).x;
                    normalList[currentVertex + 1] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(0)).y;
                    normalList[currentVertex + 2] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(0)).z;

                    normalList[currentVertex + 3] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(1)).x;
                    normalList[currentVertex + 4] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(1)).y;
                    normalList[currentVertex + 5] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(1)).z;

                    normalList[currentVertex + 6] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(2)).x;
                    normalList[currentVertex + 7] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(2)).y;
                    normalList[currentVertex + 8] = normalArrayList.get(faceArrayList.get(nextFace).normalNumberList.get(2)).z;

                    nextFace++;
                }

                vertexBuffer = ByteBuffer.allocateDirect(vertexList.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
                indexBuffer = ByteBuffer.allocateDirect(indexList.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asShortBuffer();
                colorBuffer = ByteBuffer.allocateDirect(colorList.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
                textureCoordBuffer = ByteBuffer.allocateDirect(textureCoordList.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
                normalBuffer = ByteBuffer.allocateDirect(normalList.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();

                vertexBuffer.put(vertexList).position(0);
                indexBuffer.put(indexList).position(0);
                colorBuffer.put(colorList).position(0);
                textureCoordBuffer.put(textureCoordList).position(0);
                normalBuffer.put(normalList).position(0);

                createVertexBuffer();

                uMVPMatrixHandle = glGetUniformLocation(program, U_MVPMATRIX);
                uMVMatrixHandle = glGetUniformLocation(program, U_MVMATRIX);
                uTextureUnitHandle = glGetUniformLocation(program, U_TEXTURE_UNIT);

                aPositionHandle = glGetAttribLocation(program, A_POSITION);
                aColorHandle = glGetAttribLocation(program, A_COLOR);
                aTextureCoordinateHandle = glGetAttribLocation(program, A_TEXTURE_COORDINATES);
                aNormalHandle = glGetAttribLocation(program, A_NORMAL);

                glEnableVertexAttribArray(aPositionHandle);
                glEnableVertexAttribArray(aColorHandle);
                glEnableVertexAttribArray(aTextureCoordinateHandle);
                glEnableVertexAttribArray(aNormalHandle);

                glActiveTexture(GL_TEXTURE0);
                glUniform1i(uTextureUnitHandle, 0);
            }
            catch(IOException e){

            }
        }
        catch (Resources.NotFoundException nfe){
            throw new RuntimeException("Resource not found: " + resourceID, nfe);
        }

        return 1;
    }

    public void draw(){
        glEnable(GL_DEPTH_TEST);
        bindData();
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject[0]);
        glDrawArrays(GL_TRIANGLES, 0, faceArrayList.size() * NUMBER_OF_SIDES_PER_FACE);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    public void bindData(){
        int offset = 0;
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject[0]);
        glVertexAttribPointer(aPositionHandle,          POSITION_COMPONENT_COUNT_3D,         GL_FLOAT, false, POSITION_COMPONENT_STRIDE_3D,        offset);
        offset += POSITION_COMPONENT_COUNT_3D;
        glVertexAttribPointer(aColorHandle,             COLOR_COMPONENT_COUNT,               GL_FLOAT, false, COLOR_COMPONENT_STRIDE,              numberOfVertices * offset * BYTES_PER_FLOAT);
        offset += COLOR_COMPONENT_COUNT;
        glVertexAttribPointer(aTextureCoordinateHandle, TEXTURE_COORDINATES_COMPONENT_COUNT, GL_FLOAT, false, TEXTURE_COORDINATE_COMPONENT_STRIDE, numberOfVertices * offset * BYTES_PER_FLOAT);
        offset += TEXTURE_COORDINATES_COMPONENT_COUNT;
        glVertexAttribPointer(aNormalHandle,            NORMAL_COMPONENT_COUNT,              GL_FLOAT, false, NORMAL_COMPONENT_STRIDE,             numberOfVertices * offset * BYTES_PER_FLOAT);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        /////////////////////////////////////////////////////
/*
        vertexBuffer.position(0);
        glVertexAttribPointer(aPositionHandle, POSITION_COMPONENT_COUNT_3D, GL_FLOAT, false, POSITION_COMPONENT_STRIDE_3D, vertexBuffer);

        colorBuffer.position(0);
        glVertexAttribPointer(aColorHandle, COLOR_COMPONENT_COUNT, GL_FLOAT, false, COLOR_COMPONENT_STRIDE, colorBuffer);

        textureCoordBuffer.position(0);
        glVertexAttribPointer(aTextureCoordinateHandle, TEXTURE_COORDINATES_COMPONENT_COUNT, GL_FLOAT, false, TEXTURE_COORDINATE_COMPONENT_STRIDE, textureCoordBuffer);

        normalBuffer.position(0);
        glVertexAttribPointer(aNormalHandle, NORMAL_COMPONENT_COUNT, GL_FLOAT, false, NORMAL_COMPONENT_STRIDE, normalBuffer);
*/
    }

    public void createVertexBuffer(){

        glGenBuffers(1, vertexBufferObject, 0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject[0]);
        glBufferData(GL_ARRAY_BUFFER,numberOfVertices * (POSITION_COMPONENT_COUNT_3D + COLOR_COMPONENT_COUNT + TEXTURE_COORDINATES_COMPONENT_COUNT + NORMAL_COMPONENT_COUNT) * BYTES_PER_FLOAT, null, GL_STATIC_DRAW);
        int offset = 0;
        glBufferSubData(GL_ARRAY_BUFFER, offset,
                                         numberOfVertices * POSITION_COMPONENT_COUNT_3D * BYTES_PER_FLOAT, vertexBuffer);
        offset += POSITION_COMPONENT_COUNT_3D;
        glBufferSubData(GL_ARRAY_BUFFER, numberOfVertices * offset * BYTES_PER_FLOAT,
                                         numberOfVertices * COLOR_COMPONENT_COUNT * BYTES_PER_FLOAT, colorBuffer);

        offset += COLOR_COMPONENT_COUNT;
        glBufferSubData(GL_ARRAY_BUFFER, numberOfVertices * offset * BYTES_PER_FLOAT,
                                         numberOfVertices * TEXTURE_COORDINATES_COMPONENT_COUNT * BYTES_PER_FLOAT, textureCoordBuffer);

        offset += TEXTURE_COORDINATES_COMPONENT_COUNT;
        glBufferSubData(GL_ARRAY_BUFFER, numberOfVertices * offset * BYTES_PER_FLOAT,
                                         numberOfVertices * NORMAL_COMPONENT_COUNT * BYTES_PER_FLOAT, normalBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

 

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

Another observation, if you comment out the Normal code (which was working btw), texturemapping works as intended too. Its only when I toss in normals, that texturemapping only uses one color. And the entire thing works when Vertex Buffers are removed. Weird. Not sure if this is just the emulator doing this, but I'm gonna test it on my real phone to see if it works.

    public void bindData(){
        int offset = 0;
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject[0]);
        glVertexAttribPointer(aPositionHandle,          POSITION_COMPONENT_COUNT_3D,         GL_FLOAT, false, POSITION_COMPONENT_STRIDE_3D,        offset);
        offset += POSITION_COMPONENT_COUNT_3D;
        glVertexAttribPointer(aColorHandle,             COLOR_COMPONENT_COUNT,               GL_FLOAT, false, COLOR_COMPONENT_STRIDE,              numberOfVertices * offset * BYTES_PER_FLOAT);
        offset += COLOR_COMPONENT_COUNT;
        glVertexAttribPointer(aTextureCoordinateHandle, TEXTURE_COORDINATES_COMPONENT_COUNT, GL_FLOAT, false, TEXTURE_COORDINATE_COMPONENT_STRIDE, numberOfVertices * offset * BYTES_PER_FLOAT);
        //offset += TEXTURE_COORDINATES_COMPONENT_COUNT;
        //glVertexAttribPointer(aNormalHandle,            NORMAL_COMPONENT_COUNT,              GL_FLOAT, false, NORMAL_COMPONENT_STRIDE,             numberOfVertices * offset * BYTES_PER_FLOAT);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        /////////////////////////////////////////////////////
/*
        vertexBuffer.position(0);
        glVertexAttribPointer(aPositionHandle, POSITION_COMPONENT_COUNT_3D, GL_FLOAT, false, POSITION_COMPONENT_STRIDE_3D, vertexBuffer);

        colorBuffer.position(0);
        glVertexAttribPointer(aColorHandle, COLOR_COMPONENT_COUNT, GL_FLOAT, false, COLOR_COMPONENT_STRIDE, colorBuffer);

        textureCoordBuffer.position(0);
        glVertexAttribPointer(aTextureCoordinateHandle, TEXTURE_COORDINATES_COMPONENT_COUNT, GL_FLOAT, false, TEXTURE_COORDINATE_COMPONENT_STRIDE, textureCoordBuffer);

        normalBuffer.position(0);
        glVertexAttribPointer(aNormalHandle, NORMAL_COMPONENT_COUNT, GL_FLOAT, false, NORMAL_COMPONENT_STRIDE, normalBuffer);
*/
    }

    public void createVertexBuffer(){
        glGenBuffers(1, vertexBufferObject, 0);
        int vertexLine = POSITION_COMPONENT_COUNT_3D + COLOR_COMPONENT_COUNT + TEXTURE_COORDINATES_COMPONENT_COUNT;
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject[0]);
        glBufferData(GL_ARRAY_BUFFER,numberOfVertices * vertexLine * BYTES_PER_FLOAT, null, GL_STATIC_DRAW);
        int offset = 0;
        glBufferSubData(GL_ARRAY_BUFFER, offset,
                                         numberOfVertices * POSITION_COMPONENT_COUNT_3D * BYTES_PER_FLOAT, vertexBuffer);
        offset += POSITION_COMPONENT_COUNT_3D;
        glBufferSubData(GL_ARRAY_BUFFER, numberOfVertices * offset * BYTES_PER_FLOAT,
                                         numberOfVertices * COLOR_COMPONENT_COUNT * BYTES_PER_FLOAT, colorBuffer);

        offset += COLOR_COMPONENT_COUNT;
        glBufferSubData(GL_ARRAY_BUFFER, numberOfVertices * offset * BYTES_PER_FLOAT,
                                         numberOfVertices * TEXTURE_COORDINATES_COMPONENT_COUNT * BYTES_PER_FLOAT, textureCoordBuffer);

        //offset += TEXTURE_COORDINATES_COMPONENT_COUNT;
        //glBufferSubData(GL_ARRAY_BUFFER, numberOfVertices * offset * BYTES_PER_FLOAT,
        //                                 numberOfVertices * NORMAL_COMPONENT_COUNT * BYTES_PER_FLOAT, normalBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
}

 

[EDIT] OMG it was the emulator. But I don't understand why? Works great on the phone and tablet. I set the ram up pretty high, like 4 Gigs

Edited by Psychopathetica

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By bojanzarnoski@gmx.de
      Hello,
      I want to get into coding again by programming a 2D platformer to get started, but i don't know if i should use Java or C# with the unity engine.
      I am pretty fit with Java, but with c# i have to start from scratch. What do you recommend and why?
    • By macmanmatty
      I'm still working on large RPG top down   game  that has  very large maps 500 x500 32 x 32 px  tiles  and there 5 of  these "land maps" each with three large buildings  of a   "200*200" tiled  map some with more than one map for each floor.  and shops taverns/ blacksmiths/ stables  / houses  with 20 x 20 or so tile maps.  Since you can have / control  more than one unit to optimize  the game  I was thinking  each map  ( land map, shop, tavern,  budiling ect.)  should have it's own game  loop. There would be a button created for each map you have units on to switch to that map   and when you switch them  one loop is paused  and the other one is activated? Is this a good idea?
    • By DevAndroid
      Hello everyone,
      I'm trying to display a 2D texture to screen but the rendering isn't working correctly.
      First of all I did follow this tutorial to be able to render a Text to screen (I adapted it to render with OpenGL ES 2.0) : https://learnopengl.com/code_viewer.php?code=in-practice/text_rendering
      So here is the shader I'm using :
      const char gVertexShader[] = "#version 320 es\n" "layout (location = 0) in vec4 vertex;\n" "out vec2 TexCoords;\n" "uniform mat4 projection;\n" "void main() {\n" " gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n" " TexCoords = vertex.zw;\n" "}\n"; const char gFragmentShader[] = "#version 320 es\n" "precision mediump float;\n" "in vec2 TexCoords;\n" "out vec4 color;\n" "uniform sampler2D text;\n" "uniform vec3 textColor;\n" "void main() {\n" " vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);\n" " color = vec4(textColor, 1.0) * sampled;\n" "}\n"; The render text works very well so I would like to keep those Shaders program to render a texture loaded from PNG.
      For that I'm using libPNG to load the PNG to a texture, here is my code :
      GLuint Cluster::loadPngFromPath(const char *file_name, int *width, int *height) { png_byte header[8]; FILE *fp = fopen(file_name, "rb"); if (fp == 0) { return 0; } fread(header, 1, 8, fp); if (png_sig_cmp(header, 0, 8)) { fclose(fp); return 0; } png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { fclose(fp); return 0; } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); fclose(fp); return 0; } png_infop end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); fclose(fp); return 0; } if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fp); return 0; } png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); int bit_depth, color_type; png_uint_32 temp_width, temp_height; png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type, NULL, NULL, NULL); if (width) { *width = temp_width; } if (height) { *height = temp_height; } png_read_update_info(png_ptr, info_ptr); int rowbytes = png_get_rowbytes(png_ptr, info_ptr); rowbytes += 3 - ((rowbytes-1) % 4); png_byte * image_data; image_data = (png_byte *) malloc(rowbytes * temp_height * sizeof(png_byte)+15); if (image_data == NULL) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fp); return 0; } png_bytep * row_pointers = (png_bytep *) malloc(temp_height * sizeof(png_bytep)); if (row_pointers == NULL) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); free(image_data); fclose(fp); return 0; } int i; for (i = 0; i < temp_height; i++) { row_pointers[temp_height - 1 - i] = image_data + i * rowbytes; } png_read_image(png_ptr, row_pointers); GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, GL_ZERO, GL_RGB, temp_width, temp_height, GL_ZERO, GL_RGB, GL_UNSIGNED_BYTE, image_data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); free(image_data); free(row_pointers); fclose(fp); return texture; } This code just generates the texture and I store the id on memory
      And then I want to display my texture on any position (X, Y) of my screen so I did the following (That's works, at least the positioning).
      //MY TEXTURE IS 32x32 pixels ! void Cluster::printTexture(GLuint idTexture, GLfloat x, GLfloat y) { glActiveTexture(GL_TEXTURE0); glBindVertexArray(VAO); GLfloat vertices[6][4] = { { x, y + 32, 0.0, 0.0 }, { x, y, 0.0, 1.0 }, { x + 32, y, 1.0, 1.0 }, { x, y + 32, 0.0, 0.0 }, { x + 32, y, 1.0, 1.0 }, { x + 32, y + 32, 1.0, 0.0 } }; glBindTexture(GL_TEXTURE_2D, idTexture); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferSubData(GL_ARRAY_BUFFER, GL_ZERO, sizeof(vertices), vertices); glBindBuffer(GL_ARRAY_BUFFER, GL_ZERO); glUniform1i(this->mTextShaderHandle, GL_ZERO); glDrawArrays(GL_TRIANGLE_STRIP, GL_ZERO, 6); } My .png is a blue square.
      The result is that my texture is not loaded correctly. It is not complete and there are many small black spots. I don't know what's going on ? It could be the vertices or the load ? Or maybe I need to add something on the shader. I don't know, I really need help.
      Thanks !
    • By Atwo Studios
      Hello everyone this is Anthony from Atwo Studios, as small indie company we have published 2 indie games using Game maker, one being ROY - Color Matching and the other being Solar Switch. Both can be found on iOS and the Google play store.

      For our third indie game we are wanting to make a golf puzzle based game, this would be on mobile too.

      Our current team consists of a designer,programmer and artist. I would like to add a person who is interested in making sounds effects/ music for our game.

      If you are interested in creating a game for portfolio work or just as a hobby, 
      please send me an email at : anthony@atwostudios.com

      PLEASE do attach any audio work so that my team and I can review it and determine if you are qualified for what we are looking for.

      Please check out our website for more information about us and the types of games we have made so far.
      atwostudios.com
      DOWN BELOW CAN BE FOUND VERY EARLY SNIPPET OF THE GAME TO GIVE YOU AN IDEA OF WHAT WE ARE MAKING.
      p.s. I am not a website designer
      2018-05-01_04-58-04.mp4
    • By mapmip
      Hi, I am fresh to database design, recently trying building my mobile multiplayer game, it will be something like pokemonGo.
      I have experience on MySQL, and I know some NoSQL engines like redis.
      I saw some existing game projects which store their data on both SQL database and noSQL database.
      Could anyone give some advice that what kind of data should store in SQL and what kind of data is better to be in noSQL.
      It would be nice if giving some real scenario examples.
      My understanding is data like user profile, purchase transactions should be in SQL.
      Field map information, enemy status can be NoSQL.  
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!