PDA

View Full Version : Texture in sequence



derin86
03-31-2014, 01:41 AM
Hello,
I am quite new to opengl, and this is my first discussion on this forum, I hope to not ask silly things and that this is the right section for asking this kind of things.

I am using opengl with c++ and with visual studio pro 2008.
I have to develop a code that gives in sequence a cross and then a column that have to be filled untill a specific level. Unitil now I developed the column and using a for loop I filled it. I know also how to draw a cross, but my problem is how to display them in sequence (E.G. the cross for 3 seconds and the column for 5 seconds). The code is the sequent. Thanks to all in advance.


#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>

static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
unsigned int rgba = 0xFFFFFFFF; // white
unsigned int rgba1 = 0x00000000; // black
unsigned int rgba2 = 0x00EA00FF; //green
unsigned int rgba3 = 0xFF0000FF; //red
double i;

for ( i = -2.7;i < 3.8;i+= 0.001)
{
//rectangle
glBegin(GL_QUADS);

//rectangle white
glColor4f(((rgba>>24)&0xff)/255.0f,
((rgba>>16)&0xff)/255.0f,
((rgba>>8)&0xff)/255.0f,
(rgba&0xff)/255.0f);

glVertex3f(-3,-3,-10);
glVertex3f(3,-3,-10);
glVertex3f(3,4,-10);
glVertex3f(-3,4,-10);

//rectangle black
glColor4f(((rgba1>>24)&0xff)/255.0f,
((rgba1>>16)&0xff)/255.0f,
((rgba1>>8)&0xff)/255.0f,
(rgba1&0xff)/255.0f);

glVertex3f(-2.9,-2.9,-10);
glVertex3f(2.9,-2.9,-10);
glVertex3f(2.9,3.9,-10);
glVertex3f(-2.9,3.9,-10);

//rectangle feedback(green/red)
if (i<=1.5)
{
glColor4f(((rgba2>>24)&0xff)/255.0f,
((rgba2>>16)&0xff)/255.0f,
((rgba2>>8)&0xff)/255.0f,
(rgba1&0xff)/255.0f);

glVertex3f(-2.7,-2.7,-10);
glVertex3f(2.7,-2.7,-10);
glVertex3f(2.7,(i),-10);
glVertex3f(-2.7,(i),-10);
}
else
{
glColor4f(((rgba2>>24)&0xff)/255.0f,
((rgba2>>16)&0xff)/255.0f,
((rgba2>>8)&0xff)/255.0f,
(rgba2&0xff)/255.0f);

glVertex3f(-2.7,-2.7,-10);
glVertex3f(2.7,-2.7,-10);
glVertex3f(2.7,1.5,-10);
glVertex3f(-2.7,1.5,-10);

glColor4f(((rgba3>>24)&0xff)/255.0f,
((rgba3>>16)&0xff)/255.0f,
((rgba3>>8)&0xff)/255.0f,
(rgba2&0xff)/255.0f);

glVertex3f(-2.7,2,-10);
glVertex3f(2.7,2,-10);
glVertex3f(2.7,(i),-10);
glVertex3f(-2.7,(i),-10);
}

//horizontal black bar
glColor4f(((rgba>>24)&0xff)/255.0f,
((rgba>>16)&0xff)/255.0f,
((rgba>>8)&0xff)/255.0f,
(rgba&0xff)/255.0f);

glVertex3f(-3,1.5,-10);
glVertex3f(3,1.5,-10);
glVertex3f(3,2,-10);
glVertex3f(-3,2,-10);

glEnd();
glColor4f(1, 1, 1, 1);
glutSwapBuffers();
glFlush();

}
}
static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'q':
exit(0);
break;
}
//glutPostRedisplay();
}

static void idle(void)
{
glutPostRedisplay();
}

/* Program entry point */

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(500,500);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("task");

glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);

glClearColor(0,0,0,0);

glutMainLoop();

return EXIT_SUCCESS;
}

