Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1f197cd
feat:初步完成3d context obj的创建
luckzhiwei Jun 19, 2020
1af88bb
refacor:重构canvas init的顺序
luckzhiwei Jun 19, 2020
2ecbff8
refactor:重构node工程中GRenderContexting
luckzhiwei Jun 19, 2020
7d0afd6
feat:完成初步三角形绘制相关函数,但是绘制有些问题
luckzhiwei Jun 22, 2020
aab1194
feat:add the triangle
luckzhiwei Jun 22, 2020
dd1174c
fix:修复shadersource的错误
luckzhiwei Jun 23, 2020
6d31b24
fix:派茶无法绘制三角形的问题,目前发现不是egl的环境问题导致
luckzhiwei Jun 23, 2020
b6c643d
m:排查到bufferdata的binding函数实现有误导致三角形绘制不正常
luckzhiwei Jun 24, 2020
b3e7cb1
fix:viewport/scissor等函数加上dpi
luckzhiwei Jun 24, 2020
922e15d
fix:修复bufferdata的错误
luckzhiwei Jun 28, 2020
d0253c1
fix:修复bufferdata的错误
luckzhiwei Jun 28, 2020
276a76c
feat:完成unifrom和attri向量赋值
luckzhiwei Jun 28, 2020
fff922e
m:stencil完成支持
luckzhiwei Jun 28, 2020
04a910e
feat:完成fbo相关binding函数
luckzhiwei Jun 29, 2020
3914672
feat:完成初步完成textimage2d的支持
luckzhiwei Jun 29, 2020
4cd9a72
feat:基本完成binding函数,压缩纹理相关还没有完成,正在进行测试
luckzhiwei Jun 30, 2020
42f0f4f
feat: update glcube.js
Jul 20, 2020
6ae1514
chroe: change webgl dir
Jul 20, 2020
314aafe
feat: pixelStorei fix glerror
Jul 20, 2020
a0c8047
chroe: rename
Jul 20, 2020
e825e7e
chroe:remove log
luckzhiwei Jul 29, 2020
3563dff
feat: update webgl tc.
jwxbond Jul 29, 2020
d00444d
fix: glUniformMatrix4fv
Jul 30, 2020
1daf1a5
fix: getAttribLocation name parse
Aug 3, 2020
e0d18f1
fix: webgl tc
Sep 23, 2020
dc07af0
chroe: code style
Sep 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions core/src/gcanvas/GCanvas2dContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,52 @@ void GCanvasContext::SetFillStyle(const char *str)

GColorRGBA color = StrValueToColorRGBA(str);
SetFillStyle(color);
// mock();
}

void GCanvasContext::mock()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glViewport(0, 0, mWidth, mHeight);
glClear(GL_COLOR_BUFFER_BIT);
static const GLfloat g_vertex_buffer_data[] = {
-1.0f,
-1.0f,
0.0f,
1.0f,
-1.0f,
0.0f,
0.0f,
1.0f,
0.0f,
};
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);

// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void *)0 // array buffer offset
);
// Draw the triangle !

glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

// glDisableVertexAttribArray(0);
// glFlush();
}

