I and my college project partner are doing this project. We write this in c++ and OpenGL 2.1 with the freeglut.lib We turned the cylinder to lay down on the ground by 90 degrees. Now the cylinder is oddly misshaped. How can we fix the misshape? Is this a perspective thing? We wanted to use the cylinder later as wheels for a car which we should animate driving in a circle track. Right now the wheels on the car are only 2D circles.
void draw_cylinder(GLfloat radius,
GLfloat height,
GLubyte R,
GLubyte G,
GLubyte B)
{
GLfloat x = 0.0;
GLfloat y = 0.0;
GLfloat angle = 0.0;
GLfloat angle_stepsize = 0.1;
/** Draw the tube */
glColor3ub(R - 40, G - 40, B - 40);
glBegin(GL_QUAD_STRIP);
angle = 0.0;
while (angle < 2 * PI) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(x, y, height);
glVertex3f(x, y, 0.0);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, height);
glVertex3f(radius, 0.0, 0.0);
glEnd();
/** Draw the circle on top and bottom of cylinder */
glColor3ub(R, G, B);
glBegin(GL_POLYGON);
angle = 0.0;
while (angle < 2 * PI) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(x, y, height);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, height);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, -0.4, -3.0);
//glRotatef(-40, 1.0, 0.0, 0.0);
glRotatef(-200, 1.0, 0.0, 0.0);
draw_cylinder(0.3, 0.5, 255, 160, 100);
glFlush();
}
void reshape(int width, int height)
{
if (width == 0 || height == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLdouble)width / (GLdouble)height,
0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, width, height);
}
int main(int argc, char** argv)
{
/** Initialize glut */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("Create Cylinder");
glClearColor(0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
You need to enable the Depth Test:
glEnable(GL_DEPTH_TEST);
and clear the depth buffer as well
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Example:
float angle = 0.0f;
void display(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, -0.4, -3.0);
glRotatef(angle, 0.0, 1.0, 0.0);
angle += 0.1f;
glRotatef(-200, 1.0, 0.0, 0.0);
draw_cylinder(0.3, 0.5, 255, 160, 100);
glFlush();
glutPostRedisplay();
}
Related
#include <stdio.h>
#include <math.h>
#include "glut.h"
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 400, 0.0, 300.0);
}
void drawTurbine() {
// draw the tower
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(160, 40);
glVertex2f(163, 180);
glVertex2f(160, 40);
glVertex2f(175, 40);
glVertex2f(175, 40);
glVertex2f(172, 180);
glEnd();
// Rotate object
glPushMatrix();
glRotatef(100, 0, 1, 0);
// draw first rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(173, 180);
glVertex2f(163, 180);
glVertex2f(168, 270);
glEnd();
// draw second rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(170, 174);
glVertex2f(175, 180);
glVertex2f(247, 140);
glEnd();
// draw third rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(162, 180);
glVertex2f(167, 174);
glVertex2f(88, 140);
glEnd();
// circle in the middle
float theta;
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i <= 360; i++) {
theta = i * 3.142 / 180;
glVertex2f(168 + 7 * cos(theta), 180 + 6.5 * sin(theta));
}
glEnd();
glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawTurbine();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void idle() {
display();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(700, 600);
glutInitWindowPosition(10, 10);
glutCreateWindow("Wind Turbine");
init();
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
I have created a wind turbine and I am trying to rotate the rotor blades, but the code just makes it disappear. I want it to rotate clockwise. I just need an idea on how I can make it rotate like a real wind turbine. I am new to using glRotatef, and this will be my first time rotating an object with OpenGL.
You need a variable for the current angle of the wind turbine (current_angle) and the rotation angle per frame (step_angle). Further you have to know the center of the wind turbine (center_x, center_y):
float current_angle = 0.0f;
float step_angle = 0.2f;
float center_x = 168.0f;
float center_y = 180.0f;
To rotate around a pivot you have to define a model matrix, which displace by the inverted pivot, then rotates and final transforms back by to the pivot. The rotation axis is the z axis (0, 0, 1) because your geometry is drawn in the xy-plane:
glTranslatef( center_x, center_y, 0.0f );
glRotatef(current_angle, 0, 0, 1);
current_angle += step_angle;
glTranslatef(-center_x, -center_y, 0.0f );
Preview:
Complete code of the function drawTurbine:
void drawTurbine() {
// draw the tower
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(160, 40);
glVertex2f(163, 180);
glVertex2f(160, 40);
glVertex2f(175, 40);
glVertex2f(175, 40);
glVertex2f(172, 180);
glEnd();
// Rotate object
glPushMatrix();
glTranslatef( center_x, center_y, 0.0f );
glRotatef(current_angle, 0, 0, 1);
current_angle += step_angle;
glTranslatef(-center_x, -center_y, 0.0f );
// draw first rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(173, 180);
glVertex2f(163, 180);
glVertex2f(168, 270);
glEnd();
// draw second rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(170, 174);
glVertex2f(175, 180);
glVertex2f(247, 140);
glEnd();
// draw third rotor blade
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(162, 180);
glVertex2f(167, 174);
glVertex2f(88, 140);
glEnd();
// circle in the middle
float theta;
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i <= 360; i++) {
theta = i * 3.142 / 180;
glVertex2f(168 + 7 * cos(theta), 180 + 6.5 * sin(theta));
}
glEnd();
glPopMatrix();
}
I just started learning OpenGL so I have problem with showing the object on the screen.
Display() contains this object and I want to show this one by glutDisplayFunc(Display); but it's not working, I have only grey screen after compiling. (Yeah, this object is wheel)
#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>
GLint Width = 1024, Height = 640;
const int CubeSize = 500;
void Display (void) {
glClearColor (0.7, 0.7, 0.7, 1);
glClear (GL_COLOR_BUFFER_BIT);
glColor3ub (255, 200, 200);
int iTimeElapsed = glutGet(GLUT_ELAPSED_TIME);
float fRevolveScale1 = 0.2f;
float fRevolveScale2 = 0.4f;
long i;
// clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
// push temp state
glPushMatrix();
// translate to center
glTranslatef(0.5f, 0.5f, 0.0);
// rotate around pivot
glRotatef(iTimeElapsed * fRevolveScale1, 0.0, 0.0, 1.0);
// translate to planet location
glTranslatef(0.25f, 0.25f, 0.0);
glRotatef(iTimeElapsed * fRevolveScale2, 0.0, 0.0, 1.0);
glColor3f(0.129412f, 0.129412f, 0.129412f);
glutSolidSphere(0.1f, 16, 16);
// five bolts, step 72 degree (72*5=360 degree)
glColor3f(1.0, 1.0, 1.0);
for (i = 0; i<5; ++i)
{
glPushMatrix();
glRotatef(72.0f*i, 0.0, 0.0, 1.0); // rotate coordinate 72 degree
glTranslatef(0.04f, 0.0, 0.0);// translate on the rotated coordinate
glutSolidSphere(0.01f, 16, 16);
glPopMatrix();
}
glTranslatef(0.0f, 0.0f, 0.0f);// translate on the rotated coordinate
glutSolidSphere(0.01, 16, 16);
// pop temp state
glPopMatrix();
glFlush();
glutPostRedisplay();
glFinish ();
}
void Reshape(GLint w, GLint h) {
Width = w, Height = h;
glViewport (0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}
void Keyboard(unsigned char key, int x, int y) {
const char ESCAPE = '\033 ';
if (key == ESCAPE) exit (0);
}
void main(int argc, char *argv []) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGB);
glutInitWindowSize (Width, Height);
glutCreateWindow("E X A M P L E");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutMainLoop ();
}
I've a problem with my clock. In the first 10 minutes the hour hand doesn't show the correct hour and it changes when the minute hand's change but after 10 minutes it shows correctly. Could you please help me to fix it?
I think there is a problem in these lines but I am not sure:
static void TimeEvent(int te)
{
rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
if (angle > M_TWOPI) angle = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
Here is my whole code:
#include <GL/glut.h>
#include <gl/gl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
GLUquadricObj *Cylinder;
struct tm *newtime;
time_t l_time;
int M_TWOPI=0;
GLfloat rx, ry, rz, angle;
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[]= { 5.0f, 25.0f, 15.0f, 1.0f };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
static int view_state = 1; // Ortho view = 1, Perspective = 0
void newLine(float Start, float End, float angle){
float c = cos(angle), s = sin(angle);
glVertex2f( -8.0f*Start*c, -8.0f*Start*s);
glVertex2f( -8.0f*End*c, -8.0f*End*s);
}
void Sprint( float x, float y, char *st)
{
int l,i;
l=strlen( st );
glRasterPos3f( x, y, -1);
for( i=0; i < l; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]);
}
}
static void TimeEvent(int te)
{
rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
if (angle > M_TWOPI) angle = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
void Draw_clock( GLfloat cx, GLfloat cy, GLfloat cz )
{
int hour_ticks , sec_ticks;
glPushMatrix();
glTranslatef(cx,cy,cz);
glRotatef(180, 1.0, 0.0, 0.0);
glPushMatrix();
glColor3f(1.0, 0.5, 0.0);
glTranslatef( 0, 0, 0.0);
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 2.0);
glPopMatrix();
glRotatef(90, 1.0, 0.0, 0.0);
gluCylinder(Cylinder, 0.75, 0, 4, 16, 16);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 1.0);
glTranslatef( 0, 0, 0.0);
glRotatef( (360/60) * newtime->tm_min, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 3.0);
glScalef(0.5, 0.5, 1.0);
glPopMatrix();
glRotatef( 90, 1.0, 0.0, 0.0);
glutSolidCone (0.5, 6, 6, 16);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef( 0, 0, -0.0);
glRotatef( (360/60) * newtime->tm_sec, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 4.0);
glScalef(0.25, 0.25, 1.0);
glPopMatrix();
glRotatef( 90, 1.0, 0.0, 0.0);
gluCylinder(Cylinder, 0.25, 0, 6, 16, 16);
glPopMatrix();
for(hour_ticks = 0; hour_ticks < 12; hour_ticks++)
{
glPushMatrix();// Draw next arm axis.
glColor3f(0.0, 1.0, 1.0); // give it a color
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/12) * hour_ticks, 0.0, 0.0, 1.0);
glTranslatef( 6.0, 0.0, 0.0);
glEnable(GL_LINE_SMOOTH);
glBegin(GL_LINES);
newLine(0.08f, 0.2f, 0.0f);
glEnd();
glPopMatrix();
}
for(sec_ticks = 0; sec_ticks < 60; sec_ticks++)
{
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/60) * sec_ticks, 0.0, 0.0, 1.0);
glTranslatef(6.0, 0.0, 0.0);
glutSolidCone(1.0, 2.0, 3, 4);
glPopMatrix();
}
glPopMatrix();
}
void num()
{
glColor3f( 0.0, 0.0, 1.0);
Sprint(-6.2,-0.2,"9"); //counting from center
Sprint(-0.2,-6.2,"6");
Sprint(2.8,-5.5,"5");
Sprint(5.0,-3.2,"4");
Sprint(5.0,+2.8,"2");
Sprint(2.8,+5.0,"1");
Sprint(-3.33,+4.95,"11");
Sprint(-0.2,-6.2,"6");
Sprint(-3.2,-5.45,"7");
Sprint(-0.4,5.7,"12");
Sprint(-5.35,-3.25,"8");
Sprint(-5.55,+2.8,"10");
Sprint(5.8,-0.2,"3");
}
void display_clock()
{
time(&l_time); // Get time
newtime = localtime(&l_time); // Convert to local time
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// easy way to put text on the screen.
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-8.0, 8.0, -8.0, 8.0, 1.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 3, " Clock");
Draw_clock( 0.0, 0.0, -14.0);
num();
glutSwapBuffers();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
display_clock();
glFlush();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (50, 50);
glutCreateWindow (argv[0]);
glutSetWindowTitle("Clock");
Cylinder = gluNewQuadric();
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}
You are doing integer arithmetic:
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
should be:
glRotatef(30.0 * newtime->tm_hour + 6.0) * (60.0 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
and so on.
Though, as 360/12 and 360/60 are constants you might as well replace them with the calculated values.
I believe your problem is here:
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
It must be:
glRotatef((360/12) * newtime->tm_hour + (360.0/12 / 60 ) * (newtime->tm_min+1), 0.0, 0.0, 1.0);
Having trouble drawing a heptagon in my OpenGL program. I want to draw a heptagon within a rectangle, so far I can draw the red rectangle but the heptagon isn't showing up.
I don't think I need to convert to degrees unless I wanted to rotate it right? Here is my code:
void CChildView::OnGLDraw(CDC* pDC)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int width, height;
GetSize(width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, // left
1.0, // right
0.0, // bottom
GLdouble(height) / GLdouble(width), // top
1.0, // near
-1.0); // far
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3d(1., 0., 0.);
glBegin(GL_POLYGON); // Rectangle
glVertex2d(0.25, 0.25);
glVertex2d(0.75, 0.25);
glVertex2d(0.75, 0.75);
glVertex2d(0.25, 0.75);
glEnd();
glColor3d(1., 1., 1.);
int numPoints = 7; // Heptagon
double x, y;
double radius = 0;
double centerx = 0;
double centery = 0;
glBegin(GL_POLYGON);
for (int i = 0; i < numPoints; i++)
{
x = centerx + radius * sin(2.0*M_PI*i / numPoints);
y = centery + radius * cos(2.0*M_PI*i / numPoints);
glVertex2d(x, y);
}
glEnd();
glColor3d(0., 1., 0.);
glBegin(GL_LINES);
glVertex2d(m_linefmx, m_linefmx);
glVertex2d(m_linetox, m_linetoy);
glEnd();
}
The issue is that radius is initialized to 0 and never changed, so all points are calculated as (0,0).
#include <GL/glut.h>
GLint winWidth = 600, winHeight = 600;
GLfloat x0 = 100.0, y0 = 100.0, z0 = 50.0;
GLfloat xref = 50, yref = 50.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0;
GLfloat dnear = 25.0, dfar = 125.0;
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
//glMatrixMode(GL_MODELVIEW);
//gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0,1,0,1, 0,0.1);
//gluOrtho2D(0, 1,0,1);
//gluPerspective(45, 1.2, 1, 10);
glFrustum(0, 1, 0, 1, 0, 1);
//gluPerspective(45.0, 1, 1, 15);
}
void displayFcn (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
//glPolygonMode(GL_FRONT, GL_FILL);
//glPolygonMode(GL_BACK, GL_FILL);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.5, 1.0, 0.0);
glEnd();
glFlush();
}
void reshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0,0,newWidth, newHeight);
winWidth = newWidth;
winHeight = newHeight;
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(400,200);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Test");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(reshapeFcn);
glutMainLoop();
}
This is the full source code, you can copy and paste to your VS solution and compile.
You'll need to have glut installed.
The result comes up like this:
The center of the window is 0,0 in opengl. So, when you calculate the vertices, you have to calculate them such that the center of the triangle is 0,0.
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.5, 1.0, 0.0);
glEnd();
those coords will need to be updated, you can find a discussion on finding the centers of triangles at this question: finding center of 2D triangle which sounds like it's from a similar homework assignment.
glTranslatef(-0.5f, -0.5f, 0.0f);
The default perspective for OpenGL is the origin centered and the window x and y ranging from -1 to 1. You can either change this by changing the default viewing volume or changing the coordinates of your triangle.
Either
glTranslatef(-.5f, -.5f, .0f);
or
glBegin(GL_TRIANGLES);
glVertex3f(-.5, -.5, 0.0);
glVertex3f(.50, -.5, 0.0);
glVertex3f(0, .5, 0.0);
glEnd();
will work.
It looks like you're trying to use glFrustum where you want to use glOrtho
glFrustum is supposed to be used for generating a perspective matrix. zNear is never 0. The way you call it right now generates an error GL_INVALID_VALUE, so you get the default projection matrix, the identity.