I am working on win7 microsoft visual studio 08 with Forms. I just changed my graphic card and some kind of aliasing appears in my application.
It can be apreciated that the grey lines are kind of aliased, and also the reference system, that is almost invisible.
Any idea of how to solve this?
I'll post some part of the code just in case:
1: Paint function
private: void Paint(){
int w = glControl1->Width;
int h = glControl1->Height;
float aspect_ratio = w / (float)h;
Matrix4 perpective = Matrix4::CreatePerspectiveFieldOfView(MathHelper::PiOver4, aspect_ratio, 0.1f, 100000.0f);
GL::MatrixMode(MatrixMode::Projection);
GL::LoadMatrix(perpective);
GL::MatrixMode(MatrixMode::Modelview);
GL::LoadIdentity();
GL::Viewport(0, 0, w, h);
//Situacion de la camara en el espacio
Matrix4 lookat = Matrix4::LookAt(100, 100, 100, 0, 0, 0, 0, 0, 1);
GL::LoadMatrix(lookat);
GL::Scale(this->zoom, this->zoom, this->zoom);
GL::Rotate(xrot, 1.0f, 0.0f, 0.0f);
GL::Rotate(yrot, 0.0f, 1.0f, 0.0f);
GL::Clear(ClearBufferMask::ColorBufferBit | ClearBufferMask::DepthBufferBit);
GL::ClearColor(Color::LightGray);
Draw3Daxis();
if (allowDraw){
DrawSurface();
}
}
2: function that draws everything shown in the picture (it is just a bunch of GL::Vertex3)
System::Void Draw3Daxis(){
OpcionesPlot* opciones;
opciones=OpcionesPlot::Instance();
int extra=2;
int sqrnumber=10;
//Planos en los ejes
GL::Color3(Color::White);
GL::Begin(BeginMode::Quads);
if(opciones->draw3DXYaxis){
GL::Vertex3(0,0,0);
GL::Vertex3(LENGTH,0,0);
GL::Vertex3(LENGTH,LENGTH,0);
GL::Vertex3(0,LENGTH,0);
}
if(opciones->draw3DXZaxis){
GL::Vertex3(0,0,0);
GL::Vertex3(LENGTH,0,0);
GL::Vertex3(LENGTH,0,LENGTH);
GL::Vertex3(0,0,LENGTH);
}
if(opciones->draw3DYZaxis){
GL::Vertex3(0,0,0);
GL::Vertex3(0,LENGTH,0);
GL::Vertex3(0,LENGTH,LENGTH);
GL::Vertex3(0,0,LENGTH);
}
GL::End();
// ejes XYZ
GL::Color3(Color::Black);
GL::Begin(BeginMode::Lines);
if(opciones->draw3DXYaxis){
GL::Vertex3(0,0,0);
GL::Vertex3(LENGTH+extra,0,0);
GL::Vertex3(0,0,0);
GL::Vertex3(0,LENGTH+extra,0);
}
if(opciones->draw3DXZaxis){
GL::Vertex3(0,0,0);
GL::Vertex3(LENGTH+extra,0,0);
GL::Vertex3(0,0,0);
GL::Vertex3(0,0,LENGTH+extra);
}
if(opciones->draw3DYZaxis){
GL::Vertex3(0,0,0);
GL::Vertex3(0,LENGTH+extra,0);
GL::Vertex3(0,0,0);
GL::Vertex3(0,0,LENGTH+extra);
}
//--> lineas de los planos
GL::Color3(Color::DarkGray);
for(int i=1;i<=sqrnumber;i++){
if(i==sqrnumber)
GL::Color3(Color::Black);
if(opciones->draw3DXYaxis){
GL::Vertex3(LENGTH/sqrnumber*i,0,0);
GL::Vertex3(LENGTH/sqrnumber*i,LENGTH,0);
GL::Vertex3(0,LENGTH/sqrnumber*i,0);
GL::Vertex3(LENGTH,LENGTH/sqrnumber*i,0);
}
if(opciones->draw3DXZaxis){
GL::Vertex3(LENGTH/sqrnumber*i,0,0);
GL::Vertex3(LENGTH/sqrnumber*i,0,LENGTH);
GL::Vertex3(0,0,LENGTH/sqrnumber*i);
GL::Vertex3(LENGTH,0,LENGTH/sqrnumber*i);
}
if(opciones->draw3DYZaxis){
GL::Vertex3(0,0,LENGTH/sqrnumber*i);
GL::Vertex3(0,LENGTH,LENGTH/sqrnumber*i);
GL::Vertex3(0,LENGTH/sqrnumber*i,0);
GL::Vertex3(0,LENGTH/sqrnumber*i,LENGTH);
}
}
GL::End();
// triangulos para los ejes
//----->cuadrado
GL::Color3(Color::Black);
GL::Begin(BeginMode::Quads);
float quadside=0.7;
if(opciones->draw3DXYaxis){
GL::Vertex3((float)LENGTH+extra,quadside,quadside);
GL::Vertex3((float)LENGTH+extra,-quadside,quadside);
GL::Vertex3((float)LENGTH+extra,-quadside,-quadside);
GL::Vertex3((float)LENGTH+extra,quadside,-quadside);
GL::Vertex3(quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(-quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(-quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(quadside,(float)LENGTH+extra,-quadside);
}
if(opciones->draw3DXZaxis){
GL::Vertex3((float)LENGTH+extra,quadside,quadside);
GL::Vertex3((float)LENGTH+extra,-quadside,quadside);
GL::Vertex3((float)LENGTH+extra,-quadside,-quadside);
GL::Vertex3((float)LENGTH+extra,quadside,-quadside);
GL::Vertex3(quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(-quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(-quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(quadside,-quadside,(float)LENGTH+extra);
}
if(opciones->draw3DYZaxis){
GL::Vertex3(quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(-quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(-quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(-quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(-quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(quadside,-quadside,(float)LENGTH+extra);
}
GL::End();
// --> triangulos para hacer la punta
GL::Begin(BeginMode::Triangles);
if(opciones->draw3DXYaxis){
GL::Vertex3((float)LENGTH+extra,quadside,quadside);
GL::Vertex3((float)LENGTH+extra,-quadside,quadside);
GL::Vertex3(LENGTH+extra+5,0,0);
GL::Vertex3((float)LENGTH+extra,-quadside,quadside);
GL::Vertex3((float)LENGTH+extra,-quadside,-quadside);
GL::Vertex3(LENGTH+extra+5,0,0);
GL::Vertex3((float)LENGTH+extra,-quadside,-quadside);
GL::Vertex3((float)LENGTH+extra,quadside,-quadside);
GL::Vertex3(LENGTH+extra+5,0,0);
GL::Vertex3((float)LENGTH+extra,quadside,-quadside);
GL::Vertex3((float)LENGTH+extra,quadside,quadside);
GL::Vertex3(LENGTH+extra+5,0,0);
GL::Vertex3(quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(-quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(0,LENGTH+extra+5,0);
GL::Vertex3(-quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(-quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(0,LENGTH+extra+5,0);
GL::Vertex3(-quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(0,LENGTH+extra+5,0);
GL::Vertex3(quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(0,LENGTH+extra+5,0);
}
if(opciones->draw3DXZaxis){
GL::Vertex3((float)LENGTH+extra,quadside,quadside);
GL::Vertex3((float)LENGTH+extra,-quadside,quadside);
GL::Vertex3(LENGTH+extra+5,0,0);
GL::Vertex3((float)LENGTH+extra,-quadside,quadside);
GL::Vertex3((float)LENGTH+extra,-quadside,-quadside);
GL::Vertex3(LENGTH+extra+5,0,0);
GL::Vertex3((float)LENGTH+extra,-quadside,-quadside);
GL::Vertex3((float)LENGTH+extra,quadside,-quadside);
GL::Vertex3(LENGTH+extra+5,0,0);
GL::Vertex3((float)LENGTH+extra,quadside,-quadside);
GL::Vertex3((float)LENGTH+extra,quadside,quadside);
GL::Vertex3(LENGTH+extra+5,0,0);
GL::Vertex3(quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(-quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(0,0,LENGTH+extra+5);
GL::Vertex3(-quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(-quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(0,0,LENGTH+extra+5);
GL::Vertex3(-quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(0,0,LENGTH+extra+5);
GL::Vertex3(quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(0,0,LENGTH+extra+5);
}
if(opciones->draw3DYZaxis){
GL::Vertex3(quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(-quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(0,LENGTH+extra+5,0);
GL::Vertex3(-quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(-quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(0,LENGTH+extra+5,0);
GL::Vertex3(-quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(0,LENGTH+extra+5,0);
GL::Vertex3(quadside,(float)LENGTH+extra,-quadside);
GL::Vertex3(quadside,(float)LENGTH+extra,quadside);
GL::Vertex3(0,LENGTH+extra+5,0);
GL::Vertex3(quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(-quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(0,0,LENGTH+extra+5);
GL::Vertex3(-quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(-quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(0,0,LENGTH+extra+5);
GL::Vertex3(-quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(0,0,LENGTH+extra+5);
GL::Vertex3(quadside,-quadside,(float)LENGTH+extra);
GL::Vertex3(quadside,quadside,(float)LENGTH+extra);
GL::Vertex3(0,0,LENGTH+extra+5);
}
GL::End();
Write3D(LENGTH,3,3,GLUT_BITMAP_9_BY_15,"X");
Write3D(3,LENGTH,3,GLUT_BITMAP_9_BY_15,"Y");
Write3D(3,3,LENGTH,GLUT_BITMAP_9_BY_15,"Z");
}
Try disabling depth testing before drawing each set of objects (reference planes first, axes second and then the reference grid).
It looks like your axes are Z-fighting the reference planes.
You have multiple options -
Disable depth test when drawing the lines (not the planes). This will make sure that no z-fighting occurs.
Keep depth testing on, but use glPolygonOffset when drawing the planes to offset them so that z-fighting does not happen at all.
Use GL_LEQUAL (or whatever else) as the DepthFunc when drawing the planes, but use GL_EQUAL when drawing the lines. This should make things ok.
Related
I have an issue with my code. I don't manage to change the opacity of my RenderDrawColor.
Indeed, I know that SetRenderDrawColor is defined by (renderer, R, G, B, a --> which represents the opacity value). Nevertheless, I tried to change a value but my color doesn't change and I have my rectangle fully fill again.
Here a photo of what I have with my code :
Screen that I have with my code
Here is my code, that I execute with a main.c script :
void display_end_menu(SDL_Renderer *renderer) {
if (SDL_SetRenderDrawColor(renderer, 0, 255, 0, 30) != 0) {
return_error("Impossible de modifier la couleur du render");
SDL_Quit();
}
SDL_Rect shadow_screen;
shadow_screen.w = 1280;
shadow_screen.h = 720;
shadow_screen.x = (SCREEN_WIDTH - shadow_screen.w)/2;
shadow_screen.y = (SCREEN_HEIGHT - shadow_screen.h)/2;
if (SDL_RenderFillRect(renderer, &shadow_screen) != 0) {
return_error("Impossible de dessiner le rectangle dans le render");
SDL_Quit();
}
if (SDL_SetRenderDrawColor(renderer, 255, 255, 255, 30) != 0) {
return_error("Impossible de modifier la couleur du render");
SDL_Quit();
}
SDL_Rect quit_button;
quit_button.w = 200;
quit_button.h = 50;
quit_button.x = (SCREEN_WIDTH - quit_button.w)/2;
quit_button.y = (SCREEN_HEIGHT - quit_button.h)/2;
if (SDL_RenderFillRect(renderer, &quit_button) != 0) {
return_error("Impossible de dessiner le rectangle dans le render");
SDL_Quit();
}
}
I tried several numbers for alpha such as 0, 30 and so on, but it's impossible to edit the opacity of the rectangle.
I want every cube to have a random color but with this code the colors keep changing all the time
int espacioX = -500;
int espacioY = -500;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
randomR = (double)rand() / (RAND_MAX);
randomG = (double)rand() / (RAND_MAX);
randomB = (double)rand() / (RAND_MAX);
glColor3f(randomR,randomG,randomB);
glTranslatef(espacioX,espacioY,0);
glutSolidCube(9);
glTranslatef(-espacioX,-espacioY,0);
espacioX+=10;
}
glTranslatef(-1000,0,0);
espacioY+=10;
}
How do I make them stay in the same color while the program is running?
Edit:
This is the comple code:
#include <windows.h>
/***** OpenGL library imports. *****/
#include<gl\glut.h> //basic header file for OpenGL
//#include <gl\glaux.h> // Header File For The Glaux Library
/***** C library imports. *****/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int ScreenWidth = 0;
int ScreenHeight = 0;
int last_x = 0, last_y = 0,left = 1,middle = 0,right = 0;
int zoom_y = 0;
int anglex=0, angley=0, anglez=0;
int lookfrom=450;
int ang=0;
void myGlutKeyboard(unsigned char Key, int x, int y)
{
printf("%5d\n",Key);
glutPostRedisplay();
}
void init_GL_params()
{
static GLfloat light_ambient[] = { .250, .250, .250, .250 };
static GLfloat light_diffuse[] = { .250, .250, .25, .250 };
static GLfloat light_specular[] = { .250, .250, .250, .250 };
static GLfloat light1_diffuse[] = { .250, .250, .250, .0 };
static GLfloat light1_specular[] = { .250, .250, .250, .0 };
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_LIGHT1);
glDisable (GL_TEXTURE_2D);
glDisable (GL_TEXTURE_GEN_S);
glDisable (GL_TEXTURE_GEN_T);
glShadeModel (GL_FLAT);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LESS);
glEnable (GL_CULL_FACE);
glCullFace (GL_BACK);
glFrontFace (GL_CCW);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv (GL_LIGHT1, GL_AMBIENT, light_ambient);
glColor3f (1.0, 1.0, 1.0);
}
void setLightPosition()
{
static GLfloat light_position[] = { 100, 200.0, 10, 1.0 };
static GLfloat light1_position[] = { -0.0, 300.0, 0.0, 1.0 };
// set the light position (for some reason we have to do this in model view.
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_COLOR_MATERIAL);
}
void myGlutDisplay(void)
{
init_GL_params();
// clear the window
glClearColor (0,0,0,0); //Color de fondo R G B
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
setLightPosition(); //Posici{on de la fuente de luz
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(60.0f, 1.0, 1.0f, 10000.0f);
gluLookAt( //punto de vista
0 ,100 ,lookfrom , // desde donde veo xyz
0, 0, 0, // hacia donde veo xyz
0,1,0 );
glRotatef(anglex, 1.0, 0.0, 0.0);
glRotatef(angley, 0.0, 1.0, 0.0);
glRotatef(anglez, 0.0, 0.0, 1.0);
//Empieza a dibujar
int espacioX = 500;
int espacioY = 500;
double randomR, randomG, randomB;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
randomR = (double)rand() / (RAND_MAX);
randomG = (double)rand() / (RAND_MAX);
randomB = (double)rand() / (RAND_MAX);
glColor3f(randomR,randomG,randomB);
glTranslatef(espacioX,espacioY,0);
glutSolidCube(9);
glTranslatef(-espacioX,-espacioY,0);
espacioX-=10;
}
glTranslatef(1000,0,0);
espacioY-=10;
}
glFlush(); // Flush The GL Pipeline
glutSwapBuffers();
}
void myGlutReshape2(int mWidth, int mHeight)
{
ScreenWidth = mWidth;
ScreenHeight = mHeight;
glViewport(0, 0, (GLsizei) ScreenWidth, (GLsizei) ScreenHeight);
glutPostRedisplay();
}
/****************************************************************/
void myGlutIdle(void)
{
glutPostRedisplay();
}
/***************************************************************/
void myGlutMouse(int button, int button_state, int x, int y){
if ( (button == GLUT_LEFT_BUTTON) && (button_state == GLUT_DOWN) )
{
last_x = x;
last_y = y;
left = 1;
middle = 0;
right = 0;
}
else if ((button == GLUT_RIGHT_BUTTON) && (button_state == GLUT_DOWN) )
{
zoom_y = y;
right = 1;
left = 0;
middle = 0;
}
}
void myGlutMotion(int x, int y){
if (left){
anglex+= (float) (y - last_y);
angley+= (float) (x - last_x);
last_x = x;
last_y = y;
}
if (right)
{
lookfrom += (y - zoom_y);
zoom_y = y;
}
}
void GlutInit(int argc, char** argv)
{
ScreenWidth = 700;
ScreenHeight = 700;
/****************************************/
/* Initialize GLUT and create window */
/****************************************/
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL ); //set up the display as a double buffer using RGB values
glutInitWindowPosition(90, 50); //set the position of where you want the window to open
glutInitWindowSize(ScreenWidth, ScreenHeight); //set how big you want the initial window to be
glutCreateWindow("Testing Virtual Environment" ); //what do you want the window to be called (titlebar text)
//register callbacks
glutDisplayFunc(myGlutDisplay); //Register the display callback
glutReshapeFunc(myGlutReshape2); // -- Resize handler
glutKeyboardFunc(myGlutKeyboard); // -- Keyboard handler
glutMouseFunc(myGlutMouse); // -- Mouse Click handler
glutMotionFunc(myGlutMotion); // -- Mous motion handler
glutIdleFunc(myGlutIdle); // -- Idle handler
glFlush();
}
int main( int argc, char **argv )
{
GlutInit(argc, argv);
glutMainLoop();
return 0;
}
It was a sample code so I just posted the part that I edited, sorry. When I did a cube or a sphere with an specific color it always stayed the same so i didn't realiza that this refresh the image all the time. I don't really understand the whole thing so I don't know where it's refreshing the image.
Why not set SEED to some constant value at start of myGlutDisplay(void). That will give you the same random values each frame without any big change in your code for example like this:
void myGlutDisplay(void)
{
init_GL_params();
// clear the window
glClearColor (0,0,0,0); //Color de fondo R G B
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
setLightPosition(); //Posici{on de la fuente de luz
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(60.0f, 1.0, 1.0f, 10000.0f);
gluLookAt( //punto de vista
0 ,100 ,lookfrom , // desde donde veo xyz
0, 0, 0, // hacia donde veo xyz
0,1,0 );
glRotatef(anglex, 1.0, 0.0, 0.0);
glRotatef(angley, 0.0, 1.0, 0.0);
glRotatef(anglez, 0.0, 0.0, 1.0);
//Empieza a dibujar
int espacioX = 500;
int espacioY = 500;
double randomR, randomG, randomB;
srand(0x98765432); // this is the SEED
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++){
randomR = (double)rand() / (RAND_MAX);
randomG = (double)rand() / (RAND_MAX);
randomB = (double)rand() / (RAND_MAX);
glColor3f(randomR,randomG,randomB);
glTranslatef(espacioX,espacioY,0);
glutSolidCube(9);
glTranslatef(-espacioX,-espacioY,0);
espacioX-=10;
}
glTranslatef(1000,0,0);
espacioY-=10;
}
glFlush(); // Flush The GL Pipeline
glutSwapBuffers();
}
The value of the SEED will give you the pattern. So changing the constant will change the colors. Beware wrong constant (like 0) can corrupt the randomness of the rand() function.
It seems like you are running this code inside another loop which means you are calculating your random variables every time the scene is redrawn, this would cause the colors to change and the obvious solution would be to poulate your list of cubes once and then iterate through the list drawing the cubes on each scene refresh.
However, without seeing the rest of your code it is unclear. Please include more information so we can give you a better answer.
I'm trying to develop a little Supervision System with two GtkDrawarea Widgets and update them simultaneously whenever I toogle the buttons regarded for each Drawarea. A little example I made for ploting two sinoides.
However just the first GtkDrawarea Widget are updated in did, the other remains freezed until I rezise the GtkWindow. I tried use g_timeout_add_full but I seens to work only with one GtkDrawArea.
I'm sending an image of my test aplication.
If someone could help me I will be very thankful.
PS: Sorry for my English :-)
Here is my code:
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <cairo.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
GtkWidget *window;
GtkWidget *darea1;
GtkWidget *darea2;
double w_larg = 1600;
double w_alt = 1200;
bool toggle_stat1 = false;
bool toggle_stat2 = false;
int width, height;
double fase1 = 2;
double amp1 = 1.25;
double fase2 = 4;
double amp2 = 1.25;
gboolean flag_amp1 = TRUE;
gboolean flag_amp2 = TRUE;
static gboolean invalidate_drawing1(gpointer data);
static gboolean invalidate_drawing2(gpointer data);
//*******************************************************************************
static gboolean on_toggled1(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{toggle_stat1 = true;
printf("ToggleButton01 --> Acionado / toggle_stat1 = %d\n",toggle_stat1);
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,250,invalidate_drawing1,darea1,NULL); // Variação atualizada da linha acima :-)
}
else
{toggle_stat1 = false;
printf("ToggleButton01 --> Liberado / toggle_stat1 = %d\n",toggle_stat1);
}
return FALSE;
}
//*******************************************************************************
static gboolean on_toggled2(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{toggle_stat2 = true;
printf("ToggleButton02 --> Acionado / toggle_stat2 = %d\n",toggle_stat2);
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,250,invalidate_drawing2,darea2,NULL); // Variação atualizada da linha acima :-)
}
else
{toggle_stat2 = false;
printf("ToggleButton02 --> Liberado / toggle_stat2 = %d\n",toggle_stat2);
}
return FALSE;
}
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
static gboolean draw_darea1(GtkWidget *widget, cairo_t *cr, gpointer data)
{
int valor1;
double valor2;
static const double dashed1[] = {4.0, 21.0, 2.0};
static const double dashed3[] = {2.0};
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_set_source_rgba(cr, 0.45, 0.45, 0.45, 1);
cairo_rectangle(cr,0 , 0, widget->allocation.width, widget->allocation.height);
cairo_fill(cr);
double i = widget->allocation.width;
double j = widget->allocation.height;
double sc = fase1*M_PI/i;
double y;
double h = 0;
if (amp1 != 0) h = h + j/amp1;
//---------------------------------------------------------------------------
cairo_set_source_rgba(cr, 0, 0, 1, 1);
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+2)
{
valor1 = rand()%5;
valor2 = rand()%50;
y=x*sc;
if (valor1 < 3 ) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) + valor2 );
if (valor1 == 3) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
if (valor1 > 3 ) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) - valor2 );
}
cairo_stroke(cr);
//---------------------------------------------------------------------------
cairo_set_source_rgba(cr, 1, 1, 0, 1);
cairo_set_dash(cr,dashed1, 1, 0 );
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+10)
{
y=x*sc;
cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
}
cairo_stroke(cr);
//---------------------------------------------------------------------------
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1);
cairo_set_dash(cr,dashed3, 1, 0 );
cairo_set_line_width(cr, 1.0);
cairo_move_to(cr,0,(j/2));
cairo_line_to(cr,i,(j/2));
cairo_move_to(cr,(i/2),0);
cairo_line_to(cr,(i/2),j);
cairo_stroke(cr);
cairo_destroy(cr);
return TRUE;
}
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
static gboolean draw_darea2(GtkWidget *widget, cairo_t *cr, gpointer data)
{
int valor1;
double valor2;
static const double dashed1[] = {4.0, 21.0, 2.0};
static const double dashed3[] = {2.0};
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_set_source_rgba(cr, 0.45, 0.45, 0.45, 1);
cairo_rectangle(cr,0 , 0, widget->allocation.width, widget->allocation.height);
cairo_fill(cr);
double i = widget->allocation.width;
double j = widget->allocation.height;
double sc = fase2*M_PI/i;
double y;
double h = 0;
if (amp2 != 0) h = h + j/amp2;
//---------------------
cairo_set_source_rgba(cr, 0, 0, 1, 1);
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+2)
{
valor1 = rand()%5;
valor2 = rand()%50;
y=x*sc;
if (valor1 < 3 ) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) + valor2 );
if (valor1 == 3) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
if (valor1 > 3 ) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) - valor2 );
}
cairo_stroke(cr);
//---------------------
cairo_set_source_rgba(cr, 0, 1, 0, 1);
cairo_set_dash(cr,dashed1, 1, 0 );
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+10)
{
y=x*sc;
cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
}
cairo_stroke(cr);
//---------------------
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1);
cairo_set_dash(cr,dashed3, 1, 0 );
cairo_set_line_width(cr, 1.0);
cairo_move_to(cr,0,(j/2));
cairo_line_to(cr,i,(j/2));
cairo_move_to(cr,(i/2),0);
cairo_line_to(cr,(i/2),j);
cairo_stroke(cr);
cairo_destroy(cr);
return TRUE;
}
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
static gboolean invalidate_drawing1(gpointer data)
{
GdkWindow *win;
g_print("T_Signal-1 | amp1 = %2.3f | ",amp1);
if (toggle_stat1==true)
{
if ((flag_amp1==TRUE)&&(amp1 <= 3))
amp1=amp1+0.25;
else
flag_amp1=FALSE;
if ((flag_amp1==FALSE)&&(amp1 >= 1.25))
amp1=amp1-0.25;
else
flag_amp1=TRUE;
}
//---------------------
win = gtk_widget_get_window(GTK_WIDGET(data));
if (win) {
GtkAllocation allocation;
gtk_widget_get_allocation(GTK_WIDGET(data), &allocation);
gdk_window_invalidate_rect(win, &allocation, FALSE);
}
if (flag_amp1 == TRUE)
return TRUE;
else
return FALSE;
}
//**********************
//**********************
//**********************
static gboolean invalidate_drawing2(gpointer data)
{
GdkWindow *win;
//---------------------
g_print("T_Signal-2 | amp2 = %2.3f\n",amp2);
if (toggle_stat2==true)
{
if ((flag_amp2==TRUE)&&(amp2 <= 3))
amp2=amp2+0.25;
else
flag_amp2=FALSE;
if ((flag_amp2==FALSE)&&(amp2 >= 1.25))
amp2=amp2-0.25;
else
flag_amp2=TRUE;
}
//---------------------
win = gtk_widget_get_window(GTK_WIDGET(data));
if (win) {
GtkAllocation allocation;
gtk_widget_get_allocation(GTK_WIDGET(data), &allocation);
gdk_window_invalidate_rect(win, &allocation, FALSE);
}
if (flag_amp2 == TRUE)
return TRUE;
else
return FALSE;
}
//**********************
//**********************
//**********************
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *togglebutton1;
GtkWidget *togglebutton2;
GtkWidget *togglebutton3;
GtkWidget *button1; // Amp+
GtkWidget *button2; // Amp-
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "GTK_Glade_2DrawArea01-R00.glade", NULL); //Carrega as informações do projeto glade para o GtkBuilder
window = GTK_WIDGET(gtk_builder_get_object (builder, "window1")); //Retorna o objeto Window, para mostrá-lo mais tarde
darea1 = GTK_WIDGET(gtk_builder_get_object (builder, "drawingarea1")); //Retorna o objeto DrawingArea, para mostrá-lo mais tarde
darea2 = GTK_WIDGET(gtk_builder_get_object (builder, "drawingarea2")); //Retorna o objeto DrawingArea, para mostrá-lo mais tarde
togglebutton1 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton1")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
togglebutton2 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton2")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
togglebutton3 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton3")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
button1 = GTK_WIDGET(gtk_builder_get_object (builder, "button1")); //Retorna o objeto button1, para mostrá-lo mais tarde
button2 = GTK_WIDGET(gtk_builder_get_object (builder, "button2")); //Retorna o objeto button2, para mostrá-lo mais tarde
gtk_window_set_title(GTK_WINDOW(window), "GTK 2 Viewports DrawArea 01");
gtk_builder_connect_signals (builder, NULL); //Conecta todos os eventos dentro do projeto do glade.
g_object_unref (G_OBJECT (builder)); //Exclui o GtkBuilder, pois não iremos mais usá-lo nesse exemplo.
g_signal_connect(G_OBJECT(togglebutton1) , "toggled" , G_CALLBACK(on_toggled1) , NULL);
g_signal_connect(G_OBJECT(togglebutton2) , "toggled" , G_CALLBACK(on_toggled2) , NULL);
g_signal_connect(G_OBJECT(darea1), "expose-event", G_CALLBACK(draw_darea1), NULL);
g_signal_connect(G_OBJECT(darea2), "expose-event", G_CALLBACK(draw_darea2), NULL);
g_signal_connect_swapped(G_OBJECT(window) , "destroy", G_CALLBACK(gtk_main_quit) , G_OBJECT(window));
gtk_widget_show_all(window); //Mostra a janela com seus objetos
gtk_main ();
return 0;
}
Here's the code fixed (below) after the suggestion andlabs and wogsland. Thank you.
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <cairo.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
GtkWidget *window;
GtkWidget *darea1;
GtkWidget *darea2;
bool toggle_stat1 = false;
bool toggle_stat2 = false;
double fase1 = 2;
double amp1 = 1.25;
double fase2 = 4;
double amp2 = 1.25;
gboolean flag_amp1 = TRUE;
gboolean flag_amp2 = TRUE;
//*******************************************************************************
static gboolean on_toggled1(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{toggle_stat1 = true;
printf("ToggleButton01 --> Acionado / toggle_stat1 = %d\n",toggle_stat1);
}
else
{toggle_stat1 = false;
printf("ToggleButton01 --> Liberado / toggle_stat1 = %d\n",toggle_stat1);
}
return FALSE;
}
//*******************************************************************************
static gboolean on_toggled2(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{toggle_stat2 = true;
printf("ToggleButton02 --> Acionado / toggle_stat2 = %d\n",toggle_stat2);
}
else
{toggle_stat2 = false;
printf("ToggleButton02 --> Liberado / toggle_stat2 = %d\n",toggle_stat2);
}
return FALSE;
}
//*******************************************************************************
//*******************************************************************************
static gboolean draw_darea1(GtkWidget *widget, cairo_t *cr, gpointer data)
{
static const double dashed1[] = {4.0, 21.0, 2.0};
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_set_source_rgba(cr, 0.45, 0.45, 0.45, 1);
cairo_rectangle(cr,0 , 0, widget->allocation.width, widget->allocation.height);
cairo_fill(cr);
double i = widget->allocation.width;
double j = widget->allocation.height;
double sc = fase1*M_PI/i;
double y;
double h = 0;
if (amp1 != 0) h = h + j/amp1;
//---------------------------------------------------------------------------
cairo_set_source_rgba(cr, 1, 1, 0, 1);
cairo_set_dash(cr,dashed1, 1, 0 );
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+10)
{
y=x*sc;
cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
}
cairo_stroke(cr);
return TRUE;
}
//*******************************************************************************
//*******************************************************************************
static gboolean draw_darea2(GtkWidget *widget, cairo_t *cr, gpointer data)
{
static const double dashed1[] = {4.0, 21.0, 2.0};
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_set_source_rgba(cr, 0.45, 0.45, 0.45, 1);
cairo_rectangle(cr,0 , 0, widget->allocation.width, widget->allocation.height);
cairo_fill(cr);
double i = widget->allocation.width;
double j = widget->allocation.height;
double sc = fase2*M_PI/i;
double y;
double h = 0;
if (amp2 != 0) h = h + j/amp2;
//---------------------
cairo_set_source_rgba(cr, 0, 1, 0, 1);
cairo_set_dash(cr,dashed1, 1, 0 );
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+10)
{
y=x*sc;
cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
}
cairo_stroke(cr);
return TRUE;
}
//*******************************************************************************
//*******************************************************************************
static gboolean invalidate_drawing1(GtkWidget *widget)
{
g_print("T_Signal-1 | amp1 = %2.3f | ",amp1);
if (toggle_stat1==true)
{
if ((flag_amp1==TRUE)&&(amp1 <= 3))
amp1=amp1+0.25;
else
flag_amp1=FALSE;
if ((flag_amp1==FALSE)&&(amp1 >= 1.25))
amp1=amp1-0.25;
else
flag_amp1=TRUE;
}
//---------------------
gtk_widget_queue_draw(widget);
return TRUE;
}
//**********************
//**********************
static gboolean invalidate_drawing2(GtkWidget *widget)
{
g_print("T_Signal-2 | amp2 = %2.3f\n",amp2);
if (toggle_stat2==true)
{
if ((flag_amp2==TRUE)&&(amp2 <= 3))
amp2=amp2+0.25;
else
flag_amp2=FALSE;
if ((flag_amp2==FALSE)&&(amp2 >= 1.25))
amp2=amp2-0.25;
else
flag_amp2=TRUE;
}
//---------------------
gtk_widget_queue_draw(widget);
return TRUE;
}
//**********************
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *togglebutton1;
GtkWidget *togglebutton2;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "GTK_Glade_2DrawArea01-R00.glade", NULL); //Carrega as informações do projeto glade para o GtkBuilder
window = GTK_WIDGET(gtk_builder_get_object (builder, "window1")); //Retorna o objeto Window, para mostrá-lo mais tarde
darea1 = GTK_WIDGET(gtk_builder_get_object (builder, "drawingarea1")); //Retorna o objeto DrawingArea, para mostrá-lo mais tarde
darea2 = GTK_WIDGET(gtk_builder_get_object (builder, "drawingarea2")); //Retorna o objeto DrawingArea, para mostrá-lo mais tarde
togglebutton1 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton1")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
togglebutton2 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton2")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
gtk_window_set_title(GTK_WINDOW(window), "GTK 2 Viewports DrawArea 01");
gtk_builder_connect_signals (builder, NULL); //Conecta todos os eventos dentro do projeto do glade.
g_object_unref (G_OBJECT (builder)); //Exclui o GtkBuilder, pois não iremos mais usá-lo nesse exemplo.
g_signal_connect(G_OBJECT(togglebutton1) , "toggled" , G_CALLBACK(on_toggled1) , NULL);
g_signal_connect(G_OBJECT(togglebutton2) , "toggled" , G_CALLBACK(on_toggled2) , NULL);
g_signal_connect(G_OBJECT(darea1), "expose-event", G_CALLBACK(draw_darea1), NULL);
g_signal_connect(G_OBJECT(darea2), "expose-event", G_CALLBACK(draw_darea2), NULL);
g_signal_connect_swapped(G_OBJECT(window) , "destroy", G_CALLBACK(gtk_main_quit) , G_OBJECT(window));
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,250,(GSourceFunc)invalidate_drawing1,(gpointer)darea1,NULL); // Variação atualizada da linha acima :-)
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,250,(GSourceFunc)invalidate_drawing2,(gpointer)darea2,NULL); // Variação atualizada da linha acima :-)
gtk_widget_show_all(window); //Mostra a janela com seus objetos
gtk_main ();
return 0;
}
And the Final Image.
Don't use gdk_window_invalidate_rect(). Use gtk_widget_queue_draw() or gtk_widget_queue_draw_area().
I've modified my original code (previous post) to use two textures. Here they are:
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "support.h"
using namespace std;
GLuint *images;
GLubyte *image;
GLuint texture1=0;
GLuint texture2=0;
GLuint LoadTexture(int width, int height, char *fName);
int n=200;
int m=200;
void showPictureW();
void showPictureR();
float rX=0;
float rY=0;
float rZ=0;
float tX=0;
float tY=0;
float tZ=-3.5;
float nr = -5.0;
float fr = -4.0;
float tpx = 1.0;
float tpy = 1.0;
float btx = -1.0;
float bty = -1.0;
float dZ =-2.0;
void showPictureW()
{
/* clear all pixels */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,1,1);
//face 1
glBegin( GL_POLYGON );
glTexCoord2d(1.0,1.0); glVertex3d(-1.0,1.0,-2.0);
glTexCoord2d(1.0,0.0); glVertex3d(-1,-1.0,-2.0);
glTexCoord2d(0.0,0.0); glVertex3d(1.0,-1.0,-2.0);
glEnd();
//face 2
glBegin( GL_POLYGON );
glTexCoord2d(0.0,0.0); glVertex3d(1.0,-1.0,-2.0);
glTexCoord2d(0.0,1.0); glVertex3d(1.0,1.0,-2.0);
glTexCoord2d(1.0,1.0); glVertex3d(-1.0,1.0,-2.0);
glEnd();
}
void showPictureR()
{
//clear all pixels
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,1,1);
//face 1
glBegin( GL_POLYGON );
glTexCoord2d(1.0,1.0); glVertex3d(-1.2,1.0,-2.0);
glTexCoord2d(1.0,0.0); glVertex3d(0.0,1.0,-2.0);
glTexCoord2d(0.0,0.0); glVertex3d(0.0,2.0,-2.0);
glEnd();
//face 2
glBegin( GL_POLYGON );
glTexCoord2d(0.0,0.0); glVertex3d(0.0,2.0,-2.0);
glTexCoord2d(0.0,1.0); glVertex3d(0.0,1.0,-2.0);
glTexCoord2d(1.0,1.0); glVertex3d(1.2,1.0,-2.0);
glEnd();
}
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, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture1 );
glPushMatrix();
glTranslated(0,0,0);
glRotated(rX,0,0,1);
glRotated(rY,0,1,0);
glRotated(rZ,1,0,0);
showPictureW();
glPopMatrix();
glBindTexture( GL_TEXTURE_2D, texture2 );
glPushMatrix();
glTranslated(0,0,0);
glRotated(rX,0,0,1);
glRotated(rY,0,1,0);
glRotated(rZ,1,0,0);
showPictureR();
glPopMatrix();
glutSwapBuffers();
}
static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'q':
exit(0);
break;
case '+':
break;
...
}
glutPostRedisplay();
}
static void idle(void)
{
glutPostRedisplay();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("FreeGLUT Shapes");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
texture1 = LoadTexture( 200, 200, "wall.ppm" );
texture2 = LoadTexture( 200, 200, "roof.ppm" );
glutMainLoop();
return EXIT_SUCCESS;
}
GLuint LoadTexture(int width, int height, char *fName)
{
FILE *fd;
int k, nm;
int i;
char bc[71];
float s;
unsigned int red, green, blue;
char c;
fd = fopen(fName,"r");
fscanf(fd,"%[^\n]",bc); // reads data from stream
printf("Nilai %s \n\n",bc);
if(bc[0]!='P'|| bc[1]!='3')
{
printf("%s Not a PPM file\n",bc);
exit(0);
}
printf("%s is a PPM file\n",bc);
fscanf(fd,"%s",bc);
printf("line 1 %s \n",bc);
fscanf(fd,"%s",bc);
printf("line 2 %s \n",bc);
fscanf(fd,"%s",bc);
printf("line 3 %s \n",bc);
fscanf(fd,"%s",bc);
printf("line 4 %s \n",bc);
fscanf(fd,"%c",&c);
ungetc(c,fd);
fscanf(fd,"%d %d %d", &n,&m,&k);
nm = n*m;
images = new GLuint[3*sizeof(GLuint)*nm];
for (i=0;i<nm;i++)
{
fscanf(fd,"%u %u %u", &red, &green, &blue);
images[3*nm-3*i-3] = red;
images[3*nm-3*i-2] = green;
images[3*nm-3*i-1] = blue;
}
int totSize = 3*nm;
int tot=0;
for(i=0;i<nm;i++)
{
tot++;
}
image = new BYTE[width*height*3*sizeof(BYTE)];
int nmOne = width*height;
for(int i=0;i<nmOne*3;i++)//copy each value 3 times
{
image[i] = (GLubyte)images[i];
image[i] = (GLubyte)images[i];
image[i] = (GLubyte)images[i];
}
bool wrap = true;
GLuint texture = 0;
glGenTextures( 1, &texture );
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,wrap ? GL_REPEAT : GL_CLAMP );
// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, image );
return texture;
}
My problem is that it won't load both textures at once. I've tried using one at a time and they're okay. But with the above code only the second texture ("roof.ppm") appears. I found the code below in another post which seems quite similar to my problem but I'm not sure how to use it.
void DrawFrame()
{
...
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D(0, this.Width, 0, this.Height);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// "camera" transform(s)
foreach object
{
glPushMatrix();
// per-object matrix transform(s)
// draw object
glPopMatrix();
}
}
You do call
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
in both of your showPictureW and showPictureR functions, so it should not surprise you that only the effects last of these calls can be seen in the end.
I have an openGL program thats using GLUT and I'm stuck with moving it from the top left corner to bottom right when i resize the window. I'm trying to get a ratio down and i cant figure it out.
#include <stdio.h>
#include <stdlib.h>
#include <GLUT/glut.h>
//global variables
GLsizei wh = 500, ww = 500;
double height;
double width;
double top = 500;
double bottom = 450;
int right = 50;
int left = 0;
double ratiowidth = 500/500;
double ratioheight = 500/500;
void myinit(){
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void reshape(int w, int h){
if(h == 0){
h = 1;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, (GLsizei) w, 0, (GLsizei)h);
glViewport(0,0,w,h);
glMatrixMode(GL_MODELVIEW);
int difference = h - wh;
int difference2 = w - ww;
left = left + difference2;
right = right + difference2;
wh = (GLsizei)h;
ww = (GLsizei)w;
height = h;
width = w;
}
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
top = height;
bottom = height - 50;
right = 50;
left = 0;
ratiowidth = width/height;
ratioheight = height/width;
}
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
exit(0);
}
}
void display(){
glClear (GL_COLOR_BUFFER_BIT);
glColor3i (rand(), rand(), rand());
glBegin(GL_POLYGON);
glVertex3f (left,top,0.0);
glVertex3f (right, top, 0.0);
glVertex3f (right, bottom, 0.0);
glVertex3f (left, bottom, 0.0);
glEnd();
if(bottom == 0 || bottom < 0)
bottom = 0;
else{
left = left + ratiowidth;
right = right + ratiowidth;
top = top - ratioheight;
bottom = bottom - ratioheight;
}
glutSwapBuffers();
usleep(10000);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(ww, wh);
glutInitWindowPosition(100,100);
glutCreateWindow("moving Square");
myinit();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
glBegin(GL_POLYGON);
glVertex3f (left,top,0.0);
glVertex3f (right, top, 0.0);
glVertex3f (right, bottom, 0.0);
glVertex3f (left, bottom, 0.0);
glEnd();
Careful with your winding order. glFrontFace() defaults to GL_CCW. Perhaps you meant this:
glBegin(GL_POLYGON);
glVertex3f (left, bottom, 0.0);
glVertex3f (right, bottom, 0.0);
glVertex3f (right, top, 0.0);
glVertex3f (left,top,0.0);
glEnd();
EDIT: This is what I think you were trying to do:
#include <GL/glut.h>
struct State
{
double x; // in units
double y;
double xvel; // in units per second
double yvel;
};
State curState = { 0 };
void myinit()
{
curState.xvel = 100;
curState.yvel = 100;
}
void Integrate( State* state, double dt )
{
state->x += state->xvel * dt;
state->y += state->yvel * dt;
}
double GetSeconds()
{
return glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
}
void reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, (GLsizei)w, 0, (GLsizei)h);
}
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
curState.x = 0;
curState.y = 0;
}
if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
{
exit(0);
}
}
void display()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
float w = 20;
float h = 20;
float x = curState.x;
float y = curState.y;
glColor3ub(rand()%255, rand()%255, rand()%255);
glBegin(GL_QUADS);
glVertex2f( x - w/2, y - h/2 );
glVertex2f( x + w/2, y - h/2 );
glVertex2f( x + w/2, y + h/2 );
glVertex2f( x - w/2, y + h/2 );
glEnd();
static double lastTime = GetSeconds();
double curTime = GetSeconds();
Integrate( &curState, curTime - lastTime );
lastTime = curTime;
glutSwapBuffers();
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100,100);
glutCreateWindow("moving Square");
myinit();
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Be aware that there are probably some C++-isms in there :)