GLUT displayfunc is called at event without callbacksetting - c++

i have a weird situation that display function is called at mouse or key pressed even i didn't set mouse callback or key callback also all variables are stayed constant but object is always moved whatever event is occurred
so there are two problems:
why display func is called at event even there is none of preset of callback func?
why my object or cam is moving even there are no change of variables?
float camX = 0, camY = 2, camZ = 20;
float camXdest = 0, camYdest = 0, camZdest = 0;
float camUpx = 0, camUpy = 1, camUpz = 0;
float JbaseOffest = -15;
float JoffestDegree[3] = { JbaseOffest,0,0 };
float JrotateAxis = 2, JrotateDegree = 0;
float Jsize = 1;
void Reshape(int width, int hieght) {
glViewport(0, 0, width, hieght);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float ratio = width / hieght;
gluPerspective(40, ratio, 0, 50);
}
void Rotate(float degree, int axis) {
if (axis == 0)
glRotatef(degree, 1, 0, 0);
else if (axis == 1)
glRotatef(degree, 0, 1, 0);
else if (axis == 2)
glRotatef(degree, 0, 0, 1);
}
void DrawJ(float dx = 0, float dy = 0, float dz = 0, float rotateDegree = 0, float rotateAxis = 0, float sizeCoefficient = 1) {
glPushMatrix();
//draw head of J
float size = 0.5 * sizeCoefficient;
glTranslatef(dx, dy + size * 4, dz);
glTranslatef(-size * 3, 0, 0);
Rotate(rotateDegree, rotateAxis);
int i = 0;
for (; i < 6; i++) {
glutSolidCube(size);
glTranslatef(size, 0, 0);
}
//draw body of J
glTranslatef(-7 * size / 2, 0, 0);
i = 0;
for (; i < 6; i++) {
glutSolidCube(size);
glTranslatef(0, -size, 0);
}
glPopMatrix();
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
gluLookAt(camX, camY, camZ, camXdest, camYdest, camZdest, camUpx, camUpy, camUpz);
DrawJ(JoffestDegree[0], JoffestDegree[1], JoffestDegree[2], JrotateDegree, JrotateAxis, Jsize);
//DrawS(SoffestDegree[0], SoffestDegree[1], SoffestDegree[2], SrotateDegree, SrotateAxis, Ssize);
//DrawM(MoffestDegree[0], MoffestDegree[1], MoffestDegree[2], MrotateDegree, MrotateAxis, Msize);
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Main");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
//glutKeyboardFunc(CallBackKeyEvent);
glutMainLoop();
}

Related

OpenGL- glFrustum, glLoadIdentity and glLookAt Confusion

