-1

Im trying to render using VBO's in Opengl ES 2.0.

I set up the vbo and it shows himself quite right (a triangle in the top of the screen), and I used the fragment shader to show up the loaded texture, and it is right too.

BUT, the TextureCoordinates are all 0.0f, thus rendering all wrong(1 color only).

there is my code:

#include "glee.h"
#include <windows.h>
#include <time.h>

#include <math.h>

HDC hDC;

const GLchar* vertexSource=
                                                        "#version 100\n"
                                                        "\n"
                                                        "attribute vec3 a_position;     \n"
                                                        "attribute vec2 a_texCoord; \n"

                                                        "varying vec2 v_textcoord; \n"
                                                        "uniform sampler2D s_texture;   \n"
                                                        "\n"
                                                        "void main()\n"
                                                        "{\n"
                                                        "    gl_Position = vec4(a_position, 1.0);\n"
                                                        "    v_textcoord = a_texCoord;\n"
                                                        "}\n";

const GLchar*  fragmentSource=
                                                        "#ifdef GL_ES                                   \n"
                                                        "precision mediump float;           \n"
                                                        "#else                                              \n"
                                                        "#version   100                                 \n"
                                                        "precision mediump float;           \n"
                                                        "#endif                                             \n"
                                                        "varying vec2 v_textcoord; \n"
                                                        "uniform sampler2D s_texture;   \n"


                                                        "void main() {\n"
                                                    " gl_FragColor= texture2D( s_texture, v_textcoord ); \n"
                                                        "       }\n";


#include <stdlib.h> 
float noise(float p)
{
    return (float)(rand()%(int)(p+1));
}

float fbm(float p)
{
    float f =0.5000*noise(p); p*=2.01;
                f+=0.2500*noise(p); p*=2.03;
                f+=0.2500*noise(p); p*=2.01;
                f+=0.1250*noise(p); p*=2.02;
                f+=0.0625*noise(p); p*=2.02;
                f/=0.9375;
                return f;
}


extern "C" __declspec(noreturn) void WinMainCRTStartup()
{
    HWND    hWnd;

    hWnd = CreateWindow("EDIT","EDIT",WS_POPUP|WS_VISIBLE, 0,0, 600, 400,0,0,0,0);
    hDC = GetDC(hWnd);

    // Pixel Format
PIXELFORMATDESCRIPTOR pfd = {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
            PFD_TYPE_RGBA,
            32,             // Colour buffer bit depth
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            32,             // Depth buffer bit depth
            0, 0,
            PFD_MAIN_PLANE, 
            0, 0, 0, 0 };

    SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd);
    wglMakeCurrent(hDC,wglCreateContext(hDC));

                                        glMatrixMode(GL_PROJECTION);
                                        glLoadIdentity();

                                        glMatrixMode(GL_MODELVIEW);                                                                                 

                                        glClearColor(0.0f,0.0f,0.0f,1.0f);

                                        // Enable z-buffer
                                        glEnable(GL_DEPTH_TEST);
                                        glDepthMask(GL_TRUE);
                                        glEnable(GL_CULL_FACE);

                                        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
                                        glAlphaFunc(GL_GREATER,0.1f);

                                        glEnable(GL_BLEND);
                                        glEnable(GL_ALPHA_TEST);

                                        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                                        glLoadIdentity(); 

                                        glDepthFunc(GL_LESS);               // The Type Of Depth Test To Do
                                        glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading

    SetForegroundWindow(hWnd);
    SetFocus(hWnd);

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSource, NULL);
    glCompileShader(vertexShader);
    GLint status;
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
    char buffer[512];
    glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
    OutputDebugStringA(buffer);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
    glCompileShader(fragmentShader);

    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
    glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
    OutputDebugStringA(buffer);
    glGetShaderInfoLog(vertexShader, 512, NULL, buffer);

    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);

    glLinkProgram(shaderProgram);

    glUseProgram(shaderProgram);

    GLuint handle;

    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &handle);
    glBindTexture(GL_TEXTURE_2D, handle); 

    GLboolean isTex=glIsTexture(handle);

    char * tbuffer=new char[4*512*512];

    for (int y=0;y<512;y++)
    for (int x=0;x<512;x++)
    {
        float f=rand()%255; 
        int off=(y*512)+x;
        off*=4;

        tbuffer[off+0]=f;
        tbuffer[off+1]=f;
        tbuffer[off+2]=f;
        tbuffer[off+3]=255;
    }

    glTexImage2D(GL_TEXTURE_2D,  0, GL_RGBA
                                                                , 512
                                                                , 512
                                                                , 0
                                                                , GL_RGBA
                                                                , GL_UNSIGNED_BYTE
                                                                , tbuffer);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S        ,   GL_CLAMP_TO_EDGE);  // Warning: Android OpenGL GLES MUST and can ONLY receive this param
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T        ,   GL_CLAMP_TO_EDGE); 


    glUseProgram(shaderProgram);

int n=0;
    glBindAttribLocation(shaderProgram, n++, "a_position");
    glBindAttribLocation(shaderProgram, n++, "a_texCoord");
    glUniform1i(glGetUniformLocation(shaderProgram, "s_texture"), 0);
    GLuint texCoordLoc=glGetUniformLocation(shaderProgram, "a_texCoord");


    GLuint VertexVBOID;
    GLuint IndexVBOID;

