I am trying to draw a color cube in OpenGL. All I am getting is a square.
(Is only one face of the cube visible in the output? I tried calling gluLookAt(), but that didn't make any difference to the output.)
This is my code:
#include<GL/glut.h>
#include<math.h>
float vertices[8][3]={{0,0,0},{0,0,200},{0,200,0},{200,0,0},{0,200,200},{200,0,200},{200,200,0},{200,200,200}};
float colors[8][3]={{0.6,0.9,0.1},{0.2,0.1,0.3},{0.7,0.7,0.5},{0.2,0.7,0.4},{0.6,0.6,0.4},{0.1,0.1,0.5},{0.7,0.2,0.5},{0.9,0.7,0.4}};
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glEnd();
glFlush();
}
void init()
{
glClearColor(1.0,1.0,1.0,0);
glEnable(GL_DEPTH_TEST);
glOrtho(-960,960,-720,720,-600,600);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(960,720);
glutInitWindowPosition(0,0);
glutCreateWindow("Color Cube");
glutDisplayFunc(display);
init();
glutMainLoop();
}
You are requesting a double-buffered framebuffer via the GLUT_DOUBLE flag. So you have a back buffer you are drawing into, and a front buffer which is shown in the window. When you are finished with drawing, you have to swap those buffers so that the things you have drawn now become visible and you can draw the next frame (without destroying the currently visible image). Just replace that glFlush(); at the end of your display() function by a glutSwapBuffers() call and your rendering should become visible.
Try using push and pop matrix
ex.
glPushMatrix();
//your cube's code
glPopMatrix();
glFlush();
If you want to see more than 1 side of your cube, you will need to rotate your cube.
Look up the glRotate() function, which multiplies the current matrix by a rotation. Applying this rotation (before drawing the cube) will effectively rotate your cube by a given angle, about the specified axis.
Related
I am trying to set the angle of View with gluLookAt()
Here I have my code where I tried to set the camera without results
Here the function displaycone():
void displayCone(void)
{
glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
// clear the identity matrix.
glLoadIdentity();
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-4.5);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
// scaling transfomation
glScalef(1.0,1.0,1.0);
// built-in (glut library) function , draw you a Cone.
// move the peak of the cone to the origin
glTranslatef(0.0, 0.0, -height);
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
gluLookAt(3,3,3,0,0,-4.5,0,1,0);
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}
With my main:
int main (int argc, char **argv)
{
glutInit(&argc, argv);
//double buffering used to avoid flickering problem in animation
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// window size
glutInitWindowSize(400,350);
// create the window
glutCreateWindow("Cone Rotating Animation");
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glClearColor(0.0,0.0,0.0,0.0);
//Assign the function used in events
glutDisplayFunc(displayCone);
glutReshapeFunc(reshapeCone);
glutIdleFunc(idleCone);
//Let start glut loop
glutMainLoop();
return 0;
}
The function idlecone instead changes the values of xRotated, yRotated... and displays the cone. Any ideas?
I am pretty sure I didn't understand the right moment where to use gluLookAt()...
gluLookAt changes the current matrix, similar to glTranslatef or glRotatef.
The operation defines a transformation matrix and multiplies the current matrix by the new transformation matrix.
gluLookAt has to be called before glutSolidCone, e.g.:
void displayCone(void)
{
// set matrix mode
glMatrixMode(GL_MODELVIEW);
// clear model view matrix
glLoadIdentity();
// multiply view matrix to current matrix
gluLookAt(3,3,3,0,0,-4.5,0,1,0); // <----------------------- add
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-4.5);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
// scaling transfomation
glScalef(1.0,1.0,1.0);
// built-in (glut library) function , draw you a Cone.
// move the peak of the cone to the origin
glTranslatef(0.0, 0.0, -height);
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
// gluLookAt(3,3,3,0,0,-4.5,0,1,0); <----------------------- delete
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}
I just want something like this video : https://youtu.be/dGWtdYlryQQ
It shows how to use glTranslate, glRotate, gluOrtho2d in OpenGL ,but it's not guide me anything
In my case, I draw a diamond instead of triangle and here is my condition
condition :
when I press r or R on the keyboard the diamond will rotate clockwise
when I press t or T on the keyboard the diamond will move to the right side
when I press + on the keyboard the diamond will bigger
here is my code :
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>
#include <iostream>
using namespace std;
float angle = 0;
float t,s=0.5,m=0;
void myinit(void){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}
void keyboard(unsigned char key, int x, int y){
if(key==27)
{
exit(0);
}else if(key == 82 || key == 114){
angle-=0.1;
glRotatef(angle,0,0,1);
glutPostRedisplay();
}else if(key == 84 || key == 116 )
{
t+=0.01;
glTranslatef(t,0,0);
glutPostRedisplay();
}else if(key == 43){
s+=0.01;
// m-=0.1;
// glTranslatef(m,m,0.0);
glScalef(s,s,0);
glutPostRedisplay();
}
(void)(x);
(void)(y);
}
void hut(void){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.5,0.4,0.0);
glVertex3f(0.42,0.5,0.0); // GREEN
glVertex3f(0.44,0.5,0.0);
glColor3f(1.5,1.0,0.0);
glVertex3f(0.46,0.5,0.0);
glColor3f(0.25,0.0,0.0);
glVertex3f(0.57,0.5,0.0);
glEnd();
glFlush();
glColor3f(1.5,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.44,0.55,0.0);
glVertex3f(0.42,0.5,0.0);
glVertex3f(0.46,0.5,0.0);
glColor3f(0.25,0.0,0.0);
glVertex3f(0.48,0.55,0.0);
glEnd();
glColor3f(1.5,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.48,0.55,0.0);
glVertex3f(0.46,0.5,0.0);
glVertex3f(0.50,0.5,0.0);
glColor3f(0.25,0.0,0.0);
glVertex3f(0.52,0.55,0.0);
glEnd();
glColor3f(1.5,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.52,0.55,0.0);
glVertex3f(0.50,0.5,0.0);
glVertex3f(0.54,0.5,0.0);
glColor3f(0.25,0.0,0.0);
glVertex3f(0.56,0.55,0.0);
glEnd();
glColor3f(1.5,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.56,0.55,0.0);
glVertex3f(0.54,0.5,0.0);
glVertex3f(0.57,0.5,0.0);
glEnd();
glFlush();
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow("Polygon with viewport");
myinit();
glutDisplayFunc(hut);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
And here is my output : https://drive.google.com/file/d/14HHRiCbOHK9ZSZtDOqSl4GP4aSy7UQLh/view?usp=sharing
It it’s not similar to this https://youtu.be/dGWtdYlryQQ
The operations on the matrix stack are based on one another. The reference system of each operation is the current transformation.
See the documentation of glTranslate:
glTranslate produces a translation by x y z . The current matrix (see glMatrixMode) is multiplied by this translation matrix, with the product replacing the current matrix, [...]
and see the documentation of glRotate:
glRotate produces a rotation of angle degrees around the vector x y z . The current matrix (see glMatrixMode) is multiplied by a rotation matrix with the product replacing the current matrix.
This means that glRotate does a rotation around the origin of the current local system.
While glRotatetf followed by glTranslatef results in:
glTranslatef followed by glRotatef results in:
Since you object is displaced, you have to translate it in that way, that the rotation point is placed in the origin:
glTranslatef(-0.5f, -0.5f, 0.0f);
Then you can rotate it:
glRotatef(angle, 0.0f, 0.0f, 1.0f);
And move it back:
glTranslatef(0.5f, 0.5f, 0.0f);
Note, on the Fixed Function Pipeline stack you have to "push" this operations in the reverse order. Further you should use the GL_MODELVIEW matrix stack. (See glMatrixMode.)
Remove all the matrix operations from the function keyboard and add the following to the function hut:
void hut(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(t, 0.0f, 0.0f);
glTranslatef(0.5f, 0.5f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glScalef(s, s, 0.0f);
glTranslatef(-0.5f, -0.5f, 0.0f);
.....
Further, your object gets destroyed by the aspect ratio of the view. This can be fixed by taking care of the aspect ratio when setting up the projection matrix:
float w = 640.0f;
float h = 480.0f;
glOrtho(0.0,w/h,0.0,1.0,-1.0,1.0);
glRotate rotates about the origin (0,0). Given your projection matrix (that you set with glOrtho) the origin is initially at the lower left corner of your screen, unless you use glTranslate. Your diamond is not centered at the origin, but positioned somewhat away from it. What you need to do is change the vertex values in your void hut(void) method to make the diamond centered at 0,0. Then use glTranslate to move the render origin (and thus also the diamond) to where you want it, then use glRotate.
Well, I have the following C code on a GLUT Project (I use CodeBlocks).
It draw some 2D shapes (something like Robot :p ).
I want to make the whole drawing shapes to be moved with the keyboard arrow keys. I have wrote the following, but for some reason when I push the arrow keys, it seems like it zoom in/out. It does not move.
#include <GL/glut.h>
GLuint head_x1=5, head_y1=30, head_x2=15, head_y2=30, head_x3=15,head_y3=40, head_x4=5,head_y4=40;
// shape
GLuint listID;
void MrRobot(GLsizei displayListID)
{
glNewList(displayListID,GL_COMPILE);
//Save current colour state
glPushAttrib(GL_CURRENT_BIT);
// body
glColor3f(0.5,0.5,0.5);
glBegin(GL_POLYGON);
glVertex2f(0,10);
glVertex2f(20,10);
glVertex2f(20,30);
glVertex2f(0,30);
glEnd();
// head
glColor3f(0,0,1);
glBegin(GL_POLYGON);
glVertex2f(head_x1,head_y1);
glVertex2f(head_x2,head_y2);
glVertex2f(head_x3,head_y3);
glVertex2f(head_x4,head_y4);
glEnd();
// legs
glColor3f(1,0,0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(10,10);
glVertex2f(20,0);
glVertex2f(10,-5);
glVertex2f(0,0);
glEnd();
// right hand
glColor3f(0,1,0);
glBegin(GL_TRIANGLES);
glVertex2f(20,30);
glVertex2f(30,27.5);
glVertex2f(20,25);
glEnd();
// left hand
glColor3f(0,1,0);
glBegin(GL_TRIANGLES);
glVertex2f(-10,27.5);
glVertex2f(0,30);
glVertex2f(0,25);
glEnd();
//Recall saved colour state
glPopAttrib();
glEndList();
}
void display()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
//Defining a modelview transform matrix
glScalef(0.3,0.3,0.3);
//Execute the display list (the modelview matrix will be applied)
glCallList(listID);
glFlush();
}
void keyboard(unsigned char key,int x, int y)
{
printf("\nKeyboard event detected. \nCharacter key: %c\nMouse pointer position: x=%d y=%d",key,x,y);
if (key==GLUT_KEY_UP)
{
head_y1++;
head_y2++;
head_y3++;
head_y4++;
}
if (key==GLUT_KEY_DOWN)
{
head_y1--;
head_y2--;
head_y3--;
head_y4--;
}
if (key==GLUT_KEY_LEFT)
{
head_x1--;
head_x2--;
head_x3--;
head_x4--;
}
if (key==GLUT_KEY_RIGHT)
{
head_x1++;
head_x2++;
head_x3++;
head_x4++;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitWindowPosition(50,50);
glutInitWindowSize(800,600);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("Mr Robot");
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-5,35,-5,30);
listID=glGenLists(1);
MrRobot(listID);
glutDisplayFunc(display);
glutSpecialFunc(keyboard);
glutMainLoop();
return 0;
}
Maybe is something with the axis, but have no idea what I should change.
Any help?
Well there are a lot of problems. You seem to be writing OpenGL a little bit differently.
Anyways here is the problem.
The reason its scaling is because everytime you call display you are calling glScalef(0.3, 0.3, 0.3); without loading the identity matrix.
Also when you press a key the increment is happening but you havent even called MrRobot() in the display function. You have only called it once in the main function
Also just before you call display you must call glMatrixMode(GL_MODELVIEW); or else all transformations you do will affect the GL_PROJECTION matrix
In the OpenGL code below, used for initialization and for the main function, why is the display function getting called twice? I can't see a call that would be executed other than glutDisplayFunc(display);
void init(void)
{
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
glEnable(GL_POINT_SMOOTH);
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
gluLookAt(0.0, 0.0, -5.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.0); /* up is in positive Y direction */
glOrtho(-5,5,-5,5,12,15);
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
}
int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("red 3D lighted cube");
glutInitWindowSize(1280,800 ); // Set the window's initial width & height
//glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
//glutReshapeWindow(800,800);
init();
compute();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Your display() callback is called whenever GLUT decides it wants the application (that's you) to redraw the contents of the window.
Perhaps there is some events happening as the window opens that causes a need to make sure the window is redrawn.
You're not supposed to "care"; just make sure you redraw the content in the display() function and never mind how many times it gets called.
As I am new to OpenGL I want to make operations to be performed on solid objects I made use stencils but dint get the output as expected. can any one help?
Code:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glScalef(zx,zy,zz);
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH);
glEnable(GL_STENCIL_TEST);
glColorMask(false,false,false,false);
glStencilOp(GL_REPLACE,1,1);
glStencilFunc(GL_EQUAL,1,1);
// glRenderMode(GL_FLAT||GL_PROJECTION);
GLUquadricObj *quadratic;
quadratic = gluNewQuadric();
glutSolidCylinder(rtri,.50,15,15);
glColorMask(true,true,true,true);
glStencilFunc(GL_NOTEQUAL,1,1);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
glBegin(GL_QUADS);
//code for cube!!
glFlush();
glutSwapBuffers();
}