White screen Flicker for rendering image - opengl-es-2.0

This is first time posting here, Am new to the openGL. Am trying to display the camera content with fish Eye correction(using fragment shader) . Its work fine but some times white flicker occurred on complete screen and below error code occurred during flicker.
NCGSYS_FrameMemAlloc : AllocateAnyMemoryRegion (Kernel) failed - 138.
from shared memory get the camera data(buf) and process as below
glGenTextures(1, &textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)buf);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glVertexAttribPointer ( positionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vVertices );
glVertexAttribPointer ( texCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );
glEnableVertexAttribArray ( positionLoc );
glEnableVertexAttribArray ( texCoordLoc );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureID);
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
Call eglSwapBuffers().

Related

Open the depth test, but it doesn't work?

m_Program = new GLProgram;
m_Program->initWithFilenames(“shader/gldemo.vsh”, “shader/gldemo.fsh”);
m_Program->link();
//set uniform locations
m_Program->updateUniforms();
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//create vbo
glGenBuffers(1, &vertexVBO);
glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
auto size = Director::getInstance()->getVisibleSize();
float vertercies[] = {
0,0,0.5, //first point
-1, 1,0.5,
-1, -1,0.5,
-0.3,0,0.8,
1, 1,0.8,
1, -1,0.8};
float color[] = { 1, 0,0, 1, 1,0,0, 1, 1, 0, 0, 1,
0, 1,0, 1, 0,1,0, 1, 0, 1, 0, 1};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertercies), vertercies, GL_STATIC_DRAW);
//vertex attribute "a_position"
GLuint positionLocation = glGetAttribLocation(m_Program->getProgram(), "a_position");
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
//set for color
glGenBuffers(1, &colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);
GLuint colorLocation = glGetAttribLocation(m_Program->getProgram(), "a_color");
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
//for safty
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//show
glEnable(GL_DEPTH_TEST);
Open the depth test, but it doesn't work?
It can be displayed normally, but I open the depth test, the red triangle Z is set to 0.5, the blue is set to 0.8, but their rendering order has not changed?
Multiple things:
Enabling depth testing does not mean that the depth testing is set correctly. You have to setup it
You have to make sure there is a depth buffer
You have to enable depth writing and depth testing when rendering your primitives
With openGL, you typically need to call those functions in order to have depth testing working:
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
and to enable depth writing:
glDepthMask(GL_TRUE);
And just searching for opengl enable depth testing on google gave me some neat results. You should look on the multiple resources google have in its index.

OpenGL ES 2.0 program fail on Mali400 platform

