PDA

View Full Version : opengl es help.



smultring
04-08-2010, 07:15 AM
looking for a professional viewpoint on writing opengl c code to opengl es.

This is a example:

/////////////////////////////////////////////////////////////////////////
//
// rubik.h - Renderfunctions
//
// This file was created by the OpenGL-Export-Script for Blender.
//
// Export-Time: 07.04.2010, 16:52:37
//
// Script written by Michael Gantenbrinker.
//
/////////////////////////////////////////////////////////////////////////

#ifndef __RUBIK_H__
#define __RUBIK_H__


/////////////////////////////////////////////////////////////////////////
// DEFINES
#define NUM_MESHES 1
#define NUM_LIGHTS 0


/////////////////////////////////////////////////////////////////////////
// CONSTANTS
// Vertex-Array for each Mesh
const float fVertices_1[21][3] = { {1.000000f, 1.000000f, 0.000000f}, {1.000000f, -1.000000f, 0.000000f}, {-1.000000f, -1.000000f, 0.000000f}, {-1.000000f, 1.000000f, 0.000000f},
{-0.000000f, -1.000000f, 0.000000f}, {1.000000f, -0.000000f, 0.000000f}, {0.000000f, 1.000000f, 0.000000f}, {-1.000000f, 0.000000f, 0.000000f},
{0.000000f, 0.000000f, 0.000000f}, {1.000000f, -0.000000f, 1.366215f}, {0.000000f, 0.000000f, 1.366215f}, {0.000000f, 1.000000f, 1.366215f},
{1.000000f, 1.000000f, 1.366215f}, {1.000000f, -0.000000f, 0.942688f}, {0.000000f, 0.000000f, 0.942688f}, {0.000000f, 1.000000f, 0.942688f},
{1.000000f, 1.000000f, 0.942688f}, {1.000000f, -0.577829f, 0.942688f}, {-0.000000f, -0.577828f, 0.942688f}, {-0.000000f, -0.577828f, 1.366215f},
{1.000000f, -0.577829f, 1.366215f} };
// Normal-Array for each Mesh
const float fNormals_1[21][3] = { {0.707083f, 0.707083f, 0.000000f}, {0.000000f, 0.000000f, 1.000000f}, {0.000000f, 0.000000f, 1.000000f}, {0.000000f, 0.000000f, 1.000000f},
{0.000000f, 0.000000f, 1.000000f}, {0.577349f, -0.577349f, 0.577349f}, {-0.577349f, 0.577349f, 0.577349f}, {0.000000f, 0.000000f, 1.000000f},
{-0.301492f, -0.301492f, 0.904508f}, {0.707083f, 0.000000f, 0.707083f}, {-0.707083f, 0.000000f, 0.707083f}, {-0.577349f, 0.577349f, 0.577349f},
{0.577349f, 0.577349f, 0.577349f}, {0.904508f, -0.301492f, -0.301492f}, {-0.904508f, -0.301492f, -0.301492f}, {-0.707083f, 0.707083f, 0.000000f},
{0.707083f, 0.707083f, 0.000000f}, {0.577349f, -0.577349f, -0.577349f}, {-0.577349f, -0.577349f, -0.577349f}, {-0.577349f, -0.577349f, 0.577349f},
{0.577349f, -0.577349f, 0.577349f} };

// Triangle-Indices for each Mesh

// Quad-Indices for each Mesh
const unsigned long ulQuadIndices_1[16][4] = { {6, 3, 7, 8}, {7, 2, 4, 8}, {4, 1, 5, 8}, {9, 12, 11, 10}, {5, 13, 14, 8}, {8, 14, 15, 6}, {14, 10, 11, 15}, {16, 0, 6, 15},
{15, 11, 12, 16}, {0, 16, 13, 5}, {16, 12, 9, 13}, {14, 13, 17, 18}, {10, 14, 18, 19}, {13, 9, 20, 17}, {9, 10, 19, 20}, {17, 20, 19, 18}
};

// Array with Vertex-Array-Pointers
const float* pVertexPointers[] = {&fVertices_1[0][0]};
// Array with Normal-Array-Pointers
const float* pNormalPointers[] = {&fNormals_1[0][0]};
// Array with TriangleIndex-Array-Pointers
const unsigned long* pTriangleIndexPointers[] = {0};
// Array with QuadIndex-Array-Pointers
const unsigned long* pQuadIndexPointers[] = {&ulQuadIndices_1[0][0]};

// Array with Vertex-Array-Lengths
const unsigned long ulVertexLengths[] = {21};
// Array with TriangleIndex-Array-Lengths
const unsigned long ulTriangleIndexLengths[] = {0};
// Array with QuadIndex-Array-Lengths
const unsigned long ulQuadIndexLengths[] = {16};


void Init()
{
}

void Render(int iWidth = 1024, int iHeight = 768)
{
// Setup Projektion-Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double dAspectRatio = (double)iWidth / (double)iHeight;
gluPerspective(45.0f, dAspectRatio, 1.0f, 100.0f);
glViewport(0, 0, iWidth, iHeight);
gluLookAt(5.0f, 5.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);

// Setup Lights
float lPos[] = {5.0f, 5.0f, 5.0f, 1.0f};
float lDir[] = {-0.707f, -0.707f, -0.707f};
glLightfv(GL_LIGHT0, GL_AMBIENT, StdColor_White);
glLightfv(GL_LIGHT0, GL_DIFFUSE, StdColor_BrightGrey);
glLightfv(GL_LIGHT0, GL_SPECULAR, StdColor_White);
glLightfv(GL_LIGHT0, GL_POSITION, lPos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lDir);
glLightf (GL_LIGHT0, GL_SPOT_CUTOFF, 180.f);
glLightf (GL_LIGHT0, GL_SPOT_EXPONENT, 1.0f);
glLightf (GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
glLightf (GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.6f);
glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.06f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Setup Scene-Content
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float* fVertices = 0;
float* fNormals = 0;
unsigned long* ulTriangleIndices = 0;
unsigned long* ulQuadIndices = 0;
unsigned long* ulPtr = 0;

for(int i = 0; i < NUM_MESHES; i++)
{
fVertices = (float*)pVertexPointers[i];
fNormals = (float*)pNormalPointers[i];
ulTriangleIndices = (unsigned long*)pTriangleIndexPointers[i];
ulQuadIndices = (unsigned long*)pQuadIndexPointers[i];

if(ulTriangleIndices)
{
glBegin(GL_TRIANGLES);
unsigned long t = 0;
while(t < ulTriangleIndexLengths[i]*3)
{
ulPtr = &amp;ulTriangleIndices[t];
glNormal3fv(&amp;fNormals[ulPtr[0]*3]);
glVertex3fv(&amp;fVertices[ulPtr[0]*3]);

glNormal3fv(&amp;fNormals[ulPtr[1]*3]);
glVertex3fv(&amp;fVertices[ulPtr[1]*3]);

glNormal3fv(&amp;fNormals[ulPtr[2]*3]);
glVertex3fv(&amp;fVertices[ulPtr[2]*3]);

t += 3;
}
glEnd();
}

if(ulQuadIndices)
{
glBegin(GL_QUADS);
unsigned long q = 0;
while(q < ulQuadIndexLengths[i]*4)
{
ulPtr = &amp;ulQuadIndices[q];
glNormal3fv(&amp;fNormals[ulPtr[0]*3]);
glVertex3fv(&amp;fVertices[ulPtr[0]*3]);

glNormal3fv(&amp;fNormals[ulPtr[1]*3]);
glVertex3fv(&amp;fVertices[ulPtr[1]*3]);

glNormal3fv(&amp;fNormals[ulPtr[2]*3]);
glVertex3fv(&amp;fVertices[ulPtr[2]*3]);

glNormal3fv(&amp;fNormals[ulPtr[3]*3]);
glVertex3fv(&amp;fVertices[ulPtr[3]*3]);

q += 4;
}
glEnd();
}
}
}

#endif /* __RUBIK_H__ */

ZbuffeR
04-08-2010, 07:20 AM
http://pandorawiki.org/OpenGL_ES_1.1_Tutorial#The_Basics

dorbie
04-08-2010, 12:24 PM
Writing? I assume you mean porting?

If you want answers then other posters answering deserve your modest effort in asking the question.

The main problem with your code is the immediate mode rendering using the glBegin() glVertex*() and glEnd() calls. These do not exist in OpenGL ES. Look at glDrawArrays and glDrawElements API calls to port this code, these replace the glBegin glEnd paradigm. The rest seems portable to the 1.x version of the OpenGL ES API.

I haven't used glu on ES though.

smultring
04-09-2010, 01:37 AM
Does this involve a stride?
Refering to multiple vertex arrays.

smultring
04-09-2010, 02:09 PM
Was solved today :D

smultring
04-11-2010, 08:05 AM
bump,

I've tried to render all vertices.
First i made all vertices into a new array, then gldrawarray and GL_TRIANGLE_STRIP.
Something is still wrong.

smultring
04-13-2010, 07:33 AM
Please correct me if im wrong.

This is my code:

const float fVertices_1[34][3] = { {1.000000f, 1.000000f, -1.000000f}, {1.000000f, -1.000000f, -1.000001f}, {-1.000000f, -1.000000f, -1.000000f}, {-1.000000f, 1.000000f, -1.000000f},
{1.000000f, 0.999999f, 1.273150f}, {0.999999f, -1.000001f, 1.000000f}, {-1.000000f, -1.000000f, 0.289065f}, {-1.000000f, 1.000000f, 1.000000f},
{1.000001f, 3.000000f, 1.273151f}, {-0.999999f, 3.000000f, 1.000000f}, {-0.999999f, 3.000000f, -1.000000f}, {1.000000f, 3.000000f, -1.000000f},
{-0.999999f, 5.000000f, -0.999999f}, {1.000001f, 5.000000f, -0.999999f}, {1.000001f, 5.000000f, 1.000001f}, {-0.999999f, 5.000000f, 0.289066f},
{3.000001f, 2.999999f, 1.273150f}, {3.000001f, 4.999999f, 1.000000f}, {3.000001f, 5.000000f, -1.000000f}, {3.000000f, 3.000000f, -1.000000f},
{5.000001f, 2.999999f, 1.000000f}, {5.000000f, 2.999999f, -1.000001f}, {5.000001f, 4.999999f, -1.000000f}, {5.000001f, 4.999999f, 0.289065f},
{3.000001f, 0.999999f, 1.273150f}, {5.000000f, 0.999999f, 0.999999f}, {5.000000f, 0.999999f, -1.000001f}, {3.000000f, 1.000000f, -1.000001f},
{5.000000f, 0.999999f, -1.000001f}, {5.000000f, 0.999999f, 0.999999f}, {3.000000f, -1.000001f, 0.999999f}, {5.000000f, -1.000001f, 0.289063f},
{5.000000f, -1.000001f, -1.000002f}, {3.000000f, -1.000000f, -1.000001f} };

const unsigned long ulTriangleIndices_1[2][3] = { {27, 26, 28}, {25, 24, 29} };

// Quad-Indices for each Mesh
const unsigned long ulQuadIndices_1[34][4] = { {0, 1, 2, 3}, {4, 7, 6, 5}, {0, 4, 5, 1}, {1, 5, 6, 2}, {2, 6, 7, 3}, {7, 4, 8, 9}, {3, 7, 9, 10}, {4, 0, 11, 8},
{0, 3, 10, 11}, {11, 10, 12, 13}, {10, 9, 15, 12}, {9, 8, 14, 15}, {14, 13, 12, 15}, {14, 8, 16, 17}, {13, 14, 17, 18}, {11, 13, 18, 19},
{8, 11, 19, 16}, {19, 18, 22, 21}, {18, 17, 23, 22}, {17, 16, 20, 23}, {20, 21, 22, 23}, {20, 16, 24, 25}, {21, 20, 25, 26}, {19, 21, 26, 27},
{16, 19, 27, 24}, {26, 25, 29, 28}, {29, 24, 30, 31}, {28, 29, 31, 32}, {27, 28, 32, 33}, {30, 33, 32, 31}, {0, 4, 24, 27}, {30, 24, 4, 5},
{1, 0, 27, 33}, {33, 30, 5, 1} };
int i = 0;
int u = 0;

int q = 0;
int varray_i = 0;
float *Varray[4000];
float *Narray[4000];
while(q < 2)
{
u = 0;
while (u < 3){
i = 0;
while (i < 3){
Varray[varray_i] = &amp;fVertices_1[ulTriangleIndices_1[q][u]][i];
varray_i++;
i++;
};
u++;
};
q++;
};

q = 0;
while(q < 34)
{
u = 0;
while (u < 4){
i = 0;
if(u == 3){
while (i < 3){
Varray[varray_i] = &amp;fVertices_1[ulQuadIndices_1[q][2]][i];
varray_i++;
i++;
};

};
i = 0;
while (i < 3){
Varray[varray_i] = &amp;fVertices_1[ulQuadIndices_1[q][u]][i];
varray_i++;
i++;
};
i = 0;
if ( u == 3){
while (i < 3){
Varray[varray_i] = &amp;fVertices_1[ulQuadIndices_1[q][0]][i];
varray_i++;
i++;
};};
u++;
};
q++;
};
i = 0;
q = 0;
while(i < varray_i){
while(q < 3){
Narray[i] = 255;
};
};

[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-5.0f, 5.0f, -5.5f, 5.5f, -2.0f, 5.0f);
glMatrixMode(GL_MODELVIEW);
glRotatef(3.0f, 0.0f, 0.0f, 1.0f);

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glVertexPointer(3, GL_FLOAT, 0, Varray);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_FLOAT, 0, Narray);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_POINTS, 0, varray_i);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

