14 Revize 8282d696e1 ... 9641d9d817

Autor SHA1 Zpráva Datum
  vetal 9641d9d817 Merge branch 'master' of https://notabug.org/vetal/OpenGL_MouseLook před 2 týdny
  vetal 7048b97cab objmtl fix texture y coord před 1 měsícem
  vetal 85928d9bcc objmtl and marray added před 1 měsícem
  vetal ab898cfc4d direction of movement choosen by mouse před 1 měsícem
  vetal 09c68bce6e fix head rotation před 1 měsícem
  vetal 5a00e2244e mouse rotation without borders před 1 měsícem
  vetal 977a13bf1d rotation by buttons před 1 měsícem
  vetal b98aff1b97 cx movement added před 1 měsícem
  vetal 7d9b8882f3 w s buttons, view change před 1 měsícem
  vetal 6419eaba73 persp view matrixes vertex shader před 1 měsícem
  vetal faecc18d4d OOOOOOOOOOOooooooohhh před 1 měsícem
  vetal 2f52cdaa73 Added structure for 3D object před 1 měsícem
  vetal bf55105e67 names to 'mouse look' před 1 měsícem
  vetal 0538b36388 First commit před 1 měsícem
10 změnil soubory, kde provedl 492 přidání a 0 odebrání
  1. 21 0
      Makefile
  2. 12 0
      fragment_shader.glsl
  3. 81 0
      src/bmp_reader.c
  4. 47 0
      src/bmp_reader.h
  5. 233 0
      src/main.c
  6. 40 0
      src/marray.c
  7. 23 0
      src/marray.h
  8. 19 0
      src/obj3d.c
  9. 16 0
      src/obj3d.h
  10. 0 0
      src/objmtl.c

+ 21 - 0
Makefile

@@ -0,0 +1,21 @@
+TARGET = glmouselook
+CC = gcc
+CFLAGS =
+LIBS = -lX11 -lGL -lGLU -lm
+
+PREF_SRC = ./src/
+PREF_OBJ = ./obj/
+
+
+SRCS = $(wildcard $(PREF_SRC)*.c)
+OBJS = $(patsubst $(PREF_SRC)%.c, $(PREF_OBJ)%.o, $(SRCS))
+
+$(TARGET): $(OBJS)
+	$(CC) $(OBJS) -o $(TARGET) $(LIBS)
+
+$(PREF_OBJ)%.o: $(PREF_SRC)%.c
+	$(CC) $(CFLAGS) -c $< -o $@
+
+
+clean:
+	rm $(TARGET) $(PREF_OBJ)*.o

+ 12 - 0
fragment_shader.glsl

@@ -0,0 +1,12 @@
+#version 300 es
+precision mediump float;
+
+out vec4 FragColor;
+in vec2 tcoords;
+in vec3 tnorm;
+
+uniform sampler2D tex1;
+
+void main() {
+  FragColor = texture(tex1, tcoords);
+}

+ 81 - 0
src/bmp_reader.c