void GCanvasContext::SetFillStyle(GColorRGBA c)
Expand Down
2 changes: 1 addition & 1 deletion core/src/gcanvas/GCanvas2dContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class GCanvasContext {
API_EXPORT bool InitializeGLEnvironment();
bool InitializeGLShader();
void ResetStateStack();

void mock();
void BindVertexBuffer();
void ClearGeometryDataBuffers();
API_EXPORT void SendVertexBufferToGPU(const GLenum geometry_type = GL_TRIANGLES);
Expand Down
19 changes: 14 additions & 5 deletions node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ project(canvas)
# CMake JS
include_directories(${CMAKE_JS_INC})

include_directories("./util")
include_directories("./binding/util")
include_directories("./third_party")
include_directories("./third_party/font/freetype2/")
include_directories("./renderContext/")
include_directories("./binding/renderContext/")
include_directories("./image/")

include_directories("${CORE_DIR}")
include_directories("${CORE_DIR}src/")
include_directories("${CORE_DIR}src")
include_directories("${CORE_DIR}src/webgl/")
include_directories("${CORE_DIR}src/gcanvas")
include_directories("${CORE_DIR}src/gcanvas/GL")
include_directories("${CORE_DIR}src/support")
Expand All @@ -35,15 +35,24 @@ set(SOURCE_FILES
./binding/Export.cc
./binding/Canvas.cc
./binding/CanvasRenderingContext2D.cc
./binding/CanvasRenderingContextWebGL.cc
./binding/CanvasGradient.cc
./binding/ImageData.cc
./binding/TextMetrics.cc
./binding/Image.cc
./binding/ImageWorker.cc
./binding/CanvasPattern.cc
./renderContext/GRenderContext.cc
./binding/webgl/WebGLShader.cc
./binding/webgl/WebGLBuffer.cc
./binding/webgl/WebGLProgram.cc
./binding/webgl/WebGLTexture.cc
./binding/webgl/WebGLFrameBuffer.cc
./binding/webgl/WebGLRenderBuffer.cc
./binding/webgl/WebGLActiveInfo.cc
./binding/webgl/WebGLUniformLocation.cc
./binding/renderContext/GRenderContext.cc
./binding/util/NodeBindingUtil.cc
./third_party/lodepng.cc
./util/NodeBindingUtil.cc
${CORE_DIR}src/GCanvas.cpp
${CORE_DIR}src/GCanvasManager.cpp

Expand Down
94 changes: 58 additions & 36 deletions node/binding/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace NodeBinding
checkArgs(info, 2);
mWidth = info[0].As<Napi::Number>().Int32Value();
mHeight = info[1].As<Napi::Number>().Int32Value();
mRenderContext = std::make_shared<GRenderContext>(mWidth, mHeight,2.0);
mRenderContext = std::make_shared<GRenderContext>(mWidth, mHeight, 2.0);
mRenderContext->initRenderEnviroment();
}

Expand Down Expand Up @@ -82,55 +82,77 @@ namespace NodeBinding
std::string type = info[0].As<Napi::String>().Utf8Value();
if (type == "2d")
{
if (this->context2dRef.IsEmpty())
if (mContext2dRef.IsEmpty())
{
Napi::Object obj = Context2D::NewInstance(env);
this->context2dRef = Napi::ObjectReference::New(obj);
Context2D *ctx = Napi::ObjectWrap<Context2D>::Unwrap(obj);
ctx->setRenderContext(this->mRenderContext);
mRenderContext->setType(type);
ctx->setRenderContext(mRenderContext);
ctx->setCanvasRef(this);

//save reference
mContext2dRef = Napi::ObjectReference::New(obj);
return obj;
}
else
{
return mContext2dRef.Value();
}
}
else if (type == "webgl")
{
if (mContextWebGLRef.IsEmpty())
{
Napi::Object obj = ContextWebGL::NewInstance(env);
ContextWebGL *ctx = Napi::ObjectWrap<ContextWebGL>::Unwrap(obj);
ctx->setRenderContext(mRenderContext);
mRenderContext->setType(type);
obj.Set("canvas", this->Value());

// save reference
mContextWebGLRef = Napi::ObjectReference::New(obj);
return obj;
}
else
{
return this->context2dRef.Value();
return mContextWebGLRef.Value();
}
}
else
{
throwError(info, "only support 2d now");
throwError(info, "type is invalid \n");
return Napi::Object::New(env);
}
}
void Canvas::createPNG(const Napi::CallbackInfo &info)
{
NodeBinding::checkArgs(info, 1);
std::string arg = info[0].As<Napi::String>().Utf8Value();
if (this->mRenderContext)
if (mRenderContext)
{
this->mRenderContext->makeCurrent();
this->mRenderContext->drawFrame();
this->mRenderContext->render2file(arg.c_str(), PNG_FORAMT);
mRenderContext->makeCurrent();
mRenderContext->drawFrame();
mRenderContext->render2file(arg.c_str(), PNG_FORAMT);
}
return;
}
void Canvas::createJPEG(const Napi::CallbackInfo &info)
{
NodeBinding::checkArgs(info, 1);
std::string arg = info[0].As<Napi::String>().Utf8Value();
if (this->mRenderContext)
if (mRenderContext)
{
this->mRenderContext->makeCurrent();
this->mRenderContext->drawFrame();
this->mRenderContext->render2file(arg.c_str(), JPEG_FORMAT);
mRenderContext->makeCurrent();
mRenderContext->drawFrame();
mRenderContext->render2file(arg.c_str(), JPEG_FORMAT);
}
return;
}
Napi::Value Canvas::createJPGStreamSync(const Napi::CallbackInfo &info)
{
NodeBinding::checkArgs(info, 2);
unsigned long size = 0;
Napi::Buffer<unsigned char> buffer = this->getJPGBuffer(info, size);
Napi::Buffer<unsigned char> buffer = getJPGBuffer(info, size);
if (size >= 0)
{
Napi::Function callback = info[0].As<Napi::Function>();
Expand All @@ -155,7 +177,7 @@ namespace NodeBinding
{
NodeBinding::checkArgs(info, 2);
unsigned long size = 0;
Napi::Buffer<unsigned char> buffer = this->getPNGBuffer(info, size);
Napi::Buffer<unsigned char> buffer = getPNGBuffer(info, size);
if (size >= 0)
{
Napi::Function callback = info[0].As<Napi::Function>();
Expand All @@ -177,13 +199,13 @@ namespace NodeBinding
}
Napi::Buffer<unsigned char> Canvas::getPNGBuffer(const Napi::CallbackInfo &info, unsigned long &size)
{
if (this->mRenderContext)
if (mRenderContext)
{
this->mRenderContext->makeCurrent();
this->mRenderContext->drawFrame();
mRenderContext->makeCurrent();
mRenderContext->drawFrame();
}
std::vector<unsigned char> dataPNGFormat;
int ret = this->mRenderContext->getImagePixelPNG(dataPNGFormat);
int ret = mRenderContext->getImagePixelPNG(dataPNGFormat);
if (ret == 0)
{
size = dataPNGFormat.size();
Expand All @@ -196,13 +218,13 @@ namespace NodeBinding
}
Napi::Buffer<unsigned char> Canvas::getJPGBuffer(const Napi::CallbackInfo &info, unsigned long &size)
{
if (this->mRenderContext)
if (mRenderContext)
{
this->mRenderContext->makeCurrent();
this->mRenderContext->drawFrame();
mRenderContext->makeCurrent();
mRenderContext->drawFrame();
}
unsigned char *dataJPGFormat = nullptr;
int ret = this->mRenderContext->getImagePixelJPG(&dataJPGFormat, size);
int ret = mRenderContext->getImagePixelJPG(&dataJPGFormat, size);
if (ret == 0)
{
return Napi::Buffer<unsigned char>::Copy(info.Env(), dataJPGFormat, size);
Expand All @@ -216,14 +238,14 @@ namespace NodeBinding
Napi::Buffer<unsigned char> Canvas::getRawDataBuffer(const Napi::CallbackInfo &info, unsigned long &size)
{

if (this->mDataRaw == nullptr)
if (mDataRaw == nullptr)
{
this->mDataRaw = new unsigned char[4 * mWidth * mHeight];
mDataRaw = new unsigned char[4 * mWidth * mHeight];
}
int ret = this->mRenderContext->readPixelAndSampleFromCurrentCtx(mDataRaw);
int ret = mRenderContext->readPixelAndSampleFromCurrentCtx(mDataRaw);
if (ret == 0)
{
return Napi::Buffer<unsigned char>::Copy(info.Env(), this->mDataRaw, 4 * mWidth * mHeight);
return Napi::Buffer<unsigned char>::Copy(info.Env(), mDataRaw, 4 * mWidth * mHeight);
}
else
{
Expand All @@ -237,7 +259,7 @@ namespace NodeBinding
//默认输出png 编码
if (info.Length() == 0)
{
return this->getPNGBuffer(info, size);
return getPNGBuffer(info, size);
}
else
{
Expand All @@ -247,15 +269,15 @@ namespace NodeBinding
std::string mimeType = info[0].As<Napi::String>().Utf8Value();
if (mimeType == "image/png")
{
ret = this->getPNGBuffer(info, size);
ret = getPNGBuffer(info, size);
}
else if (mimeType == "image/jpeg")
{
ret = this->getJPGBuffer(info, size);
ret = getJPGBuffer(info, size);
}
else if (mimeType == "raw")
{
ret = this->getRawDataBuffer(info, size);
ret = getRawDataBuffer(info, size);
}
}
if (size < 0)
Expand All @@ -270,11 +292,11 @@ namespace NodeBinding
}
Canvas::~Canvas()
{
this->mRenderContext = nullptr;
if (this->mDataRaw != nullptr)
mRenderContext = nullptr;
if (mDataRaw != nullptr)
{
free(this->mDataRaw);
this->mDataRaw = nullptr;
free(mDataRaw);
mDataRaw = nullptr;
}
printf("canvas destroy called \n");
}
Expand Down
5 changes: 4 additions & 1 deletion node/binding/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define CANVAS_H
#include "GRenderContext.h"
#include "CanvasRenderingContext2D.h"
#include "CanvasRenderingContextWebGL.h"
#include "CanvasGradient.h"
#include "ImageData.h"
#include "CanvasPattern.h"
Expand Down Expand Up @@ -41,7 +42,9 @@ namespace NodeBinding
Napi::Buffer<unsigned char> getPNGBuffer(const Napi::CallbackInfo &info, unsigned long &size);
Napi::Buffer<unsigned char> getJPGBuffer(const Napi::CallbackInfo &info, unsigned long &size);
Napi::Buffer<unsigned char> getRawDataBuffer(const Napi::CallbackInfo &info, unsigned long &size);
Napi::ObjectReference context2dRef;

Napi::ObjectReference mContext2dRef;
Napi::ObjectReference mContextWebGLRef;

void setWidth(const Napi::CallbackInfo &info, const Napi::Value &value);
void setHeight(const Napi::CallbackInfo &info, const Napi::Value &value);
Expand Down
Loading