dorbie
04-13-2010, 04:29 PM
Please post to the beginners forum and present your post in the form of a question that shows you are at least prepared to put in the effort others would invest responding.

Most of this code is programmatic construction of your application's vertex data and not even related directly to OpenGL.

I'm locking this thread. If you post another like this I will delete it unless you take the time to formulate a question.

You are very welcome to post questions here and we will enthusiastically respond.

dorbie
04-13-2010, 05:04 PM
OK, I've unlocked this because I now see it is a follow-up to your other thread (I have now merged the two threads).

You have a lazy posting style. Formulate a question with each thread you post if you expect an answer.

You do not need to unroll your vertex attributes into a contiguous array, you can use glDrawElements and send the index list with the vertices straight to OpenGL. This has the added benefit of exploiting vertex cache if your data is well constructed.

The GL calls look OK.

It's not clear that your glBindRenderbufferOES is needed, you should set this up at the start and test that it's working with a colored screen clear call etc. You do not need to keep binding it.

Does the colored screen clear work? This is the key to telling if you even have a viable context being displayed, if not then you have setup problems to be addressed before you even start to render geometry which could be anything from context creation to buffer swapping.

smultring
04-25-2010, 11:06 AM
I changed the code a bit and got it running properly




int depth_0 = 0;
int depth_1 = 0;
float retriangle[640000];
long retriangle_count = 0;
unsigned long* localPtr = 0;
int depth_2 = 0;
int depth_3 = 0;
float* mptr_v = 0;
unsigned long* mptr_t = 0;
int x = 0;
unsigned long* mptr_q = 0;
while (depth_0<NUM_MESHES){
mptr_v = (float*)pVertexPointers[depth_0];
mptr_t = (unsigned long*)pTriangleIndexPointers[depth_0];
mptr_q = (unsigned long*)pQuadIndexPointers[depth_0];

while (depth_1 < ulTriangleIndexLengths[depth_0]*3){
localPtr = (unsigned long*)&amp;mptr_t[depth_1];
while(depth_2<3){
x = localPtr[depth_2]*3;
while(depth_3<3){
retriangle[retriangle_count] = mptr_v[x];
retriangle_count++;
x++;
depth_3++;
};
depth_3 = 0;
depth_2++;
};
depth_2 = 0;
depth_1+=3;
};
depth_1 = 0;
while (depth_1 < ulQuadIndexLengths[depth_0]*4){
localPtr = (unsigned long*)&amp;mptr_t[depth_1];
while(depth_2<3){
x = localPtr[depth_2]*3;
while(depth_3<3){
retriangle[retriangle_count] = mptr_v[x];
retriangle_count++;
x++;
depth_3++;
};
depth_3 = 0;
depth_2++;
};
depth_2 = 0;
depth_1+=4;
};
depth_1=0;
depth_0++;
};<