@@ -0,0 +1,81 @@
+/*
+
+     This file is part of glstart.
+
+    glstart is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+    glstart is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along with Foobar. If not, see <https://www.gnu.org/licenses/>. 
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "bmp_reader.h"
+
+#ifndef BMP_READER_C
+#define BMP_READER_C
+
+void readBMPRowByRow(FILE* fp, BMPFile* bmpf) {
+  int bytes_per_pixel = bmpf->dhdr.bits_per_pixel/8;
+  int row_size = bytes_per_pixel*bmpf->dhdr.width;
+  int row_padding = (4 - (row_size % 4)) % 4;
+  int rows_written = 0;
+  unsigned char* row = (unsigned char*)malloc(row_size+row_padding);
+  unsigned char* p = &bmpf->data[(bmpf->dhdr.height-1)*row_size];
+  fseek(fp, bmpf->bhdr.pixel_offset, SEEK_SET);
+  while(rows_written < bmpf->dhdr.height) {
+    fread(row, row_size+row_padding, 1, fp);
+    if(bytes_per_pixel == 3) {
+      for(int i=0; i<row_size; i+=bytes_per_pixel) {
+        *p = row[i+2]; p++;
+        *p = row[i+1]; p++;
+        *p = row[i]; p++;
+      };
+    } else if(bytes_per_pixel == 4) {
+      for(int i=0; i<row_size; i+=bytes_per_pixel) {
+        *p = row[i+3]; p++;
+        *p = row[i+2]; p++;
+        *p = row[i+1]; p++;
+        *p = row[i]; p++;
+      };
+    } else {
+      printf("Error: don't working with bytes_per_pixel = %d\n", bytes_per_pixel);
+      exit(0);
+    };
+    rows_written++;
+    p = p - 2*row_size;
+  };
+  free(row);
+};
+
+
+BMPFile* loadBMPFile(char* fname) {
+  FILE* fp = fopen(fname, "r");
+  if(!fp) {
+    printf("Can't load file \'%s\'\n", fname);
+    return NULL;
+  };
+
+  BMPFile* bmp_file = (BMPFile*)malloc(sizeof(BMPFile));
+  fread(&bmp_file->bhdr, sizeof(BMPHeader), 1, fp);
+  fread(&bmp_file->dhdr, sizeof(DIBHeader), 1, fp);
+
+  int data_size = bmp_file->dhdr.width*bmp_file->dhdr.height*bmp_file->dhdr.bits_per_pixel/8;
+
+  bmp_file->data = (unsigned char*)malloc(data_size);
+  readBMPRowByRow(fp, bmp_file);
+  fclose(fp);
+  return bmp_file;
+};
+
+void freeBMPFile(BMPFile* bmp_file) {
+  if(bmp_file) {
+    if(bmp_file->data)
+      free(bmp_file->data);
+    free(bmp_file);
+  };
+};
+
+
+#endif

+ 47 - 0
src/bmp_reader.h

@@ -0,0 +1,47 @@
+/*
+
+     This file is part of glstart.
+
+    glstart is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+    glstart is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along with Foobar. If not, see <https://www.gnu.org/licenses/>. 
+*/
+
+#ifndef BMP_READER_H
+#define BMP_READER_H
+
+#pragma pack(1)
+typedef struct BMPHeader {
+  unsigned char ID[2];
+  unsigned int file_size;
+  unsigned char unused[4];
+  unsigned int pixel_offset;
+} BMPHeader;
+
+typedef struct DIBHeader {
+  unsigned int header_size;
+  unsigned int width;
+  unsigned int height;
+  unsigned short color_planes;
+  unsigned short bits_per_pixel;
+  unsigned int comp;
+  unsigned int data_size;
+  unsigned int pwidth;
+  unsigned int pheight;
+  unsigned int colors_count;
+  unsigned int imp_colors_count;
+} DIBHeader;
+
+typedef struct BMPFile {
+  BMPHeader bhdr;
+  DIBHeader dhdr;
+  unsigned char* data;
+} BMPFile;
+#pragma pop
+
+BMPFile* loadBMPFile(char* fname);
+void freeBMPFile(BMPFile* bmp_file);
+
+#endif

+ 233 - 0
src/main.c

