PDA

View Full Version : Simple 3D triangle - OGL ES 2.0, nothing seems wrong, but No Output



Sagar G
12-05-2013, 10:17 AM
Below id the code for NDK ES 2.0. I have tries everything I can in this world. I jst do not get 3D triangle.


void engine_redraw(struct engine* engine);

#define LOGI(...) ((void) __android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))

float fPyramid[18]; // Pyramid data - 4 triangles of 3 vertices of 3 floats
float fPyramidColor[18]; // Same for color

unsigned int uiVBO[2]; // One VBO for vertices positions, one for colors


bool bShowFPS = false;
bool bVerticalSync = true;

//temp adjustment below
GLuint globalProgramObject;
//temp adjustment above


struct saved_state
{
float angle;
int x;
int y;
};

struct engine
{
struct android_app* app;

ASensorManager* sensorManager;;
const ASensor* accelerometerSensor;
ASensorEventQueue* sensorEventQueue;

int animating;
EGLDisplay display;
EGLSurface surface;
EGLContext context;
int width;
int height;
struct saved_state state;

//own implimentation
GLuint programObject;
};

/*
typedef struct
{
GLuint programObject;
}UserData;*/

GLuint LoadShader(GLuint type, const char* shaderSource)
{
GLuint shader;
GLint compiled;

shader = glCreateShader(type);
if(shader == 0)
return (0);

glShaderSource(shader, 1, &shaderSource, NULL);

glCompileShader(shader);

glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);

if(!compiled)
{
//error printing here..
glDeleteShader(shader);
return(0);
}

return shader;
}

int Init(struct engine *esContext)
{
//UserData *userData = (UserData*)esContext-> userData;

// Setup pyramid

// Front face
fPyramid[0] = 0.0f; fPyramid[1] = 5.0f; fPyramid[2] = 0.0f;
fPyramid[3] = -3.0f; fPyramid[4] = 0.0f; fPyramid[5] = 3.0f;
fPyramid[6] = 3.0f; fPyramid[7] = 0.0f; fPyramid[8] = 3.0f;

// Left face
fPyramid[18] = 0.0f; fPyramid[19] = 5.0f; fPyramid[20] = 0.0f;
fPyramid[21] = -3.0f; fPyramid[22] = 0.0f; fPyramid[23] = -3.0f;
fPyramid[24] = -3.0f; fPyramid[25] = 0.0f; fPyramid[26] = 3.0f;

// Back face
fPyramid[9] = 0.0f; fPyramid[10] = 5.0f; fPyramid[11] = 0.0f;
fPyramid[12] = 3.0f; fPyramid[13] = 0.0f; fPyramid[14] = -3.0f;
fPyramid[15] = -3.0f; fPyramid[16] = 0.0f; fPyramid[17] = -3.0f;

// Right face
fPyramid[27] = 0.0f; fPyramid[28] = 5.0f; fPyramid[29] = 0.0f;
fPyramid[30] = 3.0f; fPyramid[31] = 0.0f; fPyramid[32] = 3.0f;
fPyramid[33] = 3.0f; fPyramid[34] = 0.0f; fPyramid[35] = -3.0f;



// Setup pyramid color

for(int i = 0; i < 4 ; i++)
{
fPyramidColor[i*9] = 1.0f; fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+2] = 0.0f;
if(i < 2)
{
fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+4] = 1.0f; fPyramidColor[i*9+5] = 0.0f;
fPyramidColor[i*9+2] = 0.0f; fPyramidColor[i*9+7] = 0.0f; fPyramidColor[i*9+8] = 1.0f;
}
else
{
fPyramidColor[i*9+2] = 0.0f; fPyramidColor[i*9+7] = 1.0f; fPyramidColor[i*9+8] = 0.0f;
fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+4] = 0.0f; fPyramidColor[i*9+5] = 1.0f;
}
}




glGenBuffers(2, uiVBO);

// Setup whole pyramid

glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
glBufferData(GL_ARRAY_BUFFER, 36*sizeof(float), fPyramid, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, uiVBO[1]);
glBufferData(GL_ARRAY_BUFFER, 36*sizeof(float), fPyramidColor, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glEnable(GL_DEPTH_TEST);


char vShaderStr[] =
"uniform mat4 projectionMatrix; \n"
"uniform mat4 modelViewMatrix; \n"
"attribute vec3 vPosition; \n"
"attribute vec3 inColor; \n"
"varying vec3 theColor; \n"
"void main() \n"
"{ \n"
" gl_Position = projectionMatrix*modelViewMatrix*vec4(vPosition, 1.0); \n"
" theColor = inColor; \n"
"} \n";

char fShaderStr[] =
"varying vec3 theColor; \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4(theColor, 1.0); \n"
"} \n";

GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;


vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr);
fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);

