I wanna make my square go up when UO is pressed.
void displayScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,x,0);
glBegin(GL_QUADS);
glVertex3f(-0.4,-0.4, -5.0);
glVertex3f( 0.4,-0.4, -5.0);
glVertex3f( 0.4, 0.4, -5.0);
glVertex3f(-0.4, 0.4, -5.0);
glEnd();
//x = x + 0.1;
glutSwapBuffers();
}
I'm using gluSpecialFunc.
void ProcessSpecialKeys(unsigned char key, int x, int y)
{
if (key == GLUT_KEY_UP)
{
x = x + 0.1;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0,0);
glutCreateWindow("OpenGL Window");
init();
glutDisplayFunc(displayScene);
glutSpecialFunc(ProcessSpecialKeys);
//glutKeyboardFunc(ProcessKeys);
glutMainLoop();
return 0;
}
When I leave x+=0.1 in the displayScene, the square goes up when I press any key.
Am I using glutSpecialFunc incorrectly? Because I used it before and it worked normally. What am I missing?
glutSpecialFunc is not invoked continuously when a key is held down. The callback is called once when a key is pressed.
freeglut provides the glutSpecialUpFunc callback which is called when a key is released.
Set a state when UP is pressed and reset the state when UP is released:
int main(int argc, char** argv)
{
// [...]
glutSpecialFunc(ProcessSpecialKeys);
glutSpecialUpFunc(ReleaseSpecialKeys);
// [...]
}
int keyUpPressed = 0;
void ProcessSpecialKeys(unsigned char key, int x, int y)
{
if (key == GLUT_KEY_UP)
keyUpPressed = 1;
}
void ReleaseSpecialKeys(unsigned char key, int x, int y)
{
if (key == GLUT_KEY_UP)
keyUpPressed = 0;
}
Change the x dependent on the state of keyUpPressed. Continuously redraw the scene by calling glutPostRedisplay in displayScene
void displayScene(void)
{
if (keyUpPressed)
x += 0.1;
// [...]
glutSwapBuffers();
glutPostRedisplay();
}
Related
I am writing a program that will continuously draw a polygon until the user right-clicks. The user can only draw if and only if the user makes a selection in the menu, if the user does not make a selection in the menu, the program will not draw. Up to now, my program has successfully drawn a polygon with the mouse, but the problem is that when I right-click to complete, the program pops up the menu instead of completing the polygon. Now how can I right-click to be able to complete the polygon, here's my program:
const int MAX = 100;
float mouseX, mouseY, ListX[MAX], ListY[MAX];
int numPoints = 0, closed = 0, shape = 0;
void mouse(int button, int state, int x, int y)
{
mouseX = x;
mouseY = y;
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if (closed || numPoints >= MAX - 1)
numPoints = 0;
closed = 0;
ListX[numPoints] = mouseX;
ListY[numPoints] = mouseY;
numPoints++;
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
closed = 1;
}
void motion(int x, int y)
{
mouseX = x;
mouseY = y;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
if (shape == 1)
{
if (numPoints)
{
glBegin(GL_LINE_STRIP);
for (int i = 0; i < numPoints; ++i)
glVertex2f(ListX[i], ListY[i]);
if (closed)
glVertex2f(ListX[0], ListY[0]);
else
glVertex2f(mouseX, mouseY);
glEnd();
}
}
glFlush();
}
void menu(int choice)
{
switch (choice)
{
case 1:
shape = 1;
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow("");
gluOrtho2D(0, 640, 480, 0);
glutCreateMenu(menu);
glutAddMenuEntry("Polygon", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutPassiveMotionFunc(motion);
glutMainLoop();
}
Edit: Thanks to genpfault, I finished the polygon by right-clicking but I don't know how to reattach it so I can re-open the menu.
...
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
if (shape == 1)
{
glutDetachMenu(GLUT_RIGHT_BUTTON); // Add this
...
}
glFlush();
}
void menu(int choice)
{
switch (choice)
{
case 1:
shape = 1;
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow("");
gluOrtho2D(0, 640, 480, 0);
glutCreateMenu(menu);
glutAddMenuEntry("Polygon", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutPassiveMotionFunc(mouse_move);
glutMainLoop();
}
Capture the return-value from glutCreateMenu(), that's the handle for the new menu.
Detach the menu in the menu callback via glutDetachMenu() and set a bool indicating you're in "add points" mode.
If you're in "add points" mode and detect a right-click, set the current menu to the menu handle from #1 via glutSetMenu(), then re-attach the menu with glutAttachMenu(). Reset the "add points" bool to false.
All together:
#include <GL/glut.h>
const int MAX = 100;
float mouseX, mouseY, ListX[MAX], ListY[MAX];
int numPoints = 0, closed = 0, shape = 0;
int hMenu = 0;
bool addingPoints = false;
void mouse(int button, int state, int x, int y)
{
mouseX = x;
mouseY = y;
if( addingPoints )
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
if (closed || numPoints >= MAX - 1)
numPoints = 0;
closed = 0;
ListX[numPoints] = mouseX;
ListY[numPoints] = mouseY;
numPoints++;
}
if( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
{
closed = 1;
addingPoints = false;
glutSetMenu(hMenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
}
}
void motion(int x, int y)
{
mouseX = x;
mouseY = y;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (shape == 1)
{
if (numPoints)
{
glBegin(GL_LINE_STRIP);
for (int i = 0; i < numPoints; ++i)
glVertex2f(ListX[i], ListY[i]);
if (closed)
glVertex2f(ListX[0], ListY[0]);
else
glVertex2f(mouseX, mouseY);
glEnd();
}
}
glutSwapBuffers();
}
void menu(int choice)
{
switch (choice)
{
case 1:
numPoints = 0;
shape = 1;
addingPoints = true;
glutDetachMenu(GLUT_RIGHT_BUTTON);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("");
hMenu = glutCreateMenu(menu);
glutSetMenu(hMenu);
glutAddMenuEntry("Polygon", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutPassiveMotionFunc(motion);
glutMainLoop();
}
in this method
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
that uses the structure i made
struct GLintPoint {
GLint x,y;
};
it says that there is an error and that "no operator "[]" matches these operands, operator types are GLintPoint[int]" where i try to assign a value from points to point. Well i did use the right brackets and it is an int in there so what is the problem? FYI this code is to draw the Sierpinski gasket with the user making the initial 3 points by clicking the screen with the mouse. Just in case you would like to see it here is the whole program.
#include <windows.h>
#include <gl/Gl.h>
#include "glut.h"
#include <iostream>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
struct GLintPoint {
GLint x,y;
};
void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();
}
void drawDot( GLint x, GLint y)
{
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
}
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
void myMouse(int button, int state, int x, int y){
static GLintPoint corner[2];
static int numCorners = 0;
if(state == GLUT_DOWN){
if(button == GLUT_LEFT_BUTTON){
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y;
if(++numCorners ==2 ){
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0;
glFlush();
}
}
else if(button == GLUT_RIGHT_BUTTON){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
}
}
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100,150);
glutCreateWindow("mouse dots");
glutDisplayFunc(display);
glutPostRedisplay();
glutMouseFunc(myMouse);
myInit();
glutMainLoop();
}
I'm inferring from the name of the function argument, points, that you intend the function to accept an array of points. But it's actually just written to take one.
So when you write points[index] the compiler is looking for operator[] in your GLintPoint struct (and there isn't one).
If you change the function prototype to take an array of GLintPoints I think you'll have better luck.
Can someone help me out in checking my code? I'm trying to draw a simple dot by clicking in the window, but I can't see any points drawn. The point recording system is copied as an example from one of the stack overflow posts.
#include <stdlib.h>
#include <stdio.h>
//#include <GL\glew.h>
#include <gl/glut.h>
//#include <GL\freeglut.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "draw.h"
//DRAW draw2;
class Point
{
int Xvalue, Yvalue;
std::string Text;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
void text(std::string text)
{
Text = text;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
//return text value
std::string rtext() { return Text; }
};
Point point[30];
int count = 0;
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
//glColor3f(0.0, 0.0, 0.0);
//glPointSize(5);
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glColor3f(1.0, 1.0, 1.0);
//glLoadIdentity();
/*Drawing here*/
//redraw
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
//draw2.Dot(x, y);
std::cout << "drawing dot " << x << " " << y << std::endl;
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f);
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
}
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
//draw2.Dot(x, y);
count++;
}
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv); //initialize toolkit
//Request double buffered true color window with Z-buffer
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); //display mode
glutInitWindowSize(1200, 800); //set window size
glutCreateWindow("NURBS Curve"); //open screen window
Init(); //additional initializations
//CALLBACK FUNCTIONS
glutMouseFunc(mouse);
glutDisplayFunc(Display); //send graphics to display window
/*
GLenum err = glewInit();
if (GLEW_OK != err)
{
fprintf(stderr, "GLEW error");
return 1;
}
*/
//pass control to glut for events, loops
glutMainLoop();
return 0;
}
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f); // wat
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
There's a short list of GL functions you can call inside a glBegin()/glEnd() pair and glPointSize() is not on it:
Only a subset of GL commands can be used between glBegin and glEnd.
The commands are
glVertex,
glColor,
glSecondaryColor,
glIndex,
glNormal,
glFogCoord,
glTexCoord,
glMultiTexCoord,
glVertexAttrib,
glEvalCoord,
glEvalPoint,
glArrayElement,
glMaterial, and
glEdgeFlag.
Also,
it is acceptable to use
glCallList or
glCallLists to execute
display lists that include only the preceding commands.
If any other GL command is executed between glBegin and glEnd,
the error flag is set and the command is ignored.
Move the glPointSize() outside the glBegin()/glEnd() pair:
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
All together:
#include <gl/glut.h>
class Point
{
int Xvalue, Yvalue;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
};
Point point[30];
int count = 0;
void Display()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
gluOrtho2D(0.0, w, 0.0, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
count++;
}
glutPostRedisplay();
}
int main( int argc, char *argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(1200, 800);
glutCreateWindow("NURBS Curve");
glutMouseFunc(mouse);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
Found out that glcolor3f and glpointsize had to be initialized first in my Init() function.
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0); //this one
glPointSize(5); //and this one
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}
I want to make the shape perform a jump while moving horizontally using the sin function but it does not even respond to the 'j' button when it is pressed ?
Am still learning Opengl thouh. Any help about where is the mistake?
#include <GLUT/glut.h>
#include <math.h>
float pointone = 0;
float ydir =0;
GLboolean turn ;
void Display();
void DrawWall();
void Anim();
void Keyboard(unsigned char key, int x, int y);
int main(int argc, char** argr) {
glutInit(&argc, argr);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(50, 50);
glutKeyboardFunc(Keyboard);
glutIdleFunc(Anim);
glutCreateWindow("Kbeer El Haramiya");
glutDisplayFunc(Display);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glPointSize(20.0);
gluOrtho2D(0.0, 1000.0, 0.0, 600.0);
glutMainLoop();
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
DrawWall();
glPushMatrix();
if(pointone<=850 && turn ==true){
pointone+=3;
turn=true;}
else if (pointone==0){
turn=true;}
else {
turn = false;
pointone-=3;
}
glTranslatef(pointone, ydir, 0);
glBegin(GL_POLYGON);
glColor3f(0.97f,0.96f,0.768f);
glVertex2i(0.0f, 0.0f);
glVertex2i(50.0f, 0.0f);
glVertex2i(50.0f, 50.0f);
glColor3f(0.70f,0.196f,0.12f);
glVertex2i(0.0f, 50.0f);
glEnd();
glPopMatrix();
glFlush();
}
void DrawWall(){
glBegin(GL_POLYGON);
glColor3f(0.97f,0.96f,0.768f);
glVertex2i(999, 0);
glVertex2i(999,600);
glVertex2i(900, 600);
glVertex2i(900, 0);
glEnd();
glBegin(GL_POLYGON);
glVertex2i(0, 200);
glVertex2i(700,200);
glVertex2i(700, 150);
glVertex2i(0,150);
glEnd();
}
void Keyboard(unsigned char key, int x, int y){
if(key == 'j') {
for(int i =0; i<361;i++){
ydir =sin(i);
glutPostRedisplay();
}
}
}
void Anim(){
glutPostRedisplay();
}
You have to update ydir somewhere in the Display() function. When you try to update it outside of this in a loop, there is only one single redraw scheduled after the Keyboard function has ended.
The code could look (for example) somehow like this:
int yint = -1; //-1 means no moving
void Display() {
if (yint > 360) // Reset when > 360°
yint = -1;
if (yint >= 0 && yint <= 360) //Update until 360° is reached
yint++;
float ydir = sin(yint);
//Draw code here
}
void Keyboard(unsigned char key, int x, int y){
if(key == 'j')
yint = 0;
}
The solution was simple, I was supposed to Create the window before calling Keyboardfunc ! :)
Just trying to draw a point using glut and glew for opengl version 4.3
my code
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
is not rendering anything, can someone tell me what did i miss? here is full code:
#include <iostream>
using namespace std;
#include "vgl.h"
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOS };
#define WIDTH 1024
#define HEIGHT 768
#define REFRESH_DELAY 10 //ms
//general
long frameCount = 0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
/////////////////////////////////////////////////////////////////////
//! Prototypes
/////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
void timerEvent(int value)
{
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount++);
}
void init (void)
{
// default initialization
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
// viewport
glViewport(0, 0, WIDTH, HEIGHT);
// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1, 10000.0);
}
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("The Abyss");
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount);
if (glewInit()) //i guess this is true on failure
{
cerr << "Error initializing glew, Program aborted." << endl;
exit(EXIT_FAILURE);
}
//Init First
init();
//Init callback for Rendering
glutDisplayFunc(Renderer);
//Main Loop
glutMainLoop();
exit(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////
//!Mouse and keyboard functionality
////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
switch (key)
{
case (27) :
exit(EXIT_SUCCESS);
break;
}
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
mouse_buttons |= 1<<button;
}
else if (state == GLUT_UP)
{
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
}
void motion(int x, int y)
{
float dx, dy;
dx = (float)(x - mouse_old_x);
dy = (float)(y - mouse_old_y);
if (mouse_buttons & 1)
{
rotate_x += dy * 0.2f;
rotate_y += dx * 0.2f;
}
else if (mouse_buttons & 4)
{
translate_z += dy * 0.01f;
}
mouse_old_x = x;
mouse_old_y = y;
}
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
You have requested a Core context. You need to:
Specify a vertex and fragment shader. There are no freebies in Core.
Stop using deprecated functionality like glBegin() and glMatrixMode().
Start using VBOs to submit your geometry.
Start using glDrawArrays() and friends to draw your geometry.