tonyo_au
03-31-2014, 09:07 PM
You could split your display function into 2 procedures - one to draw the cross and one to draw the column. Based on a flag call one or the other procedure.
If you need to display one for a certain number of seconds you will need to create a timer to control when to change the flag. You could put the timer check at the end of the display procedure.

derin86
04-01-2014, 12:38 AM
You could split your display function into 2 procedures - one to draw the cross and one to draw the column. Based on a flag call one or the other procedure.
If you need to display one for a certain number of seconds you will need to create a timer to control when to change the flag. You could put the timer check at the end of the display procedure.

First of all thanks for you answer, I tried in a different way but I did'n succed now I am gonna try your solution.
Anyway what i used before was the sequent code, if I use it with only 'if' and 'else' condition It seems to work but as soon I try with the ''else if'' the pc crasch, it could be a code problem or a notebook one? Thanks in advance. :)


static void display(void)
{
int time,timebase=0;
time=glutGet(GLUT_ELAPSED_TIME);

if (time - timebase < 3000)
{..CROSS INSTRUCTION..}

else if ((time - timebase >= 5000) && (time - timebase <= 10000))
{..EMPTY COLUMN WITH LEVEL INDICATOR...}

else
{.FILLED COLUMN..}
}

tonyo_au
04-01-2014, 05:45 PM
Unless you are copying time to timebase, this code should show you the cross for 3 sec, the filled column for 2 sec, the unfilled col for 5 sec then the filled column from then on.

I am not sure what would cause you to crash. Possible some thing wrong if the empty column code.

By breaking the code into procedures you can test each procedure separately then once each works separately you put the time control logic in.

derin86
04-02-2014, 12:25 AM
Unless you are copying time to timebase, this code should show you the cross for 3 sec, the filled column for 2 sec, the unfilled col for 5 sec then the filled column from then on.

I am not sure what would cause you to crash. Possible some thing wrong if the empty column code.