I am trying to achieve a perspective view for a rectangular plane in the X-Z axis like below.
The code below almost achieves that.
What am i doing wrong? I find the documentation to be lacking/confusing.
#include <GL/glut.h>
const int RED_COLOR[3] = {255, 0, 0};
// color to draw in
void setDrawColor(const int decimalCodeRGB[3]) {
float denominator = 255.0;
//convert to float
float r = decimalCodeRGB[0] / denominator;
float g = decimalCodeRGB[1] / denominator;
float b = decimalCodeRGB[2] / denominator;
glColor3f(r, g, b); // set draw color
}
void drawPlane() {
GLfloat A[3] = { -1, 0, 1 };
GLfloat B[3] = { 1, 0, 1 };
GLfloat C[3] = { 1, 0, -1 };
GLfloat D[3] = { -1, 0, -1 };
glBegin(GL_POLYGON);
glVertex3fv(A);
glVertex3fv(B);
glVertex3fv(C);
glVertex3fv(D);
glEnd();
}
GLfloat CamX = 0, CamY = 2, CamZ = 1;
// Display Call Back
void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear all drawings in buffer
glLoadIdentity();
gluLookAt(CamX, CamY, CamZ, 0, 0, 0, 0, 1, 0);
drawPlane();
glutSwapBuffers();// Render Now i.e convert Buffer to Picture
return;
}
// Initialization
void initialize()
{
glClearColor(0.1f, 0.1f, 0.1f, 0.1f); // Set Background Color
setDrawColor(RED_COLOR); // sets the drawing color to red
glEnable(GL_DEPTH_TEST); // enable viewing the 3d
glMatrixMode(GL_PROJECTION); // change to perspective projection
glLoadIdentity(); // what does this do?
glFrustum(-1, 1, -1, 1, 2, 10); // what does this do?
glMatrixMode(GL_MODELVIEW); // what does this do?
}
// Main
int main(int argc, char* argv[])
{
glutInit(&argc, argv); // Initialize GLUT
int x = 512, y = 512; // x and y value
glutInitWindowPosition(
(int)(glutGet(GLUT_SCREEN_WIDTH) - x) / 2,
(int)(glutGet(GLUT_SCREEN_HEIGHT) - y) / 2); // Position the window's center
glutInitWindowSize(x, y); // Set the window's initial width & height
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // initialise the buffers needed. double buff, one for RGB color the other for x,y, z
glutCreateWindow("3D Bowling Game"); // Create a window with the given title
initialize(); // Custom initialisation
glutDisplayFunc(draw); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
// End
How do I change the camera to 45 degrees to Z axis?
Am i calling the functions in the right Order?

OpenGL project won't open display window

Even though I have no errors showing from the compiler, the program starts a console and shows the following:
Process returned -1073741819 (0xC0000005) execution time : 2.266 s
Press any key to continue.
I don't know why. I have tried Visual Studio, Codeblocks, and DEV C++
#include <GL/glut.h>
#define RATIO 1.2
#define WW 100
#define WH (WW/RATIO)
#define HALFX ((int)(WW/2))
#define HALFY ((int)(WH/2))
#define deltat 0.001
int WindowWidth;
int WindowHeight;
void Display() {
glLineWidth(4.0);
float StartShape[12][2] = { {-15,-15},{-5,-15},{0,-5},{5,-15},{15,-15},{15,25},{5,25},
{5,-5},{0,0},{-5,-5},{-5,25},{-15,25} };
float EndShape[12][2] = { {-15,-15},{-5,-15},{-5,10},{0,0},{5,10},{5,-15},{15,-15},
{15,25},{5,25},{0,15},{-5,25},{-15,25} };
float IntermediateShape[12][2];
float VertexColors[12][3] = { {1,0,0},{1,1,0},{1,0,1},{0,1,0},{0,1,1},{0,0,1},{1,0.5,0},
{1,0,0.5},{0.5,1,0},{0.5,0,1},{1,0,0.5},{0,1,0.5} };
static float Tween = 0.0 - deltat;
if (Tween < 1) {
Tween += deltat;
}
for (int i = 0; i < 12; i++) {
IntermediateShape[i][0] = (1 - Tween) * StartShape[i][0] + Tween * EndShape[i][0];
IntermediateShape[i][1] = (1 - Tween) * StartShape[i][1] + Tween * EndShape[i][1];
}
glVertexPointer(2, GL_FLOAT, 0, IntermediateShape);
glColorPointer(3, GL_FLOAT, 0, VertexColors);
for (int i = 0; i < 1000000; i++) {
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_LINE_LOOP, 0, 12);
glutSwapBuffers();
glutPostRedisplay();
}
}
void InitGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-HALFX, HALFX, -HALFY, HALFY);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glShadeModel(GL_SMOOTH);
glViewport(0, 0, WindowWidth, WindowHeight);
}
void Reshape(int w, int h) {
glutReshapeWindow(w, (int)(w / RATIO));
WindowWidth = w;
WindowHeight = (int)(w / RATIO);
InitGL();
}
int main(int& argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
WindowWidth = (int)(glutGet((GLenum)GLUT_SCREEN_WIDTH) * 0.4);
WindowHeight = (int)(WindowWidth / RATIO);
glutInitWindowSize(WindowWidth, WindowHeight);
glutInitWindowPosition(((int)glutGet((GLenum)GLUT_SCREEN_WIDTH) * 0.1),
(glutGet((GLenum)GLUT_SCREEN_WIDTH) / 2) - (WindowHeight / 2));
glutCreateWindow("Bilguun Erdenebaatar Tweening Midterm Exam");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
InitGL();
glutMainLoop();
return 0;
}

