1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include "SDL.h"
#include "SDL_opengl.h"
#include <stdio.h>
#include <string.h>
#define WIDTH 640
#define HEIGHT 480
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[] = {20.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};
const float PI2 = 2.0f*3.1415926535;
int NumWraps = 100;
int NumPerWrap = 80;
float MajorRadius = 0.5f;
float MinorRadius = 0.2f;
void putVert(int i, int j) {
float wrapFrac = (j%NumPerWrap)/(float)NumPerWrap;
float phi = PI2*wrapFrac;
float theta = PI2*(i%NumWraps+wrapFrac)/(float)NumWraps;
float sinphi = sin(phi);
float cosphi = cos(phi);
float sintheta = sin(theta);
float costheta = cos(theta);
float y = MinorRadius*sinphi;
float r = MajorRadius + MinorRadius*cosphi;
float x = sintheta*r;
float z = costheta*r;
glNormal3f(sintheta*cosphi, sinphi, costheta*cosphi);
glVertex3f(x,y,z);
}
int main(int argc, char* argv[]) {
SDL_Window* window;
SDL_GLContext context;
int i,j;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
window = SDL_CreateWindow(
"OpenGL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
if (!window) {
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
return 0;
}
context = SDL_GL_CreateContext(window);
if (!context) {
fprintf(stderr, "Couldn't create context: %s\n", SDL_GetError());
return 0;
}
glMatrixMode(GL_PROJECTION);
glOrtho(-1.0f, 1.0f, -1.0f * HEIGHT/WIDTH, 1.0f * HEIGHT/WIDTH, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
// Clear the screen before drawing
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHT0);
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);
glEnable(GL_CLIP_PLANE0);
glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
glRotatef(60.0f, 0.0f, 1.0f, 0.0f);
const GLdouble clip_plane[] = {0.0, 0.0, -1.0, 0.4};
glClipPlane(GL_CLIP_PLANE0, clip_plane);
glColor3f(1.0f, 0.5f, 1.0f);
glBegin(GL_TRIANGLE_STRIP);
for (i=0; i<NumWraps; i++ ) {
for (j=0; j<NumPerWrap; j++) {
putVert(i,j);
putVert(i+1,j);
}
}
putVert(0,0);
putVert(1,0);
glEnd();
SDL_GL_SwapWindow(window);
// Don't quit - we need to reftest the canvas! SDL_Quit();
return 0;
}
|