Results 1 to 3 of 3

Thread: how to render an image in openGL-HELP

  1. #1
    Junior Member Newbie
    Join Date
    May 2009
    Posts
    3

    how to render an image in openGL-HELP

    hello guyz
    i am a newbie and i am trying to make the earth and rotate it with lighting effects..till now i have made a sphere with light effects.now i want to load an image and make that sphere look like an earth...plz help me out....thanx a lot in advance...and i am a beginner..even this code has been taken from somewhere.
    Code :
    #include <iostream>
    #include <stdlib.h>
     
    #ifdef __APPLE__
    #include <OpenGL/OpenGL.h>
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
     
    using namespace std;
     
    const float RADIUS = 7.0f; //The radius of the sphere
     
    float _angle = -60;
     
    bool _highShininess = false; //Whether the shininess parameter is high
    bool _lowSpecularity = false; //Whether the specularity parameter is high
    bool _emission = false; //Whether the emission parameter is turned on
     
    void handleKeypress(unsigned char key, int x, int y) {
    	switch (key) {
    		case 27: //Escape key
    			exit(0);
    			break;
    		case 's':
    			_highShininess = !_highShininess;
    			break;
    		case 'p':
    			_lowSpecularity = !_lowSpecularity;
    			break;
    		case 'e':
    			_emission = !_emission;
    			break;
    	}
    }
     
    void initRendering() {
    	glEnable(GL_DEPTH_TEST);
    	glEnable(GL_LIGHTING);
    	glEnable(GL_LIGHT0);
    	glEnable(GL_NORMALIZE);
    	glShadeModel(GL_SMOOTH);
    	//Disable color materials, so that glMaterial calls work
    	glDisable(GL_COLOR_MATERIAL);
    }
     
    void handleResize(int w, int h) {
    	glViewport(0, 0, w, h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
    }
     
    void drawScene() {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
     
    	glTranslatef(0.0f, 0.0f, -20.0f);
    	glRotatef(_angle, 0, 1, 0);
     
    	GLfloat ambientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
    	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
     
    	GLfloat lightColor[] = {0.6f, 0.6f, 0.6f, 1.0f};
    	GLfloat lightPos[] = {1.5f * RADIUS, 2 * RADIUS, 1.5 * RADIUS, 1.0f};
    	//Diffuse (non-shiny) light component
    	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
    	//Specular (shiny) light component
    	glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);
    	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
     
    	//Determine the specularity, emissivity, and shininess parameters, based on
    	//variables that can be toggled with keystrokes
    	float specularity;
    	if (_lowSpecularity) {
    		specularity = 0.3f;
    	}
    	else {
    		specularity = 1;
    	}
     
    	float emissivity;
    	if (_emission) {
    		emissivity = 0.05f;
    	}
    	else {
    		emissivity = 0;
    	}
     
    	float shininess;
    	if (_highShininess) {
    		shininess = 25;
    	}
    	else {
    		shininess = 12;
    	}
     
    	//The color of the sphere
    	GLfloat materialColor[] = {0.2f, 0.2f, 1.0f, 1.0f};
    	//The specular (shiny) component of the material
    	GLfloat materialSpecular[] = {specularity, specularity, specularity, 1.0f};
    	//The color emitted by the material
    	GLfloat materialEmission[] = {emissivity, emissivity, emissivity, 1.0f};
     
    	glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColor);
    	glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular);
    	glMaterialfv(GL_FRONT, GL_EMISSION, materialEmission);
    	glMaterialf(GL_FRONT, GL_SHININESS, shininess); //The shininess parameter
     
    	//Draw the sphere
    	glutSolidSphere(RADIUS, 150, 80);
     
    	glutSwapBuffers();
    }
     
    //Called every 25 milliseconds
    void update(int value) {
    	_angle += 1.5f;
    	if (_angle > 360) {
    		_angle -= 360;
    	}
     
    	glutPostRedisplay();
    	glutTimerFunc(25, update, 0);
    }
     
    int main(int argc, char** argv) {
    	glutInit(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowSize(400, 400);
     
    	glutCreateWindow("Materials - videotutorialsrock.com");
    	initRendering();
     
    	glutDisplayFunc(drawScene);
    	glutKeyboardFunc(handleKeypress);
    	glutReshapeFunc(handleResize);
    	glutTimerFunc(25, update, 0);
     
    	glutMainLoop();
    	return 0;
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: how to render an image in openGL-HELP

    You are asking how to do OpenGL texture mapping. An image must be loaded, then 'wrapped' around your sphere. See nehe tutorial #6.
    Am I doing your homework for you?

  3. #3
    Junior Member Newbie
    Join Date
    May 2009
    Posts
    3

    Re: how to render an image in openGL-HELP

    thanx dude...hey y dont we have any GUI version of openGL??Things wud have been a lil easier

Similar Threads

  1. Render image using texture with opengl es 1.1
    By Naseeb Panghal in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 08-18-2015, 04:38 PM
  2. render to renderbuffer image
    By xjtuwyf in forum OpenGL ES
    Replies: 1
    Last Post: 04-01-2013, 03:09 PM
  3. How to render background image in GL3?
    By pi3orama in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-10-2009, 08:16 AM
  4. render image in FBO
    By sungsoo in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 04-01-2009, 10:33 AM
  5. Render Image
    By nilu12 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 07-28-2008, 06:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean