Hey everyone I am struggling with Opengl and cannot seem to figure out this prob
ID: 3673597 • Letter: H
Question
Hey everyone I am struggling with Opengl and cannot seem to figure out this problem.
Here is the problem definition:
Create a program that will display a sin curve when the left mouse button is clicked on and a cos curve when the right mouse button is clicked on. They should be different colors and can be displayed at the same time.
Here is what I have so far:
#include "F:OpenGL_tutorials_Setting_OpenGL_Setting_OpenGLDependenciesglewglew.h"
#include "F:OpenGL_tutorials_Setting_OpenGL_Setting_OpenGLDependenciesreeglutreeglut.h"
#include <math.h>
//#include "F:OpenGL_tutorials_Setting_OpenGLDebugc120.PDB"
//#include "F:OpenGL_tutorials_Setting_OpenGLDebugc120.idb"
#include <iostream>
typedef struct _Vector
{
double x, y, z;
}
Vector
;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
Vector dis;
double speed = 10;
Vector pos;
double radian;
Vector dir;
#define PI 3.14159265
#define SIZE 2000
void drawline(float x1, float y1, float x2, float y2);
void sinewave();
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow(" sinusoidal wave ");
glutDisplayFunc(sinewave);
glutMainLoop();
}
void sinewave()
{
float x1, x2, y1, y2;
for (int i = 0; i<SIZE; i++)
{
x1 = (float)i / SIZE;
x2 = ((float)i +1.0) / SIZE;
y1 = sin(x1 * 2 * PI);
y2 = sin(x2 * 2 * PI);
drawline(x1, y1, x2, y2);
glutSwapBuffers();
}
}
Vector mouseCoords;
void mouse(int btn, int state, int x, int y)
{
mouseCoords.x = x - SCREEN_WIDTH / 2;
mouseCoords.y = -y + SCREEN_HEIGHT / 2;
}
void move()
{
dis.x = mouseCoords.x - pos.x;
dis.y = mouseCoords.y - pos.y;
if (sqrt(dis.y*dis.y + dis.x*dis.x) < speed)
{
pos.x = mouseCoords.x;
pos.y = mouseCoords.y;
}
else
{
pos.x += cos(radian)*speed;
pos.y += sin(radian)*speed;
}
}
void drawline(float x1, float y1, float x2, float y2)
{
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 0, 0);
glVertex2f(x1,y1);
glVertex2f(x2, y2);
//glVertex2f(pos.x, pos.y); Was not able to get mouse control to compile
glEnd();
}
Here's what my ouput is:
Explanation / Answer
I dint see your code having #include <gl/glut.h> code. If you do not have GLUT installed on your machine it doesnt works. It seems to be you are fetching from folder path. Please include glClearColor ( 0.0 , 0.0 , 0.0 , 1.0 ); to clear with Black background . Regarding mouse controls use the following :
MousePosition.X
MousePosition.Y