Related
The code was working fine but after I added the void projection(float), it's giving me unqaulified-id error inside the projection function. I'm using CodeBlocks IDE, MinGW. Please help me.
This is the error I'm getting:
||=== Build: Debug in L3 (compiler: GNU GCC Compiler) ===|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp||In function 'void projection(float)':|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp|56|error: expected unqualified-id before '=' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp|57|error: expected primary-expression before ',' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp| 57|error: expected primary-expression before ')' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp||In function 'void specialKeyboard(int, int, int)':|
||=== Build failed: 3 error(s), 15 warning(s) (0 minute(s), 0 second(s)) ===|
Moreover, if I comment everything inside projection, I get this error:
obj\Debug\src\Display.o||In function `glutInit_ATEXIT_HACK':|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `keyboardKey'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `mouseX'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `mouseY'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `toggleProjection'|
CallbackFunctions.h
#ifndef CALLBACKFUNCTIONS_H
#define CALLBACKFUNCTIONS_H
#include "GL/freeglut.h"
unsigned char keyboardKey;
char *specialKeyboardKey ="";
char *modifier="";
char *mouseButtonPressed="";
char *mouseState="";
int mouseX, mouseY;
int toggleProjection=0;
char *view="";
/* Hardware Interrupts callback functions */
void display(); //called every single frame
void reshape(int width, int height); //called whenever the dimensions of the window are changed
void idle(); //when there is no interaction with the window
void keyboard(unsigned char key, int x, int y); //whenever a keyboard key is pressed/interrupt occurs
void specialKeyboard(int button, int x, int y); //captures special keys like: SHIFT ALT CTRL F1.....
void mouse(int button, int state, int x, int y); //captures the mouse clicks and their state
void mouseMotion(int x, int y); //captures the motion of the mouse pointer in the window
void projection(float asp);
#endif // CALLBACKFUNCTIONS_H
CallbackFunctions.cpp
#include "../include/CallbackFunctions.h"
#include "../include/Text.h"
extern int dim;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//Drawing stuff
glFlush();
glutSwapBuffers();
}
void idle()
{
display();
}
/* This callback function is called whenever the window is resized or reshaped */
void reshape(int width, int height)
{
//Width To Height Ratio
float w2h = (height>0)? (float)width/height : 1;
glViewport(0, 0, width, height);
projection(w2h);
}
void projection(float asp)
{
//Setting up the Viewing Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(toggleProjection==0)
{
glOrtho(-dim*asp, +dim*asp, -dim, +dim, -dim, +dim);
}
else
{
float fov = 55.0, near = asp/5, far = asp*5;
gluPerspective(fov, asp, near, far);
//To do
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* This callback function is called when there is no activity in the window */
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
}
keyboardKey = key;
glutPostRedisplay();
}
/* This callback function is called whenever a keyboard interrupt is encountered */
void specialKeyboard(int key, int state, int a)
{
int currentModifier = glutGetModifiers();
switch(currentModifier)
{
case GLUT_ACTIVE_SHIFT:
modifier = "SHIFT";
break;
case GLUT_ACTIVE_CTRL:
modifier = "CTRL";
break;
case GLUT_ACTIVE_ALT:
modifier = "ALT";
break;
}
switch(key)
{
case GLUT_KEY_F1:
if(toggleProjection=0)
{
toggleProjection = 1;
view = "perspective";
break;
}
else
{
toggleProjection = 0;
view = "orthographic";
break;
}
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
mouseButtonPressed = "Left";
else if(button == GLUT_RIGHT_BUTTON)
mouseButtonPressed = "Right";
if(state == GLUT_DOWN)
mouseState = "down";
else if(state == GLUT_UP)
mouseState = "up";
}
void mouseMotion(int x, int y)
{
mouseX = x;
mouseY = y;
}
/*void info()
{
displayTextAt(-130, -40, 0, "mouseButton: %s", mouseButtonPressed);
displayTextAt(-130, -50, 0, "mouseButtonState: %s", mouseState);
displayTextAt(-130, -60, 0, "mouseX: %d", mouseX);
displayTextAt(-130, -70, 0, "mouseY: %d", mouseY);
displayTextAt(-130, -80, 0, "keyboard key: %c", keyboardKey);
displayTextAt(-130, -90, 0, "modifier: %s", modifier);
displayTextAt(70, -90, 0, view);
}*/
This is really a global variable declaration in a header problem. I've included the pastebin code (with fixes) for posterity.
/*This is the CallbackFunctions.h ------------------ */
#ifndef CALLBACKFUNCTIONS_H
#define CALLBACKFUNCTIONS_H
#include <GL/freeglut.h>
extern unsigned char keyboardKey;
/* Hardware Interrupts callback functions */
void display(); //called every single frame
void keyboard(unsigned char key, int x, int y); //whenever a keyboard key is pressed/interrupt occurs
#endif // CALLBACKFUNCTIONS_H
/*This is the CallbackFunctions.cpp ------------------ */
#include "CallbackFunctions.h"
unsigned char keyboardKey = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void keyboard(unsigned char key, int x, int y)
{
keyboardKey = key;
}
/* --------------- This is the Main.cpp ---------------- */
#include <GL/freeglut.h>
#include "CallbackFunctions.h"
#define WIDTH 700
#define HEIGHT 400
void setupGlutWindow(int *argc, char **argv, int width, int height);
void setupCallbackFunctions();
int main(int argc, char** argv)
{
setupGlutWindow(&argc, argv, WIDTH, HEIGHT);
setupCallbackFunctions();
glutMainLoop();
}
void setupGlutWindow(int *argc, char **argv, int width, int height)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(700, 500);
glutCreateWindow("Project L3");
glViewport(0, 0, width, height);
}
void setupCallbackFunctions()
{
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
}
I can't get my GLUT program to work properly.I want to draw text (a char array) letter by letter with some miliseconds interval. My program:
#include <string.h>
#include <windows.h>
#include <glut.h>
#include<iostream>
int hei = glutGet(GLUT_SCREEN_HEIGHT)/2;
int wid = glutGet(GLUT_SCREEN_WIDTH)/2;
void *font = GLUT_BITMAP_TIMES_ROMAN_24;
void *fonts[] =
{
GLUT_BITMAP_9_BY_15,
GLUT_BITMAP_TIMES_ROMAN_10,
GLUT_BITMAP_TIMES_ROMAN_24
};
void selectFont(int newfont)
{
font = fonts[newfont];
glutPostRedisplay();
}
void selectColor(float r, float g, float b)
{
glColor3f(r, g, b);
glutPostRedisplay();
}
char *msg;
void tick(void)
{
glutPostRedisplay();
}
void timer(int value) {
}
int element=0;
void output(int x, int y, char *string, bool center=true) {
int len;
len = strlen(string);
if(center){
glRasterPos2f((wid-len), y);
}
else {
glRasterPos2f(x, y);
}
for(int i=0;i<len;i++) {
glutBitmapCharacter(font, string[i]);
glPopMatrix();
glutSwapBuffers();
Sleep(500);
}
}
void outputmsg(int x, int y, bool center=true) {
int len;
len = strlen(msg);
if(center){
glRasterPos2f((wid-len), y);
}
else {
glRasterPos2f(x, y);
}
for(int i=0;i<len;i++) {
glutBitmapCharacter(font, msg[i]);
}
}
void updatemsg() {
msg="test msg letter by letter";
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
msg="|";
updatemsg();
if(msg!="|")
output(wid, hei, msg, true);
else
output(wid,hei,msg,true);
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y) {
switch(key){
case 27:
exit(1);
break;
}
}
int main(int argc, char **argv)
{
int i, msg_submenu, color_submenu;
glutInit(&argc, argv);
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-mono")) {
font = GLUT_BITMAP_9_BY_15;
}
}
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(wid*2, hei*2);
glutCreateWindow("CHAT");
glutFullScreen();
glClearColor(0.0, 0.0, 0.0, 1.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(tick);
selectFont(0);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
It does really weird stuff like erasing the previus one char then printing then printing the next one and the erased one. I tried everything what i could find about this topic on the forums. How can i fix it?
What you should do is also hold the length of the string you want to display and increment it every few milliseconds:
int len;
char *msg;
void incrementLength(int value){
if(msg[len]==0)return; //end of string stop the callback
len++;
glutTimerFunc(100, incrementLength, 0);
glutPostRedisplay();
}
void updatemsg() {
msg="test msg letter by letter";
len=0;
glutTimerFunc(100, incrementLength, 0);
}
And then in outputmsg (if you actually call it) you only loop until len:
void outputmsg(int x, int y, bool center=true) {
if(center){
glRasterPos2f((wid-len), y);
}
else {
glRasterPos2f(x, y);
}
for(int i=0;i<len;i++) {
glutBitmapCharacter(font, msg[i]);
}
}
This code has a couple of issues:
for(int i=0;i<len;i++) {
glutBitmapCharacter(font, string[i]);
glPopMatrix();
glutSwapBuffers();
Sleep(500);
}
You are popping a matrix that you never pushed anywhere, and you are doing it len-many times.
You are swapping buffers without clearing the screen in-between... that's going to lead to undefined results (generally a garbled mess of characters from previous frames).
You really need to re-think how you've implemented this.
I would suggest you implement some elapsed time check in your display (...) function that determines which character to print, rather than doing this in a loop that calls Sleep (...).
I am trying to draw a line based on the mouse input coordinates.
The following code compiles, however, a line is not displayed from mouse input.
int coord_1[2]; //(x,y) of first point for line
int coord_2[2]; //(x,y) of second point for line
bool ready_1=false;
bool ready_2=false;
void drawLine()
{
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(coord_1[0], coord_1[1]);
glVertex2f(coord_2[0], coord_2[1]);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1,1,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(ready_1 && ready_2)
{
drawLine();
ready_1 = ready_2 = false;
}
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN) //button pressed down
{
//only do something on release
}
if (state == GLUT_UP) //released button
{
cout << "right button up" << endl;
}
break;
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) //button pressed down
{
coord_1[0]=x;
coord_2[1]=y;
cout << "first pos" << endl;
ready_1=true;
}
if (state == GLUT_UP) //released button
{
coord_2[0]=x;
coord_2[1]=y;
cout << "second pos" << endl;
ready_2=true;
}
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA|GLUT_MULTISAMPLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(512,512);
glutCreateWindow("Asn 1");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(processNormalKeys);
// GLUT main loop
glutMainLoop();
return(0);
}
Any help or pointers in the right direction?
You probably draw the line, but since you reset your drawing condition afterwards, your line gets hidden in the next draw step (which might be VERY quick).
Remove the ready_1 = ready_2 = false; and the line should stay visible.
Instead you can set ready_2 = false when you receive a left button down event.
here is my code:
#include <stdlib.h>
#include <iostream>
#include <gl/glew.h>
#include <gl/freeglut.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <SDL.h>
using namespace std;
typedef unsigned short us;
typedef unsigned char uc;
bool *keystates = new bool[256];
bool *skeystates= new bool[256];
uc dir_m[8][2]={
{ 0, 1 },
{ 0,-1 },
{ -1,0 },
{ 1,0 },
{ 1,1},
{ 1,-1 },
{ -1,-1 },
{ -1,1 }
};
us totalbots=15;
float zoomlvl=-70.f;
float camx=0.f,camy=0.f;
struct bot_data{
float x,y,z;
float r,g,b;
float size; // "radius" of the square
us coold;
us age;
us trans:5;
us dir:3,fac:2;
bot_data(void);
void move(void);
void deliver(us bot);
};
struct bool_data{
bool ch:1,ch2:1;
};
bot_data::bot_data(void){
size=0.25f;
fac=rand()%4;
switch(fac){
case 0:x=10.f,y=10.f,z=0.f;
r=1.0f,g=0.f,b=0.f;
break;
case 1:x=-10.f,y=-10.f,z=0.f;
r=0.0f,g=0.f,b=1.f;
break;
case 2:x=-10.f,y=10.f,z=0.f;
r=1.0f,g=1.f,b=0.f;
break;
case 3:x=10.f,y=-10.f,z=0.f;
r=0.0f,g=1.f,b=0.f;
break;
}
coold=0;
trans=0;
age=0;
dir=rand()%8;
}
bot_data *cubers=new bot_data[65536];
bool_data *bools=new bool_data;
void bot_data::move(void){
float move=size/10.f;
switch(dir){
case 0:y+=move;
break;
case 1:y-=move;
break;
case 2:x-=move;
break;
case 3:x+=move;
break;
case 4:x+=move;
y+=move;
break;
case 5:x+=move;
y-=move;
break;
case 6:y-=move;
x-=move;
break;
case 7:y+=move;
x-=move;
break;
}
}
void bot_data::deliver(us bot){
age=-10;
totalbots+=3;
float tmp=size-(size/4.f);
size=0.25f;
x-=size;
y+=size;
for(uc i=1;i<=3;i++){
cubers[totalbots-i].fac=fac;
switch(fac){
case 0:cubers[totalbots-i].r=1.0f,cubers[totalbots-i].g=0.f,cubers[totalbots-i].b=0.f;
break;
case 1:cubers[totalbots-i].r=0.0f,cubers[totalbots-i].g=0.f,cubers[totalbots-i].b=1.f;
break;
case 2:cubers[totalbots-i].r=1.0f,cubers[totalbots-i].g=1.f,cubers[totalbots-i].b=0.f;
break;
case 3:cubers[totalbots-i].r=0.0f,cubers[totalbots-i].g=1.f,cubers[totalbots-i].b=0.f;
break;
}
cubers[totalbots-i].coold=coold;
cubers[totalbots-i].size=size;
switch(i){
case 1:cubers[totalbots-i].x=x;
cubers[totalbots-i].y=y-size*2;
break;
case 2:cubers[totalbots-i].x=x+size*2;
cubers[totalbots-i].y=y;
break;
case 3:cubers[totalbots-i].x=x+size*2;
cubers[totalbots-i].y=y-size*2;
break;
}
}
}
void initkeys(void){
for(uc i=0;i<255;i++){
keystates[i]=false;
skeystates[i]=false;
}
}
void p_keys(unsigned char key, int x, int y){
keystates[key]=true;
}
void keyup(unsigned char key, int x, int y){
keystates[key]=false;
}
void sp_keys(int key, int x, int y){
skeystates[key]=true;
}
void skeyup(int key, int x, int y){
skeystates[key]=false;
}
void key_func(void){
if (keystates['z']){
if(skeystates[GLUT_KEY_UP])zoomlvl+=1.f;
else if (skeystates[GLUT_KEY_DOWN])zoomlvl-=1.f;
if(zoomlvl<-100.0f)zoomlvl=-100.f;
else if(zoomlvl>-5.f)zoomlvl=-5.f;
}
else{
if(skeystates[GLUT_KEY_UP])camy-=1.f;
else if(skeystates[GLUT_KEY_DOWN])camy+=1.f;
}
if(skeystates[GLUT_KEY_LEFT])camx+=1.f;
else if(skeystates[GLUT_KEY_RIGHT])camx-=1.f;
}
void render_p(us bot){
glColor3f(cubers[bot].r,cubers[bot].g,cubers[bot].b);
glBegin(GL_QUADS);
glVertex2f(cubers[bot].size,cubers[bot].size);
glVertex2f(cubers[bot].size,-cubers[bot].size);
glVertex2f(-cubers[bot].size,-cubers[bot].size);
glVertex2f(-cubers[bot].size,cubers[bot].size);
glEnd();
}
void process_cubers(void){
for(us i=0;i<totalbots;i++){
//glPushMatrix();
glLoadIdentity();
glTranslatef(cubers[i].x,cubers[i].y,cubers[i].z);
if(cubers[i].coold==0){
cubers[i].move();
cubers[i].trans++;
if(cubers[i].trans==20){
cubers[i].trans=0;
cubers[i].coold=50;
if(cubers[i].age<100){
cubers[i].size+=0.025f;
}
else if(cubers[i].age==150){
cubers[i].deliver(i);
}
cubers[i].dir=rand()%8;
cubers[i].age+=10;
}
}
else cubers[i].coold--;
render_p(i);
//glPopMatrix();
}
}
void display(void){
key_func();
glClearColor(0.6f,0.6f,0.6f,1.f);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(camx,camy,zoomlvl);
process_cubers();
glutSwapBuffers();
SDL_Delay(1000/30);
glutPostRedisplay();
}
void resize(int width,int height){
glViewport(0,0,(GLsizei)width,(GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,(GLfloat)width/(GLfloat)height,1.f,100.f);
glMatrixMode(GL_MODELVIEW);
}
int main (int argc, char **argv) {
initkeys();
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (640, 480);
glutInitWindowPosition (0, 0);
glutCreateWindow ("opengl 1");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(resize);
glutKeyboardFunc(p_keys);
glutKeyboardUpFunc(keyup);
glutSpecialFunc(sp_keys);
glutSpecialUpFunc(skeyup);
glutMainLoop();
return 0;
}
it renders a certain amount of said squares that move each certain intervals. their move distance is their size. each time they move they grow in size until they reach the size limit (1.f in radius in this case) after a certain amount of moves (15 in this case) the square will split into 4 smaller squares and FILLING OUT THE EXACT SPACE its precursor occupied, that means it is supposed to look completely seamless..the problem is instead, i get this split second tear & glitch-looking rendering after the square splits, but other than that i found no other problems.
what i suspect is perhaps i missed some gl function i was supposed to call around the time i am rendering the new 3 squares (the splitting occurs by adding 3 new squares, and shrinking the current one to a quarter of its size, then adjusting all the squres.)
Try this:
#include <GL/glew.h>
#include <GL/glut.h>
typedef unsigned short us;
typedef unsigned char uc;
bool *keystates = new bool[256];
bool *skeystates= new bool[256];
uc dir_m[8][2]=
{
{ 0, 1 },
{ 0,-1 },
{ -1,0 },
{ 1,0 },
{ 1,1},
{ 1,-1 },
{ -1,-1 },
{ -1,1 }
};
us totalbots=15;
float zoomlvl=-70.f;
float camx=0.f,camy=0.f;
struct bot_data
{
float x,y,z;
float r,g,b;
float size; // "radius" of the square
us coold;
us age;
us trans:5;
us dir:3,fac:2;
bot_data(void);
void move(void);
void deliver(us bot);
};
struct bool_data
{
bool ch:1,ch2:1;
};
bot_data::bot_data(void)
{
size=0.25f;
fac=rand()%4;
switch(fac){
case 0:x=10.f,y=10.f,z=0.f;
r=1.0f,g=0.f,b=0.f;
break;
case 1:x=-10.f,y=-10.f,z=0.f;
r=0.0f,g=0.f,b=1.f;
break;
case 2:x=-10.f,y=10.f,z=0.f;
r=1.0f,g=1.f,b=0.f;
break;
case 3:x=10.f,y=-10.f,z=0.f;
r=0.0f,g=1.f,b=0.f;
break;
}
coold=0;
trans=0;
age=0;
dir=rand()%8;
}
bot_data *cubers=new bot_data[65536];
bool_data *bools=new bool_data;
void bot_data::move(void)
{
float move=size/10.f;
switch(dir){
case 0:y+=move;
break;
case 1:y-=move;
break;
case 2:x-=move;
break;
case 3:x+=move;
break;
case 4:x+=move;
y+=move;
break;
case 5:x+=move;
y-=move;
break;
case 6:y-=move;
x-=move;
break;
case 7:y+=move;
x-=move;
break;
}
}
void bot_data::deliver(us bot)
{
age=-10;
totalbots+=3;
float tmp=size-(size/4.f);
size=0.25f;
x-=size;
y+=size;
for(uc i=1;i<=3;i++){
cubers[totalbots-i].fac=fac;
switch(fac){
case 0:cubers[totalbots-i].r=1.0f,cubers[totalbots-i].g=0.f,cubers[totalbots-i].b=0.f;
break;
case 1:cubers[totalbots-i].r=0.0f,cubers[totalbots-i].g=0.f,cubers[totalbots-i].b=1.f;
break;
case 2:cubers[totalbots-i].r=1.0f,cubers[totalbots-i].g=1.f,cubers[totalbots-i].b=0.f;
break;
case 3:cubers[totalbots-i].r=0.0f,cubers[totalbots-i].g=1.f,cubers[totalbots-i].b=0.f;
break;
}
cubers[totalbots-i].coold=coold;
cubers[totalbots-i].size=size;
switch(i){
case 1:cubers[totalbots-i].x=x;
cubers[totalbots-i].y=y-size*2;
break;
case 2:cubers[totalbots-i].x=x+size*2;
cubers[totalbots-i].y=y;
break;
case 3:cubers[totalbots-i].x=x+size*2;
cubers[totalbots-i].y=y-size*2;
break;
}
}
}
void initkeys(void)
{
for(uc i=0;i<255;i++){
keystates[i]=false;
skeystates[i]=false;
}
}
void p_keys(unsigned char key, int x, int y)
{
keystates[key]=true;
}
void keyup(unsigned char key, int x, int y)
{
keystates[key]=false;
}
void sp_keys(int key, int x, int y)
{
skeystates[key]=true;
}
void skeyup(int key, int x, int y)
{
skeystates[key]=false;
}
void key_func(void)
{
if (keystates['z'])
{
if(skeystates[GLUT_KEY_UP])zoomlvl+=1.f;
if(skeystates[GLUT_KEY_DOWN])zoomlvl-=1.f;
if(zoomlvl<-100.0f)zoomlvl=-100.f;
if(zoomlvl>-5.f)zoomlvl=-5.f;
}
else
{
if(skeystates[GLUT_KEY_UP])camy-=1.f;
if(skeystates[GLUT_KEY_DOWN])camy+=1.f;
if(skeystates[GLUT_KEY_LEFT])camx+=1.f;
if(skeystates[GLUT_KEY_RIGHT])camx-=1.f;
}
}
void render_p(us bot)
{
glColor3f(cubers[bot].r,cubers[bot].g,cubers[bot].b);
glBegin(GL_QUADS);
glVertex2f(cubers[bot].size,cubers[bot].size);
glVertex2f(cubers[bot].size,-cubers[bot].size);
glVertex2f(-cubers[bot].size,-cubers[bot].size);
glVertex2f(-cubers[bot].size,cubers[bot].size);
glEnd();
}
void process_cubers(void)
{
for(us i=0;i<totalbots;i++){
if(cubers[i].coold==0){
cubers[i].move();
cubers[i].trans++;
if(cubers[i].trans==20){
cubers[i].trans=0;
cubers[i].coold=50;
if(cubers[i].age<100){
cubers[i].size+=0.025f;
}
else if(cubers[i].age==150){
cubers[i].deliver(i);
}
cubers[i].dir=rand()%8;
cubers[i].age+=10;
}
}
else cubers[i].coold--;
}
for(us i=0;i<totalbots;i++)
{
glPushMatrix();
glTranslatef(cubers[i].x,cubers[i].y,cubers[i].z);
render_p(i);
glPopMatrix();
}
}
void display(void)
{
key_func();
glClearColor(0.6f,0.6f,0.6f,1.f);
glClear (GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(60, w / h,1.f,100.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(camx,camy,zoomlvl);
process_cubers();
glutSwapBuffers();
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(33, timer, 0);
}
int main (int argc, char **argv)
{
initkeys();
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize (640, 480);
glutInitWindowPosition (0, 0);
glutCreateWindow ("opengl 1");
glutDisplayFunc(display);
glutKeyboardFunc(p_keys);
glutKeyboardUpFunc(keyup);
glutSpecialFunc(sp_keys);
glutSpecialUpFunc(skeyup);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
You were drawing in your simulation loop. This splits that into two loops, sim then draw.
The way graphics rendering works (OpenGL or otherwise) is that it only guarantees a "nice" seam between two triangles that are "exactly" aligned along some edge if you render them as part of the same triangle strip, the same (vertex buffer, index buffer) model pair, or some other construct that groups triangles into a single render unit.
When you render a "quad," you are really rendering a 2-triangle triangle strip, so the seam between the two triangles looks nice.
When you render two seperate quads that are aligned along some edge, the edge can look bad. Anti-aliasing can help to fix the problem a little bit, but ultimately you need to use a single render unit if you want to get rid of the tearing along seams.
The figure shows four quads (eight triangles) being rendered in a grid. The diagonals are within quads so there isn't any tearing, but the since the quads are all rendered separately there is tearing along their seams.
I wanna use up, down, left, right as part of the controls of an opengl + glut application. How do I refer to the keys inside my 'keyboard' function (the one that I pass to glutKeyboardFunc)?
You need glutSpecialFunc.
For example -
#include<GL/gl.h>
#include<GL/glut.h>
#include<stdio.h>
void
init(void)
{
/*initialize the x-y co-ordinate*/
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-320, 319,-240, 239);
glClear (GL_COLOR_BUFFER_BIT);
glFlush();
}
void
catchKey(int key, int x, int y)
{
if(key == GLUT_KEY_LEFT)
printf("Left key is pressed\n");
else if(key == GLUT_KEY_RIGHT)
printf("Right key is pressed\n");
else if(key == GLUT_KEY_DOWN)
printf("Down key is pressed\n");
else if(key == GLUT_KEY_UP)
printf("Up key is pressed\n");
}
int
main(int argc, char *argv[])
{
double x_0, y_0, x_1, y_1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(400, 400);
glutCreateWindow("Special key");
init();
glutSpecialFunc(catchKey);
glutMainLoop();
}
your functions should be something like these
void handleSpecialKeypress(int key, int x, int y) {
switch (key) {
case GLUT_KEY_LEFT:
isLeftKeyPressed = true;
if (!isRightKeyPressed) {
DO SOMETHING HERE;
}
break;
case GLUT_KEY_RIGHT:
isRightKeyPressed = true;
if (!isLeftKeyPressed) {
DO SOMETHING HERE;
}
break;
}
}
void handleSpecialKeyReleased(int key, int x, int y) {
switch (key) {
case GLUT_KEY_LEFT:
isLeftKeyPressed = false;
break;
case GLUT_KEY_RIGHT:
isRightKeyPressed = false;
break;
}
}
and you also need to invoke these in your main() method
glutSpecialFunc(handleSpecialKeypress);
glutSpecialUpFunc(handleSpecialKeyReleased);
The variables isRightKeyPressed and isLeftKeyPressed are global variables I defined.