|
-lmingw32 -lSDLmain -lSDL -lopengl32 -lglu32back to the top
SDL.lib SDLmain.lib opengl32.lib glu32.libback to the top
#include <SDL.h> #include <SDL_opengl.h> #include <stdlib.h>back to the top
#include <SDL_opengl.h> #include <SDL.h> #include <stdlib.h>back to the top
#undef main // allows write to console int main(int argc, char **argv)back to the top
SDL_Event event; SDL_Surface* pSurface; SDL_Init ( SDL_INIT_VIDEO ) ; atexit ( SDL_Quit ) ; SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 6 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); pSurface = SDL_SetVideoMode ( SCREEN_WIDTH, SCREEN_HEIGHT , 0, SDL_OPENGL | SDL_ANYFORMAT ) ; glClearColor(0.0, 0.0, 0.0, 0.0); reshape(SCREEN_WIDTH, SCREEN_HEIGHT);back to the top
while(1) { if ( SDL_PollEvent ( &event ) ) { switch(event.type) { } } renderScene(); }back to the top
void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //gluOrtho2D(LEFT, RIGHT, BOTTOM, TOP); //glOrtho(LEFT, RIGHT, BOTTOM, TOP, CLIP_NEAR, CLIP_FAR); //gluPerspective(FOV, WIDTH/HEIGHT, CLIP_NEAR, CLIP_FAR); glMatrixMode(GL_MODELVIEW); }back to the top
void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(viewcam.pos.x,viewcam.pos.y,viewcam.pos.z, viewcam.at.x,viewcam.at.y,viewcam.at.z, viewcam.up.x,viewcam.up.y,viewcam.up.z); SDL_GL_SwapBuffers( ); }back to the top
Of course, with the above code, it is assumed that cameras are defined:
typedef struct { float x; float y; float z; } VECTOR; typedef struct { VECTOR pos; VECTOR at; VECTOR up; } CAMERA; CAMERA viewcam;back to the top
glColor3f(1.0, 1.0, 1.0);back to the top
glBegin(GL_TRIANGLES); GL_POINTS, GL_QUADS, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUAD_STRIP ... glEnd(); glVertex2f(X,Y); glVertex3f(X,Y,Z);back to the top
include glText header
#include "glText.h"
Link to gltext library before linking to opengl
-lgltext
and call
initText()
(after setting the video mode.)
switch to an orthographic projection, flip the scale and move to top left
glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, w, 0, h); glScalef(1, -1, 1); glTranslatef(0, -h, 0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();back to the top
glRasterPos2i(20, 64); printString("Timmay!");back to the top
case SDL_KEYDOWN: // SDL_KEY_UP processKeys(&event.key); break; void processKeys(SDL_KeyboardEvent *key, int state) { switch(key->keysym.sym) { case SDLK_ESCAPE: // SDLK_5 SDLK_g // if (state) keypressed; else notkeypressed; exit(0); break; } }back to the top
case SDL_MOUSEMOTION: processMotion(&event.motion); break; void processMotion(SDL_MouseMotionEvent *mouse) { MouseX = mouse->x; // mouse->xrel MouseY = mouse->y; // mouse->yrel } case SDL_MOUSEBUTTONDOWN: processMouse(&event.button); break; void processMouse(SDL_MouseButtonEvent *mouse, vectorback to the top&points, SimpleColor &activeColor) { float x, y; x = (mouse->x-256); y = (256-mouse->y); if (mouse->button == SDL_BUTTON_LEFT) ;//... if (mouse->button == SDL_BUTTON_RIGHT) ;//... }
SDL_ShowCursor(SDL_DISABLE); // SDL_ENABLEback to the top
SDL_WM_GrabInput(SDL_GRAB_ON);
To limit the mouse to the window, grab raw input but don’t hide the cursor.
back to the topDisplay lists are referenced by Gluints:
GLuint ground;
They are drawn by calling them:
glCallList(ground);
To use a display list, first of all it must be generated:
ground = glGenLists(1);
To start adding to a list, declare the list that will be used:
glNewList(ground, GL_COMPILE);
To finish adding to the list:
glEndList();back to the top
Set a normal with:
glNormal3f(0.0, 1.0, 0.0);
For simple material settings:
glEnable(GL_COLOR_MATERIAL);
To enable / disable lighting use:
glEnable(GL_LIGHTING); glDisable(GL_LIGHTING);
To enable / disable a specific light use:
glEnable(GL_LIGHTi); glDisable(GL_LIGHTi);
glLightfv(GL_LIGHT0, GL_DIFFUSE, red); glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
where 'red' and 'light0_position' are vectors.
NB. position MUST have a 4th dimension with value 1.
If you are going to scale polygons that have normals, enable simple scaling of normals with:
glEnable(GL_RESCALE_NORMAL);back to the top
Steps below are for 1d lines. See notes for 2d surfaces.
Two alternatives: fine control, and automatic.
Start off with an array of coordinates (data). Then...
glMap1f(GL_MAP1_VERTEX_3, u1=0.0, u2=1.0, stride=3, order=4, data*);
glEnable(GL_MAP1_VERTEX_3);
glBegin(GL_LINES); for (i=0; i<number of points; i++) glEvalCoord1f(0.0.....1.0 = i/number of points); glEnd();
map a grid --> glMapGrid1f(partitions=30, u1=0.0, u2=1.0); eval mesh --> glEvalMesh1(GL_point/line/fill, int grid step 0, int grid step 30);
glEnable(GL_AUTO_NORMAL);
Goes along first line, then second line. eg. chessboard a1 -a8, followed by b1 - b8 etc is ccw
Change Map1, Grid1 and Mesh1 to Map2, Grid2 and Mesh2, and add additional parameters:
Map:
glMap2f(GL_MAP2_VERTEX_3, u1=0.0, u2=1.0, stride=3, order=4, v1=0.0, v2=1.0, stride=12, order=4, (GLfloat*)surfacePoints);
enable
glEnable(GL_MAP2_VERTEX_3);Map->Grid:
glMapGrid2f(partitions=20, u1=0.0, u2=1.0, partitions=20, v1=0.0, v2=1.0);
Evaluate mesh
glEvalMesh2(GL_FILL, int grid step 0, int grid step 20, int grid step 0, int grid step 20);back to the top
To draw a quadric object:
GLUquadricObj *q = gluNewQuadric(); gluQuadricDrawStyle(q, GLU_LINE); gluQuadricNormals(q, GL_SMOOTH); gluSphere (q,10,25,25); gluDeleteQuadric(q);back to the top
Get milliseconds:
time = SDL_GetTicks();back to the top