I'm writing a simple texture stream rendering program using OpenGL ES 2.0. The program works on desktop but fail on embedded platform with Mali400 GPU. The LCD goes black with the top few lines blinking. I don't know what's wrong with my code. I tried some other OpenGL ES 2.0 programs which are OK, so the problem must lay in my code. Any help will be appreciated. Thanks.
main.c
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <sys/stat.h>
static EGLDisplay display;
static EGLSurface surface;
static void render_target_init(EGLNativeWindowType nativeWindow)
{
assert((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) != EGL_NO_DISPLAY);
EGLint majorVersion;
EGLint minorVersion;
assert(eglInitialize(display, &majorVersion, &minorVersion) == EGL_TRUE);
EGLConfig config;
EGLint numConfigs;
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_NONE
};
assert(eglChooseConfig(display, configAttribs, &config, 1, &numConfigs) == EGL_TRUE);
const EGLint attribList[] = {
EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
EGL_NONE
};
assert((surface = eglCreateWindowSurface(display, config, nativeWindow, attribList)) != EGL_NO_SURFACE);
EGLContext context;
const EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
assert((context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs)) != EGL_NO_CONTEXT);
assert(eglMakeCurrent(display, surface, surface, context) == EGL_TRUE);
}
static GLuint LoadShader(const char *name, GLenum type)
{
FILE *f;
int size;
char *buff;
GLuint shader;
GLint compiled;
const char *source[1];
assert((f = fopen(name, "r")) != NULL);
// get file size
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
assert((buff = malloc(size)) != NULL);
assert(fread(buff, 1, size, f) == size);
source[0] = buff;
fclose(f);
shader = glCreateShader(type);
glShaderSource(shader, 1, source, &size);
glCompileShader(shader);
free(buff);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc(infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
fprintf(stderr, "Error compiling shader %s:\n%s\n", name, infoLog);
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
return shader;
}
static void init_GLES(int width, int height)
{
GLint linked;
GLuint program;
GLuint vertexShader;
GLuint fragmentShader;
assert((vertexShader = LoadShader("vert.glsl", GL_VERTEX_SHADER)) != 0);
assert((fragmentShader = LoadShader("frag.glsl", GL_FRAGMENT_SHADER)) != 0);
assert((program = glCreateProgram()) != 0);
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
GLint infoLen = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc(infoLen);
glGetProgramInfoLog(program, infoLen, NULL, infoLog);
fprintf(stderr, "Error linking program:\n%s\n", infoLog);
free(infoLog);
}
glDeleteProgram(program);
exit(1);
}
glClearColor(0.15f, 0.15f, 0.15f, 0.15f);
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glUseProgram(program);
GLfloat vertex[] = {
1, 0, 0,
0, 1, 0,
-1, 0, 0,
0, -1, 0,
};
GLfloat texcoord[] = {
1, 1,
0, 1,
0, 0,
1, 0,
};
GLushort index[] = {
0, 1, 2,
0, 3, 2,
};
GLuint VBO[3];
glGenBuffers(3, VBO);
GLint pos = glGetAttribLocation(program, "positionIn");
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(GLfloat) * 3, vertex, GL_STATIC_DRAW);
glEnableVertexAttribArray(pos);
glVertexAttribPointer(pos, 3, GL_FLOAT, 0, 0, 0);
GLint tex = glGetAttribLocation(program, "texcoordIn");
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(GLfloat) * 2, texcoord, GL_STATIC_DRAW);
glEnableVertexAttribArray(tex);
glVertexAttribPointer(tex, 2, GL_FLOAT, 0, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLushort), index, GL_STATIC_DRAW);
GLuint texid;
GLint texMap = glGetUniformLocation(program, "texMap");
glUniform1i(texMap, 0); // GL_TEXTURE0
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
assert(glGetError() == 0);
}
#ifdef _X_WINDOW_SYSTEM_
#include <X11/Xlib.h>
static EGLNativeWindowType CreateNativeWindow(void)
{
assert((display = XOpenDisplay(NULL)) != NULL);
int screen = DefaultScreen(display);
Window root = DefaultRootWindow(display);
Window window = XCreateWindow(display, root, 0, 0, 600, 480, 0,
DefaultDepth(display, screen), InputOutput,
DefaultVisual(display, screen),
0, NULL);
XMapWindow(display, window);
XFlush(display);
return window;
}
#endif
int display_init(int width, int height)
{
#ifdef _X_WINDOW_SYSTEM_
render_target_init(CreateNativeWindow());
#else
struct mali_native_window window;
window.width = width;
window.height = height;
render_target_init(&window);
#endif
init_GLES(width, height);
}
void render_frame(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
//glDrawArrays(GL_TRIANGLES, 0, 3);
eglSwapBuffers(display, surface);
}
void update_texture(void *data, int width, int height)
{
static int first_time = 1;
if (first_time) {
printf("create texture %d %d\n", width, height);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
first_time = 0;
}
else {
printf("update texture %d %d\n", width, height);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
}
assert(glGetError() == 0);
}
int main(void)
{
int i;
void *texture[2] = {0};
int fd = open("0.rgb", O_RDONLY);
struct stat st;
fstat(fd, &st);
texture[0] = malloc(st.st_size);
read(fd, texture[0], st.st_size);
close(fd);
fd = open("1.rgb", O_RDONLY);
fstat(fd, &st);
texture[1] = malloc(st.st_size);
read(fd, texture[1], st.st_size);
close(fd);
display_init(600, 480);
for (i = 0; i < 200; i++) {
update_texture(texture[i%2], 720, 576);
render_frame();
usleep(20000);
}
return 0;
}
vert.glsl
attribute vec3 positionIn;
attribute vec2 texcoordIn;
varying vec2 texcoord;
void main()
{
gl_Position = vec4(positionIn, 1);
texcoord = texcoordIn;
}
frag.glsl
precision mediump float;
uniform sampler2D texMap;
varying vec2 texcoord;
void main() {
vec3 color = texture2D(texMap, texcoord).rgb;
gl_FragColor = vec4(color, 1);
}
Did you try to render the frame without the texture ?
Do like gl_FragColor = vec4(1.0,0.0,0.0,1.0); then check it is still black or red.
If red check your texture function.
I think it can be a problem not to call these functions each a frame
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,c_Texture[TEXTURES_FIRST].texture_id);
glUniform1i(h_Texture[TEXTURES_FIRST],1);

OpenGLES adding a projection

I'm started to learn a OpenGLES and currently I'm reading this TUTORIAL
I have reached paragraph Adding a Projection and I'm stuck there:
// Add to render, right before the call to glViewport
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
// Modify vertices so they are within projection near/far planes
const Vertex Vertices[] = {
{{1, -1, -7}, {1, 0, 0, 1}},
{{1, 1, -7}, {0, 1, 0, 1}},
{{-1, 1, -7}, {0, 0, 1, 1}},
{{-1, -1, -7}, {0, 0, 0, 1}}
};
The author uses some variables in populateFromFrustumLeft... and doesn't explain them. I want to understand the logic of variable selection to be able to use this function in future.
Help me plz to understan the logic!

Wavefront OBJ import to OpenGL ES 2.0 problems

I finally finished building my Wavefront OBJ parser, but still have some issues rendering my test object (cube).
So this is how i have parsed my vertices and indices (faces) into arrays of data. I have ignored textures and normals for now.
Vertices:
v -0.307796 0.00433517 0
v 0.299126 0.00433517 0
v 0.299126 0.00433517 0.48337
v -0.307796 0.00433517 0.48337
v -0.307796 0.364153 0.48337
v 0.299126 0.364153 0.48337
v 0.299126 0.364153 0
v -0.307796 0.364153 0
As:
const Vertex Vertices[] = {
{-0.307796,0.00433517,0},
{0.299126,0.00433517,0},
{0.299126,0.00433517,0.48337},
{-0.307796,0.00433517,0.48337},
{-0.307796,0.364153,0.48337},
{0.299126,0.364153,0.48337},
{0.299126,0.364153,0},
{-0.307796,0.364153,0}
};
Faces:
f 7/1/1 3/2/2 2/3/3
f 3/4/4 7/5/5 6/6/6
f 5/7/7 1/8/8 4/9/9
f 1/10/10 5/11/11 8/12/12
f 7/13/13 1/14/14 8/15/15
f 1/16/16 7/17/17 2/18/18
f 3/19/19 5/20/20 4/21/21
f 5/22/22 3/23/23 6/24/24
f 5/25/25 7/26/26 8/27/27
f 7/28/28 5/29/29 6/30/30
f 3/31/31 1/32/32 2/33/33
f 1/34/34 3/35/35 4/36/36
As:
const GLubyte Indices[] = {
7,1,1, 3,2,2, 2,3,3,
3,4,4, 7,5,5, 6,6,6,
5,7,7, 1,8,8, 4,9,9,
1,10,10, 5,11,11, 8,12,12,
7,13,13, 1,14,14, 8,15,15,
1,16,16, 7,17,17, 2,18,18,
3,19,19, 5,20,20, 4,21,21,
5,22,22, 3,23,23, 6,24,24,
5,25,25, 7,26,26, 8,27,27,
7,28,28, 5,29,29, 6,30,30,
3,31,31, 1,32,32, 2,33,33,
1,34,34, 3,35,35, 4,36,36
};
Indices only as vertex positions:
const GLubyte Indices[] = {
7, 3, 2,
3, 7, 6,
5, 1, 4,
1, 5, 8,
7, 1, 8,
1, 7, 2,
3, 5, 4,
5, 3, 6,
5, 7, 8,
7, 5, 6,
3, 1, 2,
1, 3, 4
};
SetupVBO:
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
Renderingcode:
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(sin(CACurrentMediaTime()), 0, -7)];
_currentRotation += displayLink.duration * 90;
[modelView rotateBy:CC3VectorMake(_currentRotation, _currentRotation, 0)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
// 1
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
// 2
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
// 3
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
And the result is pretty much nothing at all, so there must be something that im doing completely wrong.
Furthermore will the indices cause any problems since i have not initialized the normals or texture coords?
The Wavefront obj format says that if you have sequences of 3 in the face definitions then their meaning is:
vertex-index/vertex-texture-index/vector-normal-index
You are reading all of the indices into a single array GLubyte Indices[] and using it as it if was just the indices of the vertices.
If you want to do away with textures and normals, you need to take only the first number of every triplet.
In
f 6/1/1 3/2/2 2/3/3 7/4/4
The face is a quad of vertices of indices [6,3,2,7]. Using the indices array like you have requires that you tell OpenGl that the indices are multiplexed in triplets. It does not look like you do that. It also requires additional buffers for normal and texture coordinates.

opengl es 2 just clrears the screen to default color value when using multiple VAOs

i'm fairly new to opengl es 2 and since i'm coding IOS apps, i thought id try it!
So I loaded up a default opengl program using the GLKit template in xcode 4 and after fumbling around in the provided code I begun modifying it so that I could draw multiple different kinds of vertex array objects...the problem is only the call to glClearColor seems to produce any results-the whole screen is just grey now:-(
I don't know what is wrong since I checked with many glGetError calls at key places in the code and it always seems to return 0...and I also, to the extent of my small knowledge, follow the steps required to draw with VAOs since when I modified the default code initially to display a plane it worked OK...
So i'm going to be very grateful if you guys could help the newbie!:)
here is a posting of the different relevant parts of the code:
//init VAOs:
glGenVertexArraysOES(NUM_VAO, _vertexArrayIDS);
glGenBuffers(NUM_VAO, _vertexBufferIDS);
glGenBuffers(NUM_VAO, _indexBufferIDS);
//init gl object for player:
//setupGLObject(VAO_PLAYER, gCubicVertexData, gCubicIndices, GL_STATIC_DRAW);
glBindVertexArrayOES(_vertexArrayIDS[VAO_PLAYER]);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferIDS[VAO_PLAYER]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gCubicVertexData), gCubicVertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferIDS[VAO_PLAYER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(gCubicIndices), gCubicIndices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(24));
//NSLog(#"glError after player init : %d", glGetError());
//init gl object for player wall:
//setupGLObject(VAO_PWALL, gPlayerWallVertexData, gPlayerWallIndices, GL_DYNAMIC_DRAW);
glBindVertexArrayOES(_vertexArrayIDS[VAO_PWALL]);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferIDS[VAO_PWALL]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gPlayerWallVertexData), gPlayerWallVertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferIDS[VAO_PLAYER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(gPlayerWallIndices), gPlayerWallIndices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(24));
//init gl object for wall/floor plane:
//setupGLObject(VAO_WALL_FLOOR_PLANE, gPlanePyVertexData, gPlanePyIndices, GL_STATIC_DRAW);
glBindVertexArrayOES(_vertexArrayIDS[VAO_WALL_FLOOR_PLANE]);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferIDS[VAO_WALL_FLOOR_PLANE]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gPlanePyVertexData), gPlanePyVertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferIDS[VAO_PLAYER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(gPlanePyIndices), gPlanePyIndices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(24));
glBindVertexArrayOES(0);
drawing function - glkView:(GLKView *)view drawInRect:(CGRect)rect
for (NSString *currentKey in g_renderables) {
GameObject *currentRenderable = [g_renderables objectForKey:currentKey];
if (currentRenderable.go_type == LC_FLOOR) {
glBindVertexArrayOES(_vertexArrayIDS[VAO_WALL_FLOOR_PLANE]);
glUseProgram(_program);
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, currentRenderable.go_mvm.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);
NSLog(#"glError : %d", glGetError());
glDrawElements(GL_TRIANGLE_STRIP, sizeof(gPlanePyIndices)/sizeof(gPlanePyIndices[0]), GL_UNSIGNED_INT, 0);
}
else if (currentRenderable.go_type == LC_WALL) {
glBindVertexArrayOES(_vertexArrayIDS[VAO_WALL_FLOOR_PLANE]);
glUseProgram(_program);
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, currentRenderable.go_mvm.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(gPlanePyIndices)/sizeof(gPlanePyIndices[0]), GL_UNSIGNED_INT, 0);
}
else if (currentRenderable.go_type == LC_PLAYER) {
glBindVertexArrayOES(_vertexArrayIDS[VAO_PLAYER]);
glUseProgram(_program);
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, currentRenderable.go_mvm.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(gCubicIndices)/sizeof(gCubicIndices[0]), GL_UNSIGNED_INT, 0);
}
matrix computations:
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 1000.0f);
for (NSString *currentKey in g_renderables) {
GLKMatrix4 thisMvm = GLKMatrix4Identity;
GameObject *currentRenderable = [g_renderables objectForKey:currentKey];
if (currentRenderable.go_hasVisual) {
thisMvm = GLKMatrix4Translate(thisMvm, currentRenderable.go_origin.x, currentRenderable.go_origin.y, currentRenderable.go_origin.z);
if (currentRenderable.go_type == LC_WALL || currentRenderable.go_type == LC_PLAYER) {
if (currentRenderable.go_type == LC_PLAYER) {
if (currentRenderable.go_orientation == VV_MINUS_X) {
thisMvm = GLKMatrix4RotateY(thisMvm, GLKMathDegreesToRadians(90.0f));
}
else if (currentRenderable.go_orientation == VV_PLUS_X) {
thisMvm = GLKMatrix4RotateY(thisMvm, GLKMathDegreesToRadians(-90.0f));
}
else if (currentRenderable.go_orientation == VV_PLUS_Z) {
thisMvm = GLKMatrix4RotateY(thisMvm, GLKMathDegreesToRadians(180.0f));
}
}
else {
if (currentRenderable.go_orientation == VV_MINUS_X) {
thisMvm = GLKMatrix4RotateZ(thisMvm, GLKMathDegreesToRadians(90.0f));
}
else if (currentRenderable.go_orientation == VV_PLUS_X) {
thisMvm = GLKMatrix4RotateZ(thisMvm, GLKMathDegreesToRadians(-90.0f));
}
else if (currentRenderable.go_orientation == VV_PLUS_Z) {
thisMvm = GLKMatrix4RotateX(thisMvm, GLKMathDegreesToRadians(-90.0f));
}
else if (currentRenderable.go_orientation == VV_MINUS_Z) {
thisMvm = GLKMatrix4RotateX(thisMvm, GLKMathDegreesToRadians(90.0f));
}
}
}
if (currentRenderable.go_type != LC_LINKED_WALL) thisMvm = GLKMatrix4Scale(thisMvm, currentRenderable.width, currentRenderable.height, currentRenderable.depth);
thisMvm = GLKMatrix4Multiply(GLKMatrix4MakeLookAt(g_currentCam.go_origin.x, g_currentCam.go_origin.y, g_currentCam.go_origin.z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f), thisMvm);
//NSLog(#"%f", thisMvm.m[10]);
thisMvm = GLKMatrix4Multiply(projectionMatrix, thisMvm);
currentRenderable.go_mvm = thisMvm;
}
}
That's it.Please help!!!
I think there is a bit of confusion in the way you understood the glclearcolor.
The main idea is that for every frame you should clear at least the color bit by doing a glclear, it is like cleaning a board after you did your drawings.
The glclearcolor instead instructs OpenGL which color should be used for the whole screen after the clear.
An example of this is the following:
glClearColor(1.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
This example tells OpenGL to use the red color when a color of the clear of the color_buffer_bit is required.
I hope this helps in some way.