c++/SDL2/OpenGL ES - Camera without GLUT on Android

Hello everybody,

My name is Patrick and I’m very new here (beginner with Opengl Es…)

So I develope some thinks for my Android devices with the C4Droid App + SDL2 and gcc plugin :wink:

Now i want create 3d stuff for my Android devices - so i found an cube example and edit it …


-Render A Chunk (with 5x5x5 cubes) + Camera, but i can’t use GLUT ! i dont why …

so here is my code



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengles.h>

static SDL_Window *window;
static SDL_GLContext context;


static void AspectAdjust(int w, int h)
{
	float aspectAdjust;

	aspectAdjust = (4.0f / 3.0f) / ((float)w / h);
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);
	glShadeModel(GL_SMOOTH);
}


// camera code

double  DegToRad(double angleDeg)
{
 return (M_PI * angleDeg / 180.0);
}

struct index
{
  float orientation[2];
  float position[2];
};

float linearSpeed = 0.5f;

void SetCamera()
{
    
   struct index CameraX;
    
  //  float linearSpeed = 0.5f;
    
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	
              glRotatef(CameraX.orientation[0], 1,0,0);
              glRotatef(CameraX.orientation[1], 0,1,0);
              glRotatef(CameraX.orientation[2], 0,0,1);
              glTranslatef(-CameraX.position[0],-CameraX.position[1],CameraX.position[2]);
	
	
}

/* 
   void checkKeyPresses() {

   if (GetAsyncKeyState(VK_LEFT) ) { CAMERA_X.position[0] +=
   sin(DEGTORAD(CAMERA_X.orientation[1]-90)) * linearSpeed;

   CAMERA_X.position[2] += cos(DEGTORAD(CAMERA_X.orientation[1]-90)) *
   linearSpeed; }

   if (GetAsyncKeyState(VK_RIGHT)) { CAMERA_X.position[0] +=
   sin(DEGTORAD(CAMERA_X.orientation[1]+90)) * linearSpeed;
   CAMERA_X.position[2] += cos(DEGTORAD(CAMERA_X.orientation[1]+90)) *
   linearSpeed; }

   if (GetAsyncKeyState(VK_UP)) { CAMERA_X.position[0] +=
   sin(DEGTORAD(CAMERA_X.orientation[1])) * linearSpeed; CAMERA_X.position[2]
   += cos(DEGTORAD(CAMERA_X.orientation[1])) * linearSpeed; }

   if (GetAsyncKeyState(VK_DOWN) ) { CAMERA_X.position[0] -=
   sin(DEGTORAD(CAMERA_X.orientation[1])) * linearSpeed; CAMERA_X.position[2]
   -= cos(DEGTORAD(CAMERA_X.orientation[1])) * linearSpeed; }

   if (GetAsyncKeyState(VK_INSERT)) { linearSpeed += .1; angularSpeed += .1; }
ß
   if (GetAsyncKeyState(VK_DELETE)) { if (!(linearSpeed < 0)) linearSpeed -=
     .1; if (!(angularSpeed < 0)) angularSpeed -= .1; } } 

*/






static void cube()
{

	static GLubyte color[8][4] = {
		{255, 0, 0, 0},
		{255, 0, 0, 255},
		{0, 255, 0, 255},
		{0, 255, 0, 255},
		{0, 255, 0, 255},
		{255, 255, 255, 255},
		{255, 0, 255, 255},
		{0, 0, 255, 255}
	};

	static GLfloat cube[8][3] = {
		{0.125, 0.125, -0.125},
		{0.125f, -0.125f, -0.125f},
		{-0.125f, -0.125f, -0.125f},
		{-0.125f, 0.125f, -0.125f},
		{-0.125f, 0.125f, 0.125f},
		{0.125f, 0.125f, 0.125f},
		{0.125f, -0.125f, 0.125f},
		{-0.125f, -0.125f, 0.125f}
	};



	static GLubyte indices[36] = {
		0, 3, 4,
		4, 5, 0,
		0, 5, 6,
		6, 1, 0,
		6, 7, 2,
		2, 1, 6,
		7, 4, 3,
		3, 2, 7,
		5, 4, 7,
		7, 6, 5,
		2, 3, 1,
		3, 0, 1
	};

	// glRotatef(27.0f,1.0f,1.0f,1.0f);

	glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
	glEnableClientState(GL_COLOR_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, cube);
	glEnableClientState(GL_VERTEX_ARRAY);
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
}


int chunk_x = 15;
int chunk_y = 15;
int chunk_z = 15;

float B_SIZE = 0.25f;

void Render(Uint32 time)
{
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	// glTranslatef(cam_x*0.05f,cam_y*0.05f,cam_z*0.06f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();


	int i, j, k;

	for (i = 0; i < chunk_x; i++)
	{
		for (j = 0; j < chunk_y; j++)
		{
			for (k = 0; k < chunk_z; k++)
			{
				glPushMatrix();
				glTranslatef(j * B_SIZE, i * B_SIZE, k * B_SIZE);
				cube();
				glPopMatrix();
			}
		}
	}


	SetCamera();
}