By breaking the code into procedures you can test each procedure separately then once each works separately you put the time control logic in.
Thanks for the answer!
I used the function Sleep(milliseconds) in order to show each image for a certain time and it seems to work. After this I tried to work with list but once again my pc crash :(
This is the code, there is something wrong? Thanks againg to all.

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <time.h>

const int cross=1;
const int instruction=2;
const int feedback=3;
unsigned int rgba = 0xFFFFFFFF; // white
unsigned int rgba1 = 0x00000000; // black
unsigned int rgba2 = 0x00EA00FF; //green
unsigned int rgba3 = 0xFF0000FF; //red
int i;

static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glNewList(cross,GL_COMPILE);
glBegin(GL_QUADS);//oriz
glColor4f(((rgba>>24)&0xff)/255.0f,
((rgba>>16)&0xff)/255.0f,
((rgba>>8)&0xff)/255.0f,
(rgba&0xff)/255.0f);
glVertex3f(-0.2,-1,-10);
glVertex3f(0.2,-1,-10);
glVertex3f(0.2,1,-10);
glVertex3f(-0.2,1,-10);
glBegin(GL_QUADS); //verticale
glColor4f(((rgba>>24)&0xff)/255.0f,
((rgba>>16)&0xff)/255.0f,
((rgba>>8)&0xff)/255.0f,
(rgba&0xff)/255.0f);
glVertex3f(-1,-0.2,-10);
glVertex3f(1,-0.2,-10);
glVertex3f(1,0.2,-10);
glVertex3f(-1,0.2,-10);
glEnd();
glColor4f(1, 1, 1, 1);
glutSwapBuffers();
glFlush();
Sleep(3000);
glEndList();
glNewList(instruction,GL_COMPILE_AND_EXECUTE);
glBegin(GL_QUADS);
//rectangle white-------------------
glColor4f(((rgba>>24)&0xff)/255.0f,
((rgba>>16)&0xff)/255.0f,
((rgba>>8)&0xff)/255.0f,
(rgba&0xff)/255.0f);
glVertex3f(-3,-3,-10);
glVertex3f(3,-3,-10);
glVertex3f(3,4,-10);
glVertex3f(-3,4,-10);
//rectangle black--------------------------
glColor4f(((rgba1>>24)&0xff)/255.0f,
((rgba1>>16)&0xff)/255.0f,
((rgba1>>8)&0xff)/255.0f,
(rgba1&0xff)/255.0f);
glVertex3f(-2.9,-2.9,-10);
glVertex3f(2.9,-2.9,-10);
glVertex3f(2.9,3.9,-10);
glVertex3f(-2.9,3.9,-10);
//horizontal white bar
glColor4f(((rgba>>24)&0xff)/255.0f,
((rgba>>16)&0xff)/255.0f,
((rgba>>8)&0xff)/255.0f,
(rgba&0xff)/255.0f);
glVertex3f(-3,1.5,-10);
glVertex3f(3,1.5,-10);
glVertex3f(3,2,-10);
glVertex3f(-3,2,-10);
glEnd();
glColor4f(1, 1, 1, 1);
glutSwapBuffers();
glFlush();
Sleep(2000);
glEndList();

glNewList(instruction,GL_COMPILE_AND_EXECUTE);
glBegin(GL_QUADS);
for ( i = -2.7;i < 3.8;i+= 0.001)
{
//rectangle----------------------
glBegin(GL_QUADS);
//rectangle white-------------------
glColor4f(((rgba>>24)&0xff)/255.0f,
((rgba>>16)&0xff)/255.0f,
((rgba>>8)&0xff)/255.0f,
(rgba&0xff)/255.0f);
glVertex3f(-3,-3,-10);
glVertex3f(3,-3,-10);
glVertex3f(3,4,-10);
glVertex3f(-3,4,-10);
//rectangle black--------------------------
glColor4f(((rgba1>>24)&0xff)/255.0f,
((rgba1>>16)&0xff)/255.0f,
((rgba1>>8)&0xff)/255.0f,
(rgba1&0xff)/255.0f);
glVertex3f(-2.9,-2.9,-10);
glVertex3f(2.9,-2.9,-10);
glVertex3f(2.9,3.9,-10);
glVertex3f(-2.9,3.9,-10);
//rectangle feedback(green/red)-----------------------
if (i<=1.5)//green part
{
glColor4f(((rgba2>>24)&0xff)/255.0f,
((rgba2>>16)&0xff)/255.0f,
((rgba2>>8)&0xff)/255.0f,
(rgba1&0xff)/255.0f);
glVertex3f(-2.7,-2.7,-10);
glVertex3f(2.7,-2.7,-10);
glVertex3f(2.7,(i),-10);
glVertex3f(-2.7,(i),-10);
}
else// green plus red part
{//green
glColor4f(((rgba2>>24)&0xff)/255.0f,
((rgba2>>16)&0xff)/255.0f,
((rgba2>>8)&0xff)/255.0f,
(rgba2&0xff)/255.0f);
glVertex3f(-2.7,-2.7,-10);
glVertex3f(2.7,-2.7,-10);
glVertex3f(2.7,1.5,-10);
glVertex3f(-2.7,1.5,-10);
//red part
glColor4f(((rgba3>>24)&0xff)/255.0f,
((rgba3>>16)&0xff)/255.0f,
((rgba3>>8)&0xff)/255.0f,
(rgba2&0xff)/255.0f);
glVertex3f(-2.7,2,-10);
glVertex3f(2.7,2,-10);
glVertex3f(2.7,(i),-10);
glVertex3f(-2.7,(i),-10);
}
}
//horizontal white bar
glColor4f(((rgba>>24)&0xff)/255.0f,
((rgba>>16)&0xff)/255.0f,
((rgba>>8)&0xff)/255.0f,
(rgba&0xff)/255.0f);
glVertex3f(-3,1.5,-10);
glVertex3f(3,1.5,-10);
glVertex3f(3,2,-10);
glVertex3f(-3,2,-10);
glColor4f(1, 1, 1, 1);
glutSwapBuffers();
glFlush();
glEndList();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity() ;
glCallList(cross);
glCallList(instruction);
}