struct MyVertex
  {
    float x, y, z;        //Vertex
    float nx, ny, nz;     //Normal
    float s0, t0;         //Texcoord0
  };

  MyVertex pvertex[3];
  //VERTEX 0
  pvertex[0].x = -1.0;
  pvertex[0].y = 0.0;
  pvertex[0].z = 0.0;
  pvertex[0].nx = 0.0;
  pvertex[0].ny = 0.0;
  pvertex[0].nz = 1.0;
  pvertex[0].s0 = 0.0;
  pvertex[0].t0 = 1.0;
  //VERTEX 1
  pvertex[1].x = 1.0;
  pvertex[1].y = 0.0;
  pvertex[1].z = 0.0;
  pvertex[1].nx = 0.0;
  pvertex[1].ny = 0.0;
  pvertex[1].nz = 1.0;
  pvertex[1].s0 = 1.0;
  pvertex[1].t0 = 0.0;
  //VERTEX 2
  pvertex[2].x = 0.0;
  pvertex[2].y = 1.0;
  pvertex[2].z = 0.0;
  pvertex[2].nx = 0.0;
  pvertex[2].ny = 0.0;
  pvertex[2].nz = 1.0;
  pvertex[2].s0 = 1.0;
  pvertex[2].t0 = 1.0;


  glGenBuffers(1, &VertexVBOID);
  glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
  glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertex)*3, &pvertex, GL_STATIC_DRAW);

  unsigned short pindices[6];
  pindices[0] = 0;
  pindices[1] = 1;
  pindices[2] = 2;
  pindices[3] = 0;
  pindices[4] = 3;
  pindices[5] = 1;

  glGenBuffers(1, &IndexVBOID);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*3, pindices, GL_STATIC_DRAW);

  #define BUFFER_OFFSET(i) ((void*)(i))

  glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);

  glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), BUFFER_OFFSET(0));   //The starting point of the VBO, for the vertices

  glClientActiveTexture(GL_TEXTURE0);

    glEnableVertexAttribArray(texCoordLoc);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glTexCoordPointer(2, GL_FLOAT, sizeof(MyVertex), BUFFER_OFFSET(24));   //The starting point of texcoords, 24 bytes away
    GLenum err (glGetError());

    while(!GetAsyncKeyState(VK_ESCAPE) && !GetAsyncKeyState(VK_SPACE) )
    {   
        glClearColor(0.1f,0.2f,0.3f,0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);

         glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   //The starting point of the IBO

            SwapBuffers(hDC);
            MSG msg;

        while (PeekMessage(&msg,0,0,0,PM_REMOVE)==0);
    }

    ExitProcess (0);
}

What should I do? I understood that with the vbo, the texture coordinates where already shipped to the shader. What is wrong?

genpfault
  • 51,148
  • 11
  • 85
  • 139
diego.martinez
  • 1,051
  • 2
  • 11
  • 25

1 Answers1

1

You got yourself a fairly wild mix here. You say that you're using ES 2.0, but you use a lot of API entries that are not part of ES 2.0. Then you partly mix legacy and current features in ways that are not compatible. I'll try and point out the main problems, but I might not be covering everything in detail.

  1. The PIXELFORMATDESCRIPTOR has a couple of values that look unusual. It specifies 32 for cColorBits. This is the size of just the RGB components, so it should be 24 for 8 bits per component. It also specifies 32 bits for the depth buffer, which is a value that is not supported by a lot of hardware. 24 is a much more commonly available size.
  2. glMatrixMode() and glLoadIdentity() do not exist in ES 2.0.
  3. glAlphaFunc() and GL_ALPHA_TEST do not exist in ES 2.0.
  4. glShadeModel() does not exist in ES 2.0.
  5. The code checks for success of compiling the vertex shader a second time after compiling the fragment shader, instead of checking the fragment shader.
  6. glBindAttribLocation() is called after glLinkProgram(). This has no effect. You have to either call glBindAttribLocation() before glLinkProgram(), or glGetAttribLocation() after glLinkProgram().
  7. There's a call to glGetUniformLocation() for a_texCoord, which is an attribute, not a uniform. This should be glGetAttribLocation() instead.
  8. glEnableClientState(), glVertexPointer() and glTexCoordPointer() do not exist in ES 2.0. The are also incompatible with the generic vertex attributes used in the shader code. glEnableVertexAttribArray() and glVertexAttribPointer() need to be used instead.

The biggest problems are most likely items 7 and 8 in this list, which would definitely prevent the texture coordinates from working with any version of OpenGL.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • thank you for your response. Yes I mixed code here, I took a demo i had and coded the glDrawElements part for having a isolated code to show you, I did not realize that I mixed opengl codes. my project (the good one) is a cross platform project for Windows/android, so i have parts with OpenGL ES 2.0 and parts in OpenGL. Going back to the case. I corrected the uniform and linking problems (the position of binding)and removed old code for the OpenGL. But now i dont know how i should do the VBO's thing. How should I do the VBO in ES 2.0? – diego.martinez Aug 08 '14 at 08:50
  • With glBufferData it is supposed to be uploaded to the gpu with the vertex position, texture (and normals), i thought apart of binding the buffer and calling DrawElements i should'nt need for more. What I'm missing here? I correct the code now. Thank you – diego.martinez Aug 08 '14 at 08:54
  • i tested the glVertexAttribPointer and that was the real problem here, thank you very much. :) – diego.martinez Aug 08 '14 at 14:19