int main(int argc, char *argv[])
{
	int done;
	SDL_DisplayMode mode;
	SDL_Event event;
	Uint32 then, now, frames;

	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
	{							/* Initialize SDL's Video subsystem */
		SDL_Log("Unable to initialize SDL");
		return 1;
	}

	SDL_GetDesktopDisplayMode(0, &mode);

	SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);

	// Create our window centered
	window = SDL_CreateWindow("GLES example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
							  mode.w, mode.h,
							  SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN);

	// Create our opengl context and attach it to our window
	context = SDL_GL_CreateContext(window);

	SDL_GL_MakeCurrent(window, context);

	SDL_GL_SetSwapInterval(1);


	AspectAdjust(mode.w, mode.h);

	    struct index CameraX;

   // rotation
    CameraX.orientation[0] = 4.0f; // angle -x
    CameraX.orientation[1] = 3.0f; // angle y
    CameraX.orientation[2] = 2.0f; // angle-z

   // translation
    CameraX.position[0] = -2.5f; // x  - left/right
    CameraX.position[1] = -2.5f; // y  - font/back
    CameraX.position[2] = 2.5f; // z  - up/down

    float linearSpeed = 0.5f;


	/* Main render loop */
	frames = 0;
	then = SDL_GetTicks();
	done = 0;
	int i;

	while (!done)
	{
		/* Check for events */
		++frames;
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_KEYDOWN:
				if (event.key.keysym.scancode == SDL_SCANCODE_AC_BACK)
				{
					done = 1;
				}
				break;

			case SDL_MOUSEBUTTONDOWN:
				{
				//	cam_rot--;
				CameraX.position[0] += sin(DegToRad(CameraX.orientation[1]-90)) * linearSpeed;
                        CameraX.position[2] += cos(DegToRad(CameraX.orientation[1]-90)) * linearSpeed;

				}
				break;

			case SDL_MOUSEBUTTONUP:
				{
					//cam_rot = 0.1f;
				}
				break;


			case SDL_WINDOWEVENT:
				switch (event.window.event)
				{
				case SDL_WINDOWEVENT_RESIZED:
					/* Change view port to the new window dimensions */
					AspectAdjust(event.window.data1, event.window.data2);
					/* Update window content */

					Render(SDL_GetTicks());
					SDL_GL_SwapWindow(window);
					break;
				}
			}

		}

		Render(SDL_GetTicks());
		SDL_GL_SwapWindow(window);
	}

	/* Print out some timing information */
	now = SDL_GetTicks();
	if (now > then)
	{
		SDL_Log("%2.2f frames per second
", ((double)frames * 1000) / (now - then));
	}

	SDL_GL_DeleteContext(context);
	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}


my code Works , but i cant move my camera … what’s wrong ??

  • in Main -> case SDL_MOUSEBUTTONDOWN: <- i want use this , because in SDL2 you can use it for your “touchscreen”

plz help me, I’m german, so i’m sorry for my bad english

[ATTACH=CONFIG]992[/ATTACH]

[ATTACH=CONFIG]993[/ATTACH]

[ATTACH=CONFIG]994[/ATTACH]

okey NOW it works :wink:

[ATTACH=CONFIG]1005[/ATTACH]
[ATTACH=CONFIG]1006[/ATTACH]
here is the code



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengles.h>

static SDL_Window *window;
static SDL_GLContext context;


static void AspectAdjust(int w, int h)
{
	float aspectAdjust;

	aspectAdjust = (4.0f / 3.0f) / ((float)w / h);
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);
	glShadeModel(GL_SMOOTH);
}


// camera code


double  DegToRad(double angleDeg)
{
 return (M_PI * angleDeg / 180.0);
}

// ------------

struct index
{
  float orientation[3];
  float position[3];
}CameraX;

//-------------

SDL_Event event;

float linearSpeed();

void SetCamera()
{
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

      glRotatef(CameraX.orientation[0], 1,0,0);
      glRotatef(CameraX.orientation[1], 0,1,0);
      glRotatef(CameraX.orientation[2], 0,0,1);
      glTranslatef(-CameraX.position[0],-CameraX.position[1],CameraX.position[2]);
}

void SetCameracontroller()
{
   // here your controll event ...
    CameraX.orientation[0] += 0.1f;
    CameraX.orientation[1] += 0.1f;
    CameraX.orientation[2] += 0.1f;

    CameraX.position[0] += 0.1f;

}
// -----------



