OpenGL Camera problems

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

OpenGL Camera problems

Post by VoidElite »

Ok guys. I'm pretty new to OpenGL in terms of 3D. I'm currently working on a 3D game called Cuberealm(sandbox game) for my 11th grade coursework and I don't have a damn clue how to setup:
*Co-ordinate area
*Camera position
I've tried Googling but most are for 2D or are just confusing. I was hoping you guys could tell me what's wrong with my code(it compiles but just shows a blue canvas).

Initialize function:

Code: Select all

void Initialize(void){
	printf("Initializing...\n");
	glClearColor(0.0,191.0,255.0,0.0); //aqua
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
	//glEnable(GL_DEPTH_TEST);
	
	player.camera.x=CUBE_UNIT*2;
	player.camera.y=CUBE_UNIT*0;
	player.camera.z=CUBE_UNIT*2;
	player.skinSelected=BLACK;
	setupGrid();
	grid[0].position.x=CUBE_UNIT*2;
	grid[0].position.y=CUBE_UNIT*0;
	grid[0].position.z=CUBE_UNIT*4;
	grid[0].skin=GREEN;
	grid[0].exists=TRUE;
	printf("Initialized...\n");
	return;
}
Draw function:

Code: Select all

void Draw(void){
	glClear(GL_COLOR_BUFFER_BIT);//|GL_DEPTH_BUFFER_BIT);
	printf("Drawing...\n");
	
	setColor(GREEN);
	glBegin(GL_LINES);
		glVertex3f(0.0,0.0,0.0);
		glVertex3f(25.0,0.0,0.0);
	glEnd();
	//renderGrid();\
	renderCube(grid[0]);
	
	glFlush();
	//glutSwapBuffers();
	printf("Drawed...\n");
	return;
}
Here's a screenie of what it does:
Image

Thanks in advance. ;)
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL Camera problems

Post by N64vSNES »

Code: Select all

void Initialize(void){
	printf("Initializing...\n");
	glClearColor(0.0,191.0,255.0,0.0); //aqua
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
	//glEnable(GL_DEPTH_TEST);
1- glClearColor, like all other OpenGL functions do not take 0-255 values. 0-1.

2- You're using the orthographic projection with depth, I can't think of any reason to do this. Use glFrustrum().

3- If you're trying to achieve a 3D camera then why have you commented out glEnable(GL_DEPTH_TEST) ?

4- Once you've got your matrix crap set up then you should return the matrix mode to GL_MODELVIEW

Code: Select all

	
	player.camera.x=CUBE_UNIT*2;
	player.camera.y=CUBE_UNIT*0;
	player.camera.z=CUBE_UNIT*2;
	player.skinSelected=BLACK;
	setupGrid();
	grid[0].position.x=CUBE_UNIT*2;
	grid[0].position.y=CUBE_UNIT*0;
	grid[0].position.z=CUBE_UNIT*4;
	grid[0].skin=GREEN;
	grid[0].exists=TRUE;
	printf("Initialized...\n");
	return;
}
I'm not sure why the camera is part of the player, also I don't know what CUBE_UNIT is but you've set your projection to 0.0,1.0,0.0,1.0,-1.0,1.0 so check those values are correct. Also it's probably doing no harm at all but why are you telling your function to return when it's about to return anyway?

Code: Select all

void Draw(void){
	glClear(GL_COLOR_BUFFER_BIT);//|GL_DEPTH_BUFFER_BIT);
	printf("Drawing...\n");
	
	setColor(GREEN);
	glBegin(GL_LINES);
		glVertex3f(0.0,0.0,0.0);
		glVertex3f(25.0,0.0,0.0);
	glEnd();
	//renderGrid();\
	renderCube(grid[0]);
	
	glFlush();
	//glutSwapBuffers();
	printf("Drawed...\n");
	return;
}
1- What is setColor(GREEN) doing? Maybe showing where you declare setColor() and GREEN would help.

2- The call to renderCube(grid[0]) could have something to do with it, you'll have to post the code for it though.

3- Once again, your viewport is 0.0,1.0,0.0,1.0,-1.0,1.0
and you're using 25.0.

4- Be sure you're not making any matrix transformations before calling this because this will affect it.

Code: Select all

glBegin(GL_LINES);
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(25.0,0.0,0.0);
glEnd();
Also you're claiming to have a camera problem but the only object being rendered (that you've shown us) isn't relative to the camera. Unless you're calling your transformation functions somewhere else but you haven't shown us if that's the case.

