i want to make a sphere and put a camera which focuses at the center ,having a vector (VUP=0,1,0).I want to alter the spherical coordinates r,theta,phi.
The problem is that when i run the application,the sphere appears but when i press a key it disappears.
If i delete the glMatrixMode(GL_PROJECTION) from the setupmywindow,then at the beginning the sphere doesn't appear ,but if i press a key it appears.Then , bu pressing "r" and "p" ,it changes its radius but pressing "u" and "f" it rotates only one step.
#define PI 3.1415f
float theta = 0, phi = 0, dtheta = PI / 20, dphi = PI / 20;
float r = 0.2;
void setupmywindow()
{
glClearColor(1.0,1.0,1.0,0);
glColor3f(0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glMatrixMode(GL_PROJECTION);
gluPerspective(30,1,2,100);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
}
void myaxes(double size)
{
glBegin(GL_LINES);
glColor3f(0,0,0);glVertex3f(0,0,0); glColor3f(1,0,0);glVertex3f(size,0,0); //x-axis
glColor3f(0,0,0);glVertex3f(0,0,0); glColor3f(0,1,0);glVertex3f(0,size,0); //y-axis
glColor3f(0,0,0);glVertex3f(0,0,0); glColor3f(0,0,1);glVertex3f(0,0,size); //z-axis
glEnd();
}
void sphere()
{
float z1, x1, y1, z2, x2, y2, z3, x3, y3, z4, x4, y4;
float angle = 0,translate;
glTranslatef(0.0,0.0,translate);
angle++;
glRotatef(angle, 1.0, 1.0, 1.0);
glBegin(GL_QUAD_STRIP);
for(theta=0;theta<=2.0*PI;theta+=dtheta)
{
for(phi=0;phi<=PI;phi+=dphi)
{
z1 = r * sin(phi + dphi) * cos(theta + dtheta);
x1 = r * sin(phi + dphi) * sin(theta + dtheta);
y1 = r * cos(phi + dphi);
z2 = r * sin(phi) * cos(theta + dtheta);
x2 = r * sin(phi) * sin(theta + dtheta);
y2 = r * cos(phi);
z3 = r * sin(phi) * cos(theta);
x3 = r * sin(phi) * sin(theta);
y3 = r * cos(phi);
z4 = r * sin(phi + dphi) * cos(theta);
x4 = r * sin(phi + dphi) * sin(theta);
y4 = r * cos(phi + dphi);
glColor3f(1,0,0);
glVertex3f(x4, y4, z4);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
}
}
glEnd();
}
void myDrawing()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(r,theta,phi,0,0,0,0,1,0);
sphere();
myaxes(1.5);
glutSwapBuffers();
}
void mykeyboardcontrol(unsigned char key, int x, int y)
{
switch(key){
case 'r': r+=0.1;break; //increase radius
case 'p': r-=0.1;break; //decrease radius
case 'u': theta+=PI/20;break;//increase theta angle
case 'f': phi+=PI/20;break;//increase phi angle
}
printf("r=%f theta=%f phi=%f\n",r,theta,phi);
if(key==27) exit(0);
if(theta>2*PI) theta-=2*PI;
if(phi>PI) phi-=PI;
printf("r=%f theta=%f phi=%f\n",r,theta,phi);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("Sphere");
setupmywindow();
glutDisplayFunc(myDrawing);
glutKeyboardFunc(mykeyboardcontrol);
glutMainLoop();
}
Here you go:
#include <GL/glut.h>
#include <cmath>
#include <cstdlib>
#include <cstdio>
const float PI = 3.14159f;
float camera_theta = 2.042033;
float camera_phi = 8.639376;
float camera_r = 15;
double aspect_ratio = 0;
void reshape(int w, int h)
{
aspect_ratio = (double)w / (double)h;
glViewport(0, 0, w, h);
}
void setupmywindow()
{
glClearColor(1,1,1,0);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
}
void myaxes(double size)
{
glBegin(GL_LINES);
glColor3f(0,0,0);
glVertex3f(0,0,0);
glColor3f(1,0,0);
glVertex3f(size,0,0); //x-axis
glColor3f(0,0,0);
glVertex3f(0,0,0);
glColor3f(0,1,0);
glVertex3f(0,size,0); //y-axis
glColor3f(0,0,0);
glVertex3f(0,0,0);
glColor3f(0,0,1);
glVertex3f(0,0,size); //z-axis
glEnd();
}
void sphere(float radius)
{
float z1, x1, y1, z2, x2, y2, z3, x3, y3, z4, x4, y4;
float dtheta = PI / 20;
float dphi = PI / 20;
glBegin(GL_QUADS);
for( float theta = 0; theta<=2.0*PI; theta+=dtheta )
{
for( float phi = 0; phi<=PI; phi+=dphi )
{
z1 = radius * sin(phi + dphi) * cos(theta + dtheta);
x1 = radius * sin(phi + dphi) * sin(theta + dtheta);
y1 = radius * cos(phi + dphi);
z2 = radius * sin(phi) * cos(theta + dtheta);
x2 = radius * sin(phi) * sin(theta + dtheta);
y2 = radius * cos(phi);
z3 = radius * sin(phi) * cos(theta);
x3 = radius * sin(phi) * sin(theta);
y3 = radius * cos(phi);
z4 = radius * sin(phi + dphi) * cos(theta);
x4 = radius * sin(phi + dphi) * sin(theta);
y4 = radius * cos(phi + dphi);
glColor3f(1,0,0);
glVertex3f(x4, y4, z4);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
}
}
glEnd();
}
void display(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspect_ratio, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// spherical coordinate camera transform, +Z is "up"
glTranslatef(0 ,0 , -camera_r);
glRotatef( (camera_theta - PI) * (180.0f/PI), 1,0,0 );
glRotatef( -camera_phi * (180.0f/PI), 0,0,1 );
sphere(2);
myaxes(5);
glutSwapBuffers();
}
void mykeyboardcontrol(unsigned char key, int x, int y)
{
switch(key){
case 'r': camera_r+=0.1;break; //increase radius
case 'p': camera_r-=0.1;break; //decrease radius
case 'i': camera_theta+=PI/20;break;//increase theta angle
case 'k': camera_theta-=PI/20;break;//increase theta angle
case 'j': camera_phi-=PI/20;break;//increase phi angle
case 'l': camera_phi+=PI/20;break;//increase phi angle
}
printf("r=%f theta=%f phi=%f\n",camera_r,camera_theta,camera_phi);
if(key==27) exit(0);
// clamp theta
if( camera_theta < 0 ) camera_theta = 0;
if( camera_theta > PI ) camera_theta = PI;
// wrap phi
if( camera_phi > 2*PI ) camera_phi -= 2*PI;
if( camera_phi < 2*PI ) camera_phi += 2*PI;
printf("r=%f theta=%f phi=%f\n",camera_r,camera_theta,camera_phi);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400,400);
glutCreateWindow("Sphere");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(mykeyboardcontrol);
setupmywindow();
glutMainLoop();
return 0;
}
I have no clue why you were trying to pass r, theta, and phi into gluLookAt(), so I replaced that with a fixed oblique-ish camera angle looking at the origin. Also you seem to be overwriting theta and phi in sphere(), blasting away whatever mykeyboardcontrol() is trying to do. I couldn't tell if you want to orbit the camera around the origin or draw a partial sphere.
Related
I want to draw one cylinder using OpenGL in Visual C++.
I want to make the color of the cylinder red, so I add the following code in the renderCylinder function, but it doesn't change.
glColor3f(1.0, 0.0, 0.0);
Could you help me to solve this problem?
The following codes are the full codes to make a cylinder for a test.
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <iostream>
int selectedObject = 1;
bool drawThatAxis = 0;
bool lightEffect = 1;
float fovy = 60.0, aspect = 1.0, zNear = 1.0, zFar = 100.0;
float depth = 8;
float phi = 0, theta = 0;
float downX, downY;
bool leftButton = false, middleButton = false;
void renderCylinder(double x1, double y1, double z1, double x2, double y2, double z2, double radius, GLUquadricObj* quadric);
void displayCallback(void);
GLdouble width, height;
int wd;
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH);
glutInitWindowSize(800,600);
wd = glutCreateWindow("3D Molecules");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glClearColor(0.0, 0.0, 0.0, 0.0);
GLuint id;
id = glGenLists(1);
printf("hi %d\n", id);
GLUquadric* myQuad;
myQuad = gluNewQuadric();
glNewList(id, GL_COMPILE);
renderCylinder(0, 0, 0, 5, 5, 5, 1.5, myQuad);
glEndList();
glutDisplayFunc(displayCallback);
glutMainLoop();
return 0;
}
void renderCylinder(double x1, double y1, double z1, double x2, double y2, double z2, double radius, GLUquadricObj* quadric)
{
double vx = x2 - x1;
double vy = y2 - y1;
double vz = z2 - z1;
double ax, rx, ry, rz;
double len = sqrt(vx * vx + vy * vy + vz * vz);
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(x1, y1, z1);
if (fabs(vz) < 0.0001)
{
glRotatef(90, 0, 1, 0);
ax = 57.2957795 * -atan(vy / vx);
if (vx < 0)
{
}
rx = 1;
ry = 0;
rz = 0;
}
else
{
ax = 57.2957795 * acos(vz / len);
if (vz < 0.0)
ax = -ax;
rx = -vy * vz;
ry = vx * vz;
rz = 0;
}
glRotatef(ax, rx, ry, rz);
gluQuadricOrientation(quadric, GLU_OUTSIDE);
gluCylinder(quadric, radius, radius, len, 10, 10);
glPopMatrix();
}
void displayCallback(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fovy, aspect, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 40, 0, 0, 0, 0, 1, 0);
glTranslatef(0.0, 0.0, -depth);
glRotatef(-theta, 1.0, 0.0, 0.0);
glRotatef(phi, 0.0, 1.0, 0.0);
if (lightEffect) {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
else
{
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
}
switch (selectedObject)
{
case (1):
glCallList(1);
break;
default:
break;
}
glFlush();
}
You have lighting ON so glColor is ignored and material & light properties are used instead...
To remedy that add this to your code near place where you enable light:
glEnable(GL_COLOR_MATERIAL);
I am trying to create a car game with along with obstacles. I want to add collision so every time my object hits one of the obstacles, obstacle changes color and we know there has been a collision. I also want there to be a collision around my screen so that my object can not leave the view port?
--void 'draw car' is my movable object
#include "include\freeglut.h" // OpenGL toolkit - in the local shared folder
#include <cmath>
#include <iostream>
//set up some constants
#define X_CENTRE 0.0 /* centre point of square */
#define Y_CENTRE 0.0
#define LENGTH 5.0 /* lengths of sides of square */
//forward declaration - best in the header
void drawStar(GLfloat radius, GLfloat x, GLfloat y);
void drawSquare(GLfloat length, GLfloat x, GLfloat y);
void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y);
void drawCar(GLfloat length, GLfloat x, GLfloat y);
GLfloat red = 1.0, green = 1.0, blue = 1.0;
GLint xmove = -12.0, ymove = -12.5, zmove = 0.0;
/* reshape callback function
executed when window is moved or resized. This function should be used in Tutorial 1 */
void reshape(int width, int height)
{
GLfloat aspect = (GLfloat)width / (GLfloat)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity();
// glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0); //sets the x,y,z plane from -1 to 1
if (width <= height) //if aspect is less or equal to 1
glOrtho(-10.0, 10.0, -10.0/aspect, 10.0/aspect, -1.0, 1.0);
else // aspect is greater than 1
glOrtho(-10.0 * aspect, 10.0* aspect, -10.0 , 10.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
/* display callback function
called whenever contents of window need to be re-displayed */
//this is the all important drawing method - all drawing code goes in here
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); /* clear window */
/*glColor3f(1.0, 1.0, 1.0); /* white drawing objects */
glColor3f(0.0, 0.0, 1.0); /* blue drawing objects */
/* define object to be drawn as a square polygon */
glColor3f(0.3, -3.0, 4.0);
drawSquare(2, -3, 8);
glColor3f(0.3, -1.0, 2.0);
drawSquare(2, -3, 8);
glColor3f(1.0, 0.0, 0.0);
drawSquare(2, -5, -6);
glColor3f(0.0, 1.0, 0.0);
drawSquare(2, 0.0, 0.0);
glColor3f(2.0, 3.0, 1.1);
drawSquare(2, 6.0, 0.0);
glColor3f(0.3, -1.0, 2.0);
drawSquare(2, -6, 5);
glColor3f(0.7, -1.0, 2.0);
drawSquare(2, 5, -5);
glColor3f(0.0, 0.0, 1.0);
drawSquare(2, 5, 8);
glColor3f(0.75, 0.5, 0.25);
drawCar(1,0,0);
glFlush(); /* execute drawing commands in buffer */
}
void drawCar(GLfloat length, GLfloat x, GLfloat y) {
glPushMatrix();
glTranslatef(xmove, ymove, zmove);
//glBegin(GL_TRIANGLES);
////specify the vertices (points in 3D space) of the shape - note that these are 2D points
//glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE - LENGTH / 3);
//glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE + LENGTH / 3);
//glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE + LENGTH / 3);
//glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE - LENGTH / 3);
//glEnd();
glRectf(3, 4, 6, 2);
glPopMatrix();
glFlush();
}
void keyInput(unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
ymove++;
if (ymove >= 10) ymove = 3.0;
break;
case 's':
ymove--;
// if (ymove <= -5) ymove = 0.0;
break;
case 'd':
xmove++;
// if (xmove >= 5) xmove = 0.0;
break;
case 'a':
xmove--;
// if (xmove <= -5) xmove = 0.0;
break;
}
glutPostRedisplay();
}
void drawSquare(GLfloat length, GLfloat x, GLfloat y)
{
//x1,y1 is the top left-hand corner coordinate
GLfloat x1, y1, x2, y2, x3, y3, x4, y4;
//glColor3f(0.1, 1.0, 0.2);
x1 = x - length / 2;
y1 = y + length / 2;
x2 = x + length / 2;
y2 = y + length / 2;
x3 = x + length / 2;
y3 = y - length / 2;
x4 = x - length / 2;
y4 = y - length / 2;
glBegin(GL_POLYGON);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glVertex2f(x4, y4);
glEnd();
glFlush();
}
void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y)
{
//x1,y1 is the top left-hand corner coordinate
GLfloat x1, y1, x2, y2, x3, y3, x4, y4;
//This example is for a rectangle
x1 = x - lengthX / 2;
y1 = y + lengthY / 2;
x2 = x + lengthX / 2;
y2 = y + lengthY / 2;
x3 = x + lengthX / 2;
y3 = y - lengthY / 2;
x4 = x - lengthX / 2;
y4 = y - lengthY / 2;
glBegin(GL_POLYGON);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glVertex2f(x4, y4);
glEnd();
glFlush();
}
//Draws a 5 pointed star using lines
void drawStar(GLfloat radius, GLfloat x, GLfloat y)
{
//x1,y1 is the top coordinate
//glColor3f(0.0, 1.0, 1.0);
GLfloat x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
x1 = x;
y1 = y + radius;
x2 = x + 0.90 * radius;
y2 = y + 0.40 * radius;
x3 = x + 0.65 * radius;
y3 = y - 0.55 * radius;
x4 = x - 0.65 * radius;
y4 = y - 0.55 * radius;
x5 = x - 0.90 * radius;
y5 = y + 0.40 * radius;
glLineWidth(1.0);
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x3, y3);
glVertex2f(x1, y1);
glVertex2f(x4, y4);
glVertex2f(x2, y2);
glVertex2f(x4, y4);
glVertex2f(x2, y2);
glVertex2f(x5, y5);
glVertex2f(x3, y3);
glVertex2f(x5, y5);
glEnd();
glFlush();
}
/* graphics initialisation */
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0); //Setting up the background color
}
int main(int argc, char** argv)
{
/* window management code ... */
/* initialises GLUT and processes any command line arguments */
glutInit(&argc, argv);
/* use single-buffered window and RGBA colour model */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
/* window width = 400 pixels, height = 400 pixels */
/* window width = 640 pixels, height = 480 pixels for a 4:3 ascpect ratio */
/* window width = 1024 pixels, height = 576 pixels for a 16:9 ascpect ratio */
glutInitWindowSize(750, 750);
/* window upper left corner at (100, 100) */
glutInitWindowPosition(100, 100);
/* creates an OpenGL window with command argument in its title bar */
glutCreateWindow("Example 1");
glutKeyboardFunc(keyInput);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Keep track of your window size (750, 750), and your orthographic projection multiplier (10.0), then you should be able to tell when a certain world space coordinate is at the edge of the screen from these two.
I'm trying to make a program where I'm able to zoom in & out on the figures I've drawn, but is not doing it; it's switching the orientation of the figures and if I keep scrolling, it just disappears. The function that are suppose to be executed for this to work are the MouseFunc() along with the renderScene(). Any explanation for the problem or help is welcomed.
#include"glut.h"
#include<cmath>
#include<iostream>
using namespace std;
float xr = 0, yr = 0; //to control the object's movement from left to right
// XZ position of the camera
float x = 0.0f, z = 5.0f; //Module 4
float angleX = 0.0f; //Module 4
//Shift + ArrowKey rotation
float transX = 0.0f;
float transY = 0.0f;
float rotY = 0.0f;
//end
//Mouse Commands
//float ZoomFactor = 0.5;
GLfloat theta3 = 0;
GLfloat phi = 0;
GLfloat rho = 5;
GLfloat camX = 0;
GLfloat camY = 0;
GLfloat camZ = 0;
GLfloat upX = 0;
GLfloat upY = 0;
GLfloat upZ = 0;
//end
GLfloat angle = 0.0f;
int refreshmill = 1;
void timer(int value) { //to control the rotation of the object
glutTimerFunc(refreshmill, timer, 0);
}
void myDisplay(void) {
//Circle One
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glPushMatrix();
glBegin(GL_POLYGON);
for (int x = 0; x < 360; x++) {
theta = x * 3.142 / 180;
glVertex2f(150 * cos(theta) + xr, 150 * sin(theta) + yr);
}
glEnd();
glPopMatrix();
//Circle Two
float theta2;
glPushMatrix();
glTranslatef(0.5f, 0.0f, 0.0f); // rotation
glRotatef(angle, 0.0f, 0.0f, -0.5f); // rotation
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
for (int x = 0; x < 360; x++) {
theta2 = x * 3.142 / 180;
glVertex2f(150 + 15 * cos(theta2) + xr, 15 * sin(theta2) + yr);
}
angle += 0.2; // rotation
glEnd();
glPopMatrix();
//Draw Star
glColor3ub(119, 193, 15);
glPushMatrix();
glBegin(GL_POLYGON);
glVertex2d(15 + xr, 60 + yr);
glVertex2d(75 + xr, 75 + yr); //right peak
glVertex2d(15 + xr, 90 + yr);
glVertex2d(0 + xr, 150 + yr); //Up-peak Changed
glVertex2d(-15 + xr, 90 + yr);
glVertex2d(-75 + xr, 75 + yr);
glVertex2d(-15 + xr, 60 + yr);
glVertex2d(0 + xr, 0 + yr);
glEnd();
glPopMatrix();
}
//Close code
void handleKeypress(unsigned char key, int x, int y){
switch (key){
case 27: //when the escape key is pressed the program will exit.
exit(0);
}
}
//Movement of drawing
void keyboard(int key, int x, int y) {
float fraction = 0.1f;
bool shift = false;
int mod = glutGetModifiers();
if (mod == GLUT_ACTIVE_SHIFT) {
shift = true;
}
if (!shift) {
switch (key) {
case GLUT_KEY_RIGHT: xr++; break;
case GLUT_KEY_LEFT: xr--; break;
case GLUT_KEY_UP: angleX -= 1.0f; break; //Module 4
case GLUT_KEY_DOWN: angleX += 1.0f; break; //Module 4
}
}
else {
switch (key) {
case GLUT_KEY_LEFT:// RotaciĆ³n del dibujo hacia la izquierda en el eje de Y
rotY -= 1.0f;
break;
case GLUT_KEY_RIGHT:// RotaciĆ³n del dibujo hacia la derecha en el eje de Y
rotY += 1.0f;
break;
}
}
}
//Mouse Function
void MouseFunc(int button, int state, int x, int y){
if (button > 4){
rho = rho + 3.0;
}
else (button < 3);{
rho = rho - 3.0;
}
}
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
glRotatef(rotY, 0.0, 1.0, 0.0); //Rotation with Shift + ArrowKey
GLfloat camX = rho * cos(theta3*3.1415926f / 180)*sin(phi*3.1415926f / 180);
GLfloat camY = rho * sin(theta3*3.1415926f / 180);
GLfloat camZ = rho * cos(theta3*3.1415926f / 180)*cos(phi*3.1415926f / 180);
// Reduce theta slightly to obtain another point on the same longitude line on the sphere.
GLfloat dt = 1;
GLfloat eyeXtemp = -rho * cos((theta3 - dt)*3.1415926f / 180)*sin(phi*3.1415926f / 180);
GLfloat eyeYtemp = -rho * sin((theta3 - dt)*3.1415926f / 180);
GLfloat eyeZtemp = -rho * cos((theta3 - dt)*3.1415926f / 180)*cos(phi*3.1415926f / 180);
// Connect these two points to obtain the camera's up vector.
GLfloat upX = eyeXtemp - camX;
GLfloat upY = eyeYtemp - camY;
GLfloat upZ = eyeZtemp - camZ;
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set the camera
gluLookAt(camX, camY, camZ, 0, 0, 0, upX, upY, upZ);
gluLookAt(x, 0.0f, z, x, 0.0f, z - 1.0f, 0.0f, 1.0f, 0.0f); //Module 4
glRotatef(angleX, 1, 0, 0); //Module 4
myDisplay();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(0.0f, 0.1f, 0.1f, 0.0f);
glOrtho(-250, 250, -250, 250, -250, 250); //IMPORTANT- Define from negative to positive
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Homework: Circle");
// register callbacks
glutDisplayFunc(renderScene);
glutTimerFunc(0,timer,0);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(keyboard);
glutMouseFunc(MouseFunc);
// OpenGL init
init();
// enter GLUT event processing cycle
glutMainLoop();
}
Is wired to set a perspective and an orthographic projection. Delete the perspective projection gluPerspective:
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(0.0f, 0.1f, 0.1f, 0.0f); <---- delete
glOrtho(-250, 250, -250, 250, -250, 250);
glMatrixMode(GL_MODELVIEW);
}
If you want to zoom, the the orthographic projection has to be changed (glOrtho). The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport.
Implement a mouse wheel event, which changes the zoom, in a restricted range:
e.g.
GLdouble zoom = 0.0f;
void MouseFunc(int button, int state, int x, int y){
GLdouble min_z = -100.0;
GLdouble max_z = 100.0;
if (button == 4 && zoom < max_z) {
zoom += 3.0;
}
else if (button == 3 && zoom > min_z) {
zoom -= 3.0;
}
}
Modify the orthographic projection, dependent on the zoom in the display loop renderScene:
void renderScene(void) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble ortho = 250 + zoom;
glOrtho(-ortho, ortho, -ortho, ortho, -250, 250);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// [...]
}
I'd like to create a circular rotation for the rays around the sun.
to make it look like this
How I draw curves on the canvas.
GLfloat ctrlpoints[4][3];
void drawCurves(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4) {
ctrlpoints[0][0]=x1;
ctrlpoints[0][1]=y1;
ctrlpoints[0][2]=50.0f;
ctrlpoints[1][0]=x2;
ctrlpoints[1][1]=y2;
ctrlpoints[1][2]=50.0f;
ctrlpoints[2][0]=x3;
ctrlpoints[2][1]=y3;
ctrlpoints[2][2]=50.0f;
ctrlpoints[3][0]=x4;
ctrlpoints[3][1]=y4;
ctrlpoints[3][2]=50.0f;
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
int i;
glLineWidth(3.0f);
//glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
}
How Rays are made.
void Ray (float x, float y, float s){
glColor3f(1, 1, 0);
drawCurves(x, y, x+4*s, y-7*s, x-6*s, y-27*s, x-3*s, y-30*s);
glColor3f(1, 1, 0);
drawCurves(x, y, x+7*s, y-7*s, x+1*s, y-27*s, x+4*s, y-30*s);
}
How I design the sun motive.
void Rays(float x, float y, float radius, int num_segments){
float i;
double twicePi = 2.0 * 3.142;
for (i = 0; i <= num_segments; i++) {
Ray((x+ (radius * cos((i * twicePi / num_segments))))
,(y + (radius * sin((i * twicePi / num_segments))))
,0.3);
}
}
Tried glRotation but it rotates the whole flag, I just want to rotate the rays.
You have to caluclate the direction of the ray and you have to set up the points of the curves in the direction of the ray. In computer graphics rotations are commonly calculated by matrices. But since this is a simple 2D graphic, and you have already written most of the code, the computation can be done without matrices.
You have to calculate the direction of the ray and the counterclockwise rotated orthonormal direction. This 2 directions ar the local x-axis and local y-axis of the geometry of single ray. The coordinates of the curves of a ray must be plotted along this axis.
void Rays(float x, float y, float radius, int num_segments)
{
const float twicePi = 2.0f * 3.141593f;
for (int i = 0; i <= num_segments; ++i)
{
float angle = (float)i * twicePi / num_segments;
float dir_x = sin( angle );
float dir_y = cos( angle );
Ray( dir_x, dir_y, radius, 0.3 );
}
}
float dot( float a[], float b[2] )
{
return a[0]*b[0] + a[1]*b[1];
}
void Ray ( float xx, float xy, float r, float l)
{
float yx = -xy, yy = xx; // (xx, xy) counterclockwise rotated = (-xy, xx)
float xc[]{ xx, yx };
float yc[]{ xy, yy };
float p0[]{ xx * r, xy * r };
float p1[]{ -7.0f * s, 4.0f * s };
float p2[]{ -27.0f * s, -6.0f * s };
float p3[]{ -30.0f * s, -3.0f * s };
float p4[]{ -7.0f * s, 7.0f * s };
float p5[]{ -27.0f * s, 1.0f * s };
float p6[]{ -30.0f * s, 4.0f * s };
glColor3f(1, 1, 0);
drawCurves(
p0[0], p0[1],
p0[0] + dot(xc, p1[0]), p0[1] + dot(yc, p1[1]),
p0[0] + dot(xc, p2[0]), p0[1] + dot(yc, p2[1]),
p0[0] + dot(xc, p3[0]), p0[1] + dot(yc, p3[1]) );
drawCurves(
p0[0], p0[1],
p0[0] + dot(xc, p4[0]), p0[1] + dot(yc, p4[1]),
p0[0] + dot(xc, p5[0]), p0[1] + dot(yc, p5[1]),
p0[0] + dot(xc, p6[0]), p0[1] + dot(yc, p6[1]) );
}
I am trying to map a texture to a circle using GL_POLYGON using this code:
void drawCircleOutline(Circle c, int textureindex)
{
float angle, radian, x, y; // values needed by drawCircleOutline
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureLib[textureindex]);
glBegin(GL_POLYGON);
for (angle=0.0; angle<360.0; angle+=2.0)
{
radian = angle * (pi/180.0f);
x = (float)cos(radian) * c.r + c.pos.x;
y = (float)sin(radian) * c.r + c.pos.y;
glTexCoord2f(x, y);
glVertex2f(x, y);
}
glEnd();
glDisable(GL_TEXTURE_2D);
}
it looks like this when running.
And should look like this:
Try:
radian = angle * (pi/180.0f);
xcos = (float)cos(radian);
ysin = (float)sin(radian);
x = xcos * c.r + c.pos.x;
y = ysin * c.r + c.pos.y;
tx = xcos * 0.5 + 0.5;
ty = ysin * 0.5 + 0.5;
glTexCoord2f(tx, ty);
glVertex2f(x, y);