Jump to content
  • Advertisement
Sign in to follow this  
VansFannel

OpenGL Drawing code not working

This topic is 2844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.

I'm developing an Android application with OpenGL ES 2.0 native code.

The native code compiles ok, and runs without any problem. But it doesn't draw anything.

Here is my code:

##################################################################################

#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <string.h>

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#ifdef __cplusplus
extern "C"
{
#endif

unsigned int shaderProgramID;
GLint vertexHandle;
GLint normalHandle;
GLint textureCoordHandle;

// Constants:
static const float kObjectScale = 3.f;

static const char* cubeMeshVertexShader = "
attribute vec4 vertexPosition;
attribute vec4 vertexNormal;
attribute vec2 vertexTexCoord;
varying vec2 texCoord;
varying vec4 normal;
uniform mat4 modelViewProjectionMatrix;

void main() {
gl_Position = modelViewProjectionMatrix * vertexPosition;
normal = vertexNormal;
texCoord = vertexTexCoord;
} ";


static const char* cubeMeshFragmentShader = "
precision mediump float;
varying vec2 texCoord;
varying vec4 normal;
uniform sampler2D texSampler2D;
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
} ";

static const char* meshVertexShaderNoNormalTexCoor = "
attribute vec4 vertexPosition;
void main() {
gl_Position = vertexPosition;
} ";

static const char* fragmentShaderNoNormalTexCoor = "
precision mediump float;
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
} ";

unsigned int
initShader(unsigned int shaderType, const char* source)
{
GLuint shader = glCreateShader((GLenum)shaderType);
if (shader)
{
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);

if (!compiled)
{
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen)
{
char* buf = (char*) malloc(infoLen);
if (buf)
{
glGetShaderInfoLog(shader, infoLen, NULL, buf);
free(buf);
}
glDeleteShader(shader);
shader = 0;
}
}
}
return shader;
}


unsigned int
createProgramFromBuffer(const char* vertexShaderBuffer,
const char* fragmentShaderBuffer)
{

GLuint vertexShader = initShader(GL_VERTEX_SHADER, vertexShaderBuffer);
if (!vertexShader)
{
__android_log_write(ANDROID_LOG_ERROR, "createProgramFromBuffer", "init-vertexShader");
return 0;
}

GLuint fragmentShader = initShader(GL_FRAGMENT_SHADER,
fragmentShaderBuffer);
if (!fragmentShader)
{
__android_log_write(ANDROID_LOG_ERROR, "createProgramFromBuffer", "init-fragmentShader");
return 0;
}

GLuint program = glCreateProgram();
if (program)
{
glAttachShader(program, vertexShader);

glAttachShader(program, fragmentShader);

glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);

if (linkStatus != GL_TRUE)
{
GLint bufLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
if (bufLength)
{
char* buf = (char*) malloc(bufLength);
if (buf)
{
glGetProgramInfoLog(program, bufLength, NULL, buf);
free(buf);
}
}
glDeleteProgram(program);
program = 0;
__android_log_write(ANDROID_LOG_ERROR, "glGetProgramiv", "linkStatus");
}
}
__android_log_write(ANDROID_LOG_VERBOSE, "LoaderRenderer", "createProgramFromBuffer-OK");
return program;
}


JNIEXPORT void JNICALL Java_com_company_tests_LoaderRenderer_initRendering
(JNIEnv *env, jobject obj, jint numVNormal, jint numVTexCoord, jint width, jint heigth)
{
glViewport(0, 0, width, heigth);
// Define clear color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

if ((numVNormal > 0) && (numVTexCoord > 0))
{
__android_log_write(ANDROID_LOG_VERBOSE, "initRendering", "(numVNormal > 0) && (numVTexCoord > 0)");
shaderProgramID = createProgramFromBuffer(cubeMeshVertexShader,
cubeMeshFragmentShader);
normalHandle = glGetAttribLocation(shaderProgramID,
"vertexNormal");
textureCoordHandle = glGetAttribLocation(shaderProgramID,
"vertexTexCoord");
}
else if (numVNormal == 0)
{
if (numVTexCoord == 0)
{
__android_log_write(ANDROID_LOG_VERBOSE, "initRendering", "(numVNormal == 0) && (numVTexCoord == 0)");
shaderProgramID =createProgramFromBuffer(meshVertexShaderNoNormalTexCoor,
fragmentShaderNoNormalTexCoor);
}
else
{
__android_log_write(ANDROID_LOG_VERBOSE, "initRendering", "(numVNormal == 0) && (numVTexCoord > 0)");
shaderProgramID = createProgramFromBuffer(meshVertexShaderNoNormalTexCoor,
fragmentShaderNoNormalTexCoor);
textureCoordHandle = glGetAttribLocation(shaderProgramID,
"vertexTexCoord");
}
}
else if ((numVNormal > 0) && (numVTexCoord == 0))
{
__android_log_write(ANDROID_LOG_VERBOSE, "initRendering", "(numVNormal > 0) && (numVTexCoord == 0)");
shaderProgramID =createProgramFromBuffer(meshVertexShaderNoNormalTexCoor,
fragmentShaderNoNormalTexCoor);
normalHandle = glGetAttribLocation(shaderProgramID,
"vertexNormal");
}

vertexHandle = glGetAttribLocation(shaderProgramID,
"vertexPosition");
__android_log_write(ANDROID_LOG_VERBOSE, "LoaderRenderer", "initRendering-OK");
}

JNIEXPORT void JNICALL Java_com_company_tests_LoaderRenderer_updateRendering(
JNIEnv* env, jobject obj, jint width, jint heigth)
{
// Set the viewport
glViewport(0, 0, width, heigth);
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
}

JNIEXPORT void JNICALL Java_com_company_tests_LoaderRenderer_renderFrame
(JNIEnv *env, jobject obj,
jfloatArray vertices, jfloatArray normals,
jfloatArray texCoord, jintArray indices)
{
jfloat *vertPos, *vertNorm, *vertTexC;
jint *indicesArray;

jsize numNormals = env->GetArrayLength(normals);
jsize numTexCoords = env->GetArrayLength(texCoord);

GLsizei numIndices = (GLsizei) env->GetArrayLength(indices);

vertPos = env->GetFloatArrayElements(vertices, 0);
indicesArray = env->GetIntArrayElements(indices, 0);

if (numNormals > 0)
vertNorm = env->GetFloatArrayElements(normals, 0);
if (numTexCoords > 0)
vertTexC = env->GetFloatArrayElements(texCoord, 0);

// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

glUseProgram(shaderProgramID);

glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) vertPos);
if (numNormals > 0)
glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) vertNorm);
if (numTexCoords > 0)
glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) vertTexC);

glEnableVertexAttribArray(vertexHandle);
if (numNormals > 0)
glEnableVertexAttribArray(normalHandle);
if (numTexCoords > 0)
{
glEnableVertexAttribArray(textureCoordHandle);
}
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT,
(const GLvoid*) indicesArray);

glDisable(GL_DEPTH_TEST);

glDisableVertexAttribArray(vertexHandle);
if (numNormals > 0)
glDisableVertexAttribArray(normalHandle);
if (numTexCoords > 0)
glDisableVertexAttribArray(textureCoordHandle);

if (numNormals > 0)
env->ReleaseFloatArrayElements(normals, vertNorm, 0);
if (numTexCoords > 0)
env->ReleaseFloatArrayElements(texCoord, vertTexC, 0);

env->ReleaseFloatArrayElements(vertices, vertPos, 0);
env->ReleaseIntArrayElements(indices, indicesArray, 0);
}
#ifdef __cplusplus
}
#endif




Please, do you see where is my error?

Thanks.