And again, why return when it's about to return anyway?


I'm fairly sure you're main issue is (assuming you've shown us it all) you're not switching to the matrix mode to GL_MODELVIEW.
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

Re: OpenGL Camera problems

Post by VoidElite »

N64vSNES wrote:

Code: Select all

void Initialize(void){
	printf("Initializing...\n");
	glClearColor(0.0,191.0,255.0,0.0); //aqua
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
	//glEnable(GL_DEPTH_TEST);
1- glClearColor, like all other OpenGL functions do not take 0-255 values. 0-1.

2- You're using the orthographic projection with depth, I can't think of any reason to do this. Use glFrustrum().

3- If you're trying to achieve a 3D camera then why have you commented out glEnable(GL_DEPTH_TEST) ?

4- Once you've got your matrix crap set up then you should return the matrix mode to GL_MODELVIEW

Code: Select all

	
	player.camera.x=CUBE_UNIT*2;
	player.camera.y=CUBE_UNIT*0;
	player.camera.z=CUBE_UNIT*2;
	player.skinSelected=BLACK;
	setupGrid();
	grid[0].position.x=CUBE_UNIT*2;
	grid[0].position.y=CUBE_UNIT*0;
	grid[0].position.z=CUBE_UNIT*4;
	grid[0].skin=GREEN;
	grid[0].exists=TRUE;
	printf("Initialized...\n");
	return;
}
I'm not sure why the camera is part of the player, also I don't know what CUBE_UNIT is but you've set your projection to 0.0,1.0,0.0,1.0,-1.0,1.0 so check those values are correct. Also it's probably doing no harm at all but why are you telling your function to return when it's about to return anyway?

Code: Select all

void Draw(void){
	glClear(GL_COLOR_BUFFER_BIT);//|GL_DEPTH_BUFFER_BIT);
	printf("Drawing...\n");
	
	setColor(GREEN);
	glBegin(GL_LINES);
		glVertex3f(0.0,0.0,0.0);
		glVertex3f(25.0,0.0,0.0);
	glEnd();
	//renderGrid();\
	renderCube(grid[0]);
	
	glFlush();
	//glutSwapBuffers();
	printf("Drawed...\n");
	return;
}
1- What is setColor(GREEN) doing? Maybe showing where you declare setColor() and GREEN would help.

2- The call to renderCube(grid[0]) could have something to do with it, you'll have to post the code for it though.

3- Once again, your viewport is 0.0,1.0,0.0,1.0,-1.0,1.0
and you're using 25.0.

4- Be sure you're not making any matrix transformations before calling this because this will affect it.

Code: Select all

glBegin(GL_LINES);
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(25.0,0.0,0.0);
glEnd();
Also you're claiming to have a camera problem but the only object being rendered (that you've shown us) isn't relative to the camera. Unless you're calling your transformation functions somewhere else but you haven't shown us if that's the case.

And again, why return when it's about to return anyway?


