12 Replies - 3066 Views - Last Post: 07 July 2012 - 05:39 AM Rate Topic: -----

#1 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 02:31 PM

I need to find how to get the offset of a rotation for my OpenGL test. The code they give here
void ComputePositionOffsets(float &fXOffset, float &fYOffset)
{
    const float fLoopDuration = 5.0f;
    const float fScale = 3.14159f * 2.0f / fLoopDuration;
    
    float fElapsedTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
    
    float fCurrTimeThroughLoop = fmodf(fElapsedTime, fLoopDuration);
    
    fXOffset = cosf(fCurrTimeThroughLoop * fScale) * 0.5f;
    fYOffset = sinf(fCurrTimeThroughLoop * fScale) * 0.5f;
}
for the rotation of the triangle won't work for some reason. The xOffset is always .5 and the yOffset always 0. So since I can't get the current location for x or for y or x' or y', I need to find a way to calculate the offset of the rotation.

This post has been edited by dartoscoder: 06 July 2012 - 02:44 PM


Is This A Good Question/Topic? 1
  • +

Replies To: How to find the coordinate offset of a rotation?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 02:52 PM

Are you sure that values are fixed?

It looks to me that in
fXOffset = cosf(fCurrTimeThroughLoop * fScale) * 0.5f;
fYOffset = sinf(fCurrTimeThroughLoop * fScale) * 0.5f;



fCurrTimeThroughLoop will have a value from [0.0f, 5.0f) which will give you varying values for the offsets (unless there is a bug in glutGet(GLUT_ELAPSED_TIME)).

This post has been edited by Skydiver: 06 July 2012 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 02:57 PM

They are supposed to be changing. It is trying to make a triangle rotate. Sorry I probably should have put the website I got that from. Here you go.

Also I don't think there is a bug in glutGet (I'm sure you said that as a joke but) because I tried to do the same thing in sfml with sf::Clock and sf::Time and I got the same result.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 04:40 PM

Yeah, I meant it partly as a joke. The other part was a just a subtle hint to watch multiple passes through the code if you had attached a debugger and was inspecting the values.

Anyway to get the offset of the rotation, you can a subtle refactoring. Change the code to:
void ComputePositionOffsetsForTime(int time, float &fXOffset, float &fYOffset)
{
    const float fLoopDuration = 5.0f;
    const float fScale = 3.14159f * 2.0f / fLoopDuration;
    
    float fElapsedTime = time / 1000.0f;
    
    float fCurrTimeThroughLoop = fmodf(fElapsedTime, fLoopDuration);
    
    fXOffset = cosf(fCurrTimeThroughLoop * fScale) * 0.5f;
    fYOffset = sinf(fCurrTimeThroughLoop * fScale) * 0.5f;
}

void ComputePositionOffsets(float &fXOffset, float &fYOffset)
{
    ComputePositionOffsetsForTime(glutGet(GLUT_ELAPSED_TIME), fXOffset, fYOffset);
}



So if you call:
ComputePositionOffsetsForTime(t0, x0, y0); 
ComputePositionOffsetsForTime(t1, x1, y1); 


You can get the deltas between (x0, y0) and (x1, y1) between the times t0 and t1.
Was This Post Helpful? 0
  • +
  • -

#5 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 05:53 PM

I tried something like that with sfml. I passed in the sf::Time object from the sf::Clock.getElapsedTime() function. Then I did the same operations but the result was the same. The value of fCurrTimeThroughLoop always ends up too small to be changed into a number that would cause cos to be lower than one or sin to be higher than 0. I don't understand why though.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 06:12 PM

Are you sure you are linking the correct C runtime library to compute sinf() and cosf()?

With the following code:
    for(int i = 0; i < 5000; i += 250)
    {
        float x, y;
        ComputePositionOffsetsForTime(i, x, y);
        cout << i << ": " << x << ", " << y << endl;
    }


I'm seeing these values:
0: 0.5, 0
250: 0.475528, 0.154508
500: 0.404509, 0.293892
750: 0.293893, 0.404508
1000: 0.154509, 0.475528
1250: 6.33795e-007, 0.5
1500: -0.154508, 0.475529
1750: -0.293892, 0.404509
2000: -0.404508, 0.293893
2250: -0.475528, 0.15451
2500: -0.5, 1.26759e-006
2750: -0.475529, -0.154507
3000: -0.404509, -0.293891
3250: -0.293894, -0.404507
3500: -0.15451, -0.475528
3750: -1.90139e-006, -0.5
4000: 0.154507, -0.475529
4250: 0.293891, -0.40451
4500: 0.404507, -0.293895
4750: 0.475528, -0.154511


Was This Post Helpful? 0
  • +
  • -

#7 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 07:34 PM