adding mouse event to change the color of simple circle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I draw a circle and trying to add mouse event, that change the color of the circle i cannot find good sources
Here is the whole code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define window_width 1080
#define window_height 720
void drawFilledSun() {
//static float angle;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -10);
int i, x, y;
double radius = 0.30;
//glColor3ub(253, 184, 19);
glColor3ub(255, 0, 0);
double twicePi = 2.0 * 3.142;
x = 0, y = 0;
glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
glVertex2f(x, y); // center of circle
for (i = 0; i <= 20; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi / 20)))
);
}
glEnd(); //END
}
void main_loop_function() {
int c;
drawFilledSun();
glutSwapBuffers();
c = getchar();
}
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluPerspective(45, (float)width / height, .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("GLUT Example!!!");
glutIdleFunc(main_loop_function);
GL_Setup(window_width, window_height);
glutMainLoop();
}
Add an array for the red green and blue color components of the sun and a color index:
int color_i = 0;
GLubyte color[][3] = { {255, 0, 0 }, {253, 184, 19} };
Use the color arrays to set the color attribute
void drawFilledSun() {
// [...]
glColor3ubv(color[color_i]);
Get rid of the getchar in main_loop_function, it prevents the window from responding. But call glutPostRedisplay for continuously updating the window:
void main_loop_function() {
drawFilledSun();
glutSwapBuffers();
glutPostRedisplay();
}
Use glutDisplayFunc rather than glutIdleFunc and add a glutMouseFunc callback:
int main(int argc, char** argv) {
// [...]
glutDisplayFunc(main_loop_function);
glutMouseFunc( mouseFunc );
Change the color index in the mouse callback:
void mouseFunc(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
color_i = (color_i == 0) ? 1 : 0;
}
}
See the example:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define window_width 1080
#define window_height 720
int color_i = 0;
GLubyte color[][3] = { {255, 0, 0 }, {253, 184, 19} };
void drawFilledSun() {
//static float angle;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -10);
int i, x, y;
double radius = 0.30;
glColor3ubv(color[color_i]);
double twicePi = 2.0 * 3.142;
x = 0, y = 0;
glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
glVertex2f(x, y); // center of circle
for (i = 0; i <= 20; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi / 20)))
);
}
glEnd(); //END
}
void main_loop_function() {
drawFilledSun();
glutSwapBuffers();
glutPostRedisplay();
}
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluPerspective(45, (float)width / height, .1, 100);
glMatrixMode(GL_MODELVIEW);
}
void mouseFunc(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
color_i = (color_i == 0) ? 1 : 0;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("GLUT Example!!!");
glutDisplayFunc(main_loop_function);
glutMouseFunc( mouseFunc );
GL_Setup(window_width, window_height);
glutMainLoop();
}
GLFW provided some callback functions which you need such as glfwSetCursorPosCallback.
In this case you can simply check it by a if branch while drawing, main_loop_function in your code. The rest work is about C/C++, and there are many paths to reach it such as kbhit.
By the way, it's not elegant to ues several naming styles.

OpenGL - wrong objects positioning - gluLookAt()