@@ -0,0 +1,233 @@
+/*
+
+     This file is part of glstart.
+
+    glstart is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+    glstart is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along with Foobar. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <string.h>
+#include <X11/X.h>
+#include <X11/keysymdef.h>
+#include <X11/Xlib.h>
+
+#define GL_GLEXT_PROTOTYPES
+
+#include <GL/gl.h>
+#include <GL/glx.h>
+#include <GL/glu.h>
+#include <GL/glext.h>
+#include <GL/glcorearb.h>
+#include "vertexes.h"
+#include "shaders.h"
+#include "textures.h"
+#include "obj3d.h"
+#include "objmtl.h"
+#include "marray.h"
+
+Display* dpy;
+Window root;
+GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
+XVisualInfo* vi;
+Colormap cmap;
+XSetWindowAttributes swa;
+Window win;
+GLXContext glc;
+XWindowAttributes gwa;
+XEvent xev;
+
+int main(int argc, char* argv[]) {
+
+	if(argc != 5) {
+		printf("syntax: %s <vertex shader file> <fragment shader file> <bmp file> <obj file>\n", argv[0]);
+		return 0;
+	}
+
+	dpy = XOpenDisplay(NULL);
+	if(dpy == NULL) {
+		printf("\n\tCannot connect to X server\n\n");
+		exit(0);
+	};
+
+	root = DefaultRootWindow(dpy);
+	vi = glXChooseVisual(dpy, 0, att);
+	if(vi == NULL) {
+		printf("\n\tno appropriate visual found\n\n");
+		exit(0);
+	} else {
+		printf("\n\tvisual %p selected\n", (void*)vi->visualid);
+	};
+
+	cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
+	swa.colormap = cmap;
+	swa.event_mask = KeyPressMask | PointerMotionMask;
+
+	win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
+	XMapWindow(dpy, win);
+	XStoreName(dpy, win, "OpenGL Mouse Look");
+
+	glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
+	glXMakeCurrent(dpy, win, glc);
+
+	glEnable(GL_DEPTH_TEST);
+
+
+	Obj3D o3d;
+
+	OBJFILE* ofile = readOBJFILE(argv[4]);
+	MArray* hvbo = makeVBO(ofile);
+
+	float verts[] = { 0.0f, 0.9f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f,
+                    0.9f, -0.9f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f,
+                    -0.9f, -0.9f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f,
+                  };
+
+	obj3dInit(&o3d, CreateShaderProgram(argv[1], argv[2]),
+ 									CreateVAO(hvbo->d, hvbo->size, NULL, 0),
+									hvbo->size/(sizeof(float)*8), loadBmpTexture(argv[3]));
+
+	freeMArray(hvbo);
+	freeOBJFILE(ofile);
+
+	float n = 0.05f;
+	float f = 1000.0f;
+	float l = 0.05f;
+	float t = 0.05f;
+	float cx = 0.0f;
+	float cz = 0.0f;
+	float angleh = 0.0f;
+	float anglev = 0.0f;
+
+
+	float pers[] = {
+		2*n/l, 0.0f, 0.0f, 0.0f,
+		0.0f, 2*n/t, 0.0f, 0.0f,
+		0.0f, 0.0f, (n+f)/(n-f), 2*n*f/(n-f),
+		0.0f, 0.0f, -1.0f, 0.0f,
+	};
+
+	float view[] = {
+		1.0f, 0.0f, 0.0f, 0.0f,
+		0.0f, 1.0f, 0.0f, 0.0f,
+		0.0f, 0.0f, 1.0f, 0.0f,
+		0.0f, 0.0f, 0.0f, 1.0f
+	};
+
+	//XGrabPointer(dpy, win, True, PointerMotionMask, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
+
+	while(1) {
+		if(XCheckTypedWindowEvent(dpy, win, KeyPress, &xev)) {
+			KeySym sym = XLookupKeysym(&xev.xkey, 0);
+      switch(sym) {
+        case XK_Escape:
+					glXMakeCurrent(dpy, None, NULL);
+					glXDestroyContext(dpy, glc);
+					XDestroyWindow(dpy, win);
+					XCloseDisplay(dpy);
+					exit(0);
+					break;
+				case XK_w:
+					cz += -0.1f*cos(angleh);
+					cx += -0.1f*sin(angleh);
+					break;
+				case XK_s:
+					cz += 0.1f*cos(angleh);
+					cx += 0.1f*sin(angleh);
+					break;
+				case XK_a:
+					cx += -0.1f;
+					break;
+				case XK_d:
+					cx += 0.1f;
+					break;
+				case XK_q:
+					angleh += 0.01f;
+					break;
+				case XK_e:
+					angleh += -0.01f;
+					break;
+				case XK_z:
+					anglev += 0.01f;
+					break;
+				case XK_c:
+					anglev += -0.01f;
+					break;
+
+        default:
+					break;
+      };
+    };
+
+		view[11] = -cz;
+		view[3] = -cx;
+
+		if(angleh > 3.14f) angleh -= 6.28f;
+		if(angleh < -3.14f) angleh += 6.28f;
+
+		if(anglev > 3.14f*0.5f) anglev = 3.14f*0.5f;
+		if(anglev < -3.14f*0.5f) anglev = -3.14f*0.5f;
+
+		float roth[] = {
+			cos(-angleh), 0.0f, sin(-angleh), 0.0f,
+			0.0f, 1.0f, 0.0f, 0.0f,
+			-sin(-angleh), 0.0f, cos(-angleh), 0.0f,
+			0.0f, 0.0f, 0.0f, 1.0f
+		};
+
+		float rotv[] = {
+			1.0f, 0.0f, 0.0f, 0.0f,
+			0.0f, cos(-anglev), -sin(-anglev), 0.0f,
+			0.0f, sin(-anglev), cos(-anglev), 0.0f,
+			0.0f, 0.0f, 0.0f, 1.0f
+		};
+
+
+		XGetWindowAttributes(dpy, win, &gwa);
+
+		short mx = gwa.width/2;
+		short my = gwa.height/2;
+		int dx = 0;
+		int dy = 0;
+
+		Window r_w, c_w;
+		int root_x_return, root_y_return;
+		unsigned int mask_return;
+		if(True == XQueryPointer(dpy, win, &r_w, &c_w, &root_x_return, &root_y_return, &dx, &dy, &mask_return)) {
+			dx = mx - dx;
+			dy = my - dy;
+			float dangle_xz = ((float)dx)/1000.0f;
+			float dangle_yz = ((float)dy)/1000.0f;
+			angleh += dangle_xz;
+			anglev += dangle_yz;
+		};
+
+		XWarpPointer(dpy, win, win, 0, 0, gwa.width, gwa.height, gwa.width/2, gwa.height/2);
+
+		glViewport(0, 0, gwa.width, gwa.height);
+
+		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+		glUseProgram(o3d.shaderProgram);
+		glUniform1i(glGetUniformLocation(o3d.shaderProgram, "tex1"), 0);
+		glUniformMatrix4fv(glGetUniformLocation(o3d.shaderProgram, "roth"), 1, GL_TRUE, roth);
+		glUniformMatrix4fv(glGetUniformLocation(o3d.shaderProgram, "rotv"), 1, GL_TRUE, rotv);
+		glUniformMatrix4fv(glGetUniformLocation(o3d.shaderProgram, "pers"), 1, GL_TRUE, pers);
+		glUniformMatrix4fv(glGetUniformLocation(o3d.shaderProgram, "view"), 1, GL_TRUE, view);
+		glUniformMatrix4fv(glGetUniformLocation(o3d.shaderProgram, "trans"), 1, GL_TRUE, o3d.transform);
+		glActiveTexture(GL_TEXTURE0);
+		glBindTexture(GL_TEXTURE_2D, o3d.tex);
+
+		glBindVertexArray(o3d.VAO);
+		glDrawArrays(GL_TRIANGLES, 0, o3d.vao_size);
+
+		glXSwapBuffers(dpy, win);
+	};
+};

+ 40 - 0
src/marray.c

@@ -0,0 +1,40 @@
+/*
+
+MArray
+
+*/
+
+#include "marray.h"
+
+#ifndef MARRAY_C
+#define MARRAY_C
+
+#include <stdlib.h>
+#include <string.h>
+
+MArray* createMArray() {
+	MArray* ha = (MArray*)malloc(sizeof(MArray));
+	ha->size = 0;
+	ha->msize = 0;
+	ha->d = NULL;
+	return ha;
+};
+
+void addMArrayEl(void* src, unsigned int size, MArray* ha) {
+	unsigned int newsize = ha->size + size;
+	if(newsize > ha->msize) {
+		ha->msize = newsize + MARRAY_MEM_BLOCK;
+		ha->d = realloc(ha->d, ha->msize);
+	};
+
+	memcpy(ha->d + ha->size, src, size);
+	ha->size = newsize;
+};
+
+void freeMArray(MArray* ha) {
+	free(ha->d);
+	free(ha);
+};
+
+
+#endif

