Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

uzipaz

Member Since 07 Jul 2011
Offline Last Active May 20 2013 03:56 PM
-----

Topics I've Started

File input, get stream pointer's position

25 September 2012 - 07:57 AM

Hi, I'm writing a program where I have to manipulate the stream pointer in an input file. I wrote a testing program, I wanted to test if I get the right pointer position in a file where I have written few strings in a single line.

The contents of the file are "hello world foo(int) a=b".
The code is
#include <iostream>
using namespace std;
#include <fstream>
int main()
{
	ifstream Infile;
	char c;
	Infile.open("Input.txt");
	if (Infile.is_open())
	{
		Infile.seekg(0, ios::beg);
		while (!Infile.eof())
		{
			cout << Infile.tellg() << ' ';
			Infile.get(c);
		}
	}
	return 0;
}
The output of the program is
0 2 4 6 8 10 12 14 16 18 20 22 24 26

However, should'nt it be
0 1 2 3 4 5 6 7 8 9 10 11 12 13 and so on....

Because I am reading one character at a time and from what I know, the get(char) function also reads whitespaces and endlines...

I don't understand why the position pointer is being incremented by two and not one!!? However if manipulate the pointer using seekg() func, it returns the character at that position but If i use tellg() and just read input, then it increments the pointer by two...
Any clues?

Camera and Viewing Transformations

18 August 2012 - 09:44 PM

First of all, I am aware that there is no such entity called camera in OpenGL. The eye is located at the origin looking down the negative z-axis with the up vector being the positive y-axis (please correct me if I am wrong here or anywhere).

So here's the deal, I am trying to make a class that will handle the view of the scene using keyboard input. Just like in an aircraft, there is thrust which causes the camera to move forward(if we are sitting in a cockpit). Let us say, I am using W and S keys to control the thrust of this hypothetical camera and A and D keys to control my heading(the direction at which I am looking) and Up and Down keys to control the Pitch and Left and Right to control Roll.

If you have ever played Descent, there are actually ten ways to control the ship. Thrust forward, backward.. slide left, right, slide up and down, Pitch up and down, and roll left and right.

This is what I am trying to do here. By using basic opengl transformation functions I want to build my own camera class that I can control the view the scene very easily and how I want to view it.

Here is the code so far that I was able to do:
#ifndef CAMERA_H_INCLUDED
#define CAMERA_H_INCLUDED
#include <SDL.h>
#include <GLEW\glew.h>
#include <math.h>
#include "Misc.h"
const GLfloat INCREMENT_HEADING = 0.1f;
const GLfloat INCREMENT_PITCH = 0.1f;
const GLfloat INCREMENT_ROLL = 0.1f;
const GLfloat INCREMENT_FORWARD = 1.0f;
// From Right to Left, in Bits, the keys are
// right left down up d a s w
class EyeCam
{
    private:
    GLfloat Angle_Heading;
    GLfloat Angle_Pitch;
    GLfloat Angle_Roll;
    Uint8 KeyState;
    public:
    GLfloat eyeX, eyeY, eyeZ;
    EyeCam(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat aimX, GLfloat aimY, GLfloat aimZ);
    void onEvent(SDL_Event* event);
    void update();
};
#endif // CAMERA_H_INCLUDED


#include "EyeCam.h"
#include <iostream>
EyeCam::EyeCam(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat aimX, GLfloat aimY, GLfloat aimZ)
{
    this->eyeX = eyeX;
    this->eyeY = eyeY;
    this->eyeZ = eyeZ;
    Angle_Heading = Misc::toDegrees(atan2f((eyeX - aimX), (eyeZ - aimZ)));
    GLfloat base = sqrt(pow(eyeZ-aimZ, 2) + pow(eyeX - aimX, 2));
    Angle_Pitch = Misc::toDegrees(atan2f( -(eyeY - aimY), base));
    //Angle_Roll = 0.0;
    KeyState = 0x00;
}