I've got a taks from university and have to make a small example of solar system, the objects have to rotate etc. The problem is that when I do not call GluLookAt() everything looks fine, but I would like to change the view and when I call the function, there occurs that one orbit renders completely strangely.
I do not know if problem is with wrong creation of the first orbit, or with the proper values in gluLookAt parameters. Can anyone help?
Here's how it looks without calling gluLookAt():
Here's how it looks after gluLookAt():
Here's the code:
#include "stdafx.h"
#include <GL\glut.h>
#include <math.h>
GLfloat yRotated=1;
GLfloat movement = 0;
void drawCircle(float r) { // radius
glBegin(GL_LINE_LOOP);
for (int i = 0; i <= 300; i++) {
double angle = 2 * 3.14 * i / 300;
double x = r*cos(angle);
double y = r*sin(angle);
glVertex3d(x, y, -5.5);
}
glEnd();
}
void display(void) {
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//gluLookAt(5, 5, 5, 0, 0, -8, 0, 1, 0); // 3rd coordinate - depth
float radius1 = 6;
float radius2 = 1;
//first orbit
glColor3f(1, 1, 1);
glPushMatrix();
glTranslatef(0, 0, -5.5);
drawCircle(radius1);
glPopMatrix();
//second orbit with rotation
glPushMatrix();
glRotatef(yRotated, 0, 0, 1);
glPushMatrix();
glTranslatef(radius1 / 2, 0, 0);
drawCircle(radius2);
glPopMatrix();
glPopMatrix();
//first czajnik
glColor3f(0.8, 0.2, 0.1);
glPushMatrix();
glTranslatef(0.0, 0.0, -5.5);
// glScalef(1.0, 1.0, 1.0);
glRotatef(yRotated, 0, 0, 1);
glRotatef(90, 1, 0, 0);
glutSolidSphere(1,20,20);
//second czajnik
glPushMatrix();
glColor3f(0, 0, 1);
glTranslatef(radius1/2, 0, 0);
glRotatef(yRotated, 0, 1, 0);
glutSolidSphere(0.5, 20, 20);
//third czajnik
glPushMatrix();
glTranslatef(radius2, 0, 0);
glColor3f(1, 1, 0);
glRotatef(yRotated, 0, 1, 0);
glutSolidSphere(0.2, 20, 20);
glPopMatrix();
//second czajnik pop
glPopMatrix();
//first czajnik pop
glPopMatrix();
glFlush();
}
void idle() {
yRotated += 0.1;
Sleep(2);
display();
}
void myReshape(int w, int h) {
if (w == 0 || h == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0, (GLdouble)w / (GLdouble)h, 0.5, 20.0);
glViewport(0, 0, w, h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(900, 600);
glutCreateWindow("Solar system");
//window with a title
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glClearColor(0, 0, 0, 1.0);
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
Some of your objects are at different z values, e.g. 1st orbit at -5.5, second at 0, because you "popped" the matrix.
In general, do not do so many push\pops nested into each other, matrix stack isn't made of rubber.
There is more efficient circle drawing procedure than to calculate sine and cosine for each step, e.g. to get advantage of circle being a figure of rotation:
inline void circle(F32 r, U32 quality)
{
if (r < F_ALMOST_ZERO) return;
F32 th = M_PI /(quality-1);
F32 s = sinf(th);
F32 c = cosf(th);
F32 t;
F32 x = r;
F32 y = 0;
::glBegin (GL_LINE_LOOP);
for(U32 i = 0; i < quality; i++)
{
glVertex2f(x, y);
t = x;
x = c*x + s*y;
y = -s*t + c*y;
}
::glEnd();
}
it can be optimized further by using symmetry, but this one is the basis.

Segmentation Fault on OpenGL program

I'm trying to draw the Sierpinski carpet plane fractal using OpenGL, but my program keeps receiving a SegFault error.
My code is as follows:
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
class GLintPoint
{
public:
GLint x, y;
};
int random(int m)
{
return rand() % m;
}
void drawDot(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
int isSierpinskiCarpetPixelFilled(int x, int y, int width, int height)
{
GLintPoint point;
// base case 1 of 2
if ((x <= 0)||(y <= 0)||(x>=width)||(y>=height)) //top row or left column or out of bounds should be full
{
point.x = x;
point.y = y;
drawDot(point.x, point.y);
}
{
/*
If the grid was split in 9 parts, what part(x2,y2) would x,y fit into?
*/
int x2 = x * 3 / width; // an integer from 0..2 inclusive
int y2 = y * 3 / height; // an integer from 0..2 inclusive
// base case 2 of 2
if (x2 == 1 && y2 == 1) // if in the center square, it should be empty
return 0;
// general case
/* offset x and y so it becomes bounded by 0..width/3 and 0..height/3
and prepares for recursive call
some offset is added to make sure the parts have all the correct size when
width and height isn't divisible by 3 */
x -= (x2 * width+2) / 3;
y -= (y2 * height+2) / 3;
width = (width +2-x2)/3;
height = (height+2-y2)/3;
}
return isSierpinskiCarpetPixelFilled(x, y, width, height);
}
void drawSierpinskiCarpet()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
int x = 50;
int y = 50;
isSierpinskiCarpetPixelFilled(x,y,50,50);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("The Sierpinski Carpet");
glutDisplayFunc(drawSierpinskiCarpet);
init();
glutMainLoop();
return EXIT_SUCCESS;
}
It is likely getting a stack overflow. It appears to be recursing infinitely. Each call to isSierpinskiCarpetPixelFilled results in another call:
return isSierpinskiCarpetPixelFilled(x, y, width, height);
With the given input values, the input parameters (in order) will be:
50, 50, 50, 50
0, 0, 16, 16
0, 0, 6, 6
0, 0, 2, 2
0, 0, 1, 1
0, 0, 1, 1
... (until stack overflow)