I'm fairly sure you're main issue is (assuming you've shown us it all) you're not switching to the matrix mode to GL_MODELVIEW.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>

//Some useful stuff:
#define STRING char*
#define BOOL int
#define TRUE 1
#define FALSE 0
#define DIRECTION int
//Colors:
#define COLOR int
#define BLACK 0
#define WHITE 1
#define RED 2
#define GREEN 3
#define BLUE 4
#define YELLOW 5
#define ORANGE 6
#define PINK 7
#define PURPLE 8
#define BROWN 9
//Used for axis:
#define FRONT_DIR 0
#define RIGHT_DIR 1
#define BACK_DIR 2
#define LEFT_DIR 3
#define UP_DIR 4
#define DOWN_DIR 5
//Keys:
#define ESCAPE 27
#define CHAT 'c'
#define FORWARD 'w'
#define LEFT 'a'
#define BACK 's'
#define RIGHT 'd'
#define UP 'r'
#define DOWN 'f'
//Special keys:
#define LOOK_UP GLUT_KEY_UP
#define LOOK_DOWN GLUT_KEY_DOWN
#define ROTATE_LEFT GLUT_KEY_LEFT
#define ROTATE_RIGHT GLUT_KEY_RIGHT
#define CREATE 13
#define DESTROY 8
//More useful stuff:
#define CUBE_UNIT 64
#define GRID_WIDTH 100
#define GRID_HEIGHT 100
#define GRID_DEPTH 100
#define GRID_SIZE GRID_WIDTH*GRID_HEIGHT*GRID_DEPTH
#define RESOURCES_FOLDER "Resources"
#define TEXTURES_FOLDER RESOURCES_FOLDER+"/Textures"
#define DATA_FOLDER RESOURCES_FOLDER+"/Data"
#define BMP ".bmp"

typedef struct Vector2i{
	int x,y;
}Vector2i;

typedef struct Vector2f{
	float x,y;
}Vector2f;

typedef struct Vector3i{
	int x,y,z;
}Vector3i;

typedef struct Vector3f{
	float x,y,z;
}Vector3f;

typedef struct Cube{
	Vector3f position;
	COLOR skin;
	BOOL exists;
}Cube;

typedef struct Player{
	Vector3f camera;
	DIRECTION facing;
	int skinSelected;
}Player;

int screenWidth,screenHeight;

Cube grid[GRID_SIZE];
Player player;

void gexit(void){
	//deallocate pointers here
	exit(0);
	return;
}

void setupGrid(){
	//setup grid
	return;
}

void Initialize(void){
	printf("Initializing...\n");
	glClearColor(0.0,191.0,255.0,0.0); //aqua
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
	//glEnable(GL_DEPTH_TEST);
	
	player.camera.x=CUBE_UNIT*2;
	player.camera.y=CUBE_UNIT*0;
	player.camera.z=CUBE_UNIT*2;
	player.skinSelected=BLACK;
	setupGrid();
	grid[0].position.x=CUBE_UNIT*2;
	grid[0].position.y=CUBE_UNIT*0;
	grid[0].position.z=CUBE_UNIT*4;
	grid[0].skin=GREEN;
	grid[0].exists=TRUE;
	printf("Initialized...\n");
	return;
}

void setColor(COLOR color){
	switch(color){
		case BLACK:
			glColor3f(0.0,0.0,0.0);
			break;
		case WHITE:
			glColor3f(255.0,255.0,255.0);
			break;
		case RED:
			glColor3f(255.0,0.0,0.0);
			break;
		case GREEN:
			glColor3f(0.0,255.0,0.0);
			break;
		case BLUE:
			glColor3f(0.0,0.0,255.0);
			break;
		case YELLOW:
			glColor3f(255.0,255.0,0.0);
			break;
		case ORANGE:
			glColor3f(255.0,165.0,0.0);
			break;
		case PINK:
			glColor3f(255.0,20.0,147.0);
			break;
		case PURPLE:
			glColor3f(160.0,32.0,240.0);
			break;
		case BROWN:
			glColor3f(139.0,69.0,19.0);
			break;
	};
	return;
}

void renderCube(Cube cube){
	printf("Rendering cube...\n");
	setColor(cube.skin);
	int _x,_y,_z;
	_x=cube.position.x,_y=cube.position.y,_z=cube.position.z;
	
	glBegin(GL_QUADS); //front
		glVertex3f(_x,_y,_z);
		glVertex3f(_x+CUBE_UNIT,_y,_z);
		glVertex3f(_x+CUBE_UNIT,_y+CUBE_UNIT,_z);
		glVertex3f(_x,_y+CUBE_UNIT,_z);
	glEnd();
	glFlush();
	
	glBegin(GL_QUADS); //back
	glVertex3f(_x,_y,_z+CUBE_UNIT);
	glVertex3f(_x+CUBE_UNIT,_y,_z+CUBE_UNIT);
	glVertex3f(_x+CUBE_UNIT,_y-CUBE_UNIT,_z+CUBE_UNIT);
	glVertex3f(_x,_y-CUBE_UNIT,_z+CUBE_UNIT);
	glEnd();
	glFlush();
	
	glBegin(GL_QUADS); //top
		glVertex3f(_x,_y,_z);
		glVertex3f(_x+CUBE_UNIT,_y,_z);
		glVertex3f(_x+CUBE_UNIT,_y,_z+CUBE_UNIT);
		glVertex3f(_x,_y,_z+CUBE_UNIT);
	glEnd();
	glFlush();
	
	glBegin(GL_QUADS); //bottom
		glVertex3f(_x,_y-CUBE_UNIT,_z);
		glVertex3f(_x+CUBE_UNIT,_y-CUBE_UNIT,_z);
		glVertex3f(_x+CUBE_UNIT,_y-CUBE_UNIT,_z+CUBE_UNIT);
		glVertex3f(_x,_y-CUBE_UNIT,_z+CUBE_UNIT);
	glEnd();
	glFlush();
	
	glBegin(GL_QUADS); //left
		glVertex3f(_x,_y,_z);
		glVertex3f(_x,_y,_z+CUBE_UNIT);
		glVertex3f(_x,_y-CUBE_UNIT,_z+CUBE_UNIT);
		glVertex3f(_x,_y-CUBE_UNIT,_z);
	glEnd();
	glFlush();
	
	glBegin(GL_QUADS); //right
		glVertex3f(_x+CUBE_UNIT,_y,_z);
		glVertex3f(_x+CUBE_UNIT,_y,_z+CUBE_UNIT);
		glVertex3f(_x+CUBE_UNIT,_y-CUBE_UNIT,_z+CUBE_UNIT);
		glVertex3f(_x+CUBE_UNIT,_y-CUBE_UNIT,_z);
	glEnd();
	glFlush();
	
	printf("Rendered cube...\n");
	return;
}

void renderGrid(void){
	printf("Rendering grid...\n");
	int _i;
	_i=0;
	while(_i<=GRID_SIZE){
		if(grid[_i].exists){
			renderCube(grid[_i]);
			_i++;
		}else{
			_i++;
		}
	}
	printf("Rendered grid...\n");
	return;
}

void Draw(void){
	glClear(GL_COLOR_BUFFER_BIT);//|GL_DEPTH_BUFFER_BIT);
	printf("Drawing...\n");
	
	setColor(GREEN);
	glBegin(GL_LINES);
		glVertex3f(0.0,0.0,0.0);
		glVertex3f(25.0,0.0,0.0);
	glEnd();
	//renderGrid();\
	renderCube(grid[0]);
	
	glFlush();
	//glutSwapBuffers();
	printf("Drawed...\n");
	return;
}

void Keyboard(unsigned char key,int x,int y){
	//handle user input
	switch(key){
		case ESCAPE:
			gexit();
			break;
		case FORWARD:
			//TODO: do collision checks
			player.camera.z+=CUBE_UNIT;
			break;
		case LEFT:
			player.camera.x-=CUBE_UNIT;
			break;
		case BACK:
			player.camera.x-=CUBE_UNIT;
			break;
		case RIGHT:
			player.camera.x+=CUBE_UNIT;
			break;
		case UP:
			player.camera.y+=CUBE_UNIT;
			break;
		case DOWN:
			player.camera.y-=CUBE_UNIT;
			break;
	};
	return;
}

void Keyboard2(int key,int x,int y){
	switch(key){
		case LOOK_UP:
			break;
		case LOOK_DOWN:
			break;
		case ROTATE_LEFT:
			break;
		case ROTATE_RIGHT:
			break;
	};
	return;
}

int main(int argc,char *argv[]){
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
	glutInitWindowPosition(10,10);
	glutInitWindowSize(680,480);
	glutCreateWindow("Cuberealm");
	Initialize();
	glutDisplayFunc(Draw);
	glutKeyboardFunc(Keyboard);
	glutSpecialFunc(Keyboard2);
	glutMainLoop();
	gexit();
	return 0;
}
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: OpenGL Camera problems

Post by Falco Girgis »

So basically... you didn't even bother addressing a single thing he said.

All of your color units are incorrect, and all of your coordinates are outside of your viewport to name (restate) a few...

I don't think it matters so much that you didn't change to modelview (could be wrong), because you aren't applying any transformations... But everything you're rendering is still outside of your viewport.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL Camera problems

Post by N64vSNES »

This:
GyroVorbis wrote:So basically... you didn't even bother addressing a single thing he said.
How about you actually try what I said before you posting and assuming nothing I said would work.
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Re: OpenGL Camera problems

Post by Pornomag »

Umm.. I have a question, in your defines why are you defining BOOL as an int??? Wouldn't it make sense to define it as an enum...?
Like this:

Code: Select all

enum BOOL { TRUE, FALSE }
You could typedef it so you don't need to type enum every time..
Also, doesn't C99 come with bool datatypes nowadays? Or am I mistaken?
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: OpenGL Camera problems

Post by qpHalcy0n »

It depends.

In win32, BOOL is 4 bytes. It's typedef'd to INT.
In most implementations, compilers choose to implement the C99 "bool" as a 1 byte type.

This leads to many problems if you're not careful with type consistency when copying data. Use one type consistently or define one thusly.
Post Reply