Related
I am really confused by how gluLookAt and glOrtho or gluPersective work together. Here is the problem.
I draw a 2D triangle and a 2D pentagon in the z-axis of -5.
//pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);
//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);
And Then I define my camera position (0,0,-10) and perspective
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
gluLookAt(0, 0, -10, 0, 0, -200, 0, 1, 0);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
5.2); //The far z clipping coordinate
Based on my understanding, I can see nothing in the scene. Because objects are defined in the -5 z-axis, however, the camera is at -10 z-axis, and it looks into negative z-axis. Thus, the object should be behind the camera. But why I can still see the objects in the scene?
Similarly, I can still see the object when I define my camera at postive 5 at look towards positive z-axis. Why?
Another question is why I can see the objects after I set the far z clipping coordinate to 5?
Anyone can explain this?
My full code:
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
gluLookAt(0, 0, -10, 0, 0, -200, 0, 1, 0);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
5.2); //The far z clipping coordinate
}
//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glBegin(GL_QUADS); //Begin quadrilateral coordinates
//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
glBegin(GL_TRIANGLES); //Begin triangle coordinates
//Pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);
//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);
glEnd(); //End triangle coordinates
glutSwapBuffers(); //Send the 3D scene to the screen
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size
//Create the window
glutCreateWindow("Basic Shapes - videotutorialsrock.com");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
You're saying:
I draw a 2D triangle and a 2D pentagon in the z-axis of -5
...
And Then I define my camera position (0,0,-10) and perspective
If that is really the order you're doing the operations in, then that's the culprit. OpenGL is a command-based API, not a scene graph. Calling glVertex(x, y, z) does not mean "there is a vertex with coordinates x, y, z in the scene." It means "now go and draw a verex on coordinates x, y, z." This means the vertex is transformed by the modelview and projection matrix active at the time of the glVertex() call.
In other words, you issue the vertices with the default modelview and projection matrices, so they get drawn on the screen normally. Then you change the modelview and projection; if you then went on to issue any more vertices, they would be transformed by these new modelview and projection values. But the ones issued previously are already on the screen and unaffected.
In other words, remove these two lines from your draw function:
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
The comments seem to indicate the issue rather well.
The most common setup is to define the projection matrix in the resize hook (as it depends on the aspect ratio), and the view matrix (=camera position and orientation) in the draw function, before issuing any render commands.
As #datenwolf correctly pointed out in comments, this is the common setup for single view rendering. If you have more than one viewport, each of them will probably need a different projection matrix, in which case you would set up the projection matrix along with the view matrix in rendering code, before issuing primitives.
I am using OpenGL apis to obtain isometric view of Rectangle using SSR (Scale/Shear/Rotate) method. I am able to scale and rotate rectangle. But I not getting way as how to shear a rectangle. I am new to OpenGL. Please help.
#include <gl\glut.h> // glut.h must come before gl.h and glu.h
#include <gl\gl.h>
#include <gl\glu.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex2f(20, 20);
glVertex2f(20, 70);
glVertex2f(70, 70);
glVertex2f(70, 20);
glEnd();
glScalef(0.86,0.86,0.86);
glTranslatef(200,0,0);
glRotatef(30, 0, 0,1);
glBegin(GL_QUADS);
glVertex2f(20, 20);
glVertex2f(20, 70);
glVertex2f(70, 70);
glVertex2f(70, 20);
glEnd();
glFlush();
}
void init()
{
glClearColor(0.5,0.5,0.0, 0.0);
glColor3f(1,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, // left
800, // right
800, // bottom
0, // top
0, // zNear
1 // zFar
);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(900, 1080);
glutInitWindowPosition(0, 0);
glutCreateWindow("Simple");
glutDisplayFunc(display);
init();
glutMainLoop();
}
For a shear parallel to the x-axis by the amount shear:
GLfloat m[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
shear, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
glMultMatrixf(m);
and parallel to the y-axis:
GLfloat m[16] = {
1.0f, shear, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
glMultMatrixf(m);
When you compare this to matrices specified in mathematical notation, e.g. the Wikipedia Shear matrix page, keep in mind that OpenGL matrices are specified in column major order.
Both of the above are for transformations in the 2D plane. I.e. the first one leaves the x-axis stationary, and shears the y-axis, while the second one keeps the y-axis stationary, and shears the x-axis. If you look at shear transforms in full 3D space, you get many more variations.
I don't know very much about openGL/glut, but I've used it before successfully for some exceedingly simple things in 2D.
Now I want to be able to draw spheres in 3D. I'm trying to simulate particle collisions, so all I'm really going to need to do on the graphics end is draw spheres.
Here's my abortive attempt
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Set the camera
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glutSwapBuffers();
}
void timerProc(int arg)
{
glutTimerFunc(50,timerProc,0);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glColor3f(0.0,0.0,0.0); //color = black
glPushMatrix();
glTranslated(0,0,0);
glutSolidSphere(.74, 500, 500);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
srand(time(NULL));
init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50,30);
glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)-80,glutGet(GLUT_SCREEN_HEIGHT)-60);
mainWindow=glutCreateWindow("New Window"); //global variable
WIDTH=glutGet(GLUT_WINDOW_WIDTH); //global variable
HEIGHT=glutGet(GLUT_WINDOW_HEIGHT); //global variable
glutDisplayFunc(renderScene);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glutTimerFunc(50,timerProc,0);
glutMainLoop();
return 0;
}
Hopefully all of my problems stem from one really basic mistake...
For some reason, this creates an oval. And, though the oval is pretty big (maybe about an 1/8th of the screen wide and tall), if I lower the radius down to .73 it vanishes, I'm guessing because it's too small to see.
How would I make it so that this sphere would show up circular like you'd expect, and so that as I can see everything that's happening in a given volume, say a 10x10x10 box, the way you would if you were just standing next to a box of particles that were flying around and peering into it, or a reasonable approximation. Right now it's hard to tell what exactly I'm looking at (I know that I'm standing at 1,1,1 and looking at the origin, but it's hard to grasp exactly what I'm seeing)
Also, occasionally when I run it the whole screen is just black. Then when I clean and build and run again it's fine. Not really a huge concern, but annoying, and I'd love to understand what was going on.
Also, when I the number of slices and stacks was lower, it would look fine if the radius was large, but become extremely distorted when the radius was small, which I thought was very strange...
The main problem you are having here is Z clipping. The initial Z range for the scene is (-1, 1) so you only see a part of the actual sphere and by change in its size you go out of z range.
Image
There are several problems I see in the code.
It is good to get a grasp of how the GLUT workflow actually works.
Lets see what the code does wrong.
Main
int main(int argc, char **argv)
{
srand(time(NULL));
init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50, 30);
glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH) - 80,
glutGet(GLUT_SCREEN_HEIGHT) - 60);
mainWindow = glutCreateWindow("New Window"); //global variable
WIDTH = glutGet(GLUT_WINDOW_WIDTH); //global variable
HEIGHT = glutGet(GLUT_WINDOW_HEIGHT); //global variable
glutDisplayFunc(renderScene);
Here you define the display function. It is called every time the window contents has to be invalidated. In this case it is invalidated only at start. The renderScene function does not do anything awesome, just clears the screen. So you get a black screen at the beginning.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
No need for blending at the moment. You can skip that part altogether.
glutTimerFunc(50, timerProc, 0);
Now you set up the timerProc function to be called in 50 milliseconds.
glutMainLoop();
As the documentation states: glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.
return 0;
}
Render Scene
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
This is the only place where you clear the screen. Timer Func does not do this.
glLoadIdentity();
You are reseting the matrices.
// Set the camera
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
Setting up the matrices. (One matrix to be precise)
glutSwapBuffers();
And without drawing anything you swap buffers.
}
Scene rendering function is called each time the window frame has to be redrawn.
Timer
This function does rely on the screen being cleared at first by the renderScene.
void timerProc(int arg)
{
glutTimerFunc(50, timerProc, 0);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
Not clearing this time. Only setting the color.
glColor3f(0.0, 0.0, 0.0); //color = black
glPushMatrix();
glTranslated(0, 0, 0);
glutSolidSphere(.74, 500, 500);
glPopMatrix();
glutSwapBuffers();
}
How to fix it?
Just setup the matrices. With proper Z range.
void resetTransformations() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1000, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
}
void renderScene()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
resetTransformations();
// Just to see some triangles
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0, 0.0, 0.0); //color = black
glPushMatrix();
glTranslated(0, 0, 0);
glutSolidSphere(0.74, 500, 500);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
srand(time(NULL));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50, 30);
glutInitWindowSize(256, 256);
mainWindow = glutCreateWindow("New Window"); //global variable
WIDTH = glutGet(GLUT_WINDOW_WIDTH); //global variable
HEIGHT = glutGet(GLUT_WINDOW_HEIGHT); //global variable
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();
return 0;
}
I am trying to program in OpenGL.
so wrote a test program call t_gl1.cpp
I built it successfully with
$ g++ t_gl1.cpp -lglut -lGL -lGLU -o t_gl1
No any error.
However, if I try to run it, I got
freeglut (./t_gl1): ERROR: Internal error in function fgOpenWindow
X Error of failed request: BadWindow (invalid Window parameter)
Major opcode of failed request: 4 (X_DestroyWindow)
Resource id in failed request: 0x0
Serial number of failed request: 26
Current serial number in output stream: 29
Does any one know what is going on?
Here is the code, tested on Windows and Mac, no problem. But can't get it run on Fedora nor Ubuntu
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //NEW OF THIS
glClearColor(0.7f,0.9f,1.0f,1.0f); //background, last number to be 1.0f
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
200.0); //The far z clipping coordinate
}
float _angle=30.0f; //kinda global variable
//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glTranslatef(0.0f,0.0f,-5.0f);
glPushMatrix();
glRotatef(_angle, 0.0f, 1.0f , 0.0f);
glColor3f(0.5f,0.0f,0.8f);
glBegin(GL_QUADS); //Begin quadrilateral coordinates
//Trapezoid
glVertex3f(-0.7f, -1.5f, 0.0f);
glVertex3f(0.7f, -1.5f, 0.0f);
glVertex3f(0.4f, -0.5f, 0.0f);
glVertex3f(-0.4f, -0.5f, 0.0f);
glEnd(); //End quadrilateral coordinates
glPopMatrix();
glPushMatrix(); //push
glRotatef(_angle, 1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES); //Begin triangle coordinates Begin Pentagon
glColor3f(1.0f,0.0f,0.0f);
//Pentagon
glVertex3f(0.5f, 0.5f, -0.0f);
glVertex3f(1.5f, 0.5f, -0.0f);
glVertex3f(0.5f, 1.0f, -0.0f);
glVertex3f(0.5f, 1.0f, -0.0f);
glVertex3f(1.5f, 0.5f, -0.0f);
glVertex3f(1.5f, 1.0f, -0.0f);
glVertex3f(0.5f, 1.0f, -0.0f);
glVertex3f(1.5f, 1.0f, -0.0f);
glVertex3f(1.0f, 1.5f, -0.0f);
glEnd(); //end Pentagon
glPopMatrix(); //pop
glPushMatrix();
glRotatef(_angle, -1.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
//Triangle
glVertex3f(-0.5f, 0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.5f, -0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 0.5f, -0.0f);
glEnd(); //End triangle coordinates
glPopMatrix();
glutSwapBuffers(); //Send the 3D scene to the screen
}
void update(int value)
{
_angle+=2.0f;
if(_angle>360)
_angle-=360;
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size
//Create the window
glutCreateWindow("window");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene); //display the "drwwScene" most important part, others are settings
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25,update,0); //add the timer function to make animation
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
I aggreed with talonmies.
It was because of the Nvidia problem. I could't find a official way to install Nvidia's developer's driver on Fedora 15. So I used rpmfusion. The driver runs and I can run CUDA. Only GLX related stuff are messed up. However, it also messed up Gnome 3.
So I switched to Arch Linux. With a much clean start, install Nvida and install Xorg according to the ArchWiki. And then install Gnome. Amazingly, OpenGL works. I guess the driver in Arch repo is not the developer's driver. Maybe just for display driver. However, CUDA works.
I am glad with that. And hopefully this will be helpful to some one else who want to run CUDA and OpenGL on Linux.
Thanks,
Alfred
There is nothing wrong with the code. I can successfully compile and run it on a properly configured 64 bit linux system with NVIDIA release 280.13 drivers. You have a driver or X11 driver installation or configuration problem. There is no programming help required for this question.
Since you tagged this question cuda I'm going to assume you're running NVidia hardware.
Try installing some drivers.
HI I am starting to learn openGl for C++.but at stating point I stucked.
I have 2 question that is the coordinates for drawing some objects? I mean where is X, Y and Z?
Second one I am making tutorial from some sites. and I am trying to animate my triangle.In tutorial it works but on my computer not.I Also downloaded source codes but It doesnt move. And there is no compiler error.
Edit:I solved problem. With restarting computer.I think it is poblem about my computer.
Here sample codes. I thougt that problem is glutTimerFunc().
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include<OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <gl/glut.h>
#endif
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = 30.0f;
float _cameraAngle = 0.0f;
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); //Rotate the camera
glTranslatef(0.0f, 0.0f, -5.0f); //Move forward 5 units
glPushMatrix(); //Save the transformations performed thus far
glTranslatef(0.0f, -1.0f, 0.0f); //Move to the center of the trapezoid
glRotatef(_angle, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis
glBegin(GL_QUADS);
//Trapezoid
glVertex3f(-0.7f, -0.5f, 0.0f);
glVertex3f(0.7f, -0.5f, 0.0f);
glVertex3f(0.4f, 0.5f, 0.0f);
glVertex3f(-0.4f, 0.5f, 0.0f);
glEnd();
glPopMatrix(); //Undo the move to the center of the trapezoid
glPushMatrix(); //Save the current state of transformations
glTranslatef(1.0f, 1.0f, 0.0f); //Move to the center of the pentagon
glRotatef(_angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis
glScalef(0.7f, 0.7f, 0.7f); //Scale by 0.7 in the x, y, and z directions
glBegin(GL_TRIANGLES);
//Pentagon
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
glPopMatrix(); //Undo the move to the center of the pentagon
glPushMatrix(); //Save the current state of transformations
glTranslatef(-1.0f, 1.0f, 0.0f); //Move to the center of the triangle
glRotatef(_angle, 1.0f, 2.0f, 3.0f); //Rotate about the the vector (1, 2, 3)
glBegin(GL_TRIANGLES);
//Triangle
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glEnd();
glPopMatrix(); //Undo the move to the center of the triangle
glutSwapBuffers();
}
void update(int value) {
_angle += 2.0f;
if (_angle > 360) {
_angle -= 260;
}
glutPostRedisplay(); //Tell GLUT that the display has changed
//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
//Create the window
glutCreateWindow("Transformations and Timers - videotutorialsrock.com");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(24, update, 0); //Add a timer
glutMainLoop();
return 0;
}
Regarding your first question, you can use gluProject() to apply your transform matrices (via GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX) to arbitrary vertexes.