[Edited by - VansFannel on December 8, 2010 7:52:35 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by VansFannel
Please, do you see where is my error?


No. But have you seen the FAQ and grasp what they write in "How to ask questions the smart way?" and further, did you read the famous "What have you tried?" website?

Share this post


Link to post
Share on other sites
Note there is also an article on debugging on gamedev.net and you still haven't read the other two thingies I've written about.

Share this post


Link to post
Share on other sites
Is there an article about debugging Android native code on gamedev.net?

I think it would be more useful if you recommend me articles about OpenGL ES 2.0 programming, or something about my question instead of http://www.catb.org/~esr/faqs/smart-questions.html.

I have put my code here because I though someone with experience on OpenGL programming could see my error easily.

I'm so sorry if you don't like the way I have done my question but I prefer to waste my time learning OpenGL and not talking with you about the way I ask.

Share this post


Link to post
Share on other sites
Quote:
Original post by VansFannel
Is there an article about debugging Android native code on gamedev.net?

I think it would be more useful if you recommend me articles about OpenGL ES 2.0 programming, or something about my question instead of http://www.catb.org/~esr/faqs/smart-questions.html.

I have put my code here because I though someone with experience on OpenGL programming could see my error easily.

I'm so sorry if you don't like the way I have done my question but I prefer to waste my time learning OpenGL and not talking with you about the way I ask.


You *should* spend some time on how to find errors in your code.

A question like
Quote:
Please, do you see where is my error?

upon a big fat chunk of badly formatted code only indicates that your real problem is not an OpenGL-problem, but a "can't help myself or have not tried hard enough"-problem.


A question like
Quote:
Please, do you see where is my error?

upon a big fat chunk of badly formatted code clearly shows that you don't even know where the error is at least roughly at. If that's not true, than it shows that you are lazy.


A statement like
Quote:
but I prefer to waste my time learning OpenGL and not talking with you about the way I ask.

shows that you might only be interested in ready solutions and not in learning how to help yourself. You don't want to waste time learning how to fish, you just want the fish. But you expect us to waste our unpaid and voluntary time with very, very non-specific questions and big code-dumps, not even remotely going in detail about your exact problem.


Before you state something like "phresnel, the funny thing is, you are wasting time with this post, why not with my code", then my answer is, no. I spent my time not only in assembling ready solutions, but also in other things, like touch-typing, so it took me <5 minutes, an average cigarette pause*, to write this shit.


* smoking hurts your body and your environment

Share this post


Link to post
Share on other sites
While you are answering my question, I'm checking my code to find my error.

If you believe that I haven't test my code enough before post it here, you are wrong. If I have posted my code here is because I can't find the error by myself.

And I see you have time to tell me how must I make my question, but you don't have time to check my code.

Sorry, but I don't have time to continue answering you.

Share this post


Link to post
Share on other sites
Quote:
Original post by VansFannel
While you are answering my question, I'm checking my code to find my error.

If you believe that I haven't test my code enough before post it here, you are wrong. If I have posted my code here is because I can't find the error by myself.

And I see you have time to tell me how must I make my question, but you don't have time to check my code.


Then, why not state what you have tried and what you've found out, so we don't waste time hunting for solutions that you were already unlucky at?

Quote:
Sorry, but I don't have time to continue answering you.

You know. We all have stuff to do, not only you. That's the reason why we often post seemingly harsh comments like this. It even happens that the most experienced here use to be older than many newbies, and therefore have even less time to waste.


Simply put: As long as you are on unpaid forums where ppl do voluntary work, if you want ppl to help you, try to minimize the time they must spend at your problem.



Quote:
And I see you have time to tell me how must I make my question, but you don't have time to check my code.

If you look back, you'll see I knew this cockiness comes.

Share this post


Link to post
Share on other sites
But if you would tell us what you have tried already, that would be a great help to us, that means we would have a better chance to help you.

So tell us what you have tried.

And yes, Phresnel took the time and effort to lecture you, but it is for the others: you seem to be a bit lazy based on your posting history, so we would benefit from your "improvement". And he's right: why do WE have to to ALL the debugging for you?

Again: even if you had done your work with it, we don't have a clue because you don't show us what you did.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!