void EyeCam::onEvent(SDL_Event* event)
{
    if (event->type == SDL_KEYDOWN)
    {
	    if (event->key.keysym.sym == SDLK_RETURN)
	    {
		    system("cls");
		    std::cout << "X: " << eyeX << '\n';
		    std::cout << "Y: " << eyeY << '\n';
		    std::cout << "Z: " << eyeZ << "\n\n";
		    std::cout << "Heading: " << Angle_Heading << '\n';
		    std::cout << "Pitch: " << Angle_Pitch << '\n';
		    //std::cout << "Roll: " << Angle_Roll << '\n';
	    }
	    if (event->key.keysym.sym == SDLK_w)
		    KeyState = KeyState | 0x01;
	    if (event->key.keysym.sym == SDLK_s)
		    KeyState = KeyState | 0x02;
	    if (event->key.keysym.sym == SDLK_a)
		    KeyState = KeyState | 0x04;
	    if (event->key.keysym.sym == SDLK_d)
		    KeyState = KeyState | 0x08;
	    if (event->key.keysym.sym == SDLK_UP)
		    KeyState = KeyState | 0x10;
	    if (event->key.keysym.sym == SDLK_DOWN)
		    KeyState = KeyState | 0x20;
	    if (event->key.keysym.sym == SDLK_LEFT)
		    KeyState = KeyState | 0x40;
	    if (event->key.keysym.sym == SDLK_RIGHT)
		    KeyState = KeyState | 0x80;
    }
    else if (event->type == SDL_KEYUP)
    {
	    if (event->key.keysym.sym == SDLK_w)
		    KeyState = KeyState & 0xFE;
	    if (event->key.keysym.sym == SDLK_s)
		    KeyState = KeyState & 0xFD;
	    if (event->key.keysym.sym == SDLK_a)
		    KeyState = KeyState & 0xFB;
	    if (event->key.keysym.sym == SDLK_d)
		    KeyState = KeyState & 0xF7;
	    if (event->key.keysym.sym == SDLK_UP)
		    KeyState = KeyState & 0xEF;
	    if (event->key.keysym.sym == SDLK_DOWN)
		    KeyState = KeyState & 0xDF;
	    if (event->key.keysym.sym == SDLK_LEFT)
		    KeyState = KeyState & 0xBF;
	    if (event->key.keysym.sym == SDLK_RIGHT)
		    KeyState = KeyState & 0x7F;
    }
}
void EyeCam::update()
{
    if (KeyState & 0x01) // w
    {
	    eyeX = eyeX - INCREMENT_FORWARD * sin(Misc::toRadians(Angle_Heading));
	    eyeY = eyeY + INCREMENT_FORWARD * sin(Misc::toRadians(Angle_Pitch));
	    eyeZ = eyeZ - INCREMENT_FORWARD * cos(Misc::toRadians(Angle_Heading));
    }
    if (KeyState & 0x02) // s
    {
	    eyeX = eyeX + INCREMENT_FORWARD * sin(Misc::toRadians(Angle_Heading));
	    eyeY = eyeY - INCREMENT_FORWARD * sin(Misc::toRadians(Angle_Pitch));
	    eyeZ = eyeZ + INCREMENT_FORWARD * cos(Misc::toRadians(Angle_Heading));
    }
    if (KeyState & 0x04) // a
    {
        Angle_Heading = Angle_Heading + INCREMENT_HEADING;
    }
    if (KeyState & 0x08) // d
    {

          Angle_Heading = Angle_Heading - INCREMENT_HEADING;
	}
    if (KeyState & 0x10) // Up
    {
	    Angle_Pitch = Angle_Pitch + INCREMENT_PITCH;
    }
    if (KeyState & 0x20)
    {
	    Angle_Pitch = Angle_Pitch - INCREMENT_PITCH;
    }
    if (KeyState & 0x40) // Left
    {
	    Angle_Roll = Angle_Roll + INCREMENT_ROLL;
    }
    if (KeyState & 0x80) // Right
    {
	    Angle_Roll = Angle_Roll - INCREMENT_ROLL;
    }
    glRotatef(-Angle_Pitch, 1.0, 0, 0);
    glRotatef(-Angle_Heading, 0, 1.0, 0);
    glTranslatef(-eyeX, -eyeY, -eyeZ);
    glRotatef(-Angle_Roll, 0.0, 0.0, 1.0);
}


The constructor of the class takes the same arguments as gluLookAt() function, except for the Up vector (I've not implemented it yet in my class). The position of the eye in x, y, z coordinates and a point of aim in x, y, z coordinates.

In the constructor, I am calculating two angles, the Heading and the Pitch by using spherical trigonometry and then using these angles to rotate the view accordingly. Please note that, I am not using gluLookAt() function at all in my class.

Then, by using keyboard commands, I am just making changes to the angles to adjust my aim and calculating eye coordinates to adjust my position.

Now, the problem I am having here is that, If I am originally looking down the -ve z- axis and my up is +ve y axis. then I know to adjust my heading I have to Rotate the scene around the y - axis, if I want to adjust pitch I have to Rotate around the x - axis and if I want to adjust roll I have to Rotate around the z-axis. Hence, I am using equation of circle with heading angle to calculate my eyeX and eyeZ and pitch angle to calculate y - axis. But unfortunately, this will only work till I stay in the XZ plane. Any change in the plane, I will not get the desired results.

For example, at the start if I make a 90 degree Roll such that my up now is -ve x-axis, then if I want to Pitch up relative to the camera, I have rotate around the y-axis. Originally I was rotating about the x-axis when roll was 0. Similarly, If I am looking down the -ve z-axis and then roll will perfectly fine because I am rotating around the +ve z axis, however, If I move the camera to aim at the +ve x-axis and then roll it will not work, in that case I have to rotate on the -ve x-axis to make the roll work. I was able to solve this problem by changing the glRotate function for Roll

from:
glRotatef(Angle_Roll, 0, 0, 1);
to:
glRotatef(Angle_Roll, sin(toRadians(Angle_Heading), 0, cos(toRadians(Angle_Heading));

but what if I am looking down the -ve or +ve y-axis, this again will not work as desired and also I try to change the heading then it will automatically affect the roll transformation.

I hope that I am not confusing you guys too much with the my description, please correct me if I am wrong in my assumptions.

So, the bottom line question is, what I need to do make this camera work in every orientation so that it pitchs, rolls, and turns correctly relative to its orientation.

I am not very skilled with mathematics, but I have studied trigonometry and equations of curves and shapes. Please let me know If I need to cover more mathematics in order to implement what I want.

Any input will be appreciated, Thanks.

Updating opengl version

10 August 2012 - 04:52 AM

Hi everyone,
I am learning OpenGL using this book "Addison Weslay OpenGL Programming Guide 7th Edition 2009" aka the red book.
The author in the book has specified he is using OpenGL 3.0 / 3.1 with code and explanation he has presented in the book.

I am using Code::Blocks IDE, I checked my current OpenGL version by using " glGetString(GL_VERSION) " function. Please note that for Window management I am using SDL 1.2 downloaded from the official sdl website. The version I am using as given by the function is 2.1.8781.

The header files gl.h, glu.h came with Code::Blocks IDE, I downloaded the latest version of this IDE, however I downloaded the Opengl32.lib and glu32.lib from a website that I don't remember quite a while ago, also the opengl32.dll file is already placed in my System32 folder (I am using Win7). I am using Laptop that comes with a ATI Mobility Radeon HD 4330, my display drivers are up to date.

Now my concern is that I want to use the OpenGL version specified in the redbook. I went www.opengl.org/registry and I can't find the header files, static library and dynamic library files to download.

Can someone please guide me what I need to do to update OpenGL, which files to download and where?? etc...

Regards.

undefined reference to 'function'

06 August 2012 - 08:38 AM

Hi everyone,
   I am facing a very weird compiling error. I am using OpenGL with code::blocks IDE. However, I don't think that this problem is related to OpenGL at all.

I am using math.h header file. I have an GLfloat variable that contains an angle in degrees. However, when I use this angle for sin() or cos() functions it needs to be in radians. Hence, I created a separate class named "Misc" in a separate header file that has this conversion function

Misc.h
#ifndef MISC_H_INCLUDED
#define MISC_H_INCLUDED
#include <GL\gl.h>
#include <GL\glu.h>
class Misc
{
	public:
		Misc() {}
		static GLfloat toRadians(GLfloat degrees);
		static void foo();
};

#endif // MISC_H_INCLUDED

Misc.cpp
#include "Misc.h"
GLfloat Misc::toRadians(GLfloat degrees)
{
	return degrees * (3.14159265f / 180.0f);
}
void Misc::foo()
{
}

hence, whenever I call these functions anywhere in the program, I get compilation errors of "undefined reference to 'Misc::toRadians(float)' "
and "undefined reference to 'Misc::foo()' "

however, wherever I am calling these functions, I have checked that I am including this header file in that source file....

both Misc.h and Misc.cpp are in the same folder....

However, I noticed that I defined the functions in Misc class itself, then I have no errors, If I define these functions outside the class in the same header file then I get multiple definitions error....

I am completely struck and I can't figure out what I am doing wrong here...?

Thanks for any help, appreciate it....

kinect for windows

01 August 2012 - 06:36 PM

Hi everyone,
I am looking forward to start learning the API that comes with the SDK of Kinect for Windows. I read the starting guide on the microsoft website and I think it did'nt provide enought information such as how much technical details I need to know before getting started...

Just for the heads up... I have learned the C++ language and know how to use its major features well.... and I am using it with different projects... I have some experience using SDL and worked a bit on GLUT.... but I don't have any experience with computer vision or image processing...

I do not own a Kinect device yet... but if I want to start learning the SDK and use it for some development, I need to get the hardware ofcourse...
That is why I created this topic to ask all those programmers out there that at least have some experience developing with kinect... Kindly give me some dope on this before I start to invest my time and money in learning this new API... is it worth it? what I need to know first? where to consult for the tutorials and documentation??? etc...

Any help will be appreciated....
Thank you...

PARTNERS