GLdc/samples/lights/main.c

248 lines
5.9 KiB
C
Raw Normal View History

2019-11-14 10:04:15 +00:00
#include <stdio.h>
2021-04-09 15:24:47 +00:00
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
2019-11-14 10:04:15 +00:00
#ifdef __DREAMCAST__
#include <kos.h>
#endif
2021-04-09 15:24:47 +00:00
#include "GL/gl.h"
#include "GL/glu.h"
#include "GL/glext.h"
#include "GL/glkos.h"
2019-11-14 10:04:15 +00:00
2021-04-09 15:24:47 +00:00
#ifdef __DREAMCAST__
#include <kos.h>
extern uint8_t romdisk[];
2019-11-14 10:04:15 +00:00
KOS_INIT_ROMDISK(romdisk);
2021-04-09 15:24:47 +00:00
#define IMAGE_FILENAME "/rd/NeHe.bmp"
#else
#define IMAGE_FILENAME "../samples/lights/romdisk/NeHe.bmp"
2021-04-09 15:24:47 +00:00
#endif
2019-11-14 10:04:15 +00:00
#include "../loadbmp.h"
2019-11-14 10:04:15 +00:00
float xrot, yrot, zrot;
2019-11-14 10:04:15 +00:00
int texture[1];
void LoadGLTextures() {
Image *image1;
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}
2021-04-09 15:24:47 +00:00
if (!ImageLoad(IMAGE_FILENAME, image1)) {
2019-11-14 10:04:15 +00:00
exit(1);
}
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
glGenerateMipmapEXT(GL_TEXTURE_2D);
free(image1);
2019-11-14 10:04:15 +00:00
};
void InitGL(int Width, int Height)
{
LoadGLTextures();
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glClearColor(0.5, 0.5, 0.5, 0.5);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 1.0f, 0.0f, 0.0, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = { -1.5f, -1.0f, 0.0f, 0.0f };
// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glDisable(GL_TEXTURE_2D);
2019-11-14 21:12:27 +00:00
diffuseLight[1] = 1.0f;
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT1, GL_SPECULAR, specularLight);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.0);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 4.5 / 100);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 75.0f / (100 * 100));
glEnable(GL_NORMALIZE);
2019-11-14 10:04:15 +00:00
}
void ReSizeGLScene(int Width, int Height)
{
if (Height == 0)
Height = 1;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}
int check_start() {
#ifdef __DREAMCAST__
maple_device_t *cont;
cont_state_t *state;
cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);
if(cont) {
state = (cont_state_t *)maple_dev_status(cont);
if(state)
return state->buttons & CONT_START;
}
#endif
return 0;
}
2019-11-14 10:04:15 +00:00
void DrawCube(float x, float z) {
2019-11-14 21:12:27 +00:00
static float pos = 0.0f;
2023-09-01 19:23:36 +00:00
static const float radius = 30.0f;
2019-11-14 21:12:27 +00:00
pos += 0.001f;
GLfloat position[] = { cos(pos) * radius, 15.0f, sin(pos) * radius, 1.0f };
glLightfv(GL_LIGHT1, GL_POSITION, position);
2019-11-14 10:04:15 +00:00
glPushMatrix();
glTranslatef(x, 0, z);
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glNormal3f(0, 0, -1);
2019-11-14 10:04:15 +00:00
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glNormal3f(0, 0, 1);
2019-11-14 10:04:15 +00:00
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glNormal3f(0, 1, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glNormal3f(0, -1, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glNormal3f(1, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glNormal3f(-1, 0, 0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();
}
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -55.0f);
glRotatef(45, 1, 0, 0);
glTranslatef(0, 0, -30);
glBindTexture(GL_TEXTURE_2D, texture[0]);
int x, z;
for(z = -100; z < 100; z += 10) {
for(x = -100; x < 100; x += 10) {
DrawCube(x, z);
}
}
glKosSwapBuffers();
}
int main(int argc, char **argv)
{
GLdcConfig config;
glKosInitConfig(&config);
2020-05-07 19:58:24 +00:00
config.fsaa_enabled = GL_FALSE;
glKosInitEx(&config);
2019-11-14 10:04:15 +00:00
InitGL(640, 480);
ReSizeGLScene(640, 480);
while(1) {
if(check_start())
break;
2019-11-14 10:04:15 +00:00
DrawGLScene();
}
return 0;
}