I am using #include <math.h>

but i am not getting those values. xOffset is .5 except for like once every 10 seconds where it becomes .4972 and yOffset is always 0 except for early in the beginning of the program where it is like .3.

I really don't get it.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 07:53 PM

I don't get why you are not using OpenGL functionality like glRotate3f*(), etc. The problem that you are having generally is that OpenGL is using matrices and vectors to compute transformations, and as far as I can see you're not.

You're having so many problems with graphics programming that I personally think it would be best that you actually learn some theory around graphics. You'll never learn cutting and pasting code.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 07:54 PM

So either you keep on calling glutInit() every 5 seconds or less, or ComputePositionOffsets() is only being called every 5 seconds.
Was This Post Helpful? 0
  • +
  • -

#10 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 08:09 PM

View PostButchDean, on 06 July 2012 - 08:53 PM, said:

You're having so many problems with graphics programming that I personally think it would be best that you actually learn some theory around graphics. You'll never learn cutting and pasting code.


Thanks for the input BUT I am not just copying and pasting code. I am taking a class on directX that is teaching how these graphics pipelines work. I am learning OpenGL on my own. This is the only part of all of the code (except for the GLSL stuff) that I am actually copying line for line. I am not having so many problems just many questions that you D.I.C. people are so kind to answer.

I am not using glRotatef because I know how that works and I am just doing these tutorials to learn more about modern OpenGL programming.

This post has been edited by dartoscoder: 06 July 2012 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

#11 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 08:18 PM

View PostSkydiver, on 06 July 2012 - 08:54 PM, said:

So either you keep on calling glutInit() every 5 seconds or less, or ComputePositionOffsets() is only being called every 5 seconds.


The elapsed time is always below 1. So I know it is being called more than once per second. But it still won't work correctly.

I'll post the whole program code here.
#include "stdafx.h"

GLuint program;
GLuint bufferPos[2];
GLuint offsetLocation;
sf::Window* window;

const float vertex[] = 
{
     0.0f,    0.5f, 0.0f, 1.0f,
     0.5f, -0.366f, 0.0f, 1.0f,
    -0.5f, -0.366f, 0.0f, 1.0f,
     1.0f,    0.0f, 0.0f, 1.0f,
     0.0f,    1.0f, 0.0f, 1.0f,
     0.0f,    0.0f, 1.0f, 1.0f
};

const std::string vertexShader
(
	"#version 330\n"
	"layout(location = 0) in vec4 position;\n"
	""
	"layout(location = 1) in vec4 color;\n"
	"smooth out vec4 theColor;\n"
	"uniform vec4 offset;\n"
	"void main()\n"
	"{\n"
	"	vec4 finalOffset = vec4(offset.x, offset.y, 0, 0);\n"
	"	gl_Position = position + finalOffset;\n"
	"	theColor = color;\n"
	"}\n"
);

const std::string fragmentShader(
	"#version 330\n"
	"smooth in vec4 theColor;\n"
	"out vec4 mess;\n"
	"void main()\n"
	"{\n"
	"	mess = theColor;\n"
	"}\n"
);

void positionOffset(float& xOffset, float& yOffset, sf::Time& time)
{
	const float loop = 5.f;
	const float fscale = 3.14159f * 2.f / loop;

	float yirm = time.asMilliseconds() / 1000.f;

	float currTime = fmodf(yirm, loop);

	xOffset = cosf(currTime * fscale) * .5f;
	yOffset = sinf(currTime * fscale) * .5f;
}

void adjustVertexData(float xOffset, float yOffset)
{
	std::vector<float> newData((sizeof(vertex) / sizeof(float)));
	memcpy(&newData[0], vertex, sizeof(vertex));


	for(int i = 0; i < (sizeof(vertex) / sizeof(float)); i += 4)
	{
		newData[i] += xOffset;
		newData[i + 1] += yOffset;
	}

	glBindBuffer(GL_ARRAY_BUFFER, bufferPos[0]);
	glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertex), &newData[0]);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
}

GLuint loadShader(const std::string& filename, GLenum shaderType)
{
	std::fstream stream;
	stream.open(filename.c_str());
	try
	{
		if(!stream.is_open())
		{
			std::exception e("File failed to load");
			throw e;
		}
	}
	catch (std::exception& e)
	{
		fprintf(stderr, "%s", e.what());
		throw;
	}
	std::string file;
	stream >> file;
	stream.close();

	try
	{
		return glutil::CompileShader(shaderType, file.c_str());
	}
	catch (std::exception& e)
	{
		fprintf(stderr, "%s", e.what());
		
		throw;
	}
}

GLuint createShader(GLenum eShaderType, const std::string& strShaderFile)
{
	GLuint shader = glCreateShader(eShaderType);
	const char* strFileData = strShaderFile.c_str();
	glShaderSource(shader, 1, &strFileData, NULL);

	glCompileShader(shader);

	GLint status;
	glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
	if(status == GL_FALSE)
	{
		GLint infoLogLength;
		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
		
		GLchar* log = new GLchar[infoLogLength + 1];
		glGetShaderInfoLog(shader, infoLogLength, NULL, log);
		if(eShaderType == GL_VERTEX_SHADER)
		{
			fprintf(stderr, "Vertex: ");
		}
		else if(eShaderType == GL_FRAGMENT_SHADER)
		{
			fprintf(stderr, "Frag: ");
		}
		fprintf(stderr, "Comple Shader Error:\n %s", log);
		delete[] log;
	}

	return shader;
}

GLuint createProgram(const std::vector<GLuint>& shaderList)
{
	GLuint theProgram = glCreateProgram();

	for(int i = 0; i < shaderList.size(); i++)
	{
		glAttachShader(theProgram, shaderList[i]);
	}

	glLinkProgram(theProgram);

	GLint status;
    glGetProgramiv (program, GL_LINK_STATUS, &status);
    if (status == GL_FALSE)
    {
        GLint infoLogLength;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
        
        GLchar *strInfoLog = new GLchar[infoLogLength + 1];
        glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
        fprintf(stderr, "Linker failure: %s\n", strInfoLog);
        delete[] strInfoLog;
    }

	for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
		glDetachShader(program, shaderList[iLoop]);

	return theProgram;
}

void initProgram()
{
	std::vector<GLuint> shaderList;

	shaderList.push_back(createShader(GL_VERTEX_SHADER, vertexShader));
	shaderList.push_back(createShader(GL_FRAGMENT_SHADER, fragmentShader));

	program = createProgram(shaderList);

	std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);

	offsetLocation = glGetUniformLocation(program, "offset");
}

void initBuffer()
{
	glGenBuffers(1, bufferPos);

	glBindBuffer(GL_ARRAY_BUFFER, bufferPos[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STREAM_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void draw(sf::Time& time)
{
	float yOffset = 0.f;
	float xOffset = 0.f;
	positionOffset(xOffset, yOffset, time);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	glUseProgram(program);

	std::cout << xOffset << std::endl;

	glUniform4f(offsetLocation, xOffset, yOffset, 0, 0);

	glBindBuffer(GL_ARRAY_BUFFER, bufferPos[0]);
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)48);

	glDrawArrays(GL_TRIANGLES, 0, 3);

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

void init()
{
	glload::LoadFunctions();
	initProgram();
	initBuffer();
}

int main()
{
	
	sf::ContextSettings settings;
	settings.antialiasingLevel = 8;
	settings.depthBits = 12;
	window = new sf::Window(sf::VideoMode(500, 500, 32), "Hello", sf::Style::Close, settings);
	window->setActive();

	init();

	std::cout << glGetString(GL_VERSION);

	glLoadIdentity();

	glViewport(0, 0, 500, 500);


	sf::Clock time;
	sf::Event ev;
	while(window->isOpen())
	{
		time.restart();

		if(window->pollEvent(ev))
		{
			if(ev.type == sf::Event::Closed)
			{
				window->close();
			}
		}

		sf::Time test = time.getElapsedTime();
		float a = test.asSeconds();
		draw(time.getElapsedTime());

		window->display();
	}

	return 0;
}



and here is the stdafx.h file
#pragma once

#include <Windows.h>
#include <vector>
#include "glload\gll.hpp"
#include "glload\gl_3_3.h"
#include "glload\gl_3_3_comp.h"
#include "glutil\Shader.h"
#include "SFML\Graphics.hpp"
#include <GL/GL.h>
#include <GL\GLU.h>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <math.h>



I thought that this would be a simple enough problem for just that snippit but I guess I messed it up worse than that. Also I know the way this is written is not elegant but its just for my learning so that is why I made it like this.

This post has been edited by dartoscoder: 06 July 2012 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: How to find the coordinate offset of a rotation?

Posted 06 July 2012 - 10:27 PM

Why are you calling time.restart() on line 249 everytime through your render loop?
Was This Post Helpful? 0
  • +
  • -

#13 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Re: How to find the coordinate offset of a rotation?

Posted 07 July 2012 - 05:39 AM

That restarts the SFML clock. You need to call it or the elapsed time will never reset to 0. It will just keep going up.

I tried taking that out and it worked. I then tried making a float that would just take the time.inMilliseconds() / 1000.f value and just add it every loop with clock.restart() and it worked too.

I guess it wanted total time not deltaTIme.

Thanks for the help everyone.

This post has been edited by dartoscoder: 07 July 2012 - 05:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1