programObject = glCreateProgram();
if(programObject == 0)
return (0);

globalProgramObject = programObject;

glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);

glBindAttribLocation(programObject, 0, "vPosition");
glBindAttribLocation(programObject, 2, "inColor");

glLinkProgram(programObject);

glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if(!linked)
{
GLint infoLen = 0;

glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
// error handling here
}
glDeleteProgram(programObject);
return false;
}
esContext-> programObject = programObject;

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

return true;
}

static int engine_init_display(struct engine* engine)
{
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};

const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
};


EGLint w, h, dummy, format;
EGLint numConfigs;
EGLConfig config;
EGLSurface surface;
EGLContext context;

EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

LOGI("androd_init_display called..");

eglInitialize(display, 0, 0);

eglChooseConfig(display, attribs, &config, 1, &numConfigs);

eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);

ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);

surface = eglCreateWindowSurface(display, config, engine-> app-> window, NULL);
context = eglCreateContext(display, config, NULL, context_attribs);

if(eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
{
LOGI("Unable to make current context");
return(-1);
}

eglQuerySurface(display, surface, EGL_WIDTH, &w);
eglQuerySurface(display, surface, EGL_HEIGHT, &h);

engine-> display = display;
engine-> context = context;
engine-> surface = surface;
engine-> width = w;
engine-> height = h;
engine-> state.angle = 0.0f;

// glEnable(GL_CULL_FACE);
// glDisable(GL_DEPTH_TEST);


const char* supportedExtensions = eglQueryString(engine->display, EGL_EXTENSIONS);
int i = -1;
char currentString[256];

if(Init(engine))
{
engine_redraw(engine);
return (0);
}

else
return(-1);

}

static void engine_draw_frame(struct engine* engine)
{
static float fRotationAngle = 0.0f;
static float translation = 0.0;

LOGI("starting draw frame()");

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray;
glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC) eglGetProcAddress("glBindVertexArrayOES");
if(glBindVertexArray == NULL)
{
LOGI("No Extention support found..");
return;
}
glBindVertexArray(uiVAO[0]);*/

glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
glEnableVertexAttribArray(0);//0 is or position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, uiVBO[1]);
glEnableVertexAttribArray(2);//2 is for colors
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);


// glBindBuffer(GL_ARRAY_BUFFER, uiVBO[1]);
LOGI("Bufeer bound");

int iModelViewLoc = glGetUniformLocation(globalProgramObject, "modelViewMatrix");
int iProjectionLoc = glGetUniformLocation(globalProgramObject, "projectionMatrix");
LOGI("Problem in getting float matrix");
float *retVal = getPerspectiveMatrixInFloat();

glUniformMatrix4fv(iProjectionLoc, 1, GL_FALSE, getPerspectiveMatrixInFloat());

LOGI("before getting modelview");

ESMatrix mModelView = ogmLookAt(0, 15, 40, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
LOGI("Now, I have the mmodelview");
ESMatrix mCurrent = ogmRotate(&mModelView, fRotationAngle, 0.0f, 0.01f, 1.0f);
ogmTranslate(&mCurrent, 0, 13, 33);

glUniformMatrix4fv(iModelViewLoc, 1, GL_FALSE, getCurrentMatrixInFloat(mCurrent));
// glUniformMatrix4fv(iModelViewLoc, 1, GL_FALSE, getCurrentMatrixInFloat(mModelView));

glDrawArrays(GL_TRIANGLES, 0, 12);


fRotationAngle += 0.010;

LOGI("Arrays Drawn");

eglSwapBuffers(engine-> display, engine-> surface);

}

void engine_redraw(struct engine* engine)
{
ESMatrix perspective;
ESMatrix modelview;
float aspect;

LOGI("starting redraw()");
aspect = (GLfloat)engine-> width / (GLfloat)engine-> height;

glViewport(0, 0, engine-> width, engine-> height);

ogmLoadIdentityMatrix(&perspective);
ogmSetPerspective( &perspective, 45.0f, aspect, 0.001f, 60.0f );

glUseProgram(engine-> programObject);
LOGI("program object decided");
LOGI("ending redraw()");
}