i have an assignment to draw a pyramid onto the screen and then rotate after i press the 'k' key in my keyboard.
Everything works ok, except the rotation part as the program doesn't seem to be noting me pressing the key. The code goes as follows:
void changeSize(int w, int h) {
if(h == 0)
h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
// Set perspective
gluPerspective(45.0f ,ratio, 1.0f ,1000.0f);
// return to the model view matrix mode
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void) {
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the camera
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,
0.0,0.0,0.0,
0.0f,2.0f,0.0f);
// put the geometric transformations here
// put drawing instructions here
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-100.0f, 0.0f, 0.0f);
glVertex3f( 100.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -100.0f, 0.0f);
glVertex3f(0.0f, 100.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -100.0f);
glVertex3f(0.0f, 0.0f, 100.0f);
glEnd();
//triangulos da base
glBegin(GL_TRIANGLES);
glColor3f(255.0f,255.0f,255.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glEnd();
//triangulos das faces
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
// End of frame
glutSwapBuffers();
}
// write function to process keyboard events
void rotate (unsigned char key, int x, int y) {
if (key == 'k')
glRotatef(45,1.0,1.0,0.0);
glutPostRedisplay();
}
int main(int argc, char **argv) {
// init GLUT and the window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1200,1200);
glutCreateWindow("CG#DI-UM");
// Required callback registry
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
// put here the registration of the keyboard callbacks
glutKeyboardFunc(rotate);
// OpenGL settings
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// enter GLUT's main cycle
glutMainLoop();
return 0;
}
It seems that my program is not noting me pressing the key even tho i am. Im tried only printing the keys that are being pressed, and that works, so i'm really lost here.
glRotatef seem to have no effect in rotate, because glLoadIdentity() is called at the begin of renderScene. Actually glRotatef changes the current matrix, but when the scene is rendered (renderScene), then glLoadIdentity() loads the Identity matrix to the current matrix.
Anyway it is a bad style to do changes to the current matrix in input event callbacks. Change states and values of variables in the input events and use the variables to set the current model view matrix before the scene is rendered in renderScene.
Add a global variable angle:
float angle = 0.0f;
Change the value of the variable in renderScene. e.g:
void rotate (unsigned char key, int x, int y) {
if (key == 'k') {
angle += 45.0f;
glutPostRedisplay();
}
}
And apply the rotation to the current matrix in renderScene:
void renderScene(void) {
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the camera
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,
0.0,0.0,0.0,
0.0f,2.0f,0.0f);
// apply rotation
glRotatef(angle, 1.0f, 1.0f, 0.0f);
// [...]
}
Related
I'm try to use different function to make various object and call by one function like display() in my Opengl project. But It's not working, when I called two function then show me a function another one is not showing, I don't know why,
I have tried this way:
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
#include <math.h>
void box() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
// Draw a Red 1x1 Square centered at origin
glBegin(GL_POLYGON); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.9f, 0.7f);
glVertex2f(0.9f, 0.7f);
glVertex2f(0.9f, -0.7f); // x, y
glEnd();
glFlush(); // Render now
}
void triangle()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.4f, 0.7f);
glVertex2f(0.1f, -0.7f); // x, y
glEnd();
glFlush(); // Render now
}
void display()
{
box();
triangle();
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(1000, 600);// Set the window's initial width & height
glutCreateWindow("OpenGL Setup Test");
//gluOrtho2D(-0.1,0.7,-0.1,0.3); // Create a window with the given title
//glutInitWindowSize(500, 500);// Set the window's initial width & height
glutDisplayFunc(display);// Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
Any suggestion please.
You are clearing the buffer before rendering each of the shapes, thus erasing the previous shape you just drew. Instead you should clear it only once per display. Same goes with glFlush:
void box() {
// Draw a Red 1x1 Square centered at origin
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.9f, 0.7f);
glVertex2f(0.9f, 0.7f);
glVertex2f(0.9f, -0.7f); // x, y
glEnd();
}
void triangle()
{
glBegin(GL_POLYGON);
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.4f, 0.7f);
glVertex2f(0.1f, -0.7f); // x, y
glEnd();
}
void display()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
box();
triangle();
glFlush(); // Render now
}
I'm having some troubles with OpenGL and multithreading. The thing is I cannot understand why I have this issue.
I'm using MACOS (the includes are therefore going to be different if you try my code).
Here the simple program that I manage to run without any problem.
char title[] = "3D Shapes with animation";
GLfloat anglePyramid = 0.0f; // Rotational angle for pyramid [NEW]
GLfloat angleCube = 0.0f; // Rotational angle for cube [NEW]
int refreshMills = 15; // refresh interval in milliseconds [NEW]
bool serverHasInput = false ;
float matrix[16] = {0.7599139,0.0,-0.65002376,0.0,-0.45789394,0.709777,-0.5353035,0.0,0.4613719,0.70442647,0.5393694,0.0,0.0,0.0,0.0,1.0};
float moveX = 0;
float moveY = 0;
pthread_mutex_t mymutex ;
pthread_cond_t mycondition ;
/* Initialize OpenGL Graphics */
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
}
/* Handler for window-repaint event. Called back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
//while(matrixSet == true){
// sleep(0.01);
//}
pthread_mutex_lock(&mymutex);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
glRotatef(angleCube, 1.0f, 1.0f, 1.0f); // Rotate about (1,1,1)-axis [NEW]
glMultMatrixf(matrix);
pthread_mutex_unlock(&mymutex);
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
// Update the rotational angle after each refresh [NEW]
//anglePyramid += 1.2f;
//angleCube -= 0.15f;
pthread_cond_signal(&mycondition);
pthread_mutex_unlock(&mymutex);
}
/* Called back when timer expired [NEW] */
void timer(int value) {
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(refreshMills, timer, 0); // next timer call milliseconds later
}
/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
void keyPressed (unsigned char key, int x, int y) {
if (key == 'y'){
moveY += 0.5;
cout << "y" << endl ;
}
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char* argv[]) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow(title); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
initGL(); // Our own OpenGL initialization
glutTimerFunc(0, timer, 0); // First timer call immediately [NEW]
glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
glutMainLoop(); // Enter the infinite event-processing loop
}
So this is my basic code that works like a charm.
However, when I'm trying to create a thread to make it run I cannot get theKeyboardInput anymmore as well as some other aspects that are wrong (cannot clic on the X to exit the window for instace).
Here is my code:
My OpenGL functions are all the same except for this part:
/* Main function: GLUT runs as a console application starting at main() */
void* launch(void* args) {
mainArg* arg = (mainArg*) args ;
glutInit(arg->argc, &arg->argv); // Initialize GLUT
And here is how I create the threads:
//the thread function
void *connection_handler(void *);
void * connection_thread(void *);
void * input_thread(void *);
void test();
int main(int argc , char *argv[])
{
//Draw* d = new Draw() ;
pthread_t server_thread ;
pthread_t drawing_thread ;
pthread_t input_thread ;
mainArg args ;
args.argc = &argc ;
args.argv = *argv ;
pthread_mutex_init (&mymutex, NULL);
pthread_cond_init (&mycondition, NULL);
cout << "Begin" << endl ;
if( pthread_create( &server_thread , NULL , &connection_thread , NULL) != 0)
{
cout << "could not create Connection thread" << endl ;
return 1;
}
/*if( pthread_create( &input_thread , NULL , &connection_thread , NULL) != 0)
{
cout << "could not create input thread" << endl ;
return 1;
}*/
if( pthread_create( &drawing_thread , NULL , &launch , &args) != 0)
{
perror("could not create Drawing thread");
return 1;
}
//while(1);
pthread_join(server_thread, NULL);
pthread_join(drawing_thread,NULL);
Would you happen to know what's wrong here? Cause I've been looking at it for days now and I can't seem to find an answer.
EDIT: I am not sharing the OpenGL context or anything that has to be a part of the OGL rendering on an other thread. My other thread is only a server communication with other devices.
EDIT2: I saw here a while ago that not having OGL rendering on the main thread should not be an issue OpenGL Rendering in a secondary thread
So #user1118321 was correct.
There are absolutely no problem to try and do the rendering on a background thread.
However, GLUT will not be able to access the information of the OS window (i.e keyboard press, exit window ... ) if it is done that way. Still, if the purpose of the rendering thread is just to render, no problem at all to put it on a background thread.
Hope it helps other people too.
The idea from this code is to let a windmill like structure to rotate, the problem is that the entire object rotates instead of the windmill fan itself (not the red triangles only). here is the code (I use the keys to control speed)
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
float angle = 0.00002f;
int refreshMills = 30;
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}
void Timer(int value) {
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(refreshMills, Timer, 0); // next Timer call milliseconds later
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f)
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, 0.2f);
glVertex2f(-0.2f, 0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, -0.2f);
glVertex2f(0.2f, -0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, -0.2f)
glVertex2f(-0.2f, -0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, 0.2f);
glVertex2f(0.2f, 0.4f);
glEnd();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
angle=angle+0.000002f;
glutPostRedisplay();
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, -0.6f);
glVertex2f(0.4f, -0.6f);
glEnd();
glFlush();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'a':{
angle+=1;
glutPostRedisplay();
}
case 's':
angle+=2;
glutPostRedisplay();
case 'd':
angle+=3
glutPostRedisplay();
case 'f':
angle=0;
}
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUTx
glutCreateWindow("Windmill"); // Create window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display);
glutTimerFunc(0, Timer, 0);
glutSpecialFunc(specialKeys);
glutKeyboardFunc(keyboard);
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter the event-processing loop
return 0;
}
You need to implement some sort of hierarchy(commonly a scene graph) here that uses transformation matrixes to do your transformations.
Basically, create a "Windmill" object that has its own transformation matrix. Then create a "Windmill Fan" object that has its own. Make the fan a child of the parent. The transformations essentially propagate down. Transform the Windmill, then transform the Windmill Fan.
Post on Scene Graphs
You may also want to check out the Game Development section of stackoverflow. These questions are usually not met with open arms here.
You need to clear your transformations before trying to draw a new frame. So, put a glLoadIdentity() after you glClear function.
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity(); // <-- put it here
glBegin(GL_TRIANGLES);
...
You need to call glRotate before drawing the rotating object. And before that, you need to push the current model view matrix onto the stack with glPushMatrix, then pop it with glPopMatrix before drawing the rest of the windmill.
Once you have a more complex scene you might consider using a scene graph. Note that the matrix functions like glRotate are now deprecated. You might consider switching to e.g. glm as soon as possible.
Try this:
#include <GL/glut.h> // GLUT, include glu.h and gl.h
void Timer(int value)
{
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(16, Timer, 0); // next Timer call milliseconds later
}
float rate = 1.0f;
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
rate=2;
break;
case 's':
rate=3;
break;
case 'd':
rate=4;
break;
case 'f':
rate=0;
break;
}
glutPostRedisplay();
}
float angle = 0.0f;
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// base
glPushMatrix();
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, -0.6f);
glVertex2f(0.4f, -0.6f);
glEnd();
glPopMatrix();
// prop
glPushMatrix();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
angle=angle+rate;
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, 0.2f);
glVertex2f(-0.2f, 0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, -0.2f);
glVertex2f(0.2f, -0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, -0.2f);
glVertex2f(-0.2f, -0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, 0.2f);
glVertex2f(0.2f, 0.4f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUTx
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow("Windmill"); // Create window with the given title
glutDisplayFunc(display);
glutTimerFunc(0, Timer, 0);
glutKeyboardFunc(keyboard);
glutMainLoop(); // Enter the event-processing loop
return 0;
}
It should be rect, which will takes whole width of window. I am expecting this, because frustum width is -5 to 5 and rect size is 10x10 and rect is on same z-axis position like near plnane of frustum. Result is very small and doesn't look like a rect, i don't know why?
void GLWidget::initializeGL() {
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
}
void GLWidget::resizeGL(int w, int h) {
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
double ratio = (double)w/(double)h;
double size = ((10.0/ratio)/2.0);
glFrustum(-5.0, 5.0, -size, size, 10.0, 50.0);
}
void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(5.0f, -5.0f, 0.0f);
glVertex3f(5.0f, 5.0f, 0.0f);
glVertex3f(-5.0f, 5.0f, 0.0f);
glVertex3f(-5.0f, -5.0f, 0.0f);
glEnd();
glFlush();
}
It should be rect, which will takes whole width of window.
Well, then switch your projection to something better suited, an ortho projection, for the task when drawing the rect. Also you must reset your projection matrix to identity before applying frustum or ortho.
Hint: All the code in your resizeGL should go into the paintGL method. I see you're using Qt, so you can get the widget's width and height conveniently using the width and height getter functions. Also glClearColor goes into painGL as well:
void GLWidget::initializeGL() {
}
void GLWidget::resizeGL(int w, int h) {
}
void GLWidget::paintGL()
{
double const ratio = (double)width()/(double)height();
double const size = ((10.0/ratio)/2.0);
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0, width(), height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -size, size, 10.0, 50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
draw_perspective_stuff();
glViewport(0,0, width(), height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// using identity projection, this fills the viewport
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.f, -1.f, 0.0f);
glVertex3f( 1.f, -1.f, 0.0f);
glVertex3f( 1.f, 1.f, 0.0f);
glVertex3f(-1.f, 1.f, 0.0f);
glEnd();
glFlush();
}
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.