PDA

View Full Version : Doesnt look like a crystal ball



Eniak19
06-01-2008, 07:45 AM
I want make small DLG that looks like 3DMax Material Editor,but I find it dosent go well,may code was below.


I think my code can be divided into two blocks.
first, I think draw the transparent ball,because I use the
glColor4f(1.0f,0.0f,0.0f,0.5f);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
But,it doesnt make it transparent.

second,the plane after the ball seems been affected the color,it looks like only has the red color. In fact, I want it to appeare its BMP file,which was drew by myself, a colorful chessboard,that always appears in 3D Max Material Editor when you make some glass material.

So what is worng with my code?



#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <gl/glaux.h>

#pragma comment (lib,"glaux.lib")

/* Create checkerboard texture */



GLuint texture[1];

void init(char* filename)
{
FILE *file=NULL;
assert(filename);
file = fopen(filename,"r");
if ( !file )
return;

fclose(file);

AUX_RGBImageRec *TextureImage[1];
memset( TextureImage, 0, sizeof(void *)*1);

TextureImage[0] = auxDIBImageLoad(filename);


glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D( GL_TEXTURE_2D, 0, 3,
TextureImage[0]->sizeX,
TextureImage[0]->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE,
TextureImage[0]->data
);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);

if (TextureImage[0])
{
if (TextureImage[0]->data)
{
free(TextureImage[0]->data);
}

free(TextureImage[0]);
}
// glBindTexture(GL_TEXTURE_2D, texture[0]);
}

void display(void)
{

//LIGHT
GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
GLfloat local_view[] = { 0.0 };



glClearColor(0.1, 0.1, 0.1, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);


// BALL
glEnable( GL_COLOR_MATERIAL );
glEnable(GL_BLEND);
glColor4f(1.0f,0.0f,0.0f,0.5f);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glutSolidSphere(1.0, 32, 32);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_BLEND);


// BACKGROUND
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.6);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

#include <list>
#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init("s.bmp");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();

return 0;
}

rgpc
06-02-2008, 05:05 PM
Try drawing your background BEFORE your ball.

rgpc
06-02-2008, 05:12 PM
And you should also call:

glColor4f( 1.0f, 1.0f, 1.0f, 1.0f);

Before drawing your plane. Otherwise the plane will be affected by the colour that you've selected from the previous call to glColor4f() (when you last drew the ball).