I just want to do horizontally moving wheel. There's a code but it doesn't work - wheel just moving as a circle, leaving traces. I've tried to glTranslatef(0.0f, moveY, 0.0f); but it doesn't work too.
#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>
#include <GL\GL.h>
#define window_width 1080
#define window_height 720
GLfloat angle; // wheel's rotator
GLfloat moveX; // wheel's mover by X
void DrawCircle(float cx, float cy, float r, int num_segments) {
glLoadIdentity();
glTranslatef(0, 0, -10); // set place where wheel starts to draw
glRotatef(angle, 0.0f, 0.0f, -0.1f); // rotate our wheel. it works correctly I think
glTranslatef(moveX, 0.0f, 0.0f); // but it makes me wonder. moving circle by X doesn't work
// part of a circle
glBegin(GL_TRIANGLE_FAN);
glColor3f(255, 255, 255);
for (int ii = 0; ii < num_segments; ii++) {
double twicePi = 2.0 * 3.142;
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);
float x = r * cosf(theta);
float y = r * sinf(theta);
glVertex2f(x + cx, y + cy);
}
glEnd();
// second part of a circle
int i, x, y;
double radius = 1.20;
glColor3ub(0.0, 0.0, 0.0);
double twicePi = 2.0 * 3.142;
x = 0, y = 0;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y);
for (i = 0; i <= 20; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 20))),
(y + (radius * sin(i * twicePi / 20)))
);
}
glEnd();
// there's an end of drawning of a circle
// this is where wheel starts to rotate and move
angle += 0.02f;
moveX += 0.00005f;
// there are spokes of a wheel
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, -1.2f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.2f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.2f, 0.0, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.2f, 0.0, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.f, 1.f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.f, 1.f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.f, -1.f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.f, -1.f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.2f, 0.5f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.2f, -0.5f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.2f, -0.5f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.2f, 0.5f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.2f, -0.5f, 0.0);
glEnd();
}
void main_loop_function() {
DrawCircle(0.0, 0.0, 1.45, 200);
glutSwapBuffers();
}
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
gluPerspective(45, (float) width / height, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("trata");
glutIdleFunc(main_loop_function);
GL_Setup(window_width, window_height);
glutMainLoop();
}
This is just a quick fix to make your code work so you can fuhrtherly develop and debug it. I see a number of critical issues in your code:
(0) Mixing POSIX paths with Windows paths:
#include <GL/freeglut.h>
#include <GL\GL.h>
(1) including custom headers and expecting the community to guess what is it about. I commented it out.
//#include "stdafx.h"
(2) Miswriting casts as functions - it should be:
float theta = 2.0f * 3.1415926f * (float)ii / (float)num_segments;
(3) No glutDisplayFunc in your code. You should register something like:
glutDisplayFunc(main_display_function);
before registering glutIdleFunc(). Then, in your code you should define it somewhat in the following way:
void main_display_function() {
DrawCircle(0.0, 0.0, 1.45, 200);
glutSwapBuffers();
}
(4) Even in that function you don't clear the "window content" before redrawing:
void DrawCircle(float cx, float cy, float r, int num_segments) {
glClearColor (0., 0., 0., 1);
glClear (GL_COLOR_BUFFER_BIT);
{… … … } //the rest of your code
}
(5) Passing color values of wrong data type:
glColor3f(255, 255, 255); //unsigned byte passed, float expected!
or
glColor3ub(0.0, 0.0, 0.0); //float passed, unsigned byte expected!
There are few more issues which directly address your question - you will easily understand and debug them yourself, once you fix the program to work correctly (the order of calling glRotatef and the second glTranslatef, correctly computing movex, drawing beams or spokes). Please, also take a look at this SO post.
PS. Your using a C++ compiler to build this program doesn't make it a C++ code. There is not a single line of C++ specific code in your program. Also, both OpenGL and GLUT are plain-C APIs.
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 have an openGL program that drawn 3 houses and another things but, when I compile with gcc, just appears a black screen - and this is happening with all my openGL programs
What can I do?
PS: If I resize my screen, the drawn appears.
OS: macOS Mojave
PC: MacBook Air 2017
Development in Visual Studio Code
gcc version: 10.0.0
#include <stdlib.h>
#include <stdio.h>
#include <glut/glut.h>
#include <math.h>
#define PI 3.14159265358979324
//teto
bool teto = true;
bool base = true;
bool janela = true;
bool porta = true;
void Teto(void)
{
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(20.0, 10.0, 0.0);
glVertex3f(40.0, 0.0, 0.0);
glEnd();
}
void Base(void)
{
glBegin(GL_QUADS);
glColor3f(0, 1, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 30.0, 0.0);
glVertex3f(35.0, 30.0, 0.0);
glVertex3f(35.0, 0.0, 0.0);
glEnd();
}
void Porta(void)
{
glBegin(GL_QUADS);
glColor3f(0, 0, 1);
glVertex3f(0, 0.0, 0.0);
glVertex3f(0, 10.0, 0.0);
glVertex3f(5.0, 10.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
void Janela()
{
glBegin(GL_QUADS);
glColor3f(0, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 5.0, 0.0);
glVertex3f(5.0, 5.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
void Sun()
{
static float R = 10.0; // Radius of circle.
static float X = 20.0; // X-coordinate of center of circle.
static float Y = 70.0; // Y-coordinate of center of circle.
static int numVertices = 30; // Number of vertices on circle.
float t = 0; // Angle parameter.
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
// Draw a line loop with vertices at equal angles apart on a circle
// with center at (X, Y) and radius R, The vertices are colored randomly.
glBegin(GL_LINE_LOOP);
for(i = 0; i < numVertices; ++i)
{
glColor3f(1.0, 1.0, 0.0);
glVertex3f(X + R * cos(t), Y + R * sin(t), 0.0);
t += 2 * PI / numVertices;
}
glEnd();
}
// Drawing routine.
void drawHouse(void)
{
glPopMatrix();
glPushMatrix();
//glPopMatrix();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Sun();
glTranslatef(30.0f,30.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glPopMatrix();
glScalef(0.75f, 0.75f, 0.0f);
glTranslatef(3.0f,30.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glPopMatrix();
glScalef(1.75f, 1.75f, 0.0f);
glScalef(1.25f, 1.25f, 0.0f);
//glScalef(0.75f, 0.75f, 0.0f);
glTranslatef(25.0f,20.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glFlush();
}
// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 130.0, 0.0, 100.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'c':
base = !base;
break;
case 't':
teto = !teto;
break;
case 'j':
janela = !janela;
break;
case 'p':
porta = !porta;
break;
case 's':
Sun();
//glRotatef(10.0, 0.0f, 0.0f, 1.0f);
break;
default:
break;
}
glutPostRedisplay();
}
// Main routine.
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(900, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow("House.cpp");
glutDisplayFunc(drawHouse);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
setup();
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);
The problem is, 'Objects on the table is covered with table board, so can't see it.'
( I using openGL 3.7 beta. Files that I installed is : http://ihoo1836.dothome.co.kr/opengl_vs2010+glutdlls37beta.zip )
All Codes are following.
#include<glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
float TableX = 5.0; //Table's X size
float TableY = 8.0; //Table's Y size
float TableHeight = 2.0;//Table's Height
int width=400, height=400; //Window Size
int ViewX = width/2; //for Change Viewpoint by Mouse position
int ViewY = height/2;
int ViewZ = 9;
GLUquadricObj* cyl;
void InitLight( ){
glEnable(GL_DEPTH_TEST); //for opaque
glEnable(GL_NORMALIZE); //normalize
glEnable(GL_SMOOTH); //for smooth color
glEnable(GL_LIGHTING); //light setting
glDepthMask(GL_TRUE);
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat position[]={400.0, 300.0, -700.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
//Get Mouse Position to Change ViewPoint
void MyMouseMove(int button, int state, GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Get Mouse Position to Change ViewPoint
void MyMotion(GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Draw Table
void DrawTable(){
glPushMatrix();
glTranslatef(0.0,0.0,1.0);
glColor3f(0.5, 0.25, 0.0);
cyl = gluNewQuadric();
glRotatef(-90,1.0,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 1
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 2
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(0.0, TableY, 0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 3
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(-TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 4
glPushMatrix();
glTranslatef(TableX/2.0, -TableY/2, TableHeight);
glScalef(TableX+0.5, TableY+0.5, 0.5);
glutSolidCube(1); //Board of Table
glPopMatrix();
glPushMatrix(); //triangular1 (Beside of Net)
glTranslatef(0, -TableY/2, TableHeight);
glBegin(GL_TRIANGLES);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glVertex3f(0, TableY/8.0, 0);
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(0, TableY/8.0, 0);
glEnd();
glPushMatrix(); //triangular2 (Beside of Net)
glTranslatef(TableX - TableY/8.0, 0 , 0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glEnd();
glPopMatrix();
glPushMatrix(); //Net
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(TableY/16.0, 0.0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, 0);
glVertex3f(TableY/16.0, 0.0, 0.0);
glEnd();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
//Display Callback Function
void MyDisplay( ){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
gluPerspective(60.0, (GLfloat)width/height, 0.0, 10.0);
//Change Viewpoint by Mouse Position
gluLookAt((float)(ViewX - width/2)/width*20 + 2.5, (float)(height/2 - ViewY)/height*20 + 2.5, ViewZ, TableX/2, TableY/2, TableHeight, 0.0, 1.0, 0.0);
printf("eyex = %f , eyey = %f , eyez = %f \n",(float)(ViewX - width/2.0)/width*10, (float)(height/2 - ViewY)/height*10, (float)ViewZ);
DrawTable(); //Draw Table
glutSwapBuffers(); //for 'Double Buffering'
}
//for Reshape Window
void MyReshape (int w, int h){
width = w;
height = h;
printf("width = %d, height = %d \n", width, height);
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity( );
glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
//Main Function
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(200, 200);
glutCreateWindow("OpenGL Sample Drawing");
glClearColor(0.4, 0.4, 0.4, 1.0);
InitLight(); //set Light Setting
glutDisplayFunc(MyDisplay);
glutMouseFunc(MyMouseMove); //get Mouse Position, to Change Viewpoint
glutMotionFunc(MyMotion); //get Mouse Position, to Change Viewpoint
glutReshapeFunc(MyReshape);
glutMainLoop( );
}
The third argument to gluPerspective() should be non-zero, positive, and less than the forth argument:
#include <GL/glut.h>
#include <stdio.h>
float TableX = 5.0; //Table's X size
float TableY = 8.0; //Table's Y size
float TableHeight = 2.0;//Table's Height
int ViewX = 400/2; //for Change Viewpoint by Mouse position
int ViewY = 400/2;
int ViewZ = 9;
GLUquadricObj* cyl;
void InitLight( ){
glEnable(GL_DEPTH_TEST); //for opaque
glEnable(GL_NORMALIZE); //normalize
glEnable(GL_SMOOTH); //for smooth color
glEnable(GL_LIGHTING); //light setting
glDepthMask(GL_TRUE);
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat position[]={400.0, 300.0, -700.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
//Get Mouse Position to Change ViewPoint
void MyMouseMove(int button, int state, GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Get Mouse Position to Change ViewPoint
void MyMotion(GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Draw Table
void DrawTable(){
glPushMatrix();
glTranslatef(0.0,0.0,1.0);
glColor3f(0.5, 0.25, 0.0);
cyl = gluNewQuadric();
glRotatef(-90,1.0,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 1
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 2
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(0.0, TableY, 0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 3
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(-TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 4
glPushMatrix();
glTranslatef(TableX/2.0, -TableY/2, TableHeight);
glScalef(TableX+0.5, TableY+0.5, 0.5);
glutSolidCube(1); //Board of Table
glPopMatrix();
glPushMatrix(); //triangular1 (Beside of Net)
glTranslatef(0, -TableY/2, TableHeight);
glBegin(GL_TRIANGLES);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glVertex3f(0, TableY/8.0, 0);
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(0, TableY/8.0, 0);
glEnd();
glPushMatrix(); //triangular2 (Beside of Net)
glTranslatef(TableX - TableY/8.0, 0 , 0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glEnd();
glPopMatrix();
glPushMatrix(); //Net
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(TableY/16.0, 0.0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, 0);
glVertex3f(TableY/16.0, 0.0, 0.0);
glEnd();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
//Display Callback Function
void MyDisplay( ){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity( );
double width = glutGet( GLUT_WINDOW_WIDTH );
double height = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(60.0, (GLfloat)width/height, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
//Change Viewpoint by Mouse Position
gluLookAt((float)(ViewX - width/2)/width*20 + 2.5, (float)(height/2 - ViewY)/height*20 + 2.5, ViewZ, TableX/2, TableY/2, TableHeight, 0.0, 1.0, 0.0);
printf("eyex = %f , eyey = %f , eyez = %f \n",(float)(ViewX - width/2.0)/width*10, (float)(height/2 - ViewY)/height*10, (float)ViewZ);
DrawTable(); //Draw Table
glutSwapBuffers(); //for 'Double Buffering'
}
//Main Function
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("OpenGL Sample Drawing");
glClearColor(0.4, 0.4, 0.4, 1.0);
InitLight(); //set Light Setting
glEnable( GL_DEPTH_TEST );
glutDisplayFunc(MyDisplay);
glutMouseFunc(MyMouseMove); //get Mouse Position, to Change Viewpoint
glutMotionFunc(MyMotion); //get Mouse Position, to Change Viewpoint
glutMainLoop( );
}
#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.