Languages

Menu
Sites
Language
Unexpected crash while using OpenGL

I have a problem while trying to make game using OpenGL. 

Imagine that I have Object class which hold all information about any game object which should be drawn, like this:

void Object::Init(char* vShader, char* fShader, Vertex verticies[4], std::string texture)
{
    m_shaders = new Shaders();
	m_shaders->Init(vShader, fShader);
	m_vertices[0] = verticies[0];
	m_vertices[1] = verticies[1];
	m_vertices[2] = verticies[2];
	m_vertices[3] = verticies[3];
	m_textures = new Texture(GL_TEXTURE_2D, texture);
	m_textures->Load();

	glGenBuffers(1, &m_VBO);
	glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices, GL_STATIC_DRAW);

	unsigned int Indices[] = { 0, 1, 2, 
							   0, 2, 3
	};

	glGenBuffers(1, m_IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

}

As far as I debug all parts (shaders and texture ) inited succesfully.

Then I have draw function like this:

void Object::Draw(double dt, appdata_s* ad, double offset)
{
    glUseProgram(m_shaders->program);

	glUniformMatrix4fv(ad->idx_mvp, 1, GL_FALSE, ad->mvp);

	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
	glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);

	m_textures->Bind(GL_TEXTURE0);

	u_time = glGetUniformLocation(m_shaders->program, "u_time");
	glUniform1f(u_time, dt);

	u_offset = glGetUniformLocation(m_shaders->program, "u_offset");
	glUniform1f(u_offset, offset);

	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);

	glDisableVertexAttribArray(0);
	glDisableVertexAttribArray(1);
	glUseProgram(0);
}

 

This is draw function from main file:

static void draw_gl(Evas_Object *obj)
{
    appdata_s *ad = (appdata_s *)evas_object_data_get(obj, "ad");
	float model[16], view[16];
	float aspect;
	int rotation;

	if (!ad)
		return;

/*	ad->time_stamp += ad->stride_time_stamp;

	if (ad->time_stamp > 2 * M_PI)
		ad->time_stamp = 0.0f;

	if (ad->stop_count < 0) {
		ad->angle += 1.0f;

		if (ad->angle >= 360.0f)
			ad->angle -= 360.0f;

		float remain = ad->angle - 90.0f * (float) ((int) (ad->angle / 90.0f));

		if (remain > -EPSILON && remain < EPSILON)
			ad->stop_count = 0;
	} else {
		ad->stop_count++;

		if (ad->stop_count == PAUSE_TIME)
			ad->stop_count = -1;
	}*/

	//glUseProgram(ad->program);

	init_matrix(model);
	init_matrix(view);

	rotation = elm_glview_rotation_get(obj);

	if (rotation % 180)
		aspect = (float) ad->glview_h / (float) ad->glview_w;
	else
		aspect = (float) ad->glview_w / (float) ad->glview_h;

	view_set_perspective(view, 60.0f, aspect, 1.0f, 20.0f);

	if (rotation != ad->view_rotation)
		rotate_xyz(view, (rotation - ad->view_rotation), 0.0f, 0.0f, -1.0f);

	translate_xyz(model, 0.0f, 0.0f, -1.9f);
	rotate_xyz(model, ad->angle, 1.0f, 0.0f, 0.0f);

	multiply_matrix(ad->mvp, view, model);

	glViewport(0, 0, ad->glview_w, ad->glview_h);

	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	/*glUniform4fv(ad->idx_light_dir, 1, ad->light_dir);
	glUniformMatrix4fv(ad->idx_mvp, 1, GL_FALSE, ad->mvp);
	glUniform1f(ad->idx_time_stamp, ad->time_stamp);

	glBindBuffer(GL_ARRAY_BUFFER, ad->idx_vbo);
	glVertexAttribPointer(ad->idx_vposition, 3, GL_FLOAT, GL_FALSE,
			sizeof(float) * 6, 0);
	glEnableVertexAttribArray(ad->idx_vposition);
	glVertexAttribPointer(ad->idx_vnormal, 3, GL_FLOAT, GL_FALSE,
			sizeof(float) * 6, (void*) (sizeof(float) * 3));
	glEnableVertexAttribArray(ad->idx_vnormal);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ad->idx_ibo);*/
	//glDrawElements(GL_TRIANGLES, MAX_F_COUNT * 3, GL_UNSIGNED_SHORT, 0);



	/*glDisableVertexAttribArray(ad->idx_vposition);
	glDisableVertexAttribArray(ad->idx_vnormal);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);*/

	//glUseProgram(0);

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glEnable(GL_BLEND);


	bg.Draw(0.05, ad);
	fb_title.Draw(0.05, ad);

	glDisable(GL_BLEND);

	glFlush();

	display_fps();
}

Commented parts - unused template parts which used to draw standart teapot.

And it works fine, if I try to draw 1 object (like bg.Draw()). Everything is ok. But when I trying to draw 2, 3 or more object I faced with unexpected crashes.

Does Gear S2 have some restrictions or memory problems ? Thank you. And Sorry for my english.

Responses

2 Replies
Hermet Park

Seems no problem.

Im sorry to say this, but it's hard to figure it out without crash callstack information,

because if bg or fb_title cause the crash.. who knows?

 

Please share the callstack information when your app is corrupted.

Sukwon Suh

I doubt egl context problem.

Would you please check calling evas_gl_make_current before calling gl methods?