+ 23 - 0
src/marray.h

@@ -0,0 +1,23 @@
+/*
+
+MArray
+
+*/
+
+#ifndef MARRAY_H
+#define MARRAY_H
+
+#define MARRAY_MEM_BLOCK 100
+
+typedef struct {
+	unsigned int msize;
+	unsigned int size;
+	void *d;
+} MArray;
+
+MArray* createMArray();
+void addMArrayEl(void* src, unsigned int size, MArray* ha);
+void freeMArray(MArray* ha);
+
+
+#endif

+ 19 - 0
src/obj3d.c

@@ -0,0 +1,19 @@
+#include <string.h>
+#include "obj3d.h"
+
+#ifndef OBJ3D_C
+#define OBJ3D_C
+
+void obj3dInit(Obj3D* obj, unsigned int sp, unsigned int VAO, unsigned int vao_size, unsigned int tex) {
+  memset(obj, 0, sizeof(Obj3D));
+  obj->shaderProgram = sp;
+  obj->VAO = VAO;
+  obj->vao_size = vao_size;
+  obj->tex = tex;
+  obj->transform[0] = 1.0f;
+  obj->transform[5] = 1.0f;
+  obj->transform[10] = 1.0f;
+  obj->transform[15] = 1.0f;
+};
+
+#endif

+ 16 - 0
src/obj3d.h

@@ -0,0 +1,16 @@
+
+
+#ifndef OBJ3D_H
+#define OBJ3D_H
+
+typedef struct {
+  unsigned int shaderProgram;
+  unsigned int VAO;
+  unsigned int vao_size;
+  unsigned int tex;
+  float transform[16];
+} Obj3D;
+
+void obj3dInit(Obj3D* obj, unsigned int sp, unsigned int VAO, unsigned int vao_size, unsigned int tex);
+
+#endif

+ 0 - 0
src/objmtl.c


Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů