PDA

View Full Version : Rotating and Scaling without gl

Tucan
03-14-2012, 04:33 PM
Hi everyone, I'm kinda new to gl coding, I'm taking computer graphics in university for this topic. My homework is rotating and scaling a pyramid without using gl. Where I can start and how can I accomplish the task. Need help please.

Alfonse Reinheart
03-14-2012, 05:12 PM
Didn't your professor teach you how to do that?

DanielSaucen
03-14-2012, 05:26 PM
Basic algebra can dictate a transformation matrix.

X 0 0 0
Y 3 3 3
Z 0 0 0

matrix addition applied to each vertex can represent a simple transform.
Acquire a book on linear algebra, or google matrix manipulation.

Tucan
03-15-2012, 05:03 AM
@Alfonse, he did mention about that on paper, but not about coding.

@Daniel, I understand the matrix multiplication for rotating and scaling but, then how am I going to create the pyramid and what these matrices represent compared to the pyramid? The part representing a pyramid with a matrix I really don't understand.

Kopelrativ
03-15-2012, 07:21 AM
A pyramid consists of a number of vectors. You can find some good basics about vector maths at http://www.arcsynthesis.org/gltut/Basics/Introduction.html

When you have read that, look at http://content.gpwiki.org/index.php/Matrix_math for information about matrices and transformations.

Finally, if you want to do a program of your own for doing matrix operations, I would recommend to use the glm package. This package supports the same operations as OpenGL.

Tucan
03-15-2012, 03:57 PM
So correct me if I'm wrong, I'm going to create a 4X3 matrix, which will hold the vertices of the pyramid and a function to draw the lines between them. Then when I need to rotate or scale, I multiply or do the needed calculation to this matrix and redraw the pyramid in every time?

tonyo_au
03-15-2012, 06:34 PM
You don't seem to understand matrices, try reading this

Tucan
03-28-2012, 06:05 PM
Now, I had some other assignments but finally find time to work on this. I read all your replies and tried to make something, gonna post it here so you can have a look and leave a comment.

First I defined the values that will keep the rotation information like this:
static float yaxis[33][3]= {{2,0,0},{1.75,0,-0.9682},{1.50,0,-1.323},{1.25,0,-1.561},
{1,0,-1.732},{0.75,0,-1.854},{0.50,0,-1.936},{0.25,0,-1.984},
{0,0,-2},{-0.25,0,-1.984},{-0.50,0,-1.936},{-0.75,0,-1.854},
{-1,0,-1.732},{-1.25,0,-1.561},{-1.50,0,-1.323},{-1.75,0,-0.9682},
{-2,0,0},{-1.75,0,0.9682},{-1.50,0,1.323},{-1.25,0,1.561},
{-1,0,1.732},{-0.75,0,1.854},{-0.50,0,1.936},{-0.25,0,1.984},
{0,0,2},{0.25,0,1.984},{0.50,0,1.936},{0.75,0,1.85 4},
{1,0,1.732},{1.25,0,1.561},{1.50,0,1.323},{1.75,0, 0.9682},{2,0,0} };

And defined this to select between the y values
static float yselect[3];
int count = 0;

In the initiation function, I initiate alpha to 0, scale to 1.8 and select y values

yselect[0] = yaxis[0][0];
yselect[1] = yaxis[0][1];
yselect[2] = yaxis[0][2];

while in the idle function I keep an integer for the 1/4 of the rotation, whenever that variable is 25, the scale is incremented and the variable is reset

fre++;
if (fre == 25)
{
scale=scale+0.025;
count++;
fre = 0;
}

And in every frame the y values changes according to the values I defined on top

yselect[0] = yaxis[count %33][0];
yselect[1] = yaxis[count %33][1];
yselect[2] = yaxis[count %33][2];

now in the reshape function, I used gl's lighting function, I dont know whether it is doing something or not but

void reshape (int width, int height)
{
float black[] = { 0, 0, 0, 0 };

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
gluPerspective(60, (float)width/height, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);

gluLookAt(0, 1, 6, 0, 1, 0, 0, 1, 0);

glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHT0);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}

and initiate all in main
void main (int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(512,512);
glutCreateWindow("asd");

init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutVisibilityFunc(visibility);
glutMainLoop();

return;
}

When I execute, the rotation and scaling of the pyramid works fine. However it is a bit fast but that is not important. I dont know how I can code my own lighting function, and use it. So what do you say, how can I make a lighting point source and work on it?

tonyo_au
03-29-2012, 12:27 AM
For lighting to work you need to have a normal at each vertex and look at the lighting functions

glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse_light);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

Tucan
03-29-2012, 11:52 AM
In the assignment it says dont use gl functions for lighting. These count as gl functions right?

tonyo_au
03-29-2012, 10:07 PM
Yes they are. Are you supposed to use shaders? If not, I assume you have to calculate the modified colour at each vertex. You will need to look at the maths for Blinn or Lambert lighting. Just to a quick google there is lots out there.