static void cube()
{

	static GLubyte color[8][4] = {
		{255, 0, 0, 0},
		{255, 0, 0, 255},
		{0, 255, 0, 255},
		{0, 255, 0, 255},
		{0, 255, 0, 255},
		{255, 255, 255, 255},
		{255, 0, 255, 255},
		{0, 0, 255, 255}
	};

	static GLfloat cube[8][3] = {
		{0.125, 0.125, -0.125},
		{0.125f, -0.125f, -0.125f},
		{-0.125f, -0.125f, -0.125f},
		{-0.125f, 0.125f, -0.125f},
		{-0.125f, 0.125f, 0.125f},
		{0.125f, 0.125f, 0.125f},
		{0.125f, -0.125f, 0.125f},
		{-0.125f, -0.125f, 0.125f}
	};



	static GLubyte indices[36] = {
		0, 3, 4,
		4, 5, 0,
		0, 5, 6,
		6, 1, 0,
		6, 7, 2,
		2, 1, 6,
		7, 4, 3,
		3, 2, 7,
		5, 4, 7,
		7, 6, 5,
		2, 3, 1,
		3, 0, 1
	};

	// glRotatef(27.0f,1.0f,1.0f,1.0f);

	glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
	glEnableClientState(GL_COLOR_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, cube);
	glEnableClientState(GL_VERTEX_ARRAY);
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
}


int chunk_x = 15;
int chunk_y = 15;
int chunk_z = 15;

float B_SIZE = 0.25f;

void Render(Uint32 time)
{
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	// glTranslatef(cam_x*0.05f,cam_y*0.05f,cam_z*0.06f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();


	SetCameracontroller();
	SetCamera();


	int i, j, k;

	for (i = 0; i < chunk_x; i++)
	{
		for (j = 0; j < chunk_y; j++)
		{
			for (k = 0; k < chunk_z; k++)
			{
				glPushMatrix();
				glTranslatef(j * B_SIZE, i * B_SIZE, k * B_SIZE);
				cube();
				glPopMatrix();
			}
		}
	}

}




int main(int argc, char *argv[])
{
	int done;
	SDL_DisplayMode mode;

	Uint32 then, now, frames;

	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
	{							/* Initialize SDL's Video subsystem */
		SDL_Log("Unable to initialize SDL");
		return 1;
	}

	SDL_GetDesktopDisplayMode(0, &mode);

	SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);

	// Create our window centered
	window = SDL_CreateWindow("GLES example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
							  mode.w, mode.h,
							  SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN);

	// Create our opengl context and attach it to our window
	context = SDL_GL_CreateContext(window);

	SDL_GL_MakeCurrent(window, context);

	SDL_GL_SetSwapInterval(1);

	AspectAdjust(mode.w, mode.h);

	/* Main render loop */
	frames = 0;
	then = SDL_GetTicks();
	done = 0;
	int i;

	while (!done)
	{
		/* Check for events */
		++frames;
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_KEYDOWN:
				if (event.key.keysym.scancode == SDL_SCANCODE_AC_BACK)
				{
					done = 1;
				}
				break;


// Windows Event is for Portrait or Landscape in Android

			case SDL_WINDOWEVENT:
				switch (event.window.event)
				{
				case SDL_WINDOWEVENT_RESIZED:
					/* Change view port to the new window dimensions */
					AspectAdjust(event.window.data1, event.window.data2);
					/* Update window content */

					Render(SDL_GetTicks());
					SDL_GL_SwapWindow(window);
					break;
				}
			}

		}

		Render(SDL_GetTicks());
		SDL_GL_SwapWindow(window);
	}

	/* Print out some timing information */

	now = SDL_GetTicks();
	if (now > then)
	{
		SDL_Log("%2.2f frames per second
", ((double)frames * 1000) / (now - then));
	}

	SDL_GL_DeleteContext(context);
	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}

One Question: -> how can i move forwart in the room ? :stuck_out_tongue_winking_eye:
-> rote works, x,y works, too… but i cant go over y.var in the room

In general you “move” by translating the camera. Can you try to describe in a bit more detail what is not working when you try to do that? In particular I don’t quite understand what you mean with

but i cant go over y.var in the room
? Do you mean your camera does not move beyond a certain point or do you want to actually limit where the camera can go?
Also I’d recommend testing only translating the camera (i.e. comment out the update of the rotation in SetCameracontroller()) as it can be hard to spot individual transformations if multiple happen at the same time.

I dont really understand what you mean and why it is not working especially with the yan part.

A book that helped me a lot to set up some ‘low-level’ things in my render engine that i am working was this http://www.amazon.com/SDL-Game-Development-Black-White/dp/1849696829

What i do with my camera is i rotate the lookAt matrix

What i like to do when i test my camera is to rotate it using a function and not the user input. After this works then i integrate the user input


GLfloat angle = 10.0f;
GLfloat cameraX = sin(glfwGetTime()) * angle;
GLfloat cameraZ = cos(glfwGetTime()) * angle;
glm::mat4 lookAtMatrix = glm::lookAt(/*Camera position*/glm::vec3(camX, 0.0, camZ), /* look at the origin of your coordinate system*/glm::vec3(0.0, 0.0, 0.0), /*Y is the rotational axis*/glm::vec3(0.0, 1.